1 /*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #ifndef INCLUDE_PERFETTO_BASE_TIME_H_
18 #define INCLUDE_PERFETTO_BASE_TIME_H_
19
20 #include <time.h>
21
22 #include <chrono>
23 #include <string>
24
25 #include "perfetto/base/build_config.h"
26 #include "perfetto/base/logging.h"
27
28 #if PERFETTO_BUILDFLAG(PERFETTO_OS_APPLE)
29 #include <mach/mach_init.h>
30 #include <mach/mach_port.h>
31 #include <mach/mach_time.h>
32 #include <mach/thread_act.h>
33 #endif
34
35 #if PERFETTO_BUILDFLAG(PERFETTO_OS_WASM)
36 #include <emscripten/emscripten.h>
37 #endif
38
39 namespace perfetto {
40 namespace base {
41
42 using TimeSeconds = std::chrono::seconds;
43 using TimeMillis = std::chrono::milliseconds;
44 using TimeNanos = std::chrono::nanoseconds;
45
FromPosixTimespec(const struct timespec & ts)46 inline TimeNanos FromPosixTimespec(const struct timespec& ts) {
47 return TimeNanos(ts.tv_sec * 1000000000LL + ts.tv_nsec);
48 }
49
50 void SleepMicroseconds(unsigned interval_us);
51
52 #if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
53
54 TimeNanos GetWallTimeNs();
55 TimeNanos GetThreadCPUTimeNs();
56
57 // TODO: Clock that counts time during suspend is not implemented on Windows.
GetBootTimeNs()58 inline TimeNanos GetBootTimeNs() {
59 return GetWallTimeNs();
60 }
61
62 #elif PERFETTO_BUILDFLAG(PERFETTO_OS_APPLE)
63
GetWallTimeNs()64 inline TimeNanos GetWallTimeNs() {
65 auto init_time_factor = []() -> uint64_t {
66 mach_timebase_info_data_t timebase_info;
67 mach_timebase_info(&timebase_info);
68 return timebase_info.numer / timebase_info.denom;
69 };
70
71 static uint64_t monotonic_timebase_factor = init_time_factor();
72 return TimeNanos(mach_absolute_time() * monotonic_timebase_factor);
73 }
74
75 // TODO: Clock that counts time during suspend is not implemented on Mac.
GetBootTimeNs()76 inline TimeNanos GetBootTimeNs() {
77 return GetWallTimeNs();
78 }
79
GetThreadCPUTimeNs()80 inline TimeNanos GetThreadCPUTimeNs() {
81 mach_port_t this_thread = mach_thread_self();
82 mach_msg_type_number_t count = THREAD_BASIC_INFO_COUNT;
83 thread_basic_info_data_t info{};
84 kern_return_t kr =
85 thread_info(this_thread, THREAD_BASIC_INFO,
86 reinterpret_cast<thread_info_t>(&info), &count);
87 mach_port_deallocate(mach_task_self(), this_thread);
88
89 if (kr != KERN_SUCCESS) {
90 PERFETTO_DFATAL("Failed to get CPU time.");
91 return TimeNanos(0);
92 }
93 return TimeNanos(info.user_time.seconds * 1000000000LL +
94 info.user_time.microseconds * 1000LL +
95 info.system_time.seconds * 1000000000LL +
96 info.system_time.microseconds * 1000LL);
97 }
98
99 #elif PERFETTO_BUILDFLAG(PERFETTO_OS_WASM)
100
GetWallTimeNs()101 inline TimeNanos GetWallTimeNs() {
102 return TimeNanos(static_cast<uint64_t>(emscripten_get_now()) * 1000000);
103 }
104
GetThreadCPUTimeNs()105 inline TimeNanos GetThreadCPUTimeNs() {
106 return TimeNanos(0);
107 }
108
109 // TODO: Clock that counts time during suspend is not implemented on WASM.
GetBootTimeNs()110 inline TimeNanos GetBootTimeNs() {
111 return GetWallTimeNs();
112 }
113
114 #elif PERFETTO_BUILDFLAG(PERFETTO_OS_NACL)
115
116 // Tracing time doesn't need to work on NaCl since its going away shortly. We
117 // just need to compile on it. The only function NaCl could support is
118 // GetWallTimeNs(), but to prevent false hope we leave it unimplemented.
119
GetWallTimeNs()120 inline TimeNanos GetWallTimeNs() {
121 return TimeNanos(0);
122 }
123
GetThreadCPUTimeNs()124 inline TimeNanos GetThreadCPUTimeNs() {
125 return TimeNanos(0);
126 }
127
GetBootTimeNs()128 inline TimeNanos GetBootTimeNs() {
129 return TimeNanos(0);
130 }
131
132 #else // posix
133
134 constexpr clockid_t kWallTimeClockSource = CLOCK_MONOTONIC;
135
GetTimeInternalNs(clockid_t clk_id)136 inline TimeNanos GetTimeInternalNs(clockid_t clk_id) {
137 struct timespec ts = {};
138 PERFETTO_CHECK(clock_gettime(clk_id, &ts) == 0);
139 return FromPosixTimespec(ts);
140 }
141
142 // Return ns from boot. Conversely to GetWallTimeNs, this clock counts also time
143 // during suspend (when supported).
GetBootTimeNs()144 inline TimeNanos GetBootTimeNs() {
145 // Determine if CLOCK_BOOTTIME is available on the first call.
146 static const clockid_t kBootTimeClockSource = [] {
147 struct timespec ts = {};
148 int res = clock_gettime(CLOCK_BOOTTIME, &ts);
149 return res == 0 ? CLOCK_BOOTTIME : kWallTimeClockSource;
150 }();
151 return GetTimeInternalNs(kBootTimeClockSource);
152 }
153
GetWallTimeNs()154 inline TimeNanos GetWallTimeNs() {
155 return GetTimeInternalNs(kWallTimeClockSource);
156 }
157
GetThreadCPUTimeNs()158 inline TimeNanos GetThreadCPUTimeNs() {
159 return GetTimeInternalNs(CLOCK_THREAD_CPUTIME_ID);
160 }
161 #endif
162
GetBootTimeS()163 inline TimeSeconds GetBootTimeS() {
164 return std::chrono::duration_cast<TimeSeconds>(GetBootTimeNs());
165 }
166
GetWallTimeMs()167 inline TimeMillis GetWallTimeMs() {
168 return std::chrono::duration_cast<TimeMillis>(GetWallTimeNs());
169 }
170
GetWallTimeS()171 inline TimeSeconds GetWallTimeS() {
172 return std::chrono::duration_cast<TimeSeconds>(GetWallTimeNs());
173 }
174
ToPosixTimespec(TimeMillis time)175 inline struct timespec ToPosixTimespec(TimeMillis time) {
176 struct timespec ts {};
177 const long time_s = static_cast<long>(time.count() / 1000);
178 ts.tv_sec = time_s;
179 ts.tv_nsec = (static_cast<long>(time.count()) - time_s * 1000L) * 1000000L;
180 return ts;
181 }
182
183 std::string GetTimeFmt(const std::string& fmt);
184
185 } // namespace base
186 } // namespace perfetto
187
188 #endif // INCLUDE_PERFETTO_BASE_TIME_H_
189