2 * Copyright 2016 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <folly/portability/Time.h>
18 #include <folly/Likely.h>
24 template <typename _Rep, typename _Period>
25 static void duration_to_ts(
26 std::chrono::duration<_Rep, _Period> d,
27 struct timespec* ts) {
28 ts->tv_sec = std::chrono::duration_cast<std::chrono::seconds>(d).count();
29 ts->tv_nsec = std::chrono::duration_cast<std::chrono::nanoseconds>(
30 d % std::chrono::seconds(1))
34 #if !FOLLY_HAVE_CLOCK_GETTIME
37 #include <mach/mach_init.h>
38 #include <mach/mach_port.h>
39 #include <mach/mach_time.h>
40 #include <mach/mach_types.h>
41 #include <mach/task.h>
42 #include <mach/thread_act.h>
43 #include <mach/vm_map.h>
45 static std::chrono::nanoseconds time_value_to_ns(time_value_t t) {
46 return std::chrono::seconds(t.seconds) +
47 std::chrono::microseconds(t.microseconds);
50 static int clock_process_cputime(struct timespec* ts) {
51 // Get CPU usage for live threads.
52 task_thread_times_info thread_times_info;
53 mach_msg_type_number_t thread_times_info_count = TASK_THREAD_TIMES_INFO_COUNT;
54 kern_return_t kern_result = task_info(
56 TASK_THREAD_TIMES_INFO,
57 (thread_info_t)&thread_times_info,
58 &thread_times_info_count);
59 if (UNLIKELY(kern_result != KERN_SUCCESS)) {
63 // Get CPU usage for terminated threads.
64 mach_task_basic_info task_basic_info;
65 mach_msg_type_number_t task_basic_info_count = MACH_TASK_BASIC_INFO_COUNT;
66 kern_result = task_info(
69 (thread_info_t)&task_basic_info,
70 &task_basic_info_count);
71 if (UNLIKELY(kern_result != KERN_SUCCESS)) {
75 auto cputime = time_value_to_ns(thread_times_info.user_time) +
76 time_value_to_ns(thread_times_info.system_time) +
77 time_value_to_ns(task_basic_info.user_time) +
78 time_value_to_ns(task_basic_info.system_time);
79 duration_to_ts(cputime, ts);
83 static int clock_thread_cputime(struct timespec* ts) {
84 mach_msg_type_number_t count = THREAD_BASIC_INFO_COUNT;
85 thread_basic_info_data_t thread_info_data;
86 thread_act_t thread = mach_thread_self();
87 kern_return_t kern_result = thread_info(
88 thread, THREAD_BASIC_INFO, (thread_info_t)&thread_info_data, &count);
89 mach_port_deallocate(mach_task_self(), thread);
90 if (UNLIKELY(kern_result != KERN_SUCCESS)) {
93 auto cputime = time_value_to_ns(thread_info_data.system_time) +
94 time_value_to_ns(thread_info_data.user_time);
95 duration_to_ts(cputime, ts);
99 int clock_gettime(clockid_t clk_id, struct timespec* ts) {
101 case CLOCK_REALTIME: {
102 auto now = std::chrono::system_clock::now().time_since_epoch();
103 duration_to_ts(now, ts);
106 case CLOCK_MONOTONIC: {
107 auto now = std::chrono::steady_clock::now().time_since_epoch();
108 duration_to_ts(now, ts);
111 case CLOCK_PROCESS_CPUTIME_ID:
112 return clock_process_cputime(ts);
113 case CLOCK_THREAD_CPUTIME_ID:
114 return clock_thread_cputime(ts);
121 int clock_getres(clockid_t clk_id, struct timespec* ts) {
122 if (clk_id != CLOCK_MONOTONIC) {
126 static auto info = [] {
127 static mach_timebase_info_data_t info;
128 auto result = (mach_timebase_info(&info) == KERN_SUCCESS) ? &info : nullptr;
134 ts->tv_nsec = info->numer / info->denom;
138 #elif defined(_WIN32)
144 #include <folly/portability/Windows.h>
146 using unsigned_nanos = std::chrono::duration<uint64_t, std::nano>;
148 static unsigned_nanos filetimeToUnsignedNanos(FILETIME ft) {
150 i.HighPart = ft.dwHighDateTime;
151 i.LowPart = ft.dwLowDateTime;
153 // FILETIMEs are in units of 100ns.
154 return unsigned_nanos(i.QuadPart * 100);
157 static LARGE_INTEGER performanceFrequency() {
158 static auto result = [] {
160 // On Windows XP or later, this will never fail.
161 BOOL res = QueryPerformanceFrequency(&freq);
168 extern "C" int clock_getres(clockid_t clock_id, struct timespec* res) {
175 case CLOCK_MONOTONIC: {
176 LARGE_INTEGER freq = performanceFrequency();
177 if (freq.QuadPart == -1) {
182 static constexpr size_t kNsPerSec = 1000000000;
185 res->tv_nsec = (long)((kNsPerSec + (freq.QuadPart >> 1)) / freq.QuadPart);
186 if (res->tv_nsec < 1) {
194 case CLOCK_PROCESS_CPUTIME_ID:
195 case CLOCK_THREAD_CPUTIME_ID: {
196 DWORD adj, timeIncrement;
198 if (!GetSystemTimeAdjustment(&adj, &timeIncrement, &adjDisabled)) {
204 res->tv_nsec = timeIncrement * 100;
214 extern "C" int clock_gettime(clockid_t clock_id, struct timespec* tp) {
220 const auto unanosToTimespec = [](timespec* tp, unsigned_nanos t) -> int {
221 static constexpr unsigned_nanos one_sec(std::chrono::seconds(1));
222 tp->tv_sec = std::chrono::duration_cast<std::chrono::seconds>(t).count();
223 tp->tv_nsec = (t % one_sec).count();
227 FILETIME createTime, exitTime, kernalTime, userTime;
229 case CLOCK_REALTIME: {
230 auto now = std::chrono::system_clock::now().time_since_epoch();
231 duration_to_ts(now, tp);
234 case CLOCK_MONOTONIC: {
235 auto now = std::chrono::steady_clock::now().time_since_epoch();
236 duration_to_ts(now, tp);
239 case CLOCK_PROCESS_CPUTIME_ID: {
240 if (!GetProcessTimes(
250 return unanosToTimespec(
252 filetimeToUnsignedNanos(kernalTime) +
253 filetimeToUnsignedNanos(userTime));
255 case CLOCK_THREAD_CPUTIME_ID: {
266 return unanosToTimespec(
268 filetimeToUnsignedNanos(kernalTime) +
269 filetimeToUnsignedNanos(userTime));
278 #error No clock_gettime(3) compatibility wrapper available for this platform.
286 #include <folly/portability/Windows.h>
289 char* asctime_r(const tm* tm, char* buf) {
291 if (asctime_s(tmpBuf, tm)) {
294 // Nothing we can do if the buff is to small :(
295 return strcpy(buf, tmpBuf);
298 char* ctime_r(const time_t* t, char* buf) {
300 if (ctime_s(tmpBuf, 64, t)) {
303 // Nothing we can do if the buff is to small :(
304 return strcpy(buf, tmpBuf);
307 tm* gmtime_r(const time_t* t, tm* res) {
308 if (!gmtime_s(res, t)) {
314 tm* localtime_r(const time_t* t, tm* o) {
315 if (!localtime_s(o, t)) {
321 int nanosleep(const struct timespec* request, struct timespec* remain) {
322 Sleep((DWORD)((request->tv_sec * 1000) + (request->tv_nsec / 1000000)));
323 if (remain != nullptr) {
330 char* strptime(const char* __restrict s,
331 const char* __restrict f,
332 struct tm* __restrict tm) {
333 // Isn't the C++ standard lib nice? std::get_time is defined such that its
334 // format parameters are the exact same as strptime. Of course, we have to
335 // create a string stream first, and imbue it with the current C locale, and
336 // we also have to make sure we return the right things if it fails, or
337 // if it succeeds, but this is still far simpler an implementation than any
338 // of the versions in any of the C standard libraries.
339 std::istringstream input(s);
340 input.imbue(std::locale(setlocale(LC_ALL, nullptr)));
341 input >> std::get_time(tm, f);
345 return const_cast<char*>(s + input.tellg());