| ︙ | | | ︙ | |
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
* time is always equal to:
* fileTime + (current perf counter - lastCounter)
* * 10000000 / counterFreq
*/
struct {
ULONGLONG fileTime;
volatile /* used also to compare calibration epoch */
LONGLONG perfCounter;
LONGLONG counterFreq;
} lastCC; /* Last data updated in calibration cycle */
Tcl_WideInt lastUsedTime; /* Last known (caller) virtual time in 100-ns
* (used to avoid drifts after calibrate) */
|
>
|
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
* time is always equal to:
* fileTime + (current perf counter - lastCounter)
* * 10000000 / counterFreq
*/
struct {
ULONGLONG fileTime;
ULONGLONG virtTime;
volatile /* used also to compare calibration epoch */
LONGLONG perfCounter;
LONGLONG counterFreq;
} lastCC; /* Last data updated in calibration cycle */
Tcl_WideInt lastUsedTime; /* Last known (caller) virtual time in 100-ns
* (used to avoid drifts after calibrate) */
|
| ︙ | | | ︙ | |
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
(LARGE_INTEGER) (Tcl_WideInt) 0,
(LARGE_INTEGER) (Tcl_WideInt) 0,
#else
{0, 0},
{0, 0},
#endif
{
(ULONGLONG) 0,
(LONGLONG) 0,
(LONGLONG) 0
},
(Tcl_WideInt) 0,
{ (Tcl_WideUInt) 0 },
{ (Tcl_WideInt) 0 },
|
>
|
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
(LARGE_INTEGER) (Tcl_WideInt) 0,
(LARGE_INTEGER) (Tcl_WideInt) 0,
#else
{0, 0},
{0, 0},
#endif
{
(ULONGLONG) 0,
(ULONGLONG) 0,
(LONGLONG) 0,
(LONGLONG) 0
},
(Tcl_WideInt) 0,
{ (Tcl_WideUInt) 0 },
{ (Tcl_WideInt) 0 },
|
| ︙ | | | ︙ | |
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
|
*----------------------------------------------------------------------
*
* NativeCalc100NsTicks --
*
* Calculate the current system time in 100-ns ticks since posix epoch,
* for current performance counter (curCounter), using given calibrated values.
*
* vt = lastCC.fileTime +
* 10000000 * (curPerfCounter - lastCC.perfCounter) / lastCC.counterFreq
*
* Results:
* Returns the wide integer with number of microseconds from the epoch.
*
* Side effects:
* None
*
*----------------------------------------------------------------------
*/
static inline Tcl_WideInt
NativeCalc100NsTicks(
ULONGLONG ccFileTime,
LONGLONG ccPerfCounter,
LONGLONG ccCounterFreq,
LONGLONG curCounter
) {
return ccFileTime +
((curCounter - ccPerfCounter) * 10000000 / ccCounterFreq);
}
/*
* Representing the number of 100-nanosecond intervals since posix epoch.
*/
static inline Tcl_WideInt
|
|
|
|
|
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
|
*----------------------------------------------------------------------
*
* NativeCalc100NsTicks --
*
* Calculate the current system time in 100-ns ticks since posix epoch,
* for current performance counter (curCounter), using given calibrated values.
*
* vt = lastCC.virtTime +
* 10000000 * (curPerfCounter - lastCC.perfCounter) / lastCC.counterFreq
*
* Results:
* Returns the wide integer with number of microseconds from the epoch.
*
* Side effects:
* None
*
*----------------------------------------------------------------------
*/
static inline Tcl_WideInt
NativeCalc100NsTicks(
ULONGLONG ccVirtTime,
LONGLONG ccPerfCounter,
LONGLONG ccCounterFreq,
LONGLONG curCounter
) {
return ccVirtTime +
((curCounter - ccPerfCounter) * 10000000 / ccCounterFreq);
}
/*
* Representing the number of 100-nanosecond intervals since posix epoch.
*/
static inline Tcl_WideInt
|
| ︙ | | | ︙ | |
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
|
QueryPerformanceCounter(&newPerfCounter);
if (prevUS && usecSincePosixEpoch < prevUS) {
printf("!!!!!!!!!!!!!!!!!!!!!!!!!!!time-backwards!!!! pre-struct: %I64d, %I64d, %I64d, %I64d == %I64d \n", fileTimeLastCall, perfCounterLastCall, prevPerfCounter.QuadPart, curCounterFreq,
NativeCalc100NsTicks(fileTimeLastCall,
perfCounterLastCall, curCounterFreq,
prevPerfCounter.QuadPart));
printf("!!!!!!!!!!!!!!!!!!!!!!!!!!!time-backwards!!!! new-struct: %I64d, %I64d, %I64d, %I64d == %I64d \n", timeInfo.lastCC.fileTime, timeInfo.lastCC.perfCounter, newPerfCounter.QuadPart, timeInfo.lastCC.counterFreq,
NativeCalc100NsTicks(timeInfo.lastCC.fileTime,
timeInfo.lastCC.perfCounter, timeInfo.lastCC.counterFreq,
newPerfCounter.QuadPart));
printf("!!!!!!!!!!!!!!!!!!!!!!!!!!!time-backwards!!!! prev: %I64d - now: %I64d (%I64d usec)\n", prevUS, usecSincePosixEpoch, usecSincePosixEpoch - prevUS);
Tcl_Panic("Time running backwards!!!");
}
prevUS = usecSincePosixEpoch;
fileTimeLastCall = timeInfo.lastCC.fileTime;
perfCounterLastCall = timeInfo.lastCC.perfCounter;
curCounterFreq = timeInfo.lastCC.counterFreq;
prevPerfCounter.QuadPart = newPerfCounter.QuadPart;
return usecSincePosixEpoch;
}
|
|
|
|
|
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
|
QueryPerformanceCounter(&newPerfCounter);
if (prevUS && usecSincePosixEpoch < prevUS) {
printf("!!!!!!!!!!!!!!!!!!!!!!!!!!!time-backwards!!!! pre-struct: %I64d, %I64d, %I64d, %I64d == %I64d \n", fileTimeLastCall, perfCounterLastCall, prevPerfCounter.QuadPart, curCounterFreq,
NativeCalc100NsTicks(fileTimeLastCall,
perfCounterLastCall, curCounterFreq,
prevPerfCounter.QuadPart));
printf("!!!!!!!!!!!!!!!!!!!!!!!!!!!time-backwards!!!! new-struct: %I64d, %I64d, %I64d, %I64d == %I64d \n", timeInfo.lastCC.virtTime, timeInfo.lastCC.perfCounter, newPerfCounter.QuadPart, timeInfo.lastCC.counterFreq,
NativeCalc100NsTicks(timeInfo.lastCC.virtTime,
timeInfo.lastCC.perfCounter, timeInfo.lastCC.counterFreq,
newPerfCounter.QuadPart));
printf("!!!!!!!!!!!!!!!!!!!!!!!!!!!time-backwards!!!! prev: %I64d - now: %I64d (%I64d usec)\n", prevUS, usecSincePosixEpoch, usecSincePosixEpoch - prevUS);
Tcl_Panic("Time running backwards!!!");
}
prevUS = usecSincePosixEpoch;
fileTimeLastCall = timeInfo.lastCC.virtTime;
perfCounterLastCall = timeInfo.lastCC.perfCounter;
curCounterFreq = timeInfo.lastCC.counterFreq;
prevPerfCounter.QuadPart = newPerfCounter.QuadPart;
return usecSincePosixEpoch;
}
|
| ︙ | | | ︙ | |
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
|
}
TclpInitUnlock();
}
if (timeInfo.perfCounterAvailable && timeInfo.lastCC.counterFreq!=0) {
static struct {
ULONGLONG fileTime;
volatile /* don't optimize */
LONGLONG perfCounter;
LONGLONG counterFreq;
Tcl_WideInt calibNextTime;
} cc = {0, 0, 0, 0}; /* Copy with current data of calibration cycle */
LARGE_INTEGER curCounter;
/* Current performance counter. */
/*
* Hold time section locked as short as possible
*/
if (cc.perfCounter != timeInfo.lastCC.perfCounter) {
EnterCriticalSection(&timeInfo.cs);
if (cc.perfCounter != timeInfo.lastCC.perfCounter) {
cc.perfCounter = timeInfo.lastCC.perfCounter;
cc.fileTime = timeInfo.lastCC.fileTime;
cc.counterFreq = timeInfo.lastCC.counterFreq;
cc.calibNextTime = timeInfo.calibNextTime;
}
LeaveCriticalSection(&timeInfo.cs);
}
/*
* Query the performance counter and use it to calculate the current
* time.
*/
QueryPerformanceCounter(&curCounter);
/* Calibrated file-time is saved from posix in 100-ns ticks */
curTime = NativeCalc100NsTicks(cc.fileTime,
cc.perfCounter, cc.counterFreq, curCounter.QuadPart);
/* Be sure the clock ticks never backwards (avoid backwards time-drifts) */
if ( (lastTime = timeInfo.lastUsedTime)
&& lastTime > curTime
&& lastTime - curTime < 1000000 /* bypass time-switch (drifts only) */
) {
|
|
|
|
|
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
|
}
TclpInitUnlock();
}
if (timeInfo.perfCounterAvailable && timeInfo.lastCC.counterFreq!=0) {
static struct {
ULONGLONG virtTime;
volatile /* don't optimize */
LONGLONG perfCounter;
LONGLONG counterFreq;
Tcl_WideInt calibNextTime;
} cc = {0, 0, 0, 0}; /* Copy with current data of calibration cycle */
LARGE_INTEGER curCounter;
/* Current performance counter. */
/*
* Hold time section locked as short as possible
*/
if (cc.perfCounter != timeInfo.lastCC.perfCounter) {
EnterCriticalSection(&timeInfo.cs);
if (cc.perfCounter != timeInfo.lastCC.perfCounter) {
cc.perfCounter = timeInfo.lastCC.perfCounter;
cc.virtTime = timeInfo.lastCC.virtTime;
cc.counterFreq = timeInfo.lastCC.counterFreq;
cc.calibNextTime = timeInfo.calibNextTime;
}
LeaveCriticalSection(&timeInfo.cs);
}
/*
* Query the performance counter and use it to calculate the current
* time.
*/
QueryPerformanceCounter(&curCounter);
/* Calibrated file-time is saved from posix in 100-ns ticks */
curTime = NativeCalc100NsTicks(cc.virtTime,
cc.perfCounter, cc.counterFreq, curCounter.QuadPart);
/* Be sure the clock ticks never backwards (avoid backwards time-drifts) */
if ( (lastTime = timeInfo.lastUsedTime)
&& lastTime > curTime
&& lastTime - curTime < 1000000 /* bypass time-switch (drifts only) */
) {
|
| ︙ | | | ︙ | |
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
|
*/
LARGE_INTEGER curPerfCounter;
QueryPerformanceCounter(&curPerfCounter);
timeInfo.lastCC.perfCounter = curPerfCounter.QuadPart;
timeInfo.lastCC.counterFreq = timeInfo.nominalFreq.QuadPart;
timeInfo.lastCC.fileTime = GetSystemTimeAsVirtual();
ResetCounterSamples(timeInfo.lastCC.fileTime,
timeInfo.lastCC.perfCounter,
timeInfo.lastCC.counterFreq);
/*
* Calibrate first time and wake up the calling thread.
|
|
|
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
|
*/
LARGE_INTEGER curPerfCounter;
QueryPerformanceCounter(&curPerfCounter);
timeInfo.lastCC.perfCounter = curPerfCounter.QuadPart;
timeInfo.lastCC.counterFreq = timeInfo.nominalFreq.QuadPart;
timeInfo.lastCC.fileTime = timeInfo.lastCC.virtTime = GetSystemTimeAsVirtual();
ResetCounterSamples(timeInfo.lastCC.fileTime,
timeInfo.lastCC.perfCounter,
timeInfo.lastCC.counterFreq);
/*
* Calibrate first time and wake up the calling thread.
|
| ︙ | | | ︙ | |
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
|
static void
UpdateTimeEachSecond(void)
{
LARGE_INTEGER curPerfCounter;
/* Current value returned from
* QueryPerformanceCounter. */
static int calibrationInterv = 10000000;
/* Calibration interval in 100-ns ticks (starts from 1s) */
Tcl_WideInt curFileTime; /* File time at the time this callback was
* scheduled. */
Tcl_WideInt estFreq; /* Estimated perf counter frequency. */
int driftBack; /* Sign the virtual time may drift backwards */
Tcl_WideInt vt0; /* Tcl time right now. */
|
>
>
>
|
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
|
static void
UpdateTimeEachSecond(void)
{
LARGE_INTEGER curPerfCounter;
/* Current value returned from
* QueryPerformanceCounter. */
static Tcl_WideInt lastDiff = 0;
/* Last difference between system clock and Tcl
* time. */
static int calibrationInterv = 10000000;
/* Calibration interval in 100-ns ticks (starts from 1s) */
Tcl_WideInt curFileTime; /* File time at the time this callback was
* scheduled. */
Tcl_WideInt estFreq; /* Estimated perf counter frequency. */
int driftBack; /* Sign the virtual time may drift backwards */
Tcl_WideInt vt0; /* Tcl time right now. */
|
| ︙ | | | ︙ | |
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
|
QueryPerformanceCounter(&curPerfCounter);
/*
* Current virtual time:
* vt0 = lastCC.fileTime +
* 10000000 * (curPerfCounter - lastCC.perfCounter) / lastCC.counterFreq
*/
vt0 = NativeCalc100NsTicks(timeInfo.lastCC.fileTime,
timeInfo.lastCC.perfCounter, timeInfo.lastCC.counterFreq,
curPerfCounter.QuadPart);
tdiff = vt0 - curFileTime; /* discrepancy between virtual and real-time */
/*
* If calibration still not needed (check for possible time-switch). Note, that
* NativeGetMicroseconds checks calibNextTime also, be sure it does not overflow.
* Calibrate immediately if we've too large discrepancy to the real-time (15.6 ms).
*/
if ( curFileTime < timeInfo.calibNextTime - (10000000/2) /* 0.5 sec (in 100-ns ticks). */
&& timeInfo.calibNextTime - curFileTime < 10 * 10000000 /* max. 10 seconds in-between (time-switch?) */
&& tdiff > -156000 && tdiff < 156000
) {
/* again in next one second */
// printf("-------------calibration end, not needed. ------ cur:%I64d - next:%I64d (d: %I64d) ------\n", curFileTime, timeInfo.calibNextTime, timeInfo.calibNextTime - curFileTime);
return;
}
/*
* We devide by timeInfo.lastCC.counterFreq in several places. That
* value should always be positive on a correctly functioning system. But
* it is good to be defensive about such matters. So if something goes
* wrong and the value does goes to zero, we clear the
* timeInfo.perfCounterAvailable in order to cause the calibration thread
* to shut itself down, then return without additional processing.
|
|
>
>
>
>
|
>
|
>
|
|
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
|
QueryPerformanceCounter(&curPerfCounter);
/*
* Current virtual time:
* vt0 = lastCC.fileTime +
* 10000000 * (curPerfCounter - lastCC.perfCounter) / lastCC.counterFreq
*/
vt0 = NativeCalc100NsTicks((timeInfo.lastCC.fileTime/2 + timeInfo.lastCC.virtTime/2),
timeInfo.lastCC.perfCounter, timeInfo.lastCC.counterFreq,
curPerfCounter.QuadPart);
tdiff = vt0 - curFileTime; /* discrepancy between virtual and real-time */
if (tdiff >= 10000000 || tdiff <= -10000000) {
printf("---!!!!!!!---calibration ERR, tdiff %I64d\n", tdiff);
}
/*
* If calibration still not needed (check for possible time-switch). Note, that
* NativeGetMicroseconds checks calibNextTime also, be sure it does not overflow.
* Calibrate immediately if we've too large discrepancy to the real-time (15.6 ms).
*/
#if 0
if ( curFileTime < timeInfo.calibNextTime - (10000000/2) /* 0.5 sec (in 100-ns ticks). */
&& timeInfo.calibNextTime - curFileTime < 10 * 10000000 /* max. 10 seconds in-between (time-switch?) */
&& tdiff > -156000 && tdiff < 156000 /* small discrepancy */
&& (tdiff >= 0 && tdiff <= lastDiff / 3 * 2 || tdiff < 0 && tdiff >= lastDiff / 3 * 2) /* more precise now */
) {
/* again in next one second */
printf("-------------calibration end, tdiff %I64d, *** not needed. (next in: %I64d) ------\n", tdiff, curFileTime, timeInfo.calibNextTime, timeInfo.calibNextTime - curFileTime);
lastDiff = tdiff;
return;
}
#endif
/*
* We devide by timeInfo.lastCC.counterFreq in several places. That
* value should always be positive on a correctly functioning system. But
* it is good to be defensive about such matters. So if something goes
* wrong and the value does goes to zero, we clear the
* timeInfo.perfCounterAvailable in order to cause the calibration thread
* to shut itself down, then return without additional processing.
|
| ︙ | | | ︙ | |
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
|
*
* Store the current sample into the circular buffer of samples, and
* estimate the performance counter frequency.
*/
#if 0
estFreq = AccumulateSample(curPerfCounter.QuadPart, curFileTime);
#else
estFreq = 10000000 * (curPerfCounter.QuadPart - timeInfo.lastCC.perfCounter) / (vt0 - timeInfo.lastCC.fileTime);
printf("------**-----calibration estimated, tdiff: %I64d, cntrDiff:%I64d\n", tdiff, (curPerfCounter.QuadPart - timeInfo.lastCC.perfCounter));
printf("------**-----calibration estimated Frequency %I64d, %I64d, %I64d, diff: %I64d\n", curFileTime, curPerfCounter.QuadPart, estFreq, estFreq - timeInfo.lastCC.counterFreq);
#endif
driftBack = 0;
/*
* We want to adjust things so that time appears to be continuous.
* Virtual file time, right now, is vt0.
|
>
>
|
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
|
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
|
*
* Store the current sample into the circular buffer of samples, and
* estimate the performance counter frequency.
*/
#if 0
estFreq = AccumulateSample(curPerfCounter.QuadPart, curFileTime);
#else
vt1 = curFileTime - timeInfo.lastCC.fileTime;
if (vt1) {
estFreq = 10000000 * (curPerfCounter.QuadPart - timeInfo.lastCC.perfCounter) / vt1;
#if 1
/*
* Minimize influence of estFreq if tdiff falls (in relation to last difference),
* with dual falling speed. This indicates better choice of lastCC.counterFreq.
*/
if (tdiff > 0 && tdiff < lastDiff / 2 || tdiff < 0 && tdiff > lastDiff / 2) {
printf("-----***-----calibration minimize %I64d, %I64d\n", estFreq, lastDiff);
estFreq = (estFreq + timeInfo.lastCC.counterFreq * 2) / 3;
printf("-----***-----calibration minimize %I64d, %I64d\n", estFreq, tdiff);
}
#endif
} else {
estFreq = timeInfo.lastCC.counterFreq;
}
//printf("------**-----calibration estimated, tdiff: %I64d, ** %s ** cntrDiff:%I64d\n", tdiff, (estFreq > timeInfo.lastCC.counterFreq) ? "^^^" : "vvv", (curPerfCounter.QuadPart - timeInfo.lastCC.perfCounter));
//printf("------**-----calibration estimated Frequency %I64d, %I64d, %I64d, diff: %I64d\n", curFileTime, curPerfCounter.QuadPart, estFreq, estFreq - timeInfo.lastCC.counterFreq);
#endif
driftBack = 0;
/*
* We want to adjust things so that time appears to be continuous.
* Virtual file time, right now, is vt0.
|
| ︙ | | | ︙ | |
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
|
*/
if (tdiff > 10000000 || tdiff < -10000000) {
/* More as a second difference, so could be a time-switch
/* jump to current system time, use curent estimated frequency */
vt0 = curFileTime;
timeInfo.lastUsedTime = 0; /* reset last used time */
} else {
Tcl_WideInt driftFreq; /* Frequency needed to drift virtual time into
* step over 1 second. */
/* calculate new frequency and estimate drift to the next second */
vt1 = 20000000 + curFileTime;
driftFreq = (estFreq * 20000000 / (vt1 - vt0));
/*
* Avoid too large drifts (only half of the current difference),
* that allows also be more accurate (aspire to the smallest tdiff),
* so then we can prolong calibration interval in such cases.
*/
driftFreq = timeInfo.lastCC.counterFreq +
(driftFreq - timeInfo.lastCC.counterFreq) / 2;
/*
* Average between estimated, 2 current and 5 drifted frequencies,
* (do the soft drifting as possible)
*/
estFreq = (estFreq + 2 * timeInfo.lastCC.counterFreq + 5 * driftFreq) / 8;
}
/*
* Avoid too large discrepancy from nominal frequency (0.6 %)
*/
if ( estFreq > (vt1 = (1000+3)*timeInfo.nominalFreq.QuadPart/1000)
|| estFreq < (vt1 = (1000-3)*timeInfo.nominalFreq.QuadPart/1000)
) {
estFreq = vt1;
driftBack = vt0 > curFileTime;
vt0 = curFileTime; /* too large - just reset */
}
/* If possible backwards time-drifts (larger divider now) */
if (driftBack || estFreq > timeInfo.lastCC.counterFreq) {
Tcl_WideInt nt0, nt1;
/*
* Calculate the time using new calibration values (and compare with old),
* to avoid possible backwards drifts (adjust current base time).
* This should affect at least next 10 ticks.
*/
vt1 = curPerfCounter.QuadPart + 10;
/*
* Be sure the clock ticks never backwards (avoid it by negative drifting)
* just compare native time (in 100-ns) before and hereafter using
* previous/new calibrated values) and do a small adjustment
*/
nt0 = NativeCalc100NsTicks(timeInfo.lastCC.fileTime,
timeInfo.lastCC.perfCounter, timeInfo.lastCC.counterFreq,
vt1);
nt1 = NativeCalc100NsTicks(curFileTime,
curPerfCounter.QuadPart, estFreq,
vt1);
vt1 = (nt0 - nt1); /* old time - new time */
if (vt1 > 0 && vt1 < 10000000 /* bypass time-switch */) {
/* base time should jump forwards (the same virtual time using current values) */
vt0 += vt1;
tdiff += vt1;
}
}
/* if still precise enough, grow calibration interval up to 10 seconds */
if (tdiff < -156000 || tdiff > 156000 /* 15.6-ms */) {
/* too long drift - reset calibration interval to 1 second */
calibrationInterv = 10000000;
} else if (calibrationInterv < 10*10000000) {
calibrationInterv += 10000000;
}
/* In lock commit new values to timeInfo (hold lock as short as possible) */
EnterCriticalSection(&timeInfo.cs);
timeInfo.lastCC.perfCounter = curPerfCounter.QuadPart;
timeInfo.lastCC.fileTime = vt0;
timeInfo.lastCC.counterFreq = estFreq;
timeInfo.calibNextTime = curFileTime + calibrationInterv;
LeaveCriticalSection(&timeInfo.cs);
#if 1
//printf("-------------calibration adj -- nt1:%I64d - nt0:%I64d: adj: %I64d\n", nt1, nt0, vt1);
printf("-------------calibration end, tdiff %I64d, jump -- vt:%I64d - st:%I64d: %I64d, adj: %I64d\n", tdiff,
vt0, curFileTime, (vt0 - curFileTime), vt1);
printf("-------------calibration end , new-struct: %I64d, %I64d, %I64d\n", timeInfo.lastCC.fileTime, timeInfo.lastCC.perfCounter, timeInfo.lastCC.counterFreq);
#endif
}
/*
*----------------------------------------------------------------------
*
* ResetCounterSamples --
|
>
|
>
>
>
|
>
>
>
>
>
>
|
|
|
>
|
|
|
>
>
>
|
>
|
|
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
|
*/
if (tdiff > 10000000 || tdiff < -10000000) {
/* More as a second difference, so could be a time-switch
/* jump to current system time, use curent estimated frequency */
vt0 = curFileTime;
timeInfo.lastUsedTime = 0; /* reset last used time */
estFreq = timeInfo.nominalFreq.QuadPart;
} else {
Tcl_WideInt driftFreq; /* Frequency needed to drift virtual time into
* step over 1 second. */
/* calculate new frequency and estimate drift to the next second */
vt1 = 20000000 + curFileTime;
driftFreq = (estFreq * 20000000 / (vt1 - vt0));
/*
* Avoid too large drifts (only half of the current difference),
* that allows also be more accurate (aspire to the smallest tdiff),
* so then we can prolong calibration interval in such cases.
*/
driftFreq = timeInfo.lastCC.counterFreq +
(driftFreq - timeInfo.lastCC.counterFreq) / 2;
/*
* Average between estimated, 2 current and 5 drifted frequencies,
* (do the soft drifting as possible).
* Minimize influence if tdiff falls (in relation to last difference)
*/
#if 0
if (tdiff > 0 && tdiff < lastDiff / 2 || tdiff < 0 && tdiff > lastDiff / 2) {
estFreq = (1 * estFreq + 2 * timeInfo.lastCC.counterFreq + 5 * driftFreq) / 8;
} else {
estFreq = (3 * estFreq + 3 * timeInfo.lastCC.counterFreq + 2 * driftFreq) / 8;
}
#else
estFreq = (estFreq + timeInfo.lastCC.counterFreq + driftFreq) / 3;
#endif
}
/*
* Avoid too large discrepancy from nominal frequency (0.5%)
*/
if ( estFreq > (vt1 = (1000+5)*timeInfo.nominalFreq.QuadPart/1000)
|| estFreq < (vt1 = (1000-5)*timeInfo.nominalFreq.QuadPart/1000)
) {
estFreq = vt1;
driftBack = vt0 > curFileTime;
vt0 = curFileTime; /* too large - just reset */
}
/* If possible backwards time-drifts (larger divider now) */
vt1 = 0;
if (1 || driftBack || estFreq > timeInfo.lastCC.counterFreq) {
Tcl_WideInt nt0, nt1;
/*
* Calculate the time using new calibration values (and compare with old),
* to avoid possible backwards drifts (adjust current base time).
* This should affect at least next 10 ticks.
*/
vt1 = curPerfCounter.QuadPart + 10;
/*
* Be sure the clock ticks never backwards (avoid it by negative drifting)
* just compare native time (in 100-ns) before and hereafter using
* previous/new calibrated values) and do a small adjustment
*/
nt0 = NativeCalc100NsTicks(timeInfo.lastCC.virtTime,
timeInfo.lastCC.perfCounter, timeInfo.lastCC.counterFreq,
vt1);
nt1 = NativeCalc100NsTicks(vt0,
curPerfCounter.QuadPart, estFreq,
vt1);
vt1 = (nt0 - nt1); /* old time - new time */
if (vt1 > 0 && vt1 < 10000000 /* bypass time-switch */) {
/* base time should jump forwards (the same virtual time using current values) */
vt0 += vt1;
tdiff += vt1;
//////////////////////////////////////////estFreq = 10000000 * (vt0 - timeInfo.lastCC.perfCounter) / vt1;
}
}
/* if still precise enough, grow calibration interval up to 10 seconds */
if (tdiff < -156000 || tdiff > 156000 /* 15.6-ms */) {
/* too long drift - reset calibration interval to 1 second */
calibrationInterv = 10000000;
} else if (calibrationInterv < 10*10000000) {
calibrationInterv += 10000000;
}
lastDiff = tdiff;
/* In lock commit new values to timeInfo (hold lock as short as possible) */
EnterCriticalSection(&timeInfo.cs);
timeInfo.lastCC.perfCounter = curPerfCounter.QuadPart;
timeInfo.lastCC.fileTime = curFileTime;
timeInfo.lastCC.virtTime = vt0;
timeInfo.lastCC.counterFreq = estFreq;
timeInfo.calibNextTime = curFileTime + calibrationInterv;
LeaveCriticalSection(&timeInfo.cs);
#if 1
//printf("-------------calibration adj -- nt1:%I64d - nt0:%I64d: adj: %I64d\n", nt1, nt0, vt1);
printf("-------------calibration end, tdiff %I64d, jump -- vt:%I64d - st:%I64d: %I64d, adj: %I64d\n", tdiff,
vt0, curFileTime, (vt0 - curFileTime), vt1);
printf("-------------calibration end , new-struct: %I64d, %I64d, %I64d\n", timeInfo.lastCC.virtTime, timeInfo.lastCC.perfCounter, timeInfo.lastCC.counterFreq);
#endif
}
/*
*----------------------------------------------------------------------
*
* ResetCounterSamples --
|
| ︙ | | | ︙ | |
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
|
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 {
/*
* Estimate the frequency.
*/
workPCSample = timeInfo.perfCounterSample[timeInfo.sampleNo];
|
>
>
>
>
>
>
>
>
|
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
|
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);
#if 0
FTdiff = fileTime - timeInfo.lastCC.fileTime;
if (FTdiff) {
estFreq = 10000000 * (perfCounter - timeInfo.lastCC.perfCounter) / FTdiff;
printf("------**-----calibration estimated Frequency %I64d, %I64d, %I64d, diff: %I64d\n", fileTime, perfCounter, estFreq, estFreq - timeInfo.lastCC.counterFreq);
}
return (timeInfo.nominalFreq.QuadPart + timeInfo.lastCC.counterFreq) / 2;
#endif
return timeInfo.nominalFreq.QuadPart;
} else {
/*
* Estimate the frequency.
*/
workPCSample = timeInfo.perfCounterSample[timeInfo.sampleNo];
|
| ︙ | | | ︙ | |