18 #ifdef BENCHMARK_OS_WINDOWS
20 #include <VersionHelpers.h>
24 #include <sys/resource.h>
26 #include <sys/types.h>
28 #if defined BENCHMARK_OS_FREEBSD || defined BENCHMARK_OS_MACOSX
29 #include <sys/sysctl.h>
31 #if defined(BENCHMARK_OS_MACOSX)
32 #include <mach/mach_init.h>
33 #include <mach/mach_port.h>
34 #include <mach/thread_act.h>
38 #ifdef BENCHMARK_OS_EMSCRIPTEN
39 #include <emscripten.h>
61 #pragma GCC diagnostic ignored "-Wunused-function"
65 #if defined(BENCHMARK_OS_WINDOWS)
66 double MakeTime(FILETIME
const& kernel_time, FILETIME
const& user_time) {
67 ULARGE_INTEGER kernel;
69 kernel.HighPart = kernel_time.dwHighDateTime;
70 kernel.LowPart = kernel_time.dwLowDateTime;
71 user.HighPart = user_time.dwHighDateTime;
72 user.LowPart = user_time.dwLowDateTime;
73 return (
static_cast<double>(kernel.QuadPart) +
74 static_cast<double>(user.QuadPart)) *
78 double MakeTime(
struct rusage
const& ru) {
79 return (
static_cast<double>(ru.ru_utime.tv_sec) +
80 static_cast<double>(ru.ru_utime.tv_usec) * 1e-6 +
81 static_cast<double>(ru.ru_stime.tv_sec) +
82 static_cast<double>(ru.ru_stime.tv_usec) * 1e-6);
85 #if defined(BENCHMARK_OS_MACOSX)
86 double MakeTime(thread_basic_info_data_t
const& info) {
87 return (
static_cast<double>(info.user_time.seconds) +
88 static_cast<double>(info.user_time.microseconds) * 1e-6 +
89 static_cast<double>(info.system_time.seconds) +
90 static_cast<double>(info.system_time.microseconds) * 1e-6);
93 #if defined(CLOCK_PROCESS_CPUTIME_ID) || defined(CLOCK_THREAD_CPUTIME_ID)
94 double MakeTime(
struct timespec
const& ts) {
95 return ts.tv_sec + (
static_cast<double>(ts.tv_nsec) * 1e-9);
100 std::cerr <<
"ERROR: " << msg << std::endl;
101 std::exit(EXIT_FAILURE);
107 #if defined(BENCHMARK_OS_WINDOWS)
108 HANDLE proc = GetCurrentProcess();
109 FILETIME creation_time;
111 FILETIME kernel_time;
113 if (GetProcessTimes(proc, &creation_time, &exit_time, &kernel_time,
115 return MakeTime(kernel_time, user_time);
116 DiagnoseAndExit(
"GetProccessTimes() failed");
117 #elif defined(BENCHMARK_OS_EMSCRIPTEN)
122 return emscripten_get_now() * 1e-3;
123 #elif defined(CLOCK_PROCESS_CPUTIME_ID) && !defined(BENCHMARK_OS_MACOSX)
126 struct timespec spec;
127 if (clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &spec) == 0)
128 return MakeTime(spec);
129 DiagnoseAndExit(
"clock_gettime(CLOCK_PROCESS_CPUTIME_ID, ...) failed");
132 if (getrusage(RUSAGE_SELF, &ru) == 0)
return MakeTime(ru);
133 DiagnoseAndExit(
"getrusage(RUSAGE_SELF, ...) failed");
138 #if defined(BENCHMARK_OS_WINDOWS)
139 HANDLE this_thread = GetCurrentThread();
140 FILETIME creation_time;
142 FILETIME kernel_time;
144 GetThreadTimes(this_thread, &creation_time, &exit_time, &kernel_time,
146 return MakeTime(kernel_time, user_time);
147 #elif defined(BENCHMARK_OS_MACOSX)
150 mach_msg_type_number_t
count = THREAD_BASIC_INFO_COUNT;
151 thread_basic_info_data_t info;
152 mach_port_t thread = pthread_mach_thread_np(pthread_self());
153 if (thread_info(thread, THREAD_BASIC_INFO, (thread_info_t)&info, &
count) ==
155 return MakeTime(info);
157 DiagnoseAndExit(
"ThreadCPUUsage() failed when evaluating thread_info");
158 #elif defined(BENCHMARK_OS_EMSCRIPTEN)
161 #elif defined(BENCHMARK_OS_RTEMS)
165 #elif defined(CLOCK_THREAD_CPUTIME_ID)
167 if (clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts) == 0)
return MakeTime(ts);
168 DiagnoseAndExit(
"clock_gettime(CLOCK_THREAD_CPUTIME_ID, ...) failed");
170 #error Per-thread timing is not available on your system.
177 typedef std::chrono::system_clock Clock;
178 std::time_t now = Clock::to_time_t(Clock::now());
179 const std::size_t kStorageSize = 128;
180 char storage[kStorageSize];
184 #if defined(BENCHMARK_OS_WINDOWS)
186 std::strftime(storage,
sizeof(storage),
"%x %X", ::localtime(&now));
189 std::memset(&timeinfo, 0,
sizeof(std::tm));
191 written = std::strftime(storage,
sizeof(storage),
"%F %T", &timeinfo);
194 #if defined(BENCHMARK_OS_WINDOWS)
195 written = std::strftime(storage,
sizeof(storage),
"%x %X", ::gmtime(&now));
198 std::memset(&timeinfo, 0,
sizeof(std::tm));
200 written = std::strftime(storage,
sizeof(storage),
"%F %T", &timeinfo);
203 CHECK(written < kStorageSize);