1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 // Windows Timer Primer
6 //
7 // A good article:  http://www.ddj.com/windows/184416651
8 // A good mozilla bug:  http://bugzilla.mozilla.org/show_bug.cgi?id=363258
9 //
10 // The default windows timer, GetSystemTimeAsFileTime is not very precise.
11 // It is only good to ~15.5ms.
12 //
13 // QueryPerformanceCounter is the logical choice for a high-precision timer.
14 // However, it is known to be buggy on some hardware.  Specifically, it can
15 // sometimes "jump".  On laptops, QPC can also be very expensive to call.
16 // It's 3-4x slower than timeGetTime() on desktops, but can be 10x slower
17 // on laptops.  A unittest exists which will show the relative cost of various
18 // timers on any system.
19 //
20 // The next logical choice is timeGetTime().  timeGetTime has a precision of
21 // 1ms, but only if you call APIs (timeBeginPeriod()) which affect all other
22 // applications on the system.  By default, precision is only 15.5ms.
23 // Unfortunately, we don't want to call timeBeginPeriod because we don't
24 // want to affect other applications.  Further, on mobile platforms, use of
25 // faster multimedia timers can hurt battery life.  See the intel
26 // article about this here:
27 // http://softwarecommunity.intel.com/articles/eng/1086.htm
28 //
29 // To work around all this, we're going to generally use timeGetTime().  We
30 // will only increase the system-wide timer if we're not running on battery
31 // power.
32 
33 #include "partition_alloc/partition_alloc_base/time/time.h"
34 
35 #include <windows.foundation.h>
36 // clang-format off
37 #include <windows.h> // Must be included before <mmsystem.h>
38 #include <mmsystem.h>
39 // clang-format on
40 
41 #include <atomic>
42 #include <cstdint>
43 
44 #include "build/build_config.h"
45 #include "partition_alloc/partition_alloc_base/bit_cast.h"
46 #include "partition_alloc/partition_alloc_base/check.h"
47 #include "partition_alloc/partition_alloc_base/cpu.h"
48 #include "partition_alloc/partition_alloc_base/threading/platform_thread.h"
49 #include "partition_alloc/partition_alloc_base/time/time_override.h"
50 
51 namespace partition_alloc::internal::base {
52 
53 namespace {
54 
55 // From MSDN, FILETIME "Contains a 64-bit value representing the number of
56 // 100-nanosecond intervals since January 1, 1601 (UTC)."
FileTimeToMicroseconds(const FILETIME & ft)57 int64_t FileTimeToMicroseconds(const FILETIME& ft) {
58   // Need to bit_cast to fix alignment, then divide by 10 to convert
59   // 100-nanoseconds to microseconds. This only works on little-endian
60   // machines.
61   return bit_cast<int64_t, FILETIME>(ft) / 10;
62 }
63 
CanConvertToFileTime(int64_t us)64 bool CanConvertToFileTime(int64_t us) {
65   return us >= 0 && us <= (std::numeric_limits<int64_t>::max() / 10);
66 }
67 
MicrosecondsToFileTime(int64_t us)68 FILETIME MicrosecondsToFileTime(int64_t us) {
69   PA_BASE_DCHECK(CanConvertToFileTime(us))
70       << "Out-of-range: Cannot convert " << us
71       << " microseconds to FILETIME units.";
72 
73   // Multiply by 10 to convert microseconds to 100-nanoseconds. Bit_cast will
74   // handle alignment problems. This only works on little-endian machines.
75   return bit_cast<FILETIME, int64_t>(us * 10);
76 }
77 
CurrentWallclockMicroseconds()78 int64_t CurrentWallclockMicroseconds() {
79   FILETIME ft;
80   ::GetSystemTimeAsFileTime(&ft);
81   return FileTimeToMicroseconds(ft);
82 }
83 
84 // Time between resampling the un-granular clock for this API.
85 constexpr TimeDelta kMaxTimeToAvoidDrift = Seconds(60);
86 
87 int64_t g_initial_time = 0;
88 TimeTicks g_initial_ticks;
89 
InitializeClock()90 void InitializeClock() {
91   g_initial_ticks = subtle::TimeTicksNowIgnoringOverride();
92   g_initial_time = CurrentWallclockMicroseconds();
93 }
94 
95 // Returns the current value of the performance counter.
QPCNowRaw()96 uint64_t QPCNowRaw() {
97   LARGE_INTEGER perf_counter_now = {};
98   // According to the MSDN documentation for QueryPerformanceCounter(), this
99   // will never fail on systems that run XP or later.
100   // https://msdn.microsoft.com/library/windows/desktop/ms644904.aspx
101   ::QueryPerformanceCounter(&perf_counter_now);
102   return perf_counter_now.QuadPart;
103 }
104 
105 }  // namespace
106 
107 // Time -----------------------------------------------------------------------
108 
109 namespace subtle {
TimeNowIgnoringOverride()110 Time TimeNowIgnoringOverride() {
111   if (g_initial_time == 0) {
112     InitializeClock();
113   }
114 
115   // We implement time using the high-resolution timers so that we can get
116   // timeouts which are smaller than 10-15ms.  If we just used
117   // CurrentWallclockMicroseconds(), we'd have the less-granular timer.
118   //
119   // To make this work, we initialize the clock (g_initial_time) and the
120   // counter (initial_ctr).  To compute the initial time, we can check
121   // the number of ticks that have elapsed, and compute the delta.
122   //
123   // To avoid any drift, we periodically resync the counters to the system
124   // clock.
125   while (true) {
126     TimeTicks ticks = TimeTicksNowIgnoringOverride();
127 
128     // Calculate the time elapsed since we started our timer
129     TimeDelta elapsed = ticks - g_initial_ticks;
130 
131     // Check if enough time has elapsed that we need to resync the clock.
132     if (elapsed > kMaxTimeToAvoidDrift) {
133       InitializeClock();
134       continue;
135     }
136 
137     return Time() + elapsed + Microseconds(g_initial_time);
138   }
139 }
140 
TimeNowFromSystemTimeIgnoringOverride()141 Time TimeNowFromSystemTimeIgnoringOverride() {
142   // Force resync.
143   InitializeClock();
144   return Time() + Microseconds(g_initial_time);
145 }
146 }  // namespace subtle
147 
148 // static
FromFileTime(FILETIME ft)149 Time Time::FromFileTime(FILETIME ft) {
150   if (bit_cast<int64_t, FILETIME>(ft) == 0) {
151     return Time();
152   }
153   if (ft.dwHighDateTime == std::numeric_limits<DWORD>::max() &&
154       ft.dwLowDateTime == std::numeric_limits<DWORD>::max()) {
155     return Max();
156   }
157   return Time(FileTimeToMicroseconds(ft));
158 }
159 
ToFileTime() const160 FILETIME Time::ToFileTime() const {
161   if (is_null()) {
162     return bit_cast<FILETIME, int64_t>(0);
163   }
164   if (is_max()) {
165     FILETIME result;
166     result.dwHighDateTime = std::numeric_limits<DWORD>::max();
167     result.dwLowDateTime = std::numeric_limits<DWORD>::max();
168     return result;
169   }
170   return MicrosecondsToFileTime(us_);
171 }
172 
173 // TimeTicks ------------------------------------------------------------------
174 
175 namespace {
176 
177 // We define a wrapper to adapt between the __stdcall and __cdecl call of the
178 // mock function, and to avoid a static constructor.  Assigning an import to a
179 // function pointer directly would require setup code to fetch from the IAT.
timeGetTimeWrapper()180 DWORD timeGetTimeWrapper() {
181   return timeGetTime();
182 }
183 
184 DWORD (*g_tick_function)(void) = &timeGetTimeWrapper;
185 
186 // A structure holding the most significant bits of "last seen" and a
187 // "rollover" counter.
188 union LastTimeAndRolloversState {
189   // The state as a single 32-bit opaque value.
190   std::atomic<int32_t> as_opaque_32{0};
191 
192   // The state as usable values.
193   struct {
194     // The top 8-bits of the "last" time. This is enough to check for rollovers
195     // and the small bit-size means fewer CompareAndSwap operations to store
196     // changes in state, which in turn makes for fewer retries.
197     uint8_t last_8;
198     // A count of the number of detected rollovers. Using this as bits 47-32
199     // of the upper half of a 64-bit value results in a 48-bit tick counter.
200     // This extends the total rollover period from about 49 days to about 8800
201     // years while still allowing it to be stored with last_8 in a single
202     // 32-bit value.
203     uint16_t rollovers;
204   } as_values;
205 };
206 std::atomic<int32_t> g_last_time_and_rollovers = 0;
207 static_assert(sizeof(LastTimeAndRolloversState) <=
208                   sizeof(g_last_time_and_rollovers),
209               "LastTimeAndRolloversState does not fit in a single atomic word");
210 
211 // We use timeGetTime() to implement TimeTicks::Now().  This can be problematic
212 // because it returns the number of milliseconds since Windows has started,
213 // which will roll over the 32-bit value every ~49 days.  We try to track
214 // rollover ourselves, which works if TimeTicks::Now() is called at least every
215 // 48.8 days (not 49 days because only changes in the top 8 bits get noticed).
RolloverProtectedNow()216 TimeTicks RolloverProtectedNow() {
217   LastTimeAndRolloversState state;
218   DWORD now;  // DWORD is always unsigned 32 bits.
219 
220   while (true) {
221     // Fetch the "now" and "last" tick values, updating "last" with "now" and
222     // incrementing the "rollovers" counter if the tick-value has wrapped back
223     // around. Atomic operations ensure that both "last" and "rollovers" are
224     // always updated together.
225     int32_t original =
226         g_last_time_and_rollovers.load(std::memory_order_acquire);
227     state.as_opaque_32 = original;
228     now = g_tick_function();
229     uint8_t now_8 = static_cast<uint8_t>(now >> 24);
230     if (now_8 < state.as_values.last_8) {
231       ++state.as_values.rollovers;
232     }
233     state.as_values.last_8 = now_8;
234 
235     // If the state hasn't changed, exit the loop.
236     if (state.as_opaque_32 == original) {
237       break;
238     }
239 
240     // Save the changed state. If the existing value is unchanged from the
241     // original, exit the loop.
242     int32_t check = g_last_time_and_rollovers.compare_exchange_strong(
243         original, state.as_opaque_32, std::memory_order_release);
244     if (check == original) {
245       break;
246     }
247 
248     // Another thread has done something in between so retry from the top.
249   }
250 
251   return TimeTicks() +
252          Milliseconds(now +
253                       (static_cast<uint64_t>(state.as_values.rollovers) << 32));
254 }
255 
256 // Discussion of tick counter options on Windows:
257 //
258 // (1) CPU cycle counter. (Retrieved via RDTSC)
259 // The CPU counter provides the highest resolution time stamp and is the least
260 // expensive to retrieve. However, on older CPUs, two issues can affect its
261 // reliability: First it is maintained per processor and not synchronized
262 // between processors. Also, the counters will change frequency due to thermal
263 // and power changes, and stop in some states.
264 //
265 // (2) QueryPerformanceCounter (QPC). The QPC counter provides a high-
266 // resolution (<1 microsecond) time stamp. On most hardware running today, it
267 // auto-detects and uses the constant-rate RDTSC counter to provide extremely
268 // efficient and reliable time stamps.
269 //
270 // On older CPUs where RDTSC is unreliable, it falls back to using more
271 // expensive (20X to 40X more costly) alternate clocks, such as HPET or the ACPI
272 // PM timer, and can involve system calls; and all this is up to the HAL (with
273 // some help from ACPI). According to
274 // http://blogs.msdn.com/oldnewthing/archive/2005/09/02/459952.aspx, in the
275 // worst case, it gets the counter from the rollover interrupt on the
276 // programmable interrupt timer. In best cases, the HAL may conclude that the
277 // RDTSC counter runs at a constant frequency, then it uses that instead. On
278 // multiprocessor machines, it will try to verify the values returned from
279 // RDTSC on each processor are consistent with each other, and apply a handful
280 // of workarounds for known buggy hardware. In other words, QPC is supposed to
281 // give consistent results on a multiprocessor computer, but for older CPUs it
282 // can be unreliable due bugs in BIOS or HAL.
283 //
284 // (3) System time. The system time provides a low-resolution (from ~1 to ~15.6
285 // milliseconds) time stamp but is comparatively less expensive to retrieve and
286 // more reliable. Time::EnableHighResolutionTimer() and
287 // Time::ActivateHighResolutionTimer() can be called to alter the resolution of
288 // this timer; and also other Windows applications can alter it, affecting this
289 // one.
290 
291 TimeTicks InitialNowFunction();
292 
293 // See "threading notes" in InitializeNowFunctionPointer() for details on how
294 // concurrent reads/writes to these globals has been made safe.
295 std::atomic<TimeTicksNowFunction> g_time_ticks_now_ignoring_override_function{
296     &InitialNowFunction};
297 int64_t g_qpc_ticks_per_second = 0;
298 
QPCValueToTimeDelta(LONGLONG qpc_value)299 TimeDelta QPCValueToTimeDelta(LONGLONG qpc_value) {
300   // Ensure that the assignment to |g_qpc_ticks_per_second|, made in
301   // InitializeNowFunctionPointer(), has happened by this point.
302   std::atomic_thread_fence(std::memory_order_acquire);
303 
304   PA_BASE_DCHECK(g_qpc_ticks_per_second > 0);
305 
306   // If the QPC Value is below the overflow threshold, we proceed with
307   // simple multiply and divide.
308   if (qpc_value < Time::kQPCOverflowThreshold) {
309     return Microseconds(qpc_value * Time::kMicrosecondsPerSecond /
310                         g_qpc_ticks_per_second);
311   }
312   // Otherwise, calculate microseconds in a round about manner to avoid
313   // overflow and precision issues.
314   int64_t whole_seconds = qpc_value / g_qpc_ticks_per_second;
315   int64_t leftover_ticks = qpc_value - (whole_seconds * g_qpc_ticks_per_second);
316   return Microseconds((whole_seconds * Time::kMicrosecondsPerSecond) +
317                       ((leftover_ticks * Time::kMicrosecondsPerSecond) /
318                        g_qpc_ticks_per_second));
319 }
320 
QPCNow()321 TimeTicks QPCNow() {
322   return TimeTicks() + QPCValueToTimeDelta(QPCNowRaw());
323 }
324 
InitializeNowFunctionPointer()325 void InitializeNowFunctionPointer() {
326   LARGE_INTEGER ticks_per_sec = {};
327   if (!QueryPerformanceFrequency(&ticks_per_sec)) {
328     ticks_per_sec.QuadPart = 0;
329   }
330 
331   // If Windows cannot provide a QPC implementation, TimeTicks::Now() must use
332   // the low-resolution clock.
333   //
334   // If the QPC implementation is expensive and/or unreliable, TimeTicks::Now()
335   // will still use the low-resolution clock. A CPU lacking a non-stop time
336   // counter will cause Windows to provide an alternate QPC implementation that
337   // works, but is expensive to use.
338   //
339   // Otherwise, Now uses the high-resolution QPC clock. As of 21 August 2015,
340   // ~72% of users fall within this category.
341   CPU cpu;
342   const TimeTicksNowFunction now_function =
343       (ticks_per_sec.QuadPart <= 0 || !cpu.has_non_stop_time_stamp_counter())
344           ? &RolloverProtectedNow
345           : &QPCNow;
346 
347   // Threading note 1: In an unlikely race condition, it's possible for two or
348   // more threads to enter InitializeNowFunctionPointer() in parallel. This is
349   // not a problem since all threads end up writing out the same values
350   // to the global variables, and those variable being atomic are safe to read
351   // from other threads.
352   //
353   // Threading note 2: A release fence is placed here to ensure, from the
354   // perspective of other threads using the function pointers, that the
355   // assignment to |g_qpc_ticks_per_second| happens before the function pointers
356   // are changed.
357   g_qpc_ticks_per_second = ticks_per_sec.QuadPart;
358   std::atomic_thread_fence(std::memory_order_release);
359   // Also set g_time_ticks_now_function to avoid the additional indirection via
360   // TimeTicksNowIgnoringOverride() for future calls to TimeTicks::Now(), only
361   // if it wasn't already overridden to a different value. memory_order_relaxed
362   // is sufficient since an explicit fence was inserted above.
363   base::TimeTicksNowFunction initial_time_ticks_now_function =
364       &subtle::TimeTicksNowIgnoringOverride;
365   internal::g_time_ticks_now_function.compare_exchange_strong(
366       initial_time_ticks_now_function, now_function, std::memory_order_relaxed);
367   g_time_ticks_now_ignoring_override_function.store(now_function,
368                                                     std::memory_order_relaxed);
369 }
370 
InitialNowFunction()371 TimeTicks InitialNowFunction() {
372   InitializeNowFunctionPointer();
373   return g_time_ticks_now_ignoring_override_function.load(
374       std::memory_order_relaxed)();
375 }
376 
377 }  // namespace
378 
379 // static
SetMockTickFunction(TickFunctionType ticker)380 TimeTicks::TickFunctionType TimeTicks::SetMockTickFunction(
381     TickFunctionType ticker) {
382   TickFunctionType old = g_tick_function;
383   g_tick_function = ticker;
384   g_last_time_and_rollovers.store(0, std::memory_order_relaxed);
385   return old;
386 }
387 
388 namespace subtle {
TimeTicksNowIgnoringOverride()389 TimeTicks TimeTicksNowIgnoringOverride() {
390   return g_time_ticks_now_ignoring_override_function.load(
391       std::memory_order_relaxed)();
392 }
393 }  // namespace subtle
394 
395 // static
GetClock()396 TimeTicks::Clock TimeTicks::GetClock() {
397   return Clock::WIN_ROLLOVER_PROTECTED_TIME_GET_TIME;
398 }
399 
400 // ThreadTicks ----------------------------------------------------------------
401 
402 namespace subtle {
ThreadTicksNowIgnoringOverride()403 ThreadTicks ThreadTicksNowIgnoringOverride() {
404   return ThreadTicks::GetForThread(PlatformThread::CurrentHandle());
405 }
406 }  // namespace subtle
407 
408 // static
GetForThread(const PlatformThreadHandle & thread_handle)409 ThreadTicks ThreadTicks::GetForThread(
410     const PlatformThreadHandle& thread_handle) {
411   PA_BASE_DCHECK(IsSupported());
412 
413 #if defined(ARCH_CPU_ARM64)
414   // QueryThreadCycleTime versus TSCTicksPerSecond doesn't have much relation to
415   // actual elapsed time on Windows on Arm, because QueryThreadCycleTime is
416   // backed by the actual number of CPU cycles executed, rather than a
417   // constant-rate timer like Intel. To work around this, use GetThreadTimes
418   // (which isn't as accurate but is meaningful as a measure of elapsed
419   // per-thread time).
420   FILETIME creation_time, exit_time, kernel_time, user_time;
421   ::GetThreadTimes(thread_handle.platform_handle(), &creation_time, &exit_time,
422                    &kernel_time, &user_time);
423 
424   const int64_t us = FileTimeToMicroseconds(user_time);
425 #else
426   // Get the number of TSC ticks used by the current thread.
427   ULONG64 thread_cycle_time = 0;
428   ::QueryThreadCycleTime(thread_handle.platform_handle(), &thread_cycle_time);
429 
430   // Get the frequency of the TSC.
431   const double tsc_ticks_per_second = time_internal::TSCTicksPerSecond();
432   if (tsc_ticks_per_second == 0) {
433     return ThreadTicks();
434   }
435 
436   // Return the CPU time of the current thread.
437   const double thread_time_seconds = thread_cycle_time / tsc_ticks_per_second;
438   const int64_t us =
439       static_cast<int64_t>(thread_time_seconds * Time::kMicrosecondsPerSecond);
440 #endif
441 
442   return ThreadTicks(us);
443 }
444 
445 // static
IsSupportedWin()446 bool ThreadTicks::IsSupportedWin() {
447 #if defined(ARCH_CPU_ARM64)
448   // The Arm implementation does not use QueryThreadCycleTime and therefore does
449   // not care about the time stamp counter.
450   return true;
451 #else
452   return time_internal::HasConstantRateTSC();
453 #endif
454 }
455 
456 // static
WaitUntilInitializedWin()457 void ThreadTicks::WaitUntilInitializedWin() {
458 #if !defined(ARCH_CPU_ARM64)
459   while (time_internal::TSCTicksPerSecond() == 0) {
460     ::Sleep(10);
461   }
462 #endif
463 }
464 
465 // static
FromQPCValue(LONGLONG qpc_value)466 TimeTicks TimeTicks::FromQPCValue(LONGLONG qpc_value) {
467   return TimeTicks() + QPCValueToTimeDelta(qpc_value);
468 }
469 
470 // TimeDelta ------------------------------------------------------------------
471 
472 // static
FromQPCValue(LONGLONG qpc_value)473 TimeDelta TimeDelta::FromQPCValue(LONGLONG qpc_value) {
474   return QPCValueToTimeDelta(qpc_value);
475 }
476 
477 // static
FromFileTime(FILETIME ft)478 TimeDelta TimeDelta::FromFileTime(FILETIME ft) {
479   return Microseconds(FileTimeToMicroseconds(ft));
480 }
481 
482 // static
FromWinrtDateTime(ABI::Windows::Foundation::DateTime dt)483 TimeDelta TimeDelta::FromWinrtDateTime(ABI::Windows::Foundation::DateTime dt) {
484   // UniversalTime is 100 ns intervals since January 1, 1601 (UTC)
485   return Microseconds(dt.UniversalTime / 10);
486 }
487 
ToWinrtDateTime() const488 ABI::Windows::Foundation::DateTime TimeDelta::ToWinrtDateTime() const {
489   ABI::Windows::Foundation::DateTime date_time;
490   date_time.UniversalTime = InMicroseconds() * 10;
491   return date_time;
492 }
493 
494 #if !defined(ARCH_CPU_ARM64)
495 namespace time_internal {
496 
HasConstantRateTSC()497 bool HasConstantRateTSC() {
498   static bool is_supported = CPU().has_non_stop_time_stamp_counter();
499   return is_supported;
500 }
501 
TSCTicksPerSecond()502 double TSCTicksPerSecond() {
503   PA_BASE_DCHECK(HasConstantRateTSC());
504   // The value returned by QueryPerformanceFrequency() cannot be used as the TSC
505   // frequency, because there is no guarantee that the TSC frequency is equal to
506   // the performance counter frequency.
507   // The TSC frequency is cached in a static variable because it takes some time
508   // to compute it.
509   static double tsc_ticks_per_second = 0;
510   if (tsc_ticks_per_second != 0) {
511     return tsc_ticks_per_second;
512   }
513 
514   // Increase the thread priority to reduces the chances of having a context
515   // switch during a reading of the TSC and the performance counter.
516   const int previous_priority = ::GetThreadPriority(::GetCurrentThread());
517   ::SetThreadPriority(::GetCurrentThread(), THREAD_PRIORITY_HIGHEST);
518 
519   // The first time that this function is called, make an initial reading of the
520   // TSC and the performance counter.
521 
522   static const uint64_t tsc_initial = __rdtsc();
523   static const uint64_t perf_counter_initial = QPCNowRaw();
524 
525   // Make a another reading of the TSC and the performance counter every time
526   // that this function is called.
527   const uint64_t tsc_now = __rdtsc();
528   const uint64_t perf_counter_now = QPCNowRaw();
529 
530   // Reset the thread priority.
531   ::SetThreadPriority(::GetCurrentThread(), previous_priority);
532 
533   // Make sure that at least 50 ms elapsed between the 2 readings. The first
534   // time that this function is called, we don't expect this to be the case.
535   // Note: The longer the elapsed time between the 2 readings is, the more
536   //   accurate the computed TSC frequency will be. The 50 ms value was
537   //   chosen because local benchmarks show that it allows us to get a
538   //   stddev of less than 1 tick/us between multiple runs.
539   // Note: According to the MSDN documentation for QueryPerformanceFrequency(),
540   //   this will never fail on systems that run XP or later.
541   //   https://msdn.microsoft.com/library/windows/desktop/ms644905.aspx
542   LARGE_INTEGER perf_counter_frequency = {};
543   ::QueryPerformanceFrequency(&perf_counter_frequency);
544   PA_BASE_DCHECK(perf_counter_now >= perf_counter_initial);
545   const uint64_t perf_counter_ticks = perf_counter_now - perf_counter_initial;
546   const double elapsed_time_seconds =
547       perf_counter_ticks / static_cast<double>(perf_counter_frequency.QuadPart);
548 
549   constexpr double kMinimumEvaluationPeriodSeconds = 0.05;
550   if (elapsed_time_seconds < kMinimumEvaluationPeriodSeconds) {
551     return 0;
552   }
553 
554   // Compute the frequency of the TSC.
555   PA_BASE_DCHECK(tsc_now >= tsc_initial);
556   const uint64_t tsc_ticks = tsc_now - tsc_initial;
557   tsc_ticks_per_second = tsc_ticks / elapsed_time_seconds;
558 
559   return tsc_ticks_per_second;
560 }
561 
562 }  // namespace time_internal
563 #endif  // defined(ARCH_CPU_ARM64)
564 
565 }  // namespace partition_alloc::internal::base
566