Debian Codegraph @

mozglue/misc/AwakeTimeStamp.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 "AwakeTimeStamp.h"
8
9 #ifdef XP_WIN
10 # include <windows.h>
11 #endif
12
13 #include "mozilla/Assertions.h"
14 #include "mozilla/DebugOnly.h"
15
16 namespace mozilla {
17
18 static constexpr uint64_t kUSperS = 1000000;
19 static constexpr uint64_t kUSperMS = 1000;
20 #ifndef XP_WIN
21 static constexpr uint64_t kNSperUS = 1000;
22 #endif
23
24 double AwakeTimeDuration::ToSeconds() const {
25 return static_cast<double>(mValueUs) / kUSperS;
26 }
27 double AwakeTimeDuration::ToMilliseconds() const {
28 return static_cast<double>(mValueUs) / kUSperMS;
29 }
30 double AwakeTimeDuration::ToMicroseconds() const {
31 return static_cast<double>(mValueUs);
32 }
33
34 AwakeTimeDuration AwakeTimeDuration::FromSeconds(uint64_t aSeconds) {
35 return AwakeTimeDuration(aSeconds * 1000000);
36 }
37 AwakeTimeDuration AwakeTimeDuration::FromMilliseconds(uint64_t aMilliseconds) {
38 return AwakeTimeDuration(aMilliseconds * 1000);
39 }
40 AwakeTimeDuration AwakeTimeDuration::FromMicroseconds(uint64_t aMicroseconds) {
41 return AwakeTimeDuration(aMicroseconds);
42 }
43
44 AwakeTimeDuration AwakeTimeStamp::operator-(
45 AwakeTimeStamp const& aOther) const {
46 return AwakeTimeDuration(mValueUs - aOther.mValueUs);
47 }
48
49 AwakeTimeStamp AwakeTimeStamp::operator-(
50 AwakeTimeDuration const& aOther) const {
51 return AwakeTimeStamp(mValueUs - aOther.mValueUs);
52 }
53
54 AwakeTimeStamp AwakeTimeStamp::operator+(
55 const AwakeTimeDuration& aDuration) const {
56 return AwakeTimeStamp(mValueUs + aDuration.mValueUs);
57 }
58
59 void AwakeTimeStamp::operator+=(const AwakeTimeDuration& aOther) {
60 mValueUs += aOther.mValueUs;
61 }
62
63 void AwakeTimeStamp::operator-=(const AwakeTimeDuration& aOther) {
64 MOZ_ASSERT(mValueUs >= aOther.mValueUs);
65 mValueUs -= aOther.mValueUs;
66 }
67
68 // Apple things
69 #if defined(__APPLE__) && defined(__MACH__)
70 # include <time.h>
71 # include <sys/time.h>
72 # include <sys/types.h>
73 # include <mach/mach_time.h>
74
75 AwakeTimeStamp AwakeTimeStamp::Now() {
76 return AwakeTimeStamp(clock_gettime_nsec_np(CLOCK_UPTIME_RAW) / kNSperUS);
77 }
78
79 AwakeTimeStamp AwakeTimeStamp::NowLoRes() { return Now(); }
80
81 #elif defined(XP_WIN)
82
83 // Number of hundreds of nanoseconds in a microsecond
84 static constexpr uint64_t kHNSperUS = 10;
85
86 AwakeTimeStamp AwakeTimeStamp::NowLoRes() {
87 ULONGLONG interrupt_time;
88 DebugOnly<bool> rv = QueryUnbiasedInterruptTime(&interrupt_time);
89 MOZ_ASSERT(rv);
90
91 return AwakeTimeStamp(interrupt_time / kHNSperUS);
92 }
93
94 AwakeTimeStamp AwakeTimeStamp::Now() {
95 ULONGLONG interrupt_time;
96 QueryUnbiasedInterruptTimePrecise(&interrupt_time);
97
98 return AwakeTimeStamp(interrupt_time / kHNSperUS);
99 }
100
101 #else // Linux and other POSIX but not macOS
102 # include <time.h>
103
104 uint64_t TimespecToMicroseconds(struct timespec aTs) {
105 return aTs.tv_sec * kUSperS + aTs.tv_nsec / kNSperUS;
106 }
107
108 AwakeTimeStamp AwakeTimeStamp::Now() {
109 struct timespec ts = {0};
110 DebugOnly<int> rv = clock_gettime(CLOCK_MONOTONIC, &ts);
111 MOZ_ASSERT(!rv);
112 return AwakeTimeStamp(TimespecToMicroseconds(ts));
113 }
114
115 AwakeTimeStamp AwakeTimeStamp::NowLoRes() { return Now(); }
116
117 #endif
118
119 }; // namespace mozilla