We have application logs that have record performance information for various expensive actions, we have stopwatch And
DateTime.UtcNow
, and we are aware that these values may be different than expected, even the date time is also given. ~ 20 ms precision of UtkNo My question is What can be the reason for this and can it be decided?
The recorded information is:
- Start time (
dateTime.UtcNow
) - Duration (
TimSpainFormsCondus (later) / (Double stopwatch frequency)
, whereafter
andfirst
have stopwatch values . At the beginning and end of operation - End time (
DateTime.UtcNow
)
If you are close to starttime + duration of end time But in some cases this path is closed . We have sampled 10000 such measurements, where looking for cases is end-time and (starttime + duration) differences are more than 20 milliseconds. We found the following:
- ~ 2% entries are closed> 20ms
- Of these, 100ms average of this (maximum 1.4seconds)
- Of these, the stopwatch-count expiration time in some cases is higher than the time-based expiration time, but in most cases, Windows Server 2012 R2 Hyper-V VM runs at the top of the Stopwave Windows Server 2012 R2 is
- 32 GB RAM for VM, 8 virtual CPUs < / Ul>
Machine information
This is expected to be Uses stopPouchformsCounter
(QPC) under the Stopwatch
hood, while DateTime.UtcNow
uses the computer's real-time clock (RTC) is.
- QPC comes from your CPU's timestamp counter (TSC), and it is very accurate , but accuracy in relation to UTC There is no basis for this.
- RTC is on the computer's motherboard, and is usually made with cheap crystal oscillators, it is accurate , but is not exact .
To measure the time elapsed, you want to be precise, and thus stopwatch
.
For the current time of day, you want accuracy, and thus DateTime.UtcNow
.
Should use a lot of support details.
The difference you have measured is due to the clock drift in RTC. Your RTC (All Self) will fall a few milliseconds forward or behind real time. It is quite normal, and is used by NTP synchronization from time to time. For minor adjustments, the OS will spread a few milliseconds at one time, improving small intervals during intervals.
As far as an alternative is solved, you might consider a class like this:
Public Static Class Accurate Clock {Private Stable Readonly Datetime Start Date = DateTime .Utc now; Private stationary readwatch stopwatch stopwatch = stopwatch Startup (); Public static date time GetCurrentUtcTime () {start starttime + stopwatch Apple; }}
However, the downside is that you are making assumptions that the RTC is in perfect sync when the class is started. In fact, you are not sure about it.
If you want to guarantee the some level, then you can consider making the NTP call own I have applied a class that is actually Does in It implements the interface from iSCLock
It periodically calls an NTP server, and tracks the time between the call using the stopwatch
. You will use it like this:
// Clock clocking example var watch = NetworkClock.Instance grab; // Alternatively set up NTP server during your app's startup clock. Npserver = "pool.ntp.org"; // or whatever server you want to sync // whenever you want the Datetime UTCNO = clock, get current UTC time. Now. ToDateTimeUtc (); In addition, there is something like "Multimedia Timer" or "High Precision Event Timer" (HPET) which is available on most modern hardware. It provides more accurate than the QPC, however, there is no direct category that displays in the .NET framework. If you search, you will find some implementations that wrap the Win32 function that provides it. Generally, this is redundant until you are talking things like graphics or audio from real time.
No comments:
Post a Comment