Debian Codegraph @

mozglue/misc/Now.cpp

raw
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 #include "Now.h"
8
9 #ifdef XP_WIN
10 # include "mozilla/DynamicallyLinkedFunctionPtr.h"
11 #endif // XP_WIN
12
13 #include <stdint.h>
14
15 #include "mozilla/TimeStamp.h"
16 #include "mozilla/Maybe.h"
17 #include "mozilla/Assertions.h"
18
19 namespace mozilla {
20
21 // Apple things
22 #if defined(__APPLE__) && defined(__MACH__)
23 # include <time.h>
24 # include <sys/time.h>
25 # include <sys/types.h>
26 # include <mach/mach_time.h>
27
28 static constexpr uint64_t kNSperMS = 1000000;
29
30 Maybe<uint64_t> NowExcludingSuspendMs() {
31 return Some(clock_gettime_nsec_np(CLOCK_UPTIME_RAW) / kNSperMS);
32 }
33
34 Maybe<uint64_t> NowIncludingSuspendMs() {
35 return Some(clock_gettime_nsec_np(CLOCK_MONOTONIC_RAW) / kNSperMS);
36 }
37
38 #elif defined(XP_WIN)
39
40 // Number of hundreds of nanoseconds in a millisecond
41 static constexpr uint64_t kHNSperMS = 10000;
42
43 Maybe<uint64_t> NowExcludingSuspendMs() {
44 ULONGLONG interrupt_time;
45 if (!QueryUnbiasedInterruptTime(&interrupt_time)) {
46 return Nothing();
47 }
48 return Some(interrupt_time / kHNSperMS);
49 }
50
51 Maybe<uint64_t> NowIncludingSuspendMs() {
52 static const mozilla::StaticDynamicallyLinkedFunctionPtr<void(WINAPI*)(
53 PULONGLONG)>
54 pQueryInterruptTime(L"KernelBase.dll", "QueryInterruptTime");
55 if (!pQueryInterruptTime) {
56 // On Windows, this does include the time the computer was suspended so it's
57 // an adequate fallback.
58 TimeStamp processCreation = TimeStamp::ProcessCreation();
59 TimeStamp now = TimeStamp::Now();
60 if (!processCreation.IsNull() && !now.IsNull()) {
61 return Some(uint64_t((now - processCreation).ToMilliseconds()));
62 } else {
63 return Nothing();
64 }
65 }
66 ULONGLONG interrupt_time;
67 pQueryInterruptTime(&interrupt_time);
68 return Some(interrupt_time / kHNSperMS);
69 }
70
71 #elif defined(XP_UNIX) // including BSDs and Android
72 # include <time.h>
73
74 // Number of nanoseconds in a millisecond.
75 static constexpr uint64_t kNSperMS = 1000000;
76
77 static uint64_t TimespecToMilliseconds(struct timespec aTs) {
78 return aTs.tv_sec * 1000 + aTs.tv_nsec / kNSperMS;
79 }
80
81 Maybe<uint64_t> NowExcludingSuspendMs() {
82 struct timespec ts = {0};
83
84 # ifdef XP_OPENBSD
85 if (clock_gettime(CLOCK_UPTIME, &ts)) {
86 # else
87 if (clock_gettime(CLOCK_MONOTONIC, &ts)) {
88 # endif
89 return Nothing();
90 }
91 return Some(TimespecToMilliseconds(ts));
92 }
93
94 Maybe<uint64_t> NowIncludingSuspendMs() {
95 # ifndef CLOCK_BOOTTIME
96 return Nothing();
97 # else
98 struct timespec ts = {0};
99
100 if (clock_gettime(CLOCK_BOOTTIME, &ts)) {
101 return Nothing();
102 }
103 return Some(TimespecToMilliseconds(ts));
104 # endif
105 }
106
107 #else // catch all
108
109 Maybe<uint64_t> NowExcludingSuspendMs() { return Nothing(); }
110 Maybe<uint64_t> NowIncludingSuspendMs() { return Nothing(); }
111
112 #endif
113
114 } // namespace mozilla