| ︙ | | |
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
-
-
-
-
-
-
|
#include "tclInt.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 const int normalDays[] = {
-1, 30, 58, 89, 119, 150, 180, 211, 242, 272, 303, 333, 364
|
| ︙ | | |
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
|
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
-
+
+
+
-
-
-
-
-
-
-
-
-
|
* * 10000000 / counterFreq
*/
struct {
ULONGLONG fileTime;
ULONGLONG virtTime;
volatile /* used also to compare calibration epoch */
LONGLONG perfCounter;
LONGLONG perfCounter;
LONGLONG counterFreq;
} lastCC; /* Last data updated in calibration cycle */
int freqFactor; /* Frequency factor (1000 - KHz, 1 - Hz) */
Tcl_WideInt lastUsedTime; /* Last known (caller) virtual time in 100-ns
* (used to avoid drifts after calibrate) */
/*
* Data used in developing the estimate of performance counter frequency
*/
Tcl_WideUInt fileTimeSample[SAMPLES];
/* Last 64 samples of system time. */
Tcl_WideInt perfCounterSample[SAMPLES];
/* Last 64 samples of performance counter. */
int sampleNo; /* Current sample number. */
} TimeInfo;
static TimeInfo timeInfo = {
{ NULL, 0, 0, NULL, NULL, 0 },
0,
0,
(Tcl_WideInt) 0,
|
| ︙ | | |
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
-
+
-
-
+
-
|
#endif
{
(ULONGLONG) 0,
(ULONGLONG) 0,
(LONGLONG) 0,
(LONGLONG) 0
},
(Tcl_WideInt) 0,
1000, /* KHz */
{ (Tcl_WideUInt) 0 },
{ (Tcl_WideInt) 0 },
(Tcl_WideInt) 0
0
};
/*
* Scale to convert wide click values from the TclpGetWideClicks native
* resolution to microsecond resolution and back.
*/
static struct {
|
| ︙ | | |
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
-
-
-
-
|
* Declarations for functions defined later in this file.
*/
static struct tm * ComputeGMT(const time_t *tp);
static void StopCalibration(ClientData clientData);
static DWORD WINAPI CalibrationThread(LPVOID arg);
static void UpdateTimeEachSecond(void);
static void ResetCounterSamples(Tcl_WideUInt fileTime,
Tcl_WideInt perfCounter, Tcl_WideInt perfFreq);
static Tcl_WideInt AccumulateSample(Tcl_WideInt perfCounter,
Tcl_WideUInt fileTime);
static void NativeScaleTime(Tcl_Time* timebuf,
ClientData clientData);
static Tcl_WideInt NativeGetMicroseconds(void);
static void NativeGetTime(Tcl_Time* timebuf,
ClientData clientData);
/*
|
| ︙ | | |
653
654
655
656
657
658
659
660
661
662
663
664
665
666
|
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
|
+
+
+
+
+
+
|
/*
* If the performance counter is available, start a thread to
* calibrate it.
*/
if (timeInfo.perfCounterAvailable) {
DWORD id;
/* Some systems having frequency in Hz, so save the factor here */
if (timeInfo.nominalFreq.QuadPart >= 1000000000) {
/* assume that frequency in Hz, factor used only for tolerance */
timeInfo.freqFactor = 1;
}
InitializeCriticalSection(&timeInfo.cs);
timeInfo.readyEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
timeInfo.exitEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
timeInfo.calibrationThread = CreateThread(NULL, 256,
CalibrationThread, (LPVOID) NULL, 0, &id);
SetThreadPriority(timeInfo.calibrationThread,
|
| ︙ | | |
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
|
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
|
-
-
-
-
|
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.
* When it wakes up, it will release the initialization lock.
*/
if (timeInfo.perfCounterAvailable) {
UpdateTimeEachSecond();
|
| ︙ | | |
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
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
1311
1312
1313
1314
1315
1316
1317
|
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
|
-
-
-
-
-
+
+
-
+
-
-
+
+
+
-
+
+
-
+
+
-
+
-
+
-
-
+
|
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. */
Tcl_WideInt vt1; /* Interim virtual time used during adjustments */
Tcl_WideInt tdiff; /* Difference between system clock and Tcl
* time. */
Tcl_WideInt tdiff, /* Difference between system clock and Tcl time. */
lastDiff; /* Difference of last calibration. */
printf("-------------calibration start, prev-struct: %I64d, %I64d, %I64d\n", timeInfo.lastCC.fileTime, timeInfo.lastCC.perfCounter, timeInfo.lastCC.counterFreq);
/*
* Sample system time (from posix epoch) and performance counter.
*/
curFileTime = GetSystemTimeAsVirtual();
QueryPerformanceCounter(&curPerfCounter);
printf("-------------calibration start, prev-struct: %I64d, %I64d, %I64d, pc-diff: %I64d\n", timeInfo.lastCC.fileTime, timeInfo.lastCC.perfCounter, timeInfo.lastCC.counterFreq, curPerfCounter.QuadPart - timeInfo.lastCC.perfCounter);
/*
* Current virtual time:
* vt0 = lastCC.fileTime +
* Current virtual time (using average between fileTime and virtTime):
* vt0 = (lastCC.fileTime + lastCC.virtTime) / 2 +
* 10000000 * (curPerfCounter - lastCC.perfCounter) / lastCC.counterFreq
*/
vt0 = NativeCalc100NsTicks(
vt0 = NativeCalc100NsTicks((timeInfo.lastCC.fileTime/2 + timeInfo.lastCC.virtTime/2),
(timeInfo.lastCC.fileTime/2 + timeInfo.lastCC.virtTime/2),
timeInfo.lastCC.perfCounter, timeInfo.lastCC.counterFreq,
curPerfCounter.QuadPart);
/* Differences between virtual and real-time */
tdiff = vt0 - curFileTime; /* discrepancy between virtual and real-time */
tdiff = vt0 - curFileTime;
lastDiff = timeInfo.lastCC.virtTime - timeInfo.lastCC.fileTime;
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 1
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 > -10000 && tdiff < 10000 /* very small discrepancy (1ms) */
&& (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);
//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
|
| ︙ | | |
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
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
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
|
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
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
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
|
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
+
+
+
+
+
-
+
|
return;
}
/*
* 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 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.
*/
#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.
*
* 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
* vt1 = 10000000 + curFileTime
*
* The frequency that we need to use to drift the counter back into place
* is estFreq * 20000000 / (vt1 - vt0)
* is estFreq * 10000000 / (vt1 - vt0)
*
* 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.
*/
if (tdiff > 10000000 || tdiff < -10000000) {
/* More as a second difference, so could be a time-switch
/* More as a second difference, so could be a time-switch (reset)
/* 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. */
/*
* Estimate current frequency corresponding current time / counter.
*/
vt1 = curFileTime - timeInfo.lastCC.fileTime;
if (vt1 > 0) {
estFreq = (curPerfCounter.QuadPart - timeInfo.lastCC.perfCounter) * 10000000 / vt1;
/*
* 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);
}
} 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, to-nomin: %I64d\n", curFileTime, curPerfCounter.QuadPart, estFreq, estFreq - timeInfo.lastCC.counterFreq, estFreq - timeInfo.nominalFreq.QuadPart);
#if 1
/*
/* calculate new frequency and estimate drift to the next second */
vt1 = 20000000 + curFileTime;
driftFreq = (estFreq * 20000000 / (vt1 - vt0));
* Calculate new frequency and estimate drift to the next second
*/
vt1 = 10000000 + curFileTime;
driftFreq = estFreq * 10000000 / (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;
printf("------**-----calibration lastFreq: %I64d\n", timeInfo.lastCC.counterFreq);
printf("------**-----calibration estFreq: %I64d\n", estFreq);
printf("------**-----calibration driftFreq:%I64d\n", driftFreq);
/*
* 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
#else
estFreq = (estFreq + timeInfo.lastCC.counterFreq) / 2;
#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)
) {
/* Some systems having frequency in Hz, so fewer tolerant (1.5%) */
if ( timeInfo.freqFactor == 1000 /* frequency in KHz */
|| ( estFreq > (vt1 = (1000+15)*timeInfo.nominalFreq.QuadPart/1000)
|| estFreq < (vt1 = (1000-15)*timeInfo.nominalFreq.QuadPart/1000)
)
) {
estFreq = vt1;
driftBack = vt0 > curFileTime;
vt0 = curFileTime; /* too large - just reset */
estFreq = vt1;
driftBack = vt0 > curFileTime;
vt0 = curFileTime; /* too large - just reset */
printf("************ too large: %I64d\n", estFreq);
}
}
/* If possible backwards time-drifts (larger divider now) */
vt1 = 0;
if (1 || driftBack || estFreq > timeInfo.lastCC.counterFreq) {
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.
*/
|
| ︙ | | |
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
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
|
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
|
+
-
-
-
-
-
-
-
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
|
vt0 += vt1;
tdiff += vt1;
//////////////////////////////////////////estFreq = 10000000 * (vt0 - timeInfo.lastCC.perfCounter) / vt1;
}
}
/* if still precise enough, grow calibration interval up to 10 seconds */
if ( timeInfo.freqFactor == 1000 ) { /* frequency in KHz */
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;
}
if (tdiff < -100000 || tdiff > 100000 /* 10-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 --
*
* 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
ResetCounterSamples(
Tcl_WideUInt fileTime, /* Current file time */
Tcl_WideInt perfCounter, /* Current performance counter */
Tcl_WideInt perfFreq) /* 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 performance frequency
* (not the actual, because the actual has probably run slow in the first
* case).
*/
static Tcl_WideInt
AccumulateSample(
Tcl_WideInt perfCounter,
Tcl_WideUInt fileTime)
{
Tcl_WideUInt workFTSample; /* File time sample being removed from or
* added to the circular buffer. */
Tcl_WideInt workPCSample; /* Performance counter sample being removed
* from or added to the circular buffer. */
Tcl_WideUInt lastFTSample; /* Last file time sample recorded */
Tcl_WideInt lastPCSample; /* Last performance counter sample recorded */
Tcl_WideInt FTdiff; /* Difference between last FT and current */
Tcl_WideInt PCdiff; /* Difference between last PC and current */
Tcl_WideInt 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);
#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];
workFTSample = timeInfo.fileTimeSample[timeInfo.sampleNo];
estFreq = 10000000 * (perfCounter - workPCSample)
/ (fileTime - workFTSample);
timeInfo.perfCounterSample[timeInfo.sampleNo] = perfCounter;
timeInfo.fileTimeSample[timeInfo.sampleNo] = (Tcl_WideInt) fileTime;
/*
* Advance the sample number.
*/
if (++timeInfo.sampleNo >= SAMPLES) {
timeInfo.sampleNo = 0;
}
return estFreq;
}
}
/*
*----------------------------------------------------------------------
*
* TclpGmtime --
*
* Wrapper around the 'gmtime' library function to make it thread safe.
|
| ︙ | | |