1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
-
+
+
+
+
+
+
-
-
-
-
-
-
-
|
/*
* tclWinTime.c --
*
* Contains Windows specific versions of Tcl functions that
* obtain time values from the operating system.
*
* Copyright 1995-1998 by Sun Microsystems, Inc.
*
* See the file "license.terms" for information on usage and redistribution
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
*
* RCS: @(#) $Id: tclWinTime.c,v 1.14 2003/02/14 22:16:27 kennykb Exp $
* RCS: @(#) $Id: tclWinTime.c,v 1.15 2003/04/12 19:08:56 kennykb Exp $
*/
#include "tclWinInt.h"
#define SECSPERDAY (60L * 60L * 24L)
#define SECSPERYEAR (SECSPERDAY * 365L)
#define SECSPER4YEAR (SECSPERYEAR * 4L + SECSPERDAY)
/*
* Number of samples over which to estimate the performance counter
*/
#define SAMPLES 64
/*
* The following arrays contain the day of year for the last day of
* each month, where index 1 is January.
*/
static int normalDays[] = {
-1, 30, 58, 89, 119, 150, 180, 211, 242, 272, 303, 333, 364
};
static int leapDays[] = {
-1, 30, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365
};
typedef struct ThreadSpecificData {
char tzName[64]; /* Time zone name */
struct tm tm; /* time information */
} ThreadSpecificData;
static Tcl_ThreadDataKey dataKey;
/*
* Calibration interval for the high-resolution timer, in msec
*/
static CONST unsigned long clockCalibrateWakeupInterval = 10000;
/* FIXME: 10 s -- should be about 10 min! */
/*
* Data for managing high-resolution timers.
*/
typedef struct TimeInfo {
CRITICAL_SECTION cs; /* Mutex guarding this structure */
|
| ︙ | | |
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
+
+
+
+
-
-
+
+
-
-
-
+
+
+
-
+
-
-
-
+
-
-
-
-
+
+
+
+
|
HANDLE calibrationThread; /* Handle to the thread that keeps the
* virtual clock calibrated. */
HANDLE readyEvent; /* System event used to
* trigger the requesting thread
* when the clock calibration procedure
* is initialized for the first time */
HANDLE exitEvent; /* Event to signal out of an exit handler
* to tell the calibration loop to
* terminate */
LARGE_INTEGER nominalFreq; /* Nominal frequency of the system
* performance counter, that is, the value
* returned from QueryPerformanceFrequency. */
/*
* The following values are used for calculating virtual time.
* Virtual time is always equal to:
* lastFileTime + (current perf counter - lastCounter)
* * 10000000 / curCounterFreq
* and lastFileTime and lastCounter are updated any time that
* virtual time is returned to a caller.
*/
ULARGE_INTEGER lastFileTime;
LARGE_INTEGER lastCounter;
ULARGE_INTEGER fileTimeLastCall;
LARGE_INTEGER perfCounterLastCall;
LARGE_INTEGER curCounterFreq;
/*
* The next two values are used only in the calibration thread, to track
* the frequency of the performance counter.
/*
* Data used in developing the estimate of performance counter
* frequency
*/
ULONGLONG fileTimeSample[SAMPLES];
LONGLONG lastPerfCounter; /* Performance counter the last time
* that UpdateClockEachSecond was called */
LONGLONG lastSysTime; /* System clock at the last time
/* Last 64 samples of system time */
* that UpdateClockEachSecond was called */
LONGLONG estPerfCounterFreq;
/* Current estimate of the counter frequency
* using the system clock as the standard */
LONGLONG perfCounterSample[SAMPLES];
/* Last 64 samples of performance counter */
int sampleNo; /* Current sample number */
} TimeInfo;
static TimeInfo timeInfo = {
{ NULL },
0,
0,
|
| ︙ | | |
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
+
+
+
+
+
+
+
+
+
+
+
+
+
|
#else
0,
0,
0,
#endif
0,
0,
0,
{ 0 },
{ 0 },
0,
0
};
CONST static FILETIME posixEpoch = { 0xD53E8000, 0x019DB1DE };
/*
* Declarations for functions defined later in this file.
*/
static struct tm * ComputeGMT _ANSI_ARGS_((const time_t *tp));
static void StopCalibration _ANSI_ARGS_(( ClientData ));
static DWORD WINAPI CalibrationThread _ANSI_ARGS_(( LPVOID arg ));
static void UpdateTimeEachSecond _ANSI_ARGS_(( void ));
static void ResetCounterSamples _ANSI_ARGS_((
ULONGLONG fileTime,
LONGLONG perfCounter,
LONGLONG perfFreq
));
static LONGLONG AccumulateSample _ANSI_ARGS_((
LONGLONG perfCounter,
ULONGLONG fileTime
));
/*
*----------------------------------------------------------------------
*
* TclpGetSeconds --
*
* This procedure returns the number of seconds from the epoch.
|
| ︙ | | |
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
|
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
|
-
+
|
* since it avoids an extra mutex lock in the common case.
*/
if ( !timeInfo.initialized ) {
TclpInitLock();
if ( !timeInfo.initialized ) {
timeInfo.perfCounterAvailable
= QueryPerformanceFrequency( &timeInfo.curCounterFreq );
= QueryPerformanceFrequency( &timeInfo.nominalFreq );
/*
* Some hardware abstraction layers use the CPU clock
* in place of the real-time clock as a performance counter
* reference. This results in:
* - inconsistent results among the processors on
* multi-processor systems.
|
| ︙ | | |
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
|
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
|
-
-
+
+
-
+
|
* frequency (perhaps in an attempt to calibrate the clock?)
* we use the latter rule rather than an exact match.
*/
if ( timeInfo.perfCounterAvailable
/* The following lines would do an exact match on
* crystal frequency:
* && timeInfo.curCounterFreq.QuadPart != (LONGLONG) 1193182
* && timeInfo.curCounterFreq.QuadPart != (LONGLONG) 3579545
* && timeInfo.nominalFreq.QuadPart != (LONGLONG) 1193182
* && timeInfo.nominalFreq.QuadPart != (LONGLONG) 3579545
*/
&& timeInfo.curCounterFreq.QuadPart > (LONGLONG) 15000000 ) {
&& timeInfo.nominalFreq.QuadPart > (LONGLONG) 15000000 ) {
timeInfo.perfCounterAvailable = FALSE;
}
/*
* If the performance counter is available, start a thread to
* calibrate it.
*/
|
| ︙ | | |
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
|
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
|
+
-
+
-
+
+
+
-
-
+
+
-
-
+
+
-
-
+
+
-
+
|
posixEpoch.LowPart = 0xD53E8000;
posixEpoch.HighPart = 0x019DB1DE;
EnterCriticalSection( &timeInfo.cs );
QueryPerformanceCounter( &curCounter );
/*
* If it appears to be more than 1.1 seconds since the last trip
* through the calibration loop, the performance counter may
* have jumped. Discard it. See MSDN Knowledge Base article
* have jumped forward. (See MSDN Knowledge Base article
* Q274323 for a description of the hardware problem that makes
* this test necessary.
* this test necessary.) If the counter jumps, we don't want
* to use it directly. Instead, we must return system time.
* Eventually, the calibration loop should recover.
*/
if ( curCounter.QuadPart - timeInfo.lastPerfCounter
< 11 * timeInfo.estPerfCounterFreq / 10 ) {
if ( curCounter.QuadPart - timeInfo.perfCounterLastCall.QuadPart
< 11 * timeInfo.curCounterFreq.QuadPart / 10 ) {
curFileTime = timeInfo.lastFileTime.QuadPart
+ ( ( curCounter.QuadPart - timeInfo.lastCounter.QuadPart )
curFileTime = timeInfo.fileTimeLastCall.QuadPart
+ ( ( curCounter.QuadPart - timeInfo.perfCounterLastCall.QuadPart )
* 10000000 / timeInfo.curCounterFreq.QuadPart );
timeInfo.lastFileTime.QuadPart = curFileTime;
timeInfo.lastCounter.QuadPart = curCounter.QuadPart;
timeInfo.fileTimeLastCall.QuadPart = curFileTime;
timeInfo.perfCounterLastCall.QuadPart = curCounter.QuadPart;
usecSincePosixEpoch = ( curFileTime - posixEpoch.QuadPart ) / 10;
timePtr->sec = (time_t) ( usecSincePosixEpoch / 1000000 );
timePtr->usec = (unsigned long ) ( usecSincePosixEpoch % 1000000 );
useFtime = 0;
}
}
LeaveCriticalSection( &timeInfo.cs );
}
if ( useFtime ) {
/* High resolution timer is not available. Just use ftime */
|
| ︙ | | |
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
|
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
|
-
-
+
-
+
-
-
+
+
-
-
-
-
+
+
+
|
* arg -- Client data from the CreateThread call. This parameter
* points to the static TimeInfo structure.
*
* Return value:
* None. This thread embeds an infinite loop.
*
* Side effects:
* At an interval of clockCalibrateWakeupInterval ms, this thread
* performs virtual time discipline.
* At an interval of 1 s, this thread performs virtual time discipline.
*
* Note: When this thread is entered, TclpInitLock has been called
* to safeguard the static storage. There is therefore no synchronization
* in the body of this procedure.
*
*----------------------------------------------------------------------
*/
static DWORD WINAPI
CalibrationThread( LPVOID arg )
{
FILETIME curFileTime;
DWORD waitResult;
/* Get initial system time and performance counter */
GetSystemTimeAsFileTime( &curFileTime );
QueryPerformanceCounter( &timeInfo.lastCounter );
QueryPerformanceCounter( &timeInfo.perfCounterLastCall );
QueryPerformanceFrequency( &timeInfo.curCounterFreq );
timeInfo.lastFileTime.LowPart = curFileTime.dwLowDateTime;
timeInfo.lastFileTime.HighPart = curFileTime.dwHighDateTime;
timeInfo.fileTimeLastCall.LowPart = curFileTime.dwLowDateTime;
timeInfo.fileTimeLastCall.HighPart = curFileTime.dwHighDateTime;
/* Initialize the working storage for the calibration callback */
timeInfo.lastPerfCounter = timeInfo.lastCounter.QuadPart;
timeInfo.estPerfCounterFreq = timeInfo.curCounterFreq.QuadPart;
ResetCounterSamples( timeInfo.fileTimeLastCall.QuadPart,
timeInfo.perfCounterLastCall.QuadPart,
timeInfo.curCounterFreq.QuadPart );
/*
* Wake up the calling thread. When it wakes up, it will release the
* initialization lock.
*/
SetEvent( timeInfo.readyEvent );
|
| ︙ | | |
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
|
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
|
-
-
-
-
-
-
-
-
-
+
-
+
-
-
-
+
-
-
-
+
+
-
+
+
-
+
+
+
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
-
-
+
+
-
-
+
-
+
+
+
+
+
+
+
+
+
+
+
+
-
+
-
+
-
-
-
-
+
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
+
+
+
+
+
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
+
+
+
+
+
-
+
-
-
-
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
+
+
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
|
UpdateTimeEachSecond()
{
LARGE_INTEGER curPerfCounter;
/* Current value returned from
* QueryPerformanceCounter */
LONGLONG perfCounterDiff; /* Difference between the current value
* and the value of 1 second ago */
FILETIME curSysTime; /* Current system time */
LARGE_INTEGER curFileTime; /* File time at the time this callback
* was scheduled. */
LONGLONG fileTimeDiff; /* Elapsed time on the system clock
* since the last time this procedure
* was called */
LONGLONG instantFreq; /* Instantaneous estimate of the
* performance counter frequency */
LONGLONG estFreq; /* Estimated perf counter frequency */
LONGLONG delta; /* Increment to add to the estimated
LONGLONG vt0; /* Tcl time right now */
* performance counter frequency in the
* loop filter */
LONGLONG vt1; /* Tcl time one second from now */
LONGLONG fuzz; /* Tolerance for the perf counter frequency */
LONGLONG lowBound; /* Lower bound for the frequency assuming
* 1000 ppm tolerance */
LONGLONG tdiff; /* Difference between system clock and
* Tcl time. */
LONGLONG hiBound; /* Upper bound for the frequency */
LONGLONG driftFreq; /* Frequency needed to drift virtual time
* into step over 1 second */
/*
* Get current performance counter and system time.
* Sample performance counter and system time.
*/
QueryPerformanceCounter( &curPerfCounter );
GetSystemTimeAsFileTime( &curSysTime );
curFileTime.LowPart = curSysTime.dwLowDateTime;
curFileTime.HighPart = curSysTime.dwHighDateTime;
EnterCriticalSection( &timeInfo.cs );
/*
* Several things may have gone wrong here that have to
* be checked for.
* Find out how many ticks of the performance counter and the
* system clock have elapsed since we got into this procedure.
* Estimate the current frequency.
* (1) The performance counter may have jumped.
* (2) The system clock may have been reset.
*
* In either case, we'll need to reinitialize the circular buffer
* with samples relative to the current system time and the NOMINAL
* performance frequency (not the actual, because the actual has
* probably run slow in the first case). Our estimated frequency
* will be the nominal frequency.
*/
/*
* Store the current sample into the circular buffer of samples,
* and estimate the performance counter frequency.
*/
perfCounterDiff = curPerfCounter.QuadPart - timeInfo.lastPerfCounter;
timeInfo.lastPerfCounter = curPerfCounter.QuadPart;
estFreq = AccumulateSample( curPerfCounter.QuadPart,
fileTimeDiff = curFileTime.QuadPart - timeInfo.lastSysTime;
timeInfo.lastSysTime = curFileTime.QuadPart;
curFileTime.QuadPart );
instantFreq = ( 10000000 * perfCounterDiff / fileTimeDiff );
/*
* We want to adjust things so that time appears to be continuous.
* Virtual file time, right now, is
*
* vt0 = 10000000 * ( curPerfCounter - perfCounterLastCall )
* / curCounterFreq
* + fileTimeLastCall
*
* Ideally, we would like to drift the clock into place over a
* period of 2 sec, so that virtual time 2 sec from now will be
*
* vt1 = 20000000 + curFileTime
*
* Consider this a timing glitch if instant frequency varies
* The frequency that we need to use to drift the counter back into
* significantly from the current estimate.
* place is estFreq * 20000000 / ( vt1 - vt0 )
*/
fuzz = timeInfo.estPerfCounterFreq >> 10;
lowBound = timeInfo.estPerfCounterFreq - fuzz;
hiBound = timeInfo.estPerfCounterFreq + fuzz;
vt0 = 10000000 * ( curPerfCounter.QuadPart
- timeInfo.perfCounterLastCall.QuadPart )
/ timeInfo.curCounterFreq.QuadPart
if ( instantFreq < lowBound || instantFreq > hiBound ) {
LeaveCriticalSection( &timeInfo.cs );
return;
}
/*
* Update the current estimate of performance counter frequency.
* This code is equivalent to the loop filter of a phase locked
* loop.
*/
delta = ( instantFreq - timeInfo.estPerfCounterFreq ) >> 6;
timeInfo.estPerfCounterFreq += delta;
/*
* Update the current virtual time.
*/
+ timeInfo.fileTimeLastCall.QuadPart;
vt1 = 20000000 + curFileTime.QuadPart;
/*
* If we've gotten more than a second away from system time,
* then drifting the clock is going to be pretty hopeless.
* Just let it jump. Otherwise, compute the drift frequency and
* fill in everything.
*/
tdiff = vt0 - curFileTime.QuadPart;
if ( tdiff > 10000000 || tdiff < -10000000 ) {
timeInfo.fileTimeLastCall.QuadPart = curFileTime.QuadPart;
timeInfo.curCounterFreq.QuadPart = estFreq;
} else {
driftFreq = estFreq * 20000000 / ( vt1 - vt0 );
if ( driftFreq > 1003 * estFreq / 1000 ) {
driftFreq = 1003 * estFreq / 1000;
}
if ( driftFreq < 997 * estFreq / 1000 ) {
driftFreq = 997 * estFreq / 1000;
}
timeInfo.fileTimeLastCall.QuadPart = vt0;
timeInfo.curCounterFreq.QuadPart = driftFreq;
}
timeInfo.perfCounterLastCall.QuadPart = curPerfCounter.QuadPart;
LeaveCriticalSection( &timeInfo.cs );
}
/*
*----------------------------------------------------------------------
*
* ResetCounterSamples --
*
* Fills the sample arrays in 'timeInfo' with dummy values that will
* yield the current performance counter and frequency.
*
* Results:
* None.
*
* Side effects:
* The array of samples is filled in so that it appears that there
* are SAMPLES samples at one-second intervals, separated by precisely
* the given frequency.
*
*----------------------------------------------------------------------
*/
static void
timeInfo.lastFileTime.QuadPart
+= ( ( curPerfCounter.QuadPart - timeInfo.lastCounter.QuadPart )
* 10000000 / timeInfo.curCounterFreq.QuadPart );
ResetCounterSamples( ULONGLONG fileTime,
/* Current file time */
LONGLONG perfCounter,
/* Current performance counter */
LONGLONG perfFreq )
timeInfo.lastCounter.QuadPart = curPerfCounter.QuadPart;
delta = curFileTime.QuadPart - timeInfo.lastFileTime.QuadPart;
if ( delta > 10000000 || delta < -10000000 ) {
/*
* If the virtual time slip exceeds one second, then adjusting
* the counter frequency is hopeless (it'll take over fifteen
* minutes to line up with the system clock). The most likely
* cause of this large a slip is a sudden change to the system
/* Target performance frequency */
{
int i;
for ( i = SAMPLES-1; i >= 0; --i ) {
timeInfo.perfCounterSample[i] = perfCounter;
timeInfo.fileTimeSample[i] = fileTime;
perfCounter -= perfFreq;
fileTime -= 10000000;
}
timeInfo.sampleNo = 0;
}
/*
*----------------------------------------------------------------------
*
* AccumulateSample --
*
* Updates the circular buffer of performance counter and system
* time samples with a new data point.
*
* Results:
* None.
*
* Side effects:
* The new data point replaces the oldest point in the circular
* buffer, and the descriptive statistics are updated to accumulate
* the new point.
*
* Several things may have gone wrong here that have to
* be checked for.
* (1) The performance counter may have jumped.
* (2) The system clock may have been reset.
*
* In either case, we'll need to reinitialize the circular buffer
* with samples relative to the current system time and the NOMINAL
* clock, perhaps because it was being corrected by wristwatch
* and eyeball. Accept the system time, and set the performance
* counter frequency to the current estimate.
*/
* performance frequency (not the actual, because the actual has
* probably run slow in the first case).
*/
static LONGLONG
AccumulateSample( LONGLONG perfCounter,
timeInfo.lastFileTime.QuadPart = curFileTime.QuadPart;
ULONGLONG fileTime )
timeInfo.curCounterFreq.QuadPart = timeInfo.estPerfCounterFreq;
} else {
{
ULONGLONG workFTSample; /* File time sample being removed
* from or added to the circular buffer */
LONGLONG workPCSample; /* Performance counter sample being
* removed from or added to the circular
* buffer */
ULONGLONG lastFTSample; /* Last file time sample recorded */
LONGLONG lastPCSample; /* Last performance counter sample recorded */
/*
* Compute a counter frequency that will cause virtual time to line
* up with system time one second from now, assuming that the
* performance counter continues to tick at timeInfo.estPerfCounterFreq.
*/
timeInfo.curCounterFreq.QuadPart
= 10000000 * timeInfo.estPerfCounterFreq / ( delta + 10000000 );
LONGLONG FTdiff; /* Difference between last FT and current */
LONGLONG PCdiff; /* Difference between last PC and current */
LONGLONG estFreq; /* Estimated performance counter frequency */
/* Test for jumps and reset the samples if we have one. */
if ( timeInfo.sampleNo == 0 ) {
lastPCSample = timeInfo.perfCounterSample[ timeInfo.sampleNo
+ SAMPLES - 1 ];
lastFTSample = timeInfo.fileTimeSample[ timeInfo.sampleNo
+ SAMPLES - 1 ];
} else {
lastPCSample = timeInfo.perfCounterSample[ timeInfo.sampleNo - 1 ];
lastFTSample = timeInfo.fileTimeSample[ timeInfo.sampleNo - 1 ];
}
PCdiff = perfCounter - lastPCSample;
FTdiff = fileTime - lastFTSample;
if ( PCdiff < timeInfo.nominalFreq.QuadPart * 9 / 10
|| PCdiff > timeInfo.nominalFreq.QuadPart * 11 / 10
|| FTdiff < 9000000
|| FTdiff > 11000000 ) {
ResetCounterSamples( fileTime, perfCounter,
timeInfo.nominalFreq.QuadPart );
return timeInfo.nominalFreq.QuadPart;
} else {
/*
* Limit frequency excursions to 1000 ppm from estimate
/* Estimate the frequency */
*/
if ( timeInfo.curCounterFreq.QuadPart < lowBound ) {
timeInfo.curCounterFreq.QuadPart = lowBound;
} else if ( timeInfo.curCounterFreq.QuadPart > hiBound ) {
timeInfo.curCounterFreq.QuadPart = hiBound;
}
}
LeaveCriticalSection( &timeInfo.cs );
workPCSample = timeInfo.perfCounterSample[ timeInfo.sampleNo ];
workFTSample = timeInfo.fileTimeSample[ timeInfo.sampleNo ];
estFreq = 10000000 * ( perfCounter - workPCSample )
/ ( fileTime - workFTSample );
timeInfo.perfCounterSample[ timeInfo.sampleNo ] = perfCounter;
timeInfo.fileTimeSample[ timeInfo.sampleNo ] = (LONGLONG) fileTime;
/* Advance the sample number */
if ( ++timeInfo.sampleNo >= SAMPLES ) {
timeInfo.sampleNo = 0;
}
return estFreq;
}
}
|