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.
17 #include "internal_macros.h"
19 #ifdef BENCHMARK_OS_WINDOWS
21 #undef StrCat // Don't let StrCat in string_util.h be renamed to lstrcatA
22 #include <versionhelpers.h>
26 #if !defined(BENCHMARK_OS_FUCHSIA) && !defined(BENCHMARK_OS_QURT)
27 #include <sys/resource.h>
30 #include <sys/types.h> // this header must be included before 'sys/sysctl.h' to avoid compilation error on FreeBSD
32 #if defined BENCHMARK_OS_FREEBSD || defined BENCHMARK_OS_DRAGONFLY || \
33 defined BENCHMARK_OS_MACOSX
34 #include <sys/sysctl.h>
36 #if defined(BENCHMARK_OS_MACOSX)
37 #include <mach/mach_init.h>
38 #include <mach/mach_port.h>
39 #include <mach/thread_act.h>
41 #if defined(BENCHMARK_OS_QURT)
46 #ifdef BENCHMARK_OS_EMSCRIPTEN
47 #include <emscripten.h>
62 #include "string_util.h"
66 // Suppress unused warnings on helper functions.
68 #pragma GCC diagnostic ignored "-Wunused-function"
70 #if defined(__NVCOMPILER)
71 #pragma diag_suppress declared_but_not_referenced
75 #if defined(BENCHMARK_OS_WINDOWS)
76 double MakeTime(FILETIME
const& kernel_time
, FILETIME
const& user_time
) {
77 ULARGE_INTEGER kernel
;
79 kernel
.HighPart
= kernel_time
.dwHighDateTime
;
80 kernel
.LowPart
= kernel_time
.dwLowDateTime
;
81 user
.HighPart
= user_time
.dwHighDateTime
;
82 user
.LowPart
= user_time
.dwLowDateTime
;
83 return (static_cast<double>(kernel
.QuadPart
) +
84 static_cast<double>(user
.QuadPart
)) *
87 #elif !defined(BENCHMARK_OS_FUCHSIA) && !defined(BENCHMARK_OS_QURT)
88 double MakeTime(struct rusage
const& ru
) {
89 return (static_cast<double>(ru
.ru_utime
.tv_sec
) +
90 static_cast<double>(ru
.ru_utime
.tv_usec
) * 1e-6 +
91 static_cast<double>(ru
.ru_stime
.tv_sec
) +
92 static_cast<double>(ru
.ru_stime
.tv_usec
) * 1e-6);
95 #if defined(BENCHMARK_OS_MACOSX)
96 double MakeTime(thread_basic_info_data_t
const& info
) {
97 return (static_cast<double>(info
.user_time
.seconds
) +
98 static_cast<double>(info
.user_time
.microseconds
) * 1e-6 +
99 static_cast<double>(info
.system_time
.seconds
) +
100 static_cast<double>(info
.system_time
.microseconds
) * 1e-6);
103 #if defined(CLOCK_PROCESS_CPUTIME_ID) || defined(CLOCK_THREAD_CPUTIME_ID)
104 double MakeTime(struct timespec
const& ts
) {
105 return static_cast<double>(ts
.tv_sec
) +
106 (static_cast<double>(ts
.tv_nsec
) * 1e-9);
110 BENCHMARK_NORETURN
static void DiagnoseAndExit(const char* msg
) {
111 std::cerr
<< "ERROR: " << msg
<< std::endl
;
112 std::exit(EXIT_FAILURE
);
117 double ProcessCPUUsage() {
118 #if defined(BENCHMARK_OS_WINDOWS)
119 HANDLE proc
= GetCurrentProcess();
120 FILETIME creation_time
;
122 FILETIME kernel_time
;
124 if (GetProcessTimes(proc
, &creation_time
, &exit_time
, &kernel_time
,
126 return MakeTime(kernel_time
, user_time
);
127 DiagnoseAndExit("GetProccessTimes() failed");
128 #elif defined(BENCHMARK_OS_QURT)
129 return static_cast<double>(
130 qurt_timer_timetick_to_us(qurt_timer_get_ticks())) *
132 #elif defined(BENCHMARK_OS_EMSCRIPTEN)
133 // clock_gettime(CLOCK_PROCESS_CPUTIME_ID, ...) returns 0 on Emscripten.
134 // Use Emscripten-specific API. Reported CPU time would be exactly the
135 // same as total time, but this is ok because there aren't long-latency
136 // synchronous system calls in Emscripten.
137 return emscripten_get_now() * 1e-3;
138 #elif defined(CLOCK_PROCESS_CPUTIME_ID) && !defined(BENCHMARK_OS_MACOSX)
139 // FIXME We want to use clock_gettime, but its not available in MacOS 10.11.
140 // See https://github.com/google/benchmark/pull/292
141 struct timespec spec
;
142 if (clock_gettime(CLOCK_PROCESS_CPUTIME_ID
, &spec
) == 0)
143 return MakeTime(spec
);
144 DiagnoseAndExit("clock_gettime(CLOCK_PROCESS_CPUTIME_ID, ...) failed");
147 if (getrusage(RUSAGE_SELF
, &ru
) == 0) return MakeTime(ru
);
148 DiagnoseAndExit("getrusage(RUSAGE_SELF, ...) failed");
152 double ThreadCPUUsage() {
153 #if defined(BENCHMARK_OS_WINDOWS)
154 HANDLE this_thread
= GetCurrentThread();
155 FILETIME creation_time
;
157 FILETIME kernel_time
;
159 GetThreadTimes(this_thread
, &creation_time
, &exit_time
, &kernel_time
,
161 return MakeTime(kernel_time
, user_time
);
162 #elif defined(BENCHMARK_OS_QURT)
163 return static_cast<double>(
164 qurt_timer_timetick_to_us(qurt_timer_get_ticks())) *
166 #elif defined(BENCHMARK_OS_MACOSX)
167 // FIXME We want to use clock_gettime, but its not available in MacOS 10.11.
168 // See https://github.com/google/benchmark/pull/292
169 mach_msg_type_number_t count
= THREAD_BASIC_INFO_COUNT
;
170 thread_basic_info_data_t info
;
171 mach_port_t thread
= pthread_mach_thread_np(pthread_self());
172 if (thread_info(thread
, THREAD_BASIC_INFO
,
173 reinterpret_cast<thread_info_t
>(&info
),
174 &count
) == KERN_SUCCESS
) {
175 return MakeTime(info
);
177 DiagnoseAndExit("ThreadCPUUsage() failed when evaluating thread_info");
178 #elif defined(BENCHMARK_OS_EMSCRIPTEN)
179 // Emscripten doesn't support traditional threads
180 return ProcessCPUUsage();
181 #elif defined(BENCHMARK_OS_RTEMS)
182 // RTEMS doesn't support CLOCK_THREAD_CPUTIME_ID. See
183 // https://github.com/RTEMS/rtems/blob/master/cpukit/posix/src/clockgettime.c
184 return ProcessCPUUsage();
185 #elif defined(BENCHMARK_OS_ZOS)
186 // z/OS doesn't support CLOCK_THREAD_CPUTIME_ID.
187 return ProcessCPUUsage();
188 #elif defined(BENCHMARK_OS_SOLARIS)
190 if (getrusage(RUSAGE_LWP
, &ru
) == 0) return MakeTime(ru
);
191 DiagnoseAndExit("getrusage(RUSAGE_LWP, ...) failed");
192 #elif defined(CLOCK_THREAD_CPUTIME_ID)
194 if (clock_gettime(CLOCK_THREAD_CPUTIME_ID
, &ts
) == 0) return MakeTime(ts
);
195 DiagnoseAndExit("clock_gettime(CLOCK_THREAD_CPUTIME_ID, ...) failed");
197 #error Per-thread timing is not available on your system.
201 std::string
LocalDateTimeString() {
202 // Write the local time in RFC3339 format yyyy-mm-ddTHH:MM:SS+/-HH:MM.
203 typedef std::chrono::system_clock Clock
;
204 std::time_t now
= Clock::to_time_t(Clock::now());
205 const std::size_t kTzOffsetLen
= 6;
206 const std::size_t kTimestampLen
= 19;
209 std::size_t timestamp_len
;
210 long int offset_minutes
;
211 char tz_offset_sign
= '+';
212 // tz_offset is set in one of three ways:
213 // * strftime with %z - This either returns empty or the ISO 8601 time. The
215 // ISO 8601 string can be is 7 (e.g. -03:30, plus trailing zero).
216 // * snprintf with %c%02li:%02li - The maximum length is 41 (one for %c, up to
218 // one for :, up to 19 %02li, plus trailing zero).
219 // * A fixed string of "-00:00". The maximum length is 7 (-00:00, plus
222 // Thus, the maximum size this needs to be is 41.
224 // Long enough buffer to avoid format-overflow warnings
227 #if defined(BENCHMARK_OS_WINDOWS)
228 std::tm
* timeinfo_p
= ::localtime(&now
);
231 std::tm
* timeinfo_p
= &timeinfo
;
232 ::localtime_r(&now
, &timeinfo
);
235 tz_len
= std::strftime(tz_offset
, sizeof(tz_offset
), "%z", timeinfo_p
);
237 if (tz_len
< kTzOffsetLen
&& tz_len
> 1) {
238 // Timezone offset was written. strftime writes offset as +HHMM or -HHMM,
239 // RFC3339 specifies an offset as +HH:MM or -HH:MM. To convert, we parse
240 // the offset as an integer, then reprint it to a string.
242 offset_minutes
= ::strtol(tz_offset
, NULL
, 10);
243 if (offset_minutes
< 0) {
244 offset_minutes
*= -1;
245 tz_offset_sign
= '-';
249 ::snprintf(tz_offset
, sizeof(tz_offset
), "%c%02li:%02li",
250 tz_offset_sign
, offset_minutes
/ 100, offset_minutes
% 100);
251 BM_CHECK(tz_len
== kTzOffsetLen
);
252 ((void)tz_len
); // Prevent unused variable warning in optimized build.
254 // Unknown offset. RFC3339 specifies that unknown local offsets should be
255 // written as UTC time with -00:00 timezone.
256 #if defined(BENCHMARK_OS_WINDOWS)
257 // Potential race condition if another thread calls localtime or gmtime.
258 timeinfo_p
= ::gmtime(&now
);
260 ::gmtime_r(&now
, &timeinfo
);
263 strncpy(tz_offset
, "-00:00", kTzOffsetLen
+ 1);
267 std::strftime(storage
, sizeof(storage
), "%Y-%m-%dT%H:%M:%S", timeinfo_p
);
268 BM_CHECK(timestamp_len
== kTimestampLen
);
269 // Prevent unused variable warning in optimized build.
270 ((void)kTimestampLen
);
272 std::strncat(storage
, tz_offset
, sizeof(storage
) - timestamp_len
- 1);
273 return std::string(storage
);
276 } // end namespace benchmark