Skip to content

Commit 18f9ed3

Browse files
committed
Bug 1843842 part 1 - Always use GetSystemTimePreciseAsFileTime on Windows. r=arai,emk
This was not supported on older Windows versions, but we now only support Windows 10+. Differential Revision: https://phabricator.services.mozilla.com/D183725
1 parent 8244a6c commit 18f9ed3

4 files changed

Lines changed: 10 additions & 197 deletions

File tree

‎js/src/moz.build‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,6 @@ if CONFIG["MOZ_INSTRUMENTS"]:
484484
if CONFIG["OS_ARCH"] == "WINNT":
485485
OS_LIBS += [
486486
"advapi32",
487-
"winmm",
488487
]
489488
UNIFIED_SOURCES += [
490489
"threading/windows/CpuCount.cpp",

‎js/src/vm/Initialization.cpp‎

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,6 @@ JS_PUBLIC_API const char* JS::detail::InitWithFailureDiagnostic(
129129
install_rust_hooks();
130130
#endif
131131

132-
PRMJ_NowInit();
133-
134132
if (frontendOnly == FrontendOnly::No) {
135133
// The first invocation of `ProcessCreation` creates a temporary thread
136134
// and crashes if that fails, i.e. because we're out of memory. To prevent
@@ -279,17 +277,6 @@ static void ShutdownImpl(JS::detail::FrontendOnly frontendOnly) {
279277

280278
js::wasm::ShutDown();
281279

282-
// The only difficult-to-address reason for the restriction that you can't
283-
// call JS_Init/stuff/JS_ShutDown multiple times is the Windows PRMJ
284-
// NowInit initialization code, which uses PR_CallOnce to initialize the
285-
// PRMJ_Now subsystem. (For reinitialization to be permitted, we'd need to
286-
// "reset" the called-once status -- doable, but more trouble than it's
287-
// worth now.) Initializing that subsystem from JS_Init eliminates the
288-
// problem, but initialization can take a comparatively long time (15ms or
289-
// so), so we really don't want to do it in JS_Init, and we really do want
290-
// to do it only when PRMJ_Now is eventually called.
291-
PRMJ_NowShutdown();
292-
293280
#if JS_HAS_INTL_API
294281
mozilla::intl::ICU4CLibrary::Cleanup();
295282
#endif // JS_HAS_INTL_API

‎js/src/vm/Time.cpp‎

Lines changed: 10 additions & 169 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@
88

99
#include "vm/Time.h"
1010

11-
#include "mozilla/DebugOnly.h"
12-
#include "mozilla/MathAlgorithms.h"
13-
1411
#ifdef SOLARIS
1512
# define _REENTRANT 1
1613
#endif
@@ -22,9 +19,8 @@
2219
#ifdef XP_WIN
2320
# include <windef.h>
2421
# include <winbase.h>
25-
# include <crtdbg.h> /* for _CrtSetReportMode */
26-
# include <mmsystem.h> /* for timeBegin/EndPeriod */
27-
# include <stdlib.h> /* for _set_invalid_parameter_handler */
22+
# include <crtdbg.h> /* for _CrtSetReportMode */
23+
# include <stdlib.h> /* for _set_invalid_parameter_handler */
2824
#endif
2925

3026
#ifdef XP_UNIX
@@ -37,8 +33,6 @@ extern int gettimeofday(struct timeval* tv);
3733

3834
#endif /* XP_UNIX */
3935

40-
using mozilla::DebugOnly;
41-
4236
#if defined(XP_UNIX)
4337
int64_t PRMJ_Now() {
4438
struct timeval tv;
@@ -54,6 +48,10 @@ int64_t PRMJ_Now() {
5448

5549
#else
5650

51+
# if _WIN32_WINNT < _WIN32_WINNT_WIN8
52+
extern "C" WINBASEAPI void WINAPI GetSystemTimePreciseAsFileTime(LPFILETIME);
53+
# endif
54+
5755
// Returns the number of microseconds since the Unix epoch.
5856
static double FileTimeToUnixMicroseconds(const FILETIME& ft) {
5957
// Get the time in 100ns intervals.
@@ -68,167 +66,10 @@ static double FileTimeToUnixMicroseconds(const FILETIME& ft) {
6866
return double(t) * 0.1;
6967
}
7068

71-
struct CalibrationData {
72-
double freq; /* The performance counter frequency */
73-
double offset; /* The low res 'epoch' */
74-
double timer_offset; /* The high res 'epoch' */
75-
76-
bool calibrated;
77-
78-
CRITICAL_SECTION data_lock;
79-
};
80-
81-
static CalibrationData calibration = {0};
82-
83-
static void NowCalibrate() {
84-
MOZ_ASSERT(calibration.freq > 0);
85-
86-
// By wrapping a timeBegin/EndPeriod pair of calls around this loop,
87-
// the loop seems to take much less time (1 ms vs 15ms) on Vista.
88-
timeBeginPeriod(1);
89-
FILETIME ft, ftStart;
90-
GetSystemTimeAsFileTime(&ftStart);
91-
do {
92-
GetSystemTimeAsFileTime(&ft);
93-
} while (memcmp(&ftStart, &ft, sizeof(ft)) == 0);
94-
timeEndPeriod(1);
95-
96-
LARGE_INTEGER now;
97-
QueryPerformanceCounter(&now);
98-
99-
calibration.offset = FileTimeToUnixMicroseconds(ft);
100-
calibration.timer_offset = double(now.QuadPart);
101-
calibration.calibrated = true;
102-
}
103-
104-
static const unsigned DataLockSpinCount = 4096;
105-
106-
static void(WINAPI* pGetSystemTimePreciseAsFileTime)(LPFILETIME) = nullptr;
107-
108-
void PRMJ_NowInit() {
109-
memset(&calibration, 0, sizeof(calibration));
110-
111-
// According to the documentation, QueryPerformanceFrequency will never
112-
// return false or return a non-zero frequency on systems that run
113-
// Windows XP or later. Also, the frequency is fixed so we only have to
114-
// query it once.
115-
LARGE_INTEGER liFreq;
116-
DebugOnly<BOOL> res = QueryPerformanceFrequency(&liFreq);
117-
MOZ_ASSERT(res);
118-
calibration.freq = double(liFreq.QuadPart);
119-
MOZ_ASSERT(calibration.freq > 0.0);
120-
121-
InitializeCriticalSectionAndSpinCount(&calibration.data_lock,
122-
DataLockSpinCount);
123-
124-
// Windows 8 has a new API function we can use.
125-
if (HMODULE h = GetModuleHandle("kernel32.dll")) {
126-
pGetSystemTimePreciseAsFileTime = (void(WINAPI*)(LPFILETIME))GetProcAddress(
127-
h, "GetSystemTimePreciseAsFileTime");
128-
}
129-
}
130-
131-
void PRMJ_NowShutdown() { DeleteCriticalSection(&calibration.data_lock); }
132-
133-
# define MUTEX_LOCK(m) EnterCriticalSection(m)
134-
# define MUTEX_UNLOCK(m) LeaveCriticalSection(m)
135-
# define MUTEX_SETSPINCOUNT(m, c) SetCriticalSectionSpinCount((m), (c))
136-
137-
// Please see bug 363258 for why the win32 timing code is so complex.
138-
static int64_t PRMJ_Now() {
139-
if (pGetSystemTimePreciseAsFileTime) {
140-
// Windows 8 has a new API function that does all the work.
141-
FILETIME ft;
142-
pGetSystemTimePreciseAsFileTime(&ft);
143-
return int64_t(FileTimeToUnixMicroseconds(ft));
144-
}
145-
146-
bool calibrated = false;
147-
bool needsCalibration = !calibration.calibrated;
148-
double cachedOffset = 0.0;
149-
while (true) {
150-
if (needsCalibration) {
151-
MUTEX_LOCK(&calibration.data_lock);
152-
153-
// Recalibrate only if no one else did before us.
154-
if (calibration.offset == cachedOffset) {
155-
// Since calibration can take a while, make any other
156-
// threads immediately wait.
157-
MUTEX_SETSPINCOUNT(&calibration.data_lock, 0);
158-
159-
NowCalibrate();
160-
161-
calibrated = true;
162-
163-
// Restore spin count.
164-
MUTEX_SETSPINCOUNT(&calibration.data_lock, DataLockSpinCount);
165-
}
166-
167-
MUTEX_UNLOCK(&calibration.data_lock);
168-
}
169-
170-
// Calculate a low resolution time.
171-
FILETIME ft;
172-
GetSystemTimeAsFileTime(&ft);
173-
double lowresTime = FileTimeToUnixMicroseconds(ft);
174-
175-
// Grab high resolution time.
176-
LARGE_INTEGER now;
177-
QueryPerformanceCounter(&now);
178-
double highresTimerValue = double(now.QuadPart);
179-
180-
MUTEX_LOCK(&calibration.data_lock);
181-
double highresTime = calibration.offset +
182-
PRMJ_USEC_PER_SEC *
183-
(highresTimerValue - calibration.timer_offset) /
184-
calibration.freq;
185-
cachedOffset = calibration.offset;
186-
MUTEX_UNLOCK(&calibration.data_lock);
187-
188-
// Assume the NT kernel ticks every 15.6 ms. Unfortunately there's no
189-
// good way to determine this (NtQueryTimerResolution is an undocumented
190-
// API), but 15.6 ms seems to be the max possible value. Hardcoding 15.6
191-
// means we'll recalibrate if the highres and lowres timers diverge by
192-
// more than 30 ms.
193-
static const double KernelTickInMicroseconds = 15625.25;
194-
195-
// Check for clock skew.
196-
double diff = lowresTime - highresTime;
197-
198-
// For some reason that I have not determined, the skew can be
199-
// up to twice a kernel tick. This does not seem to happen by
200-
// itself, but I have only seen it triggered by another program
201-
// doing some kind of file I/O. The symptoms are a negative diff
202-
// followed by an equally large positive diff.
203-
if (mozilla::Abs(diff) <= 2 * KernelTickInMicroseconds) {
204-
// No detectable clock skew.
205-
return int64_t(highresTime);
206-
}
207-
208-
if (calibrated) {
209-
// If we already calibrated once this instance, and the
210-
// clock is still skewed, then either the processor(s) are
211-
// wildly changing clockspeed or the system is so busy that
212-
// we get switched out for long periods of time. In either
213-
// case, it would be infeasible to make use of high
214-
// resolution results for anything, so let's resort to old
215-
// behavior for this call. It's possible that in the
216-
// future, the user will want the high resolution timer, so
217-
// we don't disable it entirely.
218-
return int64_t(lowresTime);
219-
}
220-
221-
// It is possible that when we recalibrate, we will return a
222-
// value less than what we have returned before; this is
223-
// unavoidable. We cannot tell the different between a
224-
// faulty QueryPerformanceCounter implementation and user
225-
// changes to the operating system time. Since we must
226-
// respect user changes to the operating system time, we
227-
// cannot maintain the invariant that Date.now() never
228-
// decreases; the old implementation has this behavior as
229-
// well.
230-
needsCalibration = true;
231-
}
69+
int64_t PRMJ_Now() {
70+
FILETIME ft;
71+
GetSystemTimePreciseAsFileTime(&ft);
72+
return int64_t(FileTimeToUnixMicroseconds(ft));
23273
}
23374
#endif
23475

‎js/src/vm/Time.h‎

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,6 @@ struct PRMJTime {
3737
/* Return the current local time in micro-seconds */
3838
extern int64_t PRMJ_Now();
3939

40-
/* Initialize the resources associated with PRMJ_Now. */
41-
#if defined(XP_WIN)
42-
extern void PRMJ_NowInit();
43-
#else
44-
inline void PRMJ_NowInit() {}
45-
#endif
46-
47-
/* Release the resources associated with PRMJ_Now; don't call PRMJ_Now again */
48-
#ifdef XP_WIN
49-
extern void PRMJ_NowShutdown();
50-
#else
51-
inline void PRMJ_NowShutdown() {}
52-
#endif
53-
5440
#if !JS_HAS_INTL_API
5541
/* Format a time value into a buffer. Same semantics as strftime() */
5642
extern size_t PRMJ_FormatTime(char* buf, size_t buflen, const char* fmt,

0 commit comments

Comments
 (0)