File: TimeStamp_windows.cpp

package info (click to toggle)
firefox 147.0.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,683,320 kB
  • sloc: cpp: 7,607,359; javascript: 6,533,295; ansic: 3,775,223; python: 1,415,500; xml: 634,561; asm: 438,949; java: 186,241; sh: 62,752; makefile: 18,079; objc: 13,092; perl: 12,808; yacc: 4,583; cs: 3,846; pascal: 3,448; lex: 1,720; ruby: 1,003; php: 436; lisp: 258; awk: 247; sql: 66; sed: 54; csh: 10; exp: 6
file content (128 lines) | stat: -rw-r--r-- 4,558 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "mozilla/DynamicallyLinkedFunctionPtr.h"
#include "mozilla/TimeStamp.h"
#include <intrin.h>
#include <windows.h>

// Historical note: We used to sample both QueryPerformanceCounter (QPC) and
// GetTickCount (GTC) timestamps in the past, as very early implementations of
// QPC were buggy. We had heuristics to determine if QPC is unreliable and
// would have switched to GTC in case, which could cause unexpected time
// travels between QPC and GPC values when that occured.
//
// Since Windows 8 together with the then modern CPUs, QPC became both reliable
// and almost as fast as GTC timestamps and provides a much higher resolution.
// QPC in general exists long enough even on older systems than Windows 8, such
// that we can just always rely on it, as we do in rust.

// ----------------------------------------------------------------------------
// Global variables, not changing at runtime
// ----------------------------------------------------------------------------

// Result of QueryPerformanceFrequency, set only once on startup.
static double sTicksPerSecd;
static double sTicksPerMsd;

// ----------------------------------------------------------------------------
// Useful constants
// ----------------------------------------------------------------------------

static constexpr double kMsPerSecd = 1000.0;

namespace mozilla {

// Result is in ticks.
static inline ULONGLONG PerformanceCounter() {
  LARGE_INTEGER pc;
  MOZ_ALWAYS_TRUE(::QueryPerformanceCounter(&pc));
  return pc.QuadPart;
}

static void InitConstants() {
  // Query the frequency from QPC and rely on it for all values.
  // Note: The resolution used to be sampled based on a loop of QPC calls.
  // While it is true that on most systems we cannot expect to subsequently
  // sample QPC values as fast as the QPC frequency, we still will get that
  // as resolution of the sampled values, that is we have 1 tick resolution.
  LARGE_INTEGER freq;
  bool hasQPC = ::QueryPerformanceFrequency(&freq);
  MOZ_RELEASE_ASSERT(hasQPC);
  sTicksPerSecd = double(freq.QuadPart);
  sTicksPerMsd = sTicksPerSecd / kMsPerSecd;
}

// ----------------------------------------------------------------------------
// TimeDuration and TimeStamp implementation
// ----------------------------------------------------------------------------

MFBT_API double BaseTimeDurationPlatformUtils::ToSeconds(int64_t aTicks) {
  return double(aTicks) / sTicksPerSecd;
}

MFBT_API int64_t
BaseTimeDurationPlatformUtils::TicksFromMilliseconds(double aMilliseconds) {
  double result = sTicksPerMsd * aMilliseconds;
  // NOTE: this MUST be a >= test, because int64_t(double(INT64_MAX))
  // overflows and gives INT64_MIN.
  if (result >= double(INT64_MAX)) {
    return INT64_MAX;
  }
  if (result <= double(INT64_MIN)) {
    return INT64_MIN;
  }

  return (int64_t)result;
}

// Note that we init early enough during startup such that we are supposed to
// not yet have started other threads which could try to use us.
static bool gInitialized = false;

MFBT_API void TimeStamp::Startup() {
  if (gInitialized) {
    return;
  }
  InitConstants();
  gInitialized = true;
}

MFBT_API void TimeStamp::Shutdown() {}

MFBT_API TimeStamp TimeStamp::Now(bool aHighResolution) {
  MOZ_ASSERT(gInitialized);
  return TimeStamp((TimeStampValue)PerformanceCounter());
}

// Computes and returns the process uptime in microseconds.
// Returns 0 if an error was encountered.

MFBT_API uint64_t TimeStamp::ComputeProcessUptime() {
  FILETIME start, foo, bar, baz;
  bool success = GetProcessTimes(GetCurrentProcess(), &start, &foo, &bar, &baz);
  if (!success) {
    return 0;
  }

  static const StaticDynamicallyLinkedFunctionPtr<void(WINAPI*)(LPFILETIME)>
      pGetSystemTimePreciseAsFileTime(L"kernel32.dll",
                                      "GetSystemTimePreciseAsFileTime");

  FILETIME now;
  if (pGetSystemTimePreciseAsFileTime) {
    pGetSystemTimePreciseAsFileTime(&now);
  } else {
    GetSystemTimeAsFileTime(&now);
  }

  ULARGE_INTEGER startUsec = {{start.dwLowDateTime, start.dwHighDateTime}};
  ULARGE_INTEGER nowUsec = {{now.dwLowDateTime, now.dwHighDateTime}};

  return (nowUsec.QuadPart - startUsec.QuadPart) / 10ULL;
}

}  // namespace mozilla