Diff
Not logged in

Differences From Artifact [6e26b49e0d]:

To Artifact [131c1c565b]:


40
41
42
43
44
45
46

47
48
49
50
51
52
53
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54







+







 * time is always equal to:
 *    virtTimeBase + (currentPerfCounter - perfCounter)
 *			* 10000000 / nominalFreq
 */
typedef struct TimeCalibInfo {
    LONGLONG perfCounter;	/* QPC value of last calibrated virtual time */
    Tcl_WideInt virtTimeBase;	/* Last virtual time base (in 100-ns) */
    Tcl_WideInt monoTimeBase;	/* Last monotonic time base (in 100-ns) */
    Tcl_WideInt sysTime;	/* Last real system time (in 100-ns),
				   truncated to VT_SYSTMR_DIST (100ms) */
} TimeCalibInfo;

/* Milliseconds <-> 100-ns ticks */
#define MsToT100ns(ms)   ((ms) * 10000)
#define T100nsToMs(ms)   ((ms) / 10000)
97
98
99
100
101
102
103
104

105
106
107
108
109
110
111
112
113
114
98
99
100
101
102
103
104

105
106


107
108
109
110
111
112
113







-
+

-
-







#endif
    LARGE_INTEGER posixEpoch;	/* Posix epoch expressed as 100-ns ticks since
				 * the windows epoch. */
    TimeCalibInfo lastCI;	/* Last virtual timer-data updated in the
				 * calibration process. */
    volatile LONG lastCIEpoch;	/* Calibration epoch (increased each 100ms) */
    
    size_t lastUsedTime;	/* Last known (caller) offset to virtual time
    size_t lastUsedTime;	/* Last known (caller) offset to time base
				 * (used to avoid back-drifts after calibrate) */
    size_t lastTimeJumpEpoch;	/* Last known epoch since last time-jump. */
    Tcl_WideInt lastTimeJump;	/* Last known time-jump of thread. */
} TimeInfo;

static TimeInfo timeInfo = {
    { NULL, 0, 0, NULL, NULL, 0 },
    0,
    0,
    (LONGLONG) 0,
143
144
145
146
147
148
149
150

151
152
153
154
155
156
157
142
143
144
145
146
147
148

149
150
151
152
153
154
155
156







-
+







/*
 * Declarations for functions defined later in this file.
 */

static struct tm *	ComputeGMT(const time_t *tp);
static void		NativeScaleTime(Tcl_Time* timebuf,
			    ClientData clientData);
static Tcl_WideInt	NativeGetMicroseconds(void);
static Tcl_WideInt	NativeGetMicroseconds(int monotonic);
static void		NativeGetTime(Tcl_Time* timebuf,
			    ClientData clientData);

/*
 * TIP #233 (Virtualized Time): Data for the time hooks, if any.
 */

185
186
187
188
189
190
191
192

193
194

195
196


197
198


199
200
201
202
203
204
205
206
207
208
209
210

211
212
213
214
215
216
217

218
219
220

221
222
223
224
225
226
227
184
185
186
187
188
189
190

191
192

193
194
195
196
197


198
199
200
201
202
203
204
205
206
207
208
209
210

211

212
213
214
215
216

217
218
219

220
221
222
223
224
225
226
227







-
+

-
+


+
+
-
-
+
+











-
+
-





-
+


-
+







    return curCounter.QuadPart; /* no factor configured */
#endif
}

/*
 *----------------------------------------------------------------------
 *
 * NativeCalc100NsTicks --
 * NativeCalc100NsOffs --
 *
 *	Calculate the current system time in 100-ns ticks since posix epoch,
 *	Calculate the current system time in 100-ns ticks since some base,
 *	for current performance counter (curCounter), using given calibrated values.
 *
 *	offs = (curCounter - lastCI.perfCounter) * 10000000 / nominalFreq
 *
 *	vt = lastCI.virtTimeBase
 *		+ (curCounter - lastCI.perfCounter) * 10000000 / nominalFreq
 *	vt = lastCI.virtTimeBase + offs
 *	mt = lastCI.monoTimeBase + offs
 *
 * Results:
 *	Returns the wide integer with number of 100-ns ticks from the epoch.
 *
 * Side effects:
 *	None
 *
 *----------------------------------------------------------------------
 */

static inline Tcl_WideInt
NativeCalc100NsTicks(
NativeCalc100NsOffs(
    ULONGLONG ciVirtTimeBase,
    LONGLONG ciPerfCounter,
    LONGLONG curCounter
) {
    curCounter -= ciPerfCounter; /* current distance */
    if (!curCounter) {
    	return ciVirtTimeBase; /* virtual time without offset */
    	return 0; /* virtual time without offset */
    }
    /* virtual time with offset */
    return ciVirtTimeBase + curCounter * 10000000 / timeInfo.nominalFreq;
    return curCounter * 10000000 / timeInfo.nominalFreq;
}

/*
 * Representing the number of 100-nanosecond intervals since posix epoch.
 */
static inline Tcl_WideInt
GetSystemTimeAsVirtual(void)
252
253
254
255
256
257
258
259
260
261
262
263


264
265
266
267
268
269
270
271
272
252
253
254
255
256
257
258


259


260
261


262
263
264
265
266
267
268







-
-

-
-
+
+
-
-







 *
 *----------------------------------------------------------------------
 */

unsigned long
TclpGetSeconds(void)
{
    Tcl_WideInt usecSincePosixEpoch;

    /* Try to use high resolution timer */
    if ( tclGetTimeProcPtr == NativeGetTime
      && (usecSincePosixEpoch = NativeGetMicroseconds())
    if (tclGetTimeProcPtr == NativeGetTime) {
	return NativeGetMicroseconds(0) / 1000000;
    ) {
	return usecSincePosixEpoch / 1000000;
    } else {
	Tcl_Time t;

	tclGetTimeProcPtr(&t, tclTimeClientData);	/* Tcl_GetTime inlined. */
	return t.sec;
    }
}
289
290
291
292
293
294
295
296
297
298
299

300
301
302

303
304
305
306
307
308
309
285
286
287
288
289
290
291


292

293



294
295
296
297
298
299
300
301







-
-

-
+
-
-
-
+







 *
 *----------------------------------------------------------------------
 */

unsigned long
TclpGetClicks(void)
{
    Tcl_WideInt usecSincePosixEpoch;

    /* Try to use high resolution timer */
    if ( tclGetTimeProcPtr == NativeGetTime
    if (tclGetTimeProcPtr == NativeGetTime) {
      && (usecSincePosixEpoch = NativeGetMicroseconds())
    ) {
	return (unsigned long)usecSincePosixEpoch;
	return (unsigned long)NativeGetMicroseconds(1);
    } else {
	/*
	* Use the Tcl_GetTime abstraction to get the time in microseconds, as
	* nearly as we can, and return it.
	*/

	Tcl_Time now;		/* Current Tcl time */
412
413
414
415
416
417
418








419




























420
421
422

423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418

419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448

449
450
451
452
453
454
455
456
457
458
459
460

































461
462
463
464
465
466
467







+
+
+
+
+
+
+
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+


-
+











-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-







 *
 *----------------------------------------------------------------------
 */

Tcl_WideInt 
TclpGetMicroseconds(void)
{
    /* Use high resolution timer if possible */
    if (tclGetTimeProcPtr == NativeGetTime) {
    	return NativeGetMicroseconds(0);
    } else {
	/*
	 * Use the Tcl_GetTime abstraction to get the time in microseconds, as
	 * nearly as we can, and return it.
	 */
#if 1

	Tcl_Time now;

	tclGetTimeProcPtr(&now, tclTimeClientData);	/* Tcl_GetTime inlined */
	return TCL_TIME_TO_USEC(now);
    }
}

/*
 *----------------------------------------------------------------------
 *
 * TclpGetMicroseconds --
 *
 *	This procedure returns a WideInt value that represents the highest
 *	resolution clock in microseconds available on the system.
 *
 * Results:
 *	Number of microseconds (from the epoch).
 *
 * Side effects:
 *	None.
 *
 *----------------------------------------------------------------------
 */

Tcl_WideInt 
TclpGetUTimeMonotonic(void)
{
    /* Use high resolution timer if possible */
    if (tclGetTimeProcPtr == NativeGetTime) {
    	return NativeGetMicroseconds();
    	return NativeGetMicroseconds(1); /* monotonic based time */
    } else {
	/*
	 * Use the Tcl_GetTime abstraction to get the time in microseconds, as
	 * nearly as we can, and return it.
	 */

	Tcl_Time now;

	tclGetTimeProcPtr(&now, tclTimeClientData);	/* Tcl_GetTime inlined */
	return TCL_TIME_TO_USEC(now);
    }

#else
    static Tcl_WideInt prevUS = 0;

    Tcl_WideInt usecSincePosixEpoch;

    /* Try to use high resolution timer */
    if (tclGetTimeProcPtr == NativeGetTime) {
	if ( !(usecSincePosixEpoch = NativeGetMicroseconds()) ) {
	    usecSincePosixEpoch = GetSystemTimeAsVirtual() / 10; /* in 100-ns */
	    printf("!!!!!!!!!!!!!!!!!!!!!!!!!!!no-native-ms!!!!!!!!!!!\n");
	}
    } else {
	/*
	 * Use the Tcl_GetTime abstraction to get the time in microseconds, as
	 * nearly as we can, and return it.
	 */

	Tcl_Time now;

	tclGetTimeProcPtr(&now, tclTimeClientData);	/* Tcl_GetTime inlined */
	usecSincePosixEpoch = (((Tcl_WideInt)now.sec) * 1000000) + now.usec;
	printf("!!!!!!!!!!!!!!!!!!!!!!!!!!!no-native-ms!!!!!!!!!!!\n");
    }

    	if (prevUS && usecSincePosixEpoch < prevUS) {
	    printf("!!!!!!!!!!!!!!!!!!!!!!!!!!!time-backwards!!!! prev: %I64d - now: %I64d (%I64d usec)\n", prevUS, usecSincePosixEpoch, usecSincePosixEpoch - prevUS);
	    Tcl_Panic("Time running backwards!!!");
    	}
    	prevUS = usecSincePosixEpoch;

	return usecSincePosixEpoch;
#endif
}

/*
 *----------------------------------------------------------------------
 *
 * TclpGetTimeZone --
 *
517
518
519
520
521
522
523
524
525
526
527
528


529
530
531


532
533
534
535
536
537
538
511
512
513
514
515
516
517


518


519
520



521
522
523
524
525
526
527
528
529







-
-

-
-
+
+
-
-
-
+
+







 *----------------------------------------------------------------------
 */

void
Tcl_GetTime(
    Tcl_Time *timePtr)		/* Location to store time information. */
{
    Tcl_WideInt usecSincePosixEpoch;

    /* Try to use high resolution timer */
    if ( tclGetTimeProcPtr == NativeGetTime
      && (usecSincePosixEpoch = NativeGetMicroseconds())
    if ( tclGetTimeProcPtr == NativeGetTime) {
    	Tcl_WideInt now = NativeGetMicroseconds(0);
    ) {
	timePtr->sec = (long) (usecSincePosixEpoch / 1000000);
	timePtr->usec = (unsigned long) (usecSincePosixEpoch % 1000000);
	timePtr->sec = (long) (now / 1000000);
	timePtr->usec = (unsigned long) (now % 1000000);
    } else {
    	tclGetTimeProcPtr(timePtr, tclTimeClientData);
    }
}

/*
 *----------------------------------------------------------------------
569
570
571
572
573
574
575
576

577
578

579
580
581
582


583
584
585
586


587
588

589
590
591
592
593
594
595
560
561
562
563
564
565
566

567
568

569
570
571


572
573
574
575


576
577
578

579
580
581
582
583
584
585
586







-
+

-
+


-
-
+
+


-
-
+
+

-
+







 *	This procedure scales number of microseconds if expected.
 *
 * Results:
 *	Number of microseconds scaled using tclScaleTimeProcPtr.
 *
 *----------------------------------------------------------------------
 */
Tcl_WideInt
void
TclpScaleUTime(
    Tcl_WideInt usec)
    Tcl_WideInt *usec)
{
    /* Native scale is 1:1. */
    if (tclScaleTimeProcPtr == NativeScaleTime) {
	return usec;
    if (tclScaleTimeProcPtr != NativeScaleTime) {
	return;
    } else {
	Tcl_Time scTime;
	scTime.sec = usec / 1000000;
	scTime.usec = usec % 1000000;
	scTime.sec = *usec / 1000000;
	scTime.usec = *usec % 1000000;
	tclScaleTimeProcPtr(&scTime, tclTimeClientData);
	return ((Tcl_WideInt)scTime.sec) * 1000000 + scTime.usec;
	*usec = ((Tcl_WideInt)scTime.sec) * 1000000 + scTime.usec;
    }
}

/*
 *----------------------------------------------------------------------
 *
 * NativeGetMicroseconds --
609
610
611
612
613
614
615
616


617
618
619
620
621
622
623
600
601
602
603
604
605
606

607
608
609
610
611
612
613
614
615







-
+
+







 *	and monitor these values, adjusting them as necessary to correct for
 *	drift in the performance counter's oscillator.
 *
 *----------------------------------------------------------------------
 */

static Tcl_WideInt
NativeGetMicroseconds(void)
NativeGetMicroseconds(
    int monotonic)
{
    static size_t nomObtainSTPerfCntrDist = 0;
				/* Nominal distance in perf-counter ticks to
				 * obtain system timer (avoids unneeded syscalls). */
    Tcl_WideInt curTime;	/* Current time in 100-ns ticks since epoch */

    /*
741
742
743
744
745
746
747

748
749
750


751

752
753
754
755
756
757
758
733
734
735
736
737
738
739
740
741
742
743
744
745

746
747
748
749
750
751
752
753







+



+
+
-
+







	     * If the performance counter is available, initialize
	     */

	    if (timeInfo.perfCounterAvailable) {
		InitializeCriticalSection(&timeInfo.cs);

		timeInfo.lastCI.perfCounter = NativePerformanceCounter();
		/* base of the real-time (and last known system time) */
		timeInfo.lastCI.sysTime =
		    timeInfo.lastCI.virtTimeBase = GetSystemTimeAsVirtual();

		/* base of the monotonic time */
		timeInfo.lastCI.monoTimeBase = NativeCalc100NsOffs(
		timeInfo.lastTimeJumpEpoch = 1; /* let the caller know we've epoch */
			0, timeInfo.lastCI.perfCounter);
	    }
	    timeInfo.initialized = TRUE;
	}
	TclpInitUnlock();
    }

    if (timeInfo.perfCounterAvailable) {
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
806
807
808
809
810
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







-
+




+
-
+








-
+
+
+
+
+



-
+
















+
+
+





-
-

+


+
+
+
+
+
+
+
+
+
+
+







	  EnterCriticalSection(&timeInfo.cs);
	  if (ci.sysTime != trSysTime) { /* again in lock (done in other thread) */

	    /*
	     * Recalibration / Adjustment of base values.
	     */

	    Tcl_WideInt vt0;		/* Desired virtual time */
	    Tcl_WideInt vt0, vt1;	/* Desired virtual time */
	    Tcl_WideInt tdiff;		/* Time difference to the system time */
	    Tcl_WideInt lastTime;	/* Used to compare with last known time */

	    /* New desired virtual time using current base values */
	    vt1 = vt0 = ci.virtTimeBase
	    vt0 = NativeCalc100NsTicks(ci.virtTimeBase, ci.perfCounter, curCounter);
		+ NativeCalc100NsOffs(ci.perfCounter, curCounter);

	    tdiff = vt0 - sysTime;
	    /* If we can adjust offsets (not a jump to new system time) */
	    if (MsToT100ns(-800) < tdiff && tdiff < MsToT100ns(800)) {

		/* Allow small drift if discrepancy larger as expected */
//!!!		printf("************* tdiff: %I64d\n", tdiff);
		if (tdiff <= MsToT100ns(-VT_MAX_DISCREPANCY)) {
		   vt0 += MsToT100ns(VT_MAX_DRIFT_TIME);
		    vt0 += MsToT100ns(VT_MAX_DRIFT_TIME);
		}
		else
		if (tdiff <= MsToT100ns(-VT_MAX_DRIFT_TIME)) {
		    vt0 -= tdiff / 2; /* small drift forwards */
		}
		else
		if (tdiff >= MsToT100ns(VT_MAX_DISCREPANCY)) {
		   vt0 -= MsToT100ns(VT_MAX_DRIFT_TIME);
		    vt0 -= MsToT100ns(VT_MAX_DRIFT_TIME);
		}
		
		/*
		 * Be sure the clock ticks never backwards (avoid backwards 
		 * time-drifts). If time-reset (< 800ms) just use curent time
		 * (avoid time correction in such case).
		 */
		if ( (lastTime = (ci.virtTimeBase + timeInfo.lastUsedTime))
		  && (lastTime -= vt0) > 0 /* offset to vt0 */
		  && lastTime < MsToT100ns(800) /* bypass time-switch (drifts only) */
		) {
//!!!		    printf("************* forwards 1: %I64d, last-time: %I64d, distance: %I64d\n", lastTime, vt0, (vt0 - trSysTime));
		    vt0 += lastTime; /* hold on the time a bit */
//!!!		    printf("************* forwards 1: %I64d, last-time: %I64d, distance: %I64d\n", lastTime, ci.virtTimeBase, (vt0 - trSysTime));
		}

		/* difference for addjustment of monotonic base */
		tdiff = vt0 - vt1;

	    } else {
		/* 
		 * The time-jump (reset or initial), we should use system time
		 * instead of virtual to recalibrate offsets (let the time jump).
		 */
		timeInfo.lastTimeJump = T100nsToUs(sysTime - vt0); /* 100-ns */;
		timeInfo.lastTimeJumpEpoch++;
		vt0 = sysTime;
		tdiff = 0;
//!!!		printf("************* reset time: %I64d *****************\n", vt0);
	    }

	    /*
	     * Now adjust monotonic time base, note this time should absolutely
	     * never ticks backwards (relative the last known monotonic time).
	     */
	    ci.monoTimeBase += NativeCalc100NsOffs(ci.perfCounter, curCounter);
	    ci.monoTimeBase += tdiff;
	    lastTime = (timeInfo.lastCI.monoTimeBase + timeInfo.lastUsedTime);
	    if (ci.monoTimeBase < lastTime) {
		ci.monoTimeBase = lastTime; /* freeze monotonic time a bit */
	    }

	    /* 
	     * Adjustment of current base for virtual time. This will also
	     * prevent too large counter difference (resp. max distance ~ 100ms).
	     */
	    ci.virtTimeBase = vt0;
	    ci.perfCounter = curCounter;
878
879
880
881
882
883
884
885

886
887
888


889
890










891
892
893
894
895
896
897
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







-
+
-
-

+
+
-
-
+
+
+
+
+
+
+
+
+
+








//!!!	    printf("************* recalibrated: %I64d, %I64d adj. %I64d, distance: %I64d\n", vt0, ci.virtTimeBase, ci.perfCounter, (vt0 - trSysTime));
	  
	  } /* end lock */
	  LeaveCriticalSection(&timeInfo.cs);
	} /* common info lastCI contains actual data */
	
    calcVT:	
    calcVT:
	/* Calculate actual virtual time now using performance counter */
	curTime = NativeCalc100NsTicks(ci.virtTimeBase, ci.perfCounter, curCounter);

	/* Calculate actual time-offset using performance counter */
	curTime = NativeCalc100NsOffs(ci.perfCounter, curCounter);
	/* Save last used time (offset) and return virtual time */
	timeInfo.lastUsedTime = (size_t)(curTime - ci.virtTimeBase);
	/* Save last used time (offset) */
	timeInfo.lastUsedTime = (size_t)curTime;
	if (monotonic) {
	    /* Use monotonic time base */
	    curTime += ci.monoTimeBase;
	} else {
	    /* Use real-time base */
	    curTime += ci.virtTimeBase;
	}
	/* Return virtual time */
	return T100nsToUs(curTime); /* 100-ns to microseconds */
    }

    /*
     * High resolution timer is not available.
     */

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
938
939
940
941
942
943
944

945
946






947
948
949











950
951
952
953
954
955
956







-
+

-
-
-
-
-
-
+
+
+
-
-
-
-
-
-
-
-
-
-
-







 */

static void
NativeGetTime(
    Tcl_Time *timePtr,
    ClientData clientData)
{
    Tcl_WideInt usecSincePosixEpoch;
    Tcl_WideInt now;

    /*
     * Try to use high resolution timer.
     */
    if ( (usecSincePosixEpoch = NativeGetMicroseconds()) ) {
	timePtr->sec = (long) (usecSincePosixEpoch / 1000000);
	timePtr->usec = (unsigned long) (usecSincePosixEpoch % 1000000);
    now = NativeGetMicroseconds(0);
    timePtr->sec = (long) (now / 1000000);
    timePtr->usec = (unsigned long) (now % 1000000);
    } else {
	/*
	* High resolution timer is not available. Just use ftime.
	*/

	struct _timeb t;

	_ftime(&t);
	timePtr->sec = (long)t.time;
	timePtr->usec = t.millitm * 1000;
    }
}

/*
 *----------------------------------------------------------------------
 *
 * TclpGetTZName --
 *
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
1375
1376
1377
1378
1379
1380
1381















1382
1383
1384
1385
1386
1387
1388
1389







-
-
-
-
-
-
-
-
-
-
-
-
-
-
-








    if (scaleProc) {
	*scaleProc = tclScaleTimeProcPtr;
    }
    if (clientData) {
	*clientData = tclTimeClientData;
    }
}

Tcl_WideInt
TclpGetLastTimeJump(size_t *epoch)
{
    if (timeInfo.lastTimeJumpEpoch > *epoch) {
    	*epoch = timeInfo.lastTimeJumpEpoch;
	return timeInfo.lastTimeJump;
    };
    return 0;
}
size_t
TclpGetLastTimeJumpEpoch(void)
{
    return timeInfo.lastTimeJumpEpoch;
}

/*
 * Local Variables:
 * mode: c
 * c-basic-offset: 4
 * fill-column: 78
 * End:
 */