| 1 | // Copyright 2015 Google Inc. All rights reserved. |
| 2 | // |
| 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 |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 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. |
| 14 | |
| 15 | #include "timers.h" |
| 16 | #include "internal_macros.h" |
| 17 | |
| 18 | #ifdef BENCHMARK_OS_WINDOWS |
| 19 | #include <Shlwapi.h> |
| 20 | #include <VersionHelpers.h> |
| 21 | #include <Windows.h> |
| 22 | #else |
| 23 | #include <fcntl.h> |
| 24 | #ifndef __Fuchsia__ |
| 25 | #include <sys/resource.h> |
| 26 | #endif |
| 27 | #include <sys/time.h> |
| 28 | #include <sys/types.h> // this header must be included before 'sys/sysctl.h' to avoid compilation error on FreeBSD |
| 29 | #include <unistd.h> |
| 30 | #if defined BENCHMARK_OS_FREEBSD || defined BENCHMARK_OS_MACOSX |
| 31 | #include <sys/sysctl.h> |
| 32 | #endif |
| 33 | #if defined(BENCHMARK_OS_MACOSX) |
| 34 | #include <mach/mach_init.h> |
| 35 | #include <mach/mach_port.h> |
| 36 | #include <mach/thread_act.h> |
| 37 | #endif |
| 38 | #endif |
| 39 | |
| 40 | #include <cerrno> |
| 41 | #include <cstdint> |
| 42 | #include <cstdio> |
| 43 | #include <cstdlib> |
| 44 | #include <cstring> |
| 45 | #include <ctime> |
| 46 | #include <iostream> |
| 47 | #include <limits> |
| 48 | #include <mutex> |
| 49 | |
| 50 | #include "check.h" |
| 51 | #include "log.h" |
| 52 | #include "sleep.h" |
| 53 | #include "string_util.h" |
| 54 | |
| 55 | namespace benchmark { |
| 56 | |
| 57 | // Suppress unused warnings on helper functions. |
| 58 | #if defined(__GNUC__) |
| 59 | #pragma GCC diagnostic ignored "-Wunused-function" |
| 60 | #endif |
| 61 | |
| 62 | namespace { |
| 63 | #if defined(BENCHMARK_OS_WINDOWS) |
| 64 | double MakeTime(FILETIME const& kernel_time, FILETIME const& user_time) { |
| 65 | ULARGE_INTEGER kernel; |
| 66 | ULARGE_INTEGER user; |
| 67 | kernel.HighPart = kernel_time.dwHighDateTime; |
| 68 | kernel.LowPart = kernel_time.dwLowDateTime; |
| 69 | user.HighPart = user_time.dwHighDateTime; |
| 70 | user.LowPart = user_time.dwLowDateTime; |
| 71 | return (static_cast<double>(kernel.QuadPart) + |
| 72 | static_cast<double>(user.QuadPart)) * |
| 73 | 1e-7; |
| 74 | } |
| 75 | #elif !defined(__Fuchsia__) |
| 76 | double MakeTime(struct rusage const& ru) { |
| 77 | return (static_cast<double>(ru.ru_utime.tv_sec) + |
| 78 | static_cast<double>(ru.ru_utime.tv_usec) * 1e-6 + |
| 79 | static_cast<double>(ru.ru_stime.tv_sec) + |
| 80 | static_cast<double>(ru.ru_stime.tv_usec) * 1e-6); |
| 81 | } |
| 82 | #endif |
| 83 | #if defined(BENCHMARK_OS_MACOSX) |
| 84 | double MakeTime(thread_basic_info_data_t const& info) { |
| 85 | return (static_cast<double>(info.user_time.seconds) + |
| 86 | static_cast<double>(info.user_time.microseconds) * 1e-6 + |
| 87 | static_cast<double>(info.system_time.seconds) + |
| 88 | static_cast<double>(info.system_time.microseconds) * 1e-6); |
| 89 | } |
| 90 | #endif |
| 91 | #if defined(CLOCK_PROCESS_CPUTIME_ID) || defined(CLOCK_THREAD_CPUTIME_ID) |
| 92 | double MakeTime(struct timespec const& ts) { |
| 93 | return ts.tv_sec + (static_cast<double>(ts.tv_nsec) * 1e-9); |
| 94 | } |
| 95 | #endif |
| 96 | |
| 97 | BENCHMARK_NORETURN static void DiagnoseAndExit(const char* msg) { |
| 98 | std::cerr << "ERROR: " << msg << std::endl; |
| 99 | std::exit(EXIT_FAILURE); |
| 100 | } |
| 101 | |
| 102 | } // end namespace |
| 103 | |
| 104 | double ProcessCPUUsage() { |
| 105 | // FIXME We want to use clock_gettime, but its not available in MacOS 10.11. See |
| 106 | // https://github.com/google/benchmark/pull/292 |
| 107 | #if defined(CLOCK_PROCESS_CPUTIME_ID) && !defined(BENCHMARK_OS_MACOSX) |
| 108 | struct timespec spec; |
| 109 | if (clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &spec) == 0) |
| 110 | return MakeTime(spec); |
| 111 | DiagnoseAndExit("clock_gettime(CLOCK_PROCESS_CPUTIME_ID, ...) failed" ); |
| 112 | #elif defined(BENCHMARK_OS_WINDOWS) |
| 113 | HANDLE proc = GetCurrentProcess(); |
| 114 | FILETIME creation_time; |
| 115 | FILETIME exit_time; |
| 116 | FILETIME kernel_time; |
| 117 | FILETIME user_time; |
| 118 | if (GetProcessTimes(proc, &creation_time, &exit_time, &kernel_time, |
| 119 | &user_time)) |
| 120 | return MakeTime(kernel_time, user_time); |
| 121 | DiagnoseAndExit("GetProccessTimes() failed" ); |
| 122 | #else |
| 123 | struct rusage ru; |
| 124 | if (getrusage(RUSAGE_SELF, &ru) == 0) return MakeTime(ru); |
| 125 | DiagnoseAndExit("clock_gettime(CLOCK_PROCESS_CPUTIME_ID, ...) failed" ); |
| 126 | #endif |
| 127 | } |
| 128 | |
| 129 | double ThreadCPUUsage() { |
| 130 | // FIXME We want to use clock_gettime, but its not available in MacOS 10.11. See |
| 131 | // https://github.com/google/benchmark/pull/292 |
| 132 | #if defined(CLOCK_THREAD_CPUTIME_ID) && !defined(BENCHMARK_OS_MACOSX) |
| 133 | struct timespec ts; |
| 134 | if (clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts) == 0) return MakeTime(ts); |
| 135 | DiagnoseAndExit("clock_gettime(CLOCK_THREAD_CPUTIME_ID, ...) failed" ); |
| 136 | #elif defined(BENCHMARK_OS_WINDOWS) |
| 137 | HANDLE this_thread = GetCurrentThread(); |
| 138 | FILETIME creation_time; |
| 139 | FILETIME exit_time; |
| 140 | FILETIME kernel_time; |
| 141 | FILETIME user_time; |
| 142 | GetThreadTimes(this_thread, &creation_time, &exit_time, &kernel_time, |
| 143 | &user_time); |
| 144 | return MakeTime(kernel_time, user_time); |
| 145 | #elif defined(BENCHMARK_OS_MACOSX) |
| 146 | mach_msg_type_number_t count = THREAD_BASIC_INFO_COUNT; |
| 147 | thread_basic_info_data_t info; |
| 148 | mach_port_t thread = pthread_mach_thread_np(pthread_self()); |
| 149 | if (thread_info(thread, THREAD_BASIC_INFO, (thread_info_t)&info, &count) == |
| 150 | KERN_SUCCESS) { |
| 151 | return MakeTime(info); |
| 152 | } |
| 153 | DiagnoseAndExit("ThreadCPUUsage() failed when evaluating thread_info" ); |
| 154 | #else |
| 155 | #error Per-thread timing is not available on your system. |
| 156 | #endif |
| 157 | } |
| 158 | |
| 159 | namespace { |
| 160 | |
| 161 | std::string DateTimeString(bool local) { |
| 162 | typedef std::chrono::system_clock Clock; |
| 163 | std::time_t now = Clock::to_time_t(Clock::now()); |
| 164 | const std::size_t kStorageSize = 128; |
| 165 | char storage[kStorageSize]; |
| 166 | std::size_t written; |
| 167 | |
| 168 | if (local) { |
| 169 | #if defined(BENCHMARK_OS_WINDOWS) |
| 170 | written = |
| 171 | std::strftime(storage, sizeof(storage), "%x %X" , ::localtime(&now)); |
| 172 | #else |
| 173 | std::tm timeinfo; |
| 174 | std::memset(&timeinfo, 0, sizeof(std::tm)); |
| 175 | ::localtime_r(&now, &timeinfo); |
| 176 | written = std::strftime(storage, sizeof(storage), "%F %T" , &timeinfo); |
| 177 | #endif |
| 178 | } else { |
| 179 | #if defined(BENCHMARK_OS_WINDOWS) |
| 180 | written = std::strftime(storage, sizeof(storage), "%x %X" , ::gmtime(&now)); |
| 181 | #else |
| 182 | std::tm timeinfo; |
| 183 | std::memset(&timeinfo, 0, sizeof(std::tm)); |
| 184 | ::gmtime_r(&now, &timeinfo); |
| 185 | written = std::strftime(storage, sizeof(storage), "%F %T" , &timeinfo); |
| 186 | #endif |
| 187 | } |
| 188 | CHECK(written < kStorageSize); |
| 189 | ((void)written); // prevent unused variable in optimized mode. |
| 190 | return std::string(storage); |
| 191 | } |
| 192 | |
| 193 | } // end namespace |
| 194 | |
| 195 | std::string LocalDateTimeString() { return DateTimeString(true); } |
| 196 | |
| 197 | } // end namespace benchmark |
| 198 | |