1 // Copyright 2015 Google Inc. All rights reserved.
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
16 #include "internal_macros.h"
18 #ifdef BENCHMARK_OS_WINDOWS
20 #undef StrCat // Don't let StrCat in string_util.h be renamed to lstrcatA
21 #include <versionhelpers.h>
25 #ifndef BENCHMARK_OS_FUCHSIA
26 #include <sys/resource.h>
29 #include <sys/types.h> // this header must be included before 'sys/sysctl.h' to avoid compilation error on FreeBSD
31 #if defined BENCHMARK_OS_FREEBSD || defined BENCHMARK_OS_MACOSX
32 #include <sys/sysctl.h>
34 #if defined(BENCHMARK_OS_MACOSX)
35 #include <mach/mach_init.h>
36 #include <mach/mach_port.h>
37 #include <mach/thread_act.h>
41 #ifdef BENCHMARK_OS_EMSCRIPTEN
42 #include <emscripten.h>
58 #include "string_util.h"
62 // Suppress unused warnings on helper functions.
64 #pragma GCC diagnostic ignored "-Wunused-function"
68 #if defined(BENCHMARK_OS_WINDOWS)
69 double MakeTime(FILETIME
const& kernel_time
, FILETIME
const& user_time
) {
70 ULARGE_INTEGER kernel
;
72 kernel
.HighPart
= kernel_time
.dwHighDateTime
;
73 kernel
.LowPart
= kernel_time
.dwLowDateTime
;
74 user
.HighPart
= user_time
.dwHighDateTime
;
75 user
.LowPart
= user_time
.dwLowDateTime
;
76 return (static_cast<double>(kernel
.QuadPart
) +
77 static_cast<double>(user
.QuadPart
)) *
80 #elif !defined(BENCHMARK_OS_FUCHSIA)
81 double MakeTime(struct rusage
const& ru
) {
82 return (static_cast<double>(ru
.ru_utime
.tv_sec
) +
83 static_cast<double>(ru
.ru_utime
.tv_usec
) * 1e-6 +
84 static_cast<double>(ru
.ru_stime
.tv_sec
) +
85 static_cast<double>(ru
.ru_stime
.tv_usec
) * 1e-6);
88 #if defined(BENCHMARK_OS_MACOSX)
89 double MakeTime(thread_basic_info_data_t
const& info
) {
90 return (static_cast<double>(info
.user_time
.seconds
) +
91 static_cast<double>(info
.user_time
.microseconds
) * 1e-6 +
92 static_cast<double>(info
.system_time
.seconds
) +
93 static_cast<double>(info
.system_time
.microseconds
) * 1e-6);
96 #if defined(CLOCK_PROCESS_CPUTIME_ID) || defined(CLOCK_THREAD_CPUTIME_ID)
97 double MakeTime(struct timespec
const& ts
) {
98 return ts
.tv_sec
+ (static_cast<double>(ts
.tv_nsec
) * 1e-9);
102 BENCHMARK_NORETURN
static void DiagnoseAndExit(const char* msg
) {
103 std::cerr
<< "ERROR: " << msg
<< std::endl
;
104 std::exit(EXIT_FAILURE
);
109 double ProcessCPUUsage() {
110 #if defined(BENCHMARK_OS_WINDOWS)
111 HANDLE proc
= GetCurrentProcess();
112 FILETIME creation_time
;
114 FILETIME kernel_time
;
116 if (GetProcessTimes(proc
, &creation_time
, &exit_time
, &kernel_time
,
118 return MakeTime(kernel_time
, user_time
);
119 DiagnoseAndExit("GetProccessTimes() failed");
120 #elif defined(BENCHMARK_OS_EMSCRIPTEN)
121 // clock_gettime(CLOCK_PROCESS_CPUTIME_ID, ...) returns 0 on Emscripten.
122 // Use Emscripten-specific API. Reported CPU time would be exactly the
123 // same as total time, but this is ok because there aren't long-latency
124 // syncronous system calls in Emscripten.
125 return emscripten_get_now() * 1e-3;
126 #elif defined(CLOCK_PROCESS_CPUTIME_ID) && !defined(BENCHMARK_OS_MACOSX)
127 // FIXME We want to use clock_gettime, but its not available in MacOS 10.11. See
128 // https://github.com/google/benchmark/pull/292
129 struct timespec spec
;
130 if (clock_gettime(CLOCK_PROCESS_CPUTIME_ID
, &spec
) == 0)
131 return MakeTime(spec
);
132 DiagnoseAndExit("clock_gettime(CLOCK_PROCESS_CPUTIME_ID, ...) failed");
135 if (getrusage(RUSAGE_SELF
, &ru
) == 0) return MakeTime(ru
);
136 DiagnoseAndExit("getrusage(RUSAGE_SELF, ...) failed");
140 double ThreadCPUUsage() {
141 #if defined(BENCHMARK_OS_WINDOWS)
142 HANDLE this_thread
= GetCurrentThread();
143 FILETIME creation_time
;
145 FILETIME kernel_time
;
147 GetThreadTimes(this_thread
, &creation_time
, &exit_time
, &kernel_time
,
149 return MakeTime(kernel_time
, user_time
);
150 #elif defined(BENCHMARK_OS_MACOSX)
151 // FIXME We want to use clock_gettime, but its not available in MacOS 10.11. See
152 // https://github.com/google/benchmark/pull/292
153 mach_msg_type_number_t count
= THREAD_BASIC_INFO_COUNT
;
154 thread_basic_info_data_t info
;
155 mach_port_t thread
= pthread_mach_thread_np(pthread_self());
156 if (thread_info(thread
, THREAD_BASIC_INFO
, (thread_info_t
)&info
, &count
) ==
158 return MakeTime(info
);
160 DiagnoseAndExit("ThreadCPUUsage() failed when evaluating thread_info");
161 #elif defined(BENCHMARK_OS_EMSCRIPTEN)
162 // Emscripten doesn't support traditional threads
163 return ProcessCPUUsage();
164 #elif defined(BENCHMARK_OS_RTEMS)
165 // RTEMS doesn't support CLOCK_THREAD_CPUTIME_ID. See
166 // https://github.com/RTEMS/rtems/blob/master/cpukit/posix/src/clockgettime.c
167 return ProcessCPUUsage();
168 #elif defined(BENCHMARK_OS_SOLARIS)
170 if (getrusage(RUSAGE_LWP
, &ru
) == 0) return MakeTime(ru
);
171 DiagnoseAndExit("getrusage(RUSAGE_LWP, ...) failed");
172 #elif defined(CLOCK_THREAD_CPUTIME_ID)
174 if (clock_gettime(CLOCK_THREAD_CPUTIME_ID
, &ts
) == 0) return MakeTime(ts
);
175 DiagnoseAndExit("clock_gettime(CLOCK_THREAD_CPUTIME_ID, ...) failed");
177 #error Per-thread timing is not available on your system.
183 std::string
DateTimeString(bool local
) {
184 typedef std::chrono::system_clock Clock
;
185 std::time_t now
= Clock::to_time_t(Clock::now());
186 const std::size_t kStorageSize
= 128;
187 char storage
[kStorageSize
];
191 #if defined(BENCHMARK_OS_WINDOWS)
193 std::strftime(storage
, sizeof(storage
), "%x %X", ::localtime(&now
));
196 ::localtime_r(&now
, &timeinfo
);
197 written
= std::strftime(storage
, sizeof(storage
), "%F %T", &timeinfo
);
200 #if defined(BENCHMARK_OS_WINDOWS)
201 written
= std::strftime(storage
, sizeof(storage
), "%x %X", ::gmtime(&now
));
204 ::gmtime_r(&now
, &timeinfo
);
205 written
= std::strftime(storage
, sizeof(storage
), "%F %T", &timeinfo
);
208 CHECK(written
< kStorageSize
);
209 ((void)written
); // prevent unused variable in optimized mode.
210 return std::string(storage
);
215 std::string
LocalDateTimeString() { return DateTimeString(true); }
217 } // end namespace benchmark