From 8dde174dd4188f1b4d55f6d88a01a1d3887f507b Mon Sep 17 00:00:00 2001 From: sebres Date: Thu, 9 Feb 2017 13:45:14 +0000 Subject: =?UTF-8?q?[win]=20accomplished=20winTime=20module=20using=20very?= =?UTF-8?q?=20fast=20wide=20clicks,=20with=20denominator=20scale=20to/from?= =?UTF-8?q?=20microseconds,=20and=20therefore=20more=20precise=20"timerate?= =?UTF-8?q?"=20results=20under=20windows=20(using=20similar=20mechanisms?= =?UTF-8?q?=20as=20by=20Mac=20OSX).=20Especially=20multi-threaded,=20becau?= =?UTF-8?q?se=20it=20works=20without=20lock=20opposite=20to=20microseconds?= =?UTF-8?q?=20(that=20use=20crictical=20section,=20because=20of=20the=20ca?= =?UTF-8?q?libration=20thread).=20The=20reason=20for=20usage=20of=20wide?= =?UTF-8?q?=20clicks=20instead=20microseconds=20explains=20following=20exa?= =?UTF-8?q?mple=20(shows=2020%=20performance=20deference):=20%=20timerate?= =?UTF-8?q?=20-calibrate=20{}=20%=20timerate=20{clock=20microseconds}=2050?= =?UTF-8?q?00=200.297037=20=C2=B5s/#=2014465901=20#=203366585=20#/sec=2042?= =?UTF-8?q?96.906=20nett-ms=20%=20timerate=20{clock=20clicks}=205000=200.2?= =?UTF-8?q?47797=20=C2=B5s/#=2016869084=20#=204035554=20#/sec=204180.116?= =?UTF-8?q?=20nett-ms?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- generic/tclInt.h | 10 ++---- win/tclWinTime.c | 107 +++++++++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 103 insertions(+), 14 deletions(-) diff --git a/generic/tclInt.h b/generic/tclInt.h index fb0bcb7..3c21de0 100644 --- a/generic/tclInt.h +++ b/generic/tclInt.h @@ -3186,6 +3186,7 @@ MODULE_SCOPE int TclpLoadMemory(Tcl_Interp *interp, void *buffer, MODULE_SCOPE void TclInitThreadStorage(void); MODULE_SCOPE void TclFinalizeThreadDataThread(void); MODULE_SCOPE void TclFinalizeThreadStorage(void); + #ifdef TCL_WIDE_CLICKS MODULE_SCOPE Tcl_WideInt TclpGetWideClicks(void); MODULE_SCOPE double TclpWideClicksToNanoseconds(Tcl_WideInt clicks); @@ -3194,17 +3195,12 @@ MODULE_SCOPE double TclpWideClickInMicrosec(void); # ifdef _WIN32 # define TCL_WIDE_CLICKS 1 MODULE_SCOPE Tcl_WideInt TclpGetWideClicks(void); +MODULE_SCOPE double TclpWideClickInMicrosec(void); # define TclpWideClicksToNanoseconds(clicks) \ - ((double)(clicks) * 1000) -# define TclpWideClickInMicrosec() (1) + ((double)(clicks) * TclpWideClickInMicrosec() * 1000) # endif #endif -#ifndef _WIN32 MODULE_SCOPE Tcl_WideInt TclpGetMicroseconds(void); -#else -# define TclpGetMicroseconds() \ - TclpGetWideClicks() -#endif MODULE_SCOPE int TclZlibInit(Tcl_Interp *interp); MODULE_SCOPE void * TclpThreadCreateKey(void); diff --git a/win/tclWinTime.c b/win/tclWinTime.c index 06ea6cd..7cbc1ba 100644 --- a/win/tclWinTime.c +++ b/win/tclWinTime.c @@ -110,6 +110,17 @@ static TimeInfo timeInfo = { }; /* + * Scale to convert wide click values from the TclpGetWideClicks native + * resolution to microsecond resolution and back. + */ +static struct { + int initialized; /* 1 if initialized, 0 otherwise */ + int perfCounter; /* 1 if performance counter usable for wide clicks */ + double microsecsScale; /* Denominator scale between clock / microsecs */ +} wideClick = {0, 0.0}; + + +/* * Declarations for functions defined later in this file. */ @@ -221,7 +232,7 @@ TclpGetClicks(void) * resolution clock in microseconds available on the system. * * Results: - * Number of microseconds (from the epoch). + * Number of microseconds (from some start time). * * Side effects: * This should be used for time-delta resp. for measurement purposes @@ -234,6 +245,87 @@ TclpGetClicks(void) Tcl_WideInt TclpGetWideClicks(void) { + LARGE_INTEGER curCounter; + + if (!wideClick.initialized) { + LARGE_INTEGER perfCounterFreq; + + /* + * The frequency of the performance counter is fixed at system boot and + * is consistent across all processors. Therefore, the frequency need + * only be queried upon application initialization. + */ + if (QueryPerformanceFrequency(&perfCounterFreq)) { + wideClick.perfCounter = 1; + wideClick.microsecsScale = 1000000.0 / perfCounterFreq.QuadPart; + } else { + /* fallback using microseconds */ + wideClick.perfCounter = 0; + wideClick.microsecsScale = 1; + } + + wideClick.initialized = 1; + } + if (wideClick.perfCounter) { + if (QueryPerformanceCounter(&curCounter)) { + return (Tcl_WideInt)curCounter.QuadPart; + } + /* fallback using microseconds */ + wideClick.perfCounter = 0; + wideClick.microsecsScale = 1; + return TclpGetMicroseconds(); + } else { + return TclpGetMicroseconds(); + } +} + +/* + *---------------------------------------------------------------------- + * + * TclpWideClickInMicrosec -- + * + * This procedure return scale to convert wide click values from the + * TclpGetWideClicks native resolution to microsecond resolution + * and back. + * + * Results: + * 1 click in microseconds as double. + * + * Side effects: + * None. + * + *---------------------------------------------------------------------- + */ + +double +TclpWideClickInMicrosec(void) +{ + if (!wideClick.initialized) { + (void)TclpGetWideClicks(); /* initialize */ + } + return wideClick.microsecsScale; +} + +/* + *---------------------------------------------------------------------- + * + * 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 +TclpGetMicroseconds(void) +{ Tcl_WideInt usecSincePosixEpoch; /* Try to use high resolution timer */ @@ -346,6 +438,9 @@ NativeScaleTime( static Tcl_WideInt NativeGetMicroseconds(void) { + static LARGE_INTEGER posixEpoch; + /* Posix epoch expressed as 100-ns ticks since + * the windows epoch. */ /* * Initialize static storage on the first trip through. * @@ -356,6 +451,10 @@ NativeGetMicroseconds(void) if (!timeInfo.initialized) { TclpInitLock(); if (!timeInfo.initialized) { + + posixEpoch.LowPart = 0xD53E8000; + posixEpoch.HighPart = 0x019DB1DE; + timeInfo.perfCounterAvailable = QueryPerformanceFrequency(&timeInfo.nominalFreq); @@ -468,15 +567,9 @@ NativeGetMicroseconds(void) /* Current performance counter. */ Tcl_WideInt curFileTime;/* Current estimated time, expressed as 100-ns * ticks since the Windows epoch. */ - static LARGE_INTEGER posixEpoch; - /* Posix epoch expressed as 100-ns ticks since - * the windows epoch. */ Tcl_WideInt usecSincePosixEpoch; /* Current microseconds since Posix epoch. */ - posixEpoch.LowPart = 0xD53E8000; - posixEpoch.HighPart = 0x019DB1DE; - QueryPerformanceCounter(&curCounter); /* -- cgit v0.12