Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 8001fe6

Browse files
Windows: use GetSystemTimePreciseAsFileTime if available
PostgreSQL on Windows 8 or Windows Server 2012 will now get high-resolution timestamps by dynamically loading the GetSystemTimePreciseAsFileTime function. It'll fall back to to GetSystemTimeAsFileTime if the higher precision variant isn't found, so the same binaries without problems on older Windows releases. No attempt is made to detect the Windows version. Only the presence or absence of the desired function is considered. Craig Ringer
1 parent 519b075 commit 8001fe6

File tree

3 files changed

+59
-2
lines changed

3 files changed

+59
-2
lines changed

src/backend/main/main.c

+6
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,12 @@ startup_hacks(const char *progname)
260260

261261
/* In case of general protection fault, don't show GUI popup box */
262262
SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX);
263+
264+
#ifndef HAVE_GETTIMEOFDAY
265+
/* Figure out which syscall to use to capture timestamp information */
266+
init_win32_gettimeofday();
267+
#endif
268+
263269
}
264270
#endif /* WIN32 */
265271

src/include/port.h

+2
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,8 @@ extern FILE *pgwin32_popen(const char *command, const char *type);
328328
#ifndef HAVE_GETTIMEOFDAY
329329
/* Last parameter not used */
330330
extern int gettimeofday(struct timeval * tp, struct timezone * tzp);
331+
/* On windows we need to call some backend start setup for accurate timing */
332+
extern void init_win32_gettimeofday(void);
331333
#endif
332334
#else /* !WIN32 */
333335

src/port/gettimeofday.c

+51-2
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,59 @@ static const unsigned __int64 epoch = UINT64CONST(116444736000000000);
4141
#define FILETIME_UNITS_PER_SEC 10000000L
4242
#define FILETIME_UNITS_PER_USEC 10
4343

44+
/*
45+
* Both GetSystemTimeAsFileTime and GetSystemTimePreciseAsFileTime share a
46+
* signature, so we can just store a pointer to whichever we find. This
47+
* is the pointer's type.
48+
*/
49+
typedef VOID (WINAPI *PgGetSystemTimeFn)(LPFILETIME);
50+
51+
/* Storage for the function we pick at runtime */
52+
static PgGetSystemTimeFn pg_get_system_time = NULL;
53+
54+
/*
55+
* During backend startup, determine if GetSystemTimePreciseAsFileTime is
56+
* available and use it; if not, fall back to GetSystemTimeAsFileTime.
57+
*/
58+
void
59+
init_win32_gettimeofday(void)
60+
{
61+
/*
62+
* Because it's guaranteed that kernel32.dll will be linked into our
63+
* address space already, we don't need to LoadLibrary it and worry about
64+
* closing it afterwards, so we're not using Pg's dlopen/dlsym() wrapper.
65+
*
66+
* We'll just look up the address of GetSystemTimePreciseAsFileTime if
67+
* present.
68+
*
69+
* While we could look up the Windows version and skip this on Windows
70+
* versions below Windows 8 / Windows Server 2012 there isn't much point,
71+
* and determining the windows version is its self somewhat Windows version
72+
* and development SDK specific...
73+
*/
74+
pg_get_system_time = (PgGetSystemTimeFn) GetProcAddress(
75+
GetModuleHandle(TEXT("kernel32.dll")),
76+
"GetSystemTimePreciseAsFileTime");
77+
if (pg_get_system_time == NULL)
78+
{
79+
/*
80+
* The expected error from GetLastError() is ERROR_PROC_NOT_FOUND, if
81+
* the function isn't present. No other error should occur.
82+
*
83+
* It's too early in startup to elog(...) if we get some unexpected
84+
* error, and not serious enough to warrant a fprintf to stderr about
85+
* it or save the error and report it later. So silently fall back to
86+
* GetSystemTimeAsFileTime irrespective of why the failure occurred.
87+
*/
88+
pg_get_system_time = &GetSystemTimeAsFileTime;
89+
}
90+
91+
}
92+
4493
/*
4594
* timezone information is stored outside the kernel so tzp isn't used anymore.
4695
*
47-
* Note: this function is not for Win32 high precision timing purpose. See
96+
* Note: this function is not for Win32 high precision timing purposes. See
4897
* elapsed_time().
4998
*/
5099
int
@@ -53,7 +102,7 @@ gettimeofday(struct timeval * tp, struct timezone * tzp)
53102
FILETIME file_time;
54103
ULARGE_INTEGER ularge;
55104

56-
GetSystemTimeAsFileTime(&file_time);
105+
(*pg_get_system_time)(&file_time);
57106
ularge.LowPart = file_time.dwLowDateTime;
58107
ularge.HighPart = file_time.dwHighDateTime;
59108

0 commit comments

Comments
 (0)