1*58b9f456SAndroid Build Coastguard Worker // Copyright 2015 Google Inc. All rights reserved.
2*58b9f456SAndroid Build Coastguard Worker //
3*58b9f456SAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License");
4*58b9f456SAndroid Build Coastguard Worker // you may not use this file except in compliance with the License.
5*58b9f456SAndroid Build Coastguard Worker // You may obtain a copy of the License at
6*58b9f456SAndroid Build Coastguard Worker //
7*58b9f456SAndroid Build Coastguard Worker // http://www.apache.org/licenses/LICENSE-2.0
8*58b9f456SAndroid Build Coastguard Worker //
9*58b9f456SAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
10*58b9f456SAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS,
11*58b9f456SAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*58b9f456SAndroid Build Coastguard Worker // See the License for the specific language governing permissions and
13*58b9f456SAndroid Build Coastguard Worker // limitations under the License.
14*58b9f456SAndroid Build Coastguard Worker
15*58b9f456SAndroid Build Coastguard Worker #include "timers.h"
16*58b9f456SAndroid Build Coastguard Worker #include "internal_macros.h"
17*58b9f456SAndroid Build Coastguard Worker
18*58b9f456SAndroid Build Coastguard Worker #ifdef BENCHMARK_OS_WINDOWS
19*58b9f456SAndroid Build Coastguard Worker #include <shlwapi.h>
20*58b9f456SAndroid Build Coastguard Worker #undef StrCat // Don't let StrCat in string_util.h be renamed to lstrcatA
21*58b9f456SAndroid Build Coastguard Worker #include <versionhelpers.h>
22*58b9f456SAndroid Build Coastguard Worker #include <windows.h>
23*58b9f456SAndroid Build Coastguard Worker #else
24*58b9f456SAndroid Build Coastguard Worker #include <fcntl.h>
25*58b9f456SAndroid Build Coastguard Worker #ifndef BENCHMARK_OS_FUCHSIA
26*58b9f456SAndroid Build Coastguard Worker #include <sys/resource.h>
27*58b9f456SAndroid Build Coastguard Worker #endif
28*58b9f456SAndroid Build Coastguard Worker #include <sys/time.h>
29*58b9f456SAndroid Build Coastguard Worker #include <sys/types.h> // this header must be included before 'sys/sysctl.h' to avoid compilation error on FreeBSD
30*58b9f456SAndroid Build Coastguard Worker #include <unistd.h>
31*58b9f456SAndroid Build Coastguard Worker #if defined BENCHMARK_OS_FREEBSD || defined BENCHMARK_OS_MACOSX
32*58b9f456SAndroid Build Coastguard Worker #include <sys/sysctl.h>
33*58b9f456SAndroid Build Coastguard Worker #endif
34*58b9f456SAndroid Build Coastguard Worker #if defined(BENCHMARK_OS_MACOSX)
35*58b9f456SAndroid Build Coastguard Worker #include <mach/mach_init.h>
36*58b9f456SAndroid Build Coastguard Worker #include <mach/mach_port.h>
37*58b9f456SAndroid Build Coastguard Worker #include <mach/thread_act.h>
38*58b9f456SAndroid Build Coastguard Worker #endif
39*58b9f456SAndroid Build Coastguard Worker #endif
40*58b9f456SAndroid Build Coastguard Worker
41*58b9f456SAndroid Build Coastguard Worker #ifdef BENCHMARK_OS_EMSCRIPTEN
42*58b9f456SAndroid Build Coastguard Worker #include <emscripten.h>
43*58b9f456SAndroid Build Coastguard Worker #endif
44*58b9f456SAndroid Build Coastguard Worker
45*58b9f456SAndroid Build Coastguard Worker #include <cerrno>
46*58b9f456SAndroid Build Coastguard Worker #include <cstdint>
47*58b9f456SAndroid Build Coastguard Worker #include <cstdio>
48*58b9f456SAndroid Build Coastguard Worker #include <cstdlib>
49*58b9f456SAndroid Build Coastguard Worker #include <cstring>
50*58b9f456SAndroid Build Coastguard Worker #include <ctime>
51*58b9f456SAndroid Build Coastguard Worker #include <iostream>
52*58b9f456SAndroid Build Coastguard Worker #include <limits>
53*58b9f456SAndroid Build Coastguard Worker #include <mutex>
54*58b9f456SAndroid Build Coastguard Worker
55*58b9f456SAndroid Build Coastguard Worker #include "check.h"
56*58b9f456SAndroid Build Coastguard Worker #include "log.h"
57*58b9f456SAndroid Build Coastguard Worker #include "sleep.h"
58*58b9f456SAndroid Build Coastguard Worker #include "string_util.h"
59*58b9f456SAndroid Build Coastguard Worker
60*58b9f456SAndroid Build Coastguard Worker namespace benchmark {
61*58b9f456SAndroid Build Coastguard Worker
62*58b9f456SAndroid Build Coastguard Worker // Suppress unused warnings on helper functions.
63*58b9f456SAndroid Build Coastguard Worker #if defined(__GNUC__)
64*58b9f456SAndroid Build Coastguard Worker #pragma GCC diagnostic ignored "-Wunused-function"
65*58b9f456SAndroid Build Coastguard Worker #endif
66*58b9f456SAndroid Build Coastguard Worker
67*58b9f456SAndroid Build Coastguard Worker namespace {
68*58b9f456SAndroid Build Coastguard Worker #if defined(BENCHMARK_OS_WINDOWS)
MakeTime(FILETIME const & kernel_time,FILETIME const & user_time)69*58b9f456SAndroid Build Coastguard Worker double MakeTime(FILETIME const& kernel_time, FILETIME const& user_time) {
70*58b9f456SAndroid Build Coastguard Worker ULARGE_INTEGER kernel;
71*58b9f456SAndroid Build Coastguard Worker ULARGE_INTEGER user;
72*58b9f456SAndroid Build Coastguard Worker kernel.HighPart = kernel_time.dwHighDateTime;
73*58b9f456SAndroid Build Coastguard Worker kernel.LowPart = kernel_time.dwLowDateTime;
74*58b9f456SAndroid Build Coastguard Worker user.HighPart = user_time.dwHighDateTime;
75*58b9f456SAndroid Build Coastguard Worker user.LowPart = user_time.dwLowDateTime;
76*58b9f456SAndroid Build Coastguard Worker return (static_cast<double>(kernel.QuadPart) +
77*58b9f456SAndroid Build Coastguard Worker static_cast<double>(user.QuadPart)) *
78*58b9f456SAndroid Build Coastguard Worker 1e-7;
79*58b9f456SAndroid Build Coastguard Worker }
80*58b9f456SAndroid Build Coastguard Worker #elif !defined(BENCHMARK_OS_FUCHSIA)
81*58b9f456SAndroid Build Coastguard Worker double MakeTime(struct rusage const& ru) {
82*58b9f456SAndroid Build Coastguard Worker return (static_cast<double>(ru.ru_utime.tv_sec) +
83*58b9f456SAndroid Build Coastguard Worker static_cast<double>(ru.ru_utime.tv_usec) * 1e-6 +
84*58b9f456SAndroid Build Coastguard Worker static_cast<double>(ru.ru_stime.tv_sec) +
85*58b9f456SAndroid Build Coastguard Worker static_cast<double>(ru.ru_stime.tv_usec) * 1e-6);
86*58b9f456SAndroid Build Coastguard Worker }
87*58b9f456SAndroid Build Coastguard Worker #endif
88*58b9f456SAndroid Build Coastguard Worker #if defined(BENCHMARK_OS_MACOSX)
MakeTime(thread_basic_info_data_t const & info)89*58b9f456SAndroid Build Coastguard Worker double MakeTime(thread_basic_info_data_t const& info) {
90*58b9f456SAndroid Build Coastguard Worker return (static_cast<double>(info.user_time.seconds) +
91*58b9f456SAndroid Build Coastguard Worker static_cast<double>(info.user_time.microseconds) * 1e-6 +
92*58b9f456SAndroid Build Coastguard Worker static_cast<double>(info.system_time.seconds) +
93*58b9f456SAndroid Build Coastguard Worker static_cast<double>(info.system_time.microseconds) * 1e-6);
94*58b9f456SAndroid Build Coastguard Worker }
95*58b9f456SAndroid Build Coastguard Worker #endif
96*58b9f456SAndroid Build Coastguard Worker #if defined(CLOCK_PROCESS_CPUTIME_ID) || defined(CLOCK_THREAD_CPUTIME_ID)
MakeTime(struct timespec const & ts)97*58b9f456SAndroid Build Coastguard Worker double MakeTime(struct timespec const& ts) {
98*58b9f456SAndroid Build Coastguard Worker return ts.tv_sec + (static_cast<double>(ts.tv_nsec) * 1e-9);
99*58b9f456SAndroid Build Coastguard Worker }
100*58b9f456SAndroid Build Coastguard Worker #endif
101*58b9f456SAndroid Build Coastguard Worker
DiagnoseAndExit(const char * msg)102*58b9f456SAndroid Build Coastguard Worker BENCHMARK_NORETURN static void DiagnoseAndExit(const char* msg) {
103*58b9f456SAndroid Build Coastguard Worker std::cerr << "ERROR: " << msg << std::endl;
104*58b9f456SAndroid Build Coastguard Worker std::exit(EXIT_FAILURE);
105*58b9f456SAndroid Build Coastguard Worker }
106*58b9f456SAndroid Build Coastguard Worker
107*58b9f456SAndroid Build Coastguard Worker } // end namespace
108*58b9f456SAndroid Build Coastguard Worker
ProcessCPUUsage()109*58b9f456SAndroid Build Coastguard Worker double ProcessCPUUsage() {
110*58b9f456SAndroid Build Coastguard Worker #if defined(BENCHMARK_OS_WINDOWS)
111*58b9f456SAndroid Build Coastguard Worker HANDLE proc = GetCurrentProcess();
112*58b9f456SAndroid Build Coastguard Worker FILETIME creation_time;
113*58b9f456SAndroid Build Coastguard Worker FILETIME exit_time;
114*58b9f456SAndroid Build Coastguard Worker FILETIME kernel_time;
115*58b9f456SAndroid Build Coastguard Worker FILETIME user_time;
116*58b9f456SAndroid Build Coastguard Worker if (GetProcessTimes(proc, &creation_time, &exit_time, &kernel_time,
117*58b9f456SAndroid Build Coastguard Worker &user_time))
118*58b9f456SAndroid Build Coastguard Worker return MakeTime(kernel_time, user_time);
119*58b9f456SAndroid Build Coastguard Worker DiagnoseAndExit("GetProccessTimes() failed");
120*58b9f456SAndroid Build Coastguard Worker #elif defined(BENCHMARK_OS_EMSCRIPTEN)
121*58b9f456SAndroid Build Coastguard Worker // clock_gettime(CLOCK_PROCESS_CPUTIME_ID, ...) returns 0 on Emscripten.
122*58b9f456SAndroid Build Coastguard Worker // Use Emscripten-specific API. Reported CPU time would be exactly the
123*58b9f456SAndroid Build Coastguard Worker // same as total time, but this is ok because there aren't long-latency
124*58b9f456SAndroid Build Coastguard Worker // syncronous system calls in Emscripten.
125*58b9f456SAndroid Build Coastguard Worker return emscripten_get_now() * 1e-3;
126*58b9f456SAndroid Build Coastguard Worker #elif defined(CLOCK_PROCESS_CPUTIME_ID) && !defined(BENCHMARK_OS_MACOSX)
127*58b9f456SAndroid Build Coastguard Worker // FIXME We want to use clock_gettime, but its not available in MacOS 10.11. See
128*58b9f456SAndroid Build Coastguard Worker // https://github.com/google/benchmark/pull/292
129*58b9f456SAndroid Build Coastguard Worker struct timespec spec;
130*58b9f456SAndroid Build Coastguard Worker if (clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &spec) == 0)
131*58b9f456SAndroid Build Coastguard Worker return MakeTime(spec);
132*58b9f456SAndroid Build Coastguard Worker DiagnoseAndExit("clock_gettime(CLOCK_PROCESS_CPUTIME_ID, ...) failed");
133*58b9f456SAndroid Build Coastguard Worker #else
134*58b9f456SAndroid Build Coastguard Worker struct rusage ru;
135*58b9f456SAndroid Build Coastguard Worker if (getrusage(RUSAGE_SELF, &ru) == 0) return MakeTime(ru);
136*58b9f456SAndroid Build Coastguard Worker DiagnoseAndExit("getrusage(RUSAGE_SELF, ...) failed");
137*58b9f456SAndroid Build Coastguard Worker #endif
138*58b9f456SAndroid Build Coastguard Worker }
139*58b9f456SAndroid Build Coastguard Worker
ThreadCPUUsage()140*58b9f456SAndroid Build Coastguard Worker double ThreadCPUUsage() {
141*58b9f456SAndroid Build Coastguard Worker #if defined(BENCHMARK_OS_WINDOWS)
142*58b9f456SAndroid Build Coastguard Worker HANDLE this_thread = GetCurrentThread();
143*58b9f456SAndroid Build Coastguard Worker FILETIME creation_time;
144*58b9f456SAndroid Build Coastguard Worker FILETIME exit_time;
145*58b9f456SAndroid Build Coastguard Worker FILETIME kernel_time;
146*58b9f456SAndroid Build Coastguard Worker FILETIME user_time;
147*58b9f456SAndroid Build Coastguard Worker GetThreadTimes(this_thread, &creation_time, &exit_time, &kernel_time,
148*58b9f456SAndroid Build Coastguard Worker &user_time);
149*58b9f456SAndroid Build Coastguard Worker return MakeTime(kernel_time, user_time);
150*58b9f456SAndroid Build Coastguard Worker #elif defined(BENCHMARK_OS_MACOSX)
151*58b9f456SAndroid Build Coastguard Worker // FIXME We want to use clock_gettime, but its not available in MacOS 10.11. See
152*58b9f456SAndroid Build Coastguard Worker // https://github.com/google/benchmark/pull/292
153*58b9f456SAndroid Build Coastguard Worker mach_msg_type_number_t count = THREAD_BASIC_INFO_COUNT;
154*58b9f456SAndroid Build Coastguard Worker thread_basic_info_data_t info;
155*58b9f456SAndroid Build Coastguard Worker mach_port_t thread = pthread_mach_thread_np(pthread_self());
156*58b9f456SAndroid Build Coastguard Worker if (thread_info(thread, THREAD_BASIC_INFO, (thread_info_t)&info, &count) ==
157*58b9f456SAndroid Build Coastguard Worker KERN_SUCCESS) {
158*58b9f456SAndroid Build Coastguard Worker return MakeTime(info);
159*58b9f456SAndroid Build Coastguard Worker }
160*58b9f456SAndroid Build Coastguard Worker DiagnoseAndExit("ThreadCPUUsage() failed when evaluating thread_info");
161*58b9f456SAndroid Build Coastguard Worker #elif defined(BENCHMARK_OS_EMSCRIPTEN)
162*58b9f456SAndroid Build Coastguard Worker // Emscripten doesn't support traditional threads
163*58b9f456SAndroid Build Coastguard Worker return ProcessCPUUsage();
164*58b9f456SAndroid Build Coastguard Worker #elif defined(BENCHMARK_OS_RTEMS)
165*58b9f456SAndroid Build Coastguard Worker // RTEMS doesn't support CLOCK_THREAD_CPUTIME_ID. See
166*58b9f456SAndroid Build Coastguard Worker // https://github.com/RTEMS/rtems/blob/master/cpukit/posix/src/clockgettime.c
167*58b9f456SAndroid Build Coastguard Worker return ProcessCPUUsage();
168*58b9f456SAndroid Build Coastguard Worker #elif defined(BENCHMARK_OS_SOLARIS)
169*58b9f456SAndroid Build Coastguard Worker struct rusage ru;
170*58b9f456SAndroid Build Coastguard Worker if (getrusage(RUSAGE_LWP, &ru) == 0) return MakeTime(ru);
171*58b9f456SAndroid Build Coastguard Worker DiagnoseAndExit("getrusage(RUSAGE_LWP, ...) failed");
172*58b9f456SAndroid Build Coastguard Worker #elif defined(CLOCK_THREAD_CPUTIME_ID)
173*58b9f456SAndroid Build Coastguard Worker struct timespec ts;
174*58b9f456SAndroid Build Coastguard Worker if (clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts) == 0) return MakeTime(ts);
175*58b9f456SAndroid Build Coastguard Worker DiagnoseAndExit("clock_gettime(CLOCK_THREAD_CPUTIME_ID, ...) failed");
176*58b9f456SAndroid Build Coastguard Worker #else
177*58b9f456SAndroid Build Coastguard Worker #error Per-thread timing is not available on your system.
178*58b9f456SAndroid Build Coastguard Worker #endif
179*58b9f456SAndroid Build Coastguard Worker }
180*58b9f456SAndroid Build Coastguard Worker
181*58b9f456SAndroid Build Coastguard Worker namespace {
182*58b9f456SAndroid Build Coastguard Worker
DateTimeString(bool local)183*58b9f456SAndroid Build Coastguard Worker std::string DateTimeString(bool local) {
184*58b9f456SAndroid Build Coastguard Worker typedef std::chrono::system_clock Clock;
185*58b9f456SAndroid Build Coastguard Worker std::time_t now = Clock::to_time_t(Clock::now());
186*58b9f456SAndroid Build Coastguard Worker const std::size_t kStorageSize = 128;
187*58b9f456SAndroid Build Coastguard Worker char storage[kStorageSize];
188*58b9f456SAndroid Build Coastguard Worker std::size_t written;
189*58b9f456SAndroid Build Coastguard Worker
190*58b9f456SAndroid Build Coastguard Worker if (local) {
191*58b9f456SAndroid Build Coastguard Worker #if defined(BENCHMARK_OS_WINDOWS)
192*58b9f456SAndroid Build Coastguard Worker written =
193*58b9f456SAndroid Build Coastguard Worker std::strftime(storage, sizeof(storage), "%x %X", ::localtime(&now));
194*58b9f456SAndroid Build Coastguard Worker #else
195*58b9f456SAndroid Build Coastguard Worker std::tm timeinfo;
196*58b9f456SAndroid Build Coastguard Worker ::localtime_r(&now, &timeinfo);
197*58b9f456SAndroid Build Coastguard Worker written = std::strftime(storage, sizeof(storage), "%F %T", &timeinfo);
198*58b9f456SAndroid Build Coastguard Worker #endif
199*58b9f456SAndroid Build Coastguard Worker } else {
200*58b9f456SAndroid Build Coastguard Worker #if defined(BENCHMARK_OS_WINDOWS)
201*58b9f456SAndroid Build Coastguard Worker written = std::strftime(storage, sizeof(storage), "%x %X", ::gmtime(&now));
202*58b9f456SAndroid Build Coastguard Worker #else
203*58b9f456SAndroid Build Coastguard Worker std::tm timeinfo;
204*58b9f456SAndroid Build Coastguard Worker ::gmtime_r(&now, &timeinfo);
205*58b9f456SAndroid Build Coastguard Worker written = std::strftime(storage, sizeof(storage), "%F %T", &timeinfo);
206*58b9f456SAndroid Build Coastguard Worker #endif
207*58b9f456SAndroid Build Coastguard Worker }
208*58b9f456SAndroid Build Coastguard Worker CHECK(written < kStorageSize);
209*58b9f456SAndroid Build Coastguard Worker ((void)written); // prevent unused variable in optimized mode.
210*58b9f456SAndroid Build Coastguard Worker return std::string(storage);
211*58b9f456SAndroid Build Coastguard Worker }
212*58b9f456SAndroid Build Coastguard Worker
213*58b9f456SAndroid Build Coastguard Worker } // end namespace
214*58b9f456SAndroid Build Coastguard Worker
LocalDateTimeString()215*58b9f456SAndroid Build Coastguard Worker std::string LocalDateTimeString() { return DateTimeString(true); }
216*58b9f456SAndroid Build Coastguard Worker
217*58b9f456SAndroid Build Coastguard Worker } // end namespace benchmark
218