Skip to content

Commit

Permalink
Fix OS X thread timings
Browse files Browse the repository at this point in the history
  • Loading branch information
EricWF committed Sep 2, 2016
1 parent d2cbeac commit ef4640b
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/timers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,14 @@
#include "string_util.h"

namespace benchmark {
namespace {

// Suppress unused warnings on helper functions.
#if defined(__GNUC__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-function"
#endif

namespace {
#if defined(BENCHMARK_OS_WINDOWS)
double MakeTime(FILETIME const& kernel_time, FILETIME const& user_time) {
ULARGE_INTEGER kernel;
Expand Down Expand Up @@ -87,6 +93,10 @@ double MakeTime(thread_basic_info_data_t const& info) {

} // end namespace

#if defined(__GNUC__)
#pragma GCC diagnostic pop
#endif

double ProcessCPUUsage() {
#if defined(CLOCK_PROCESS_CPUTIME_ID)
struct timespec spec;
Expand Down Expand Up @@ -146,6 +156,16 @@ double ThreadCPUUsage() {
#elif defined(BENCHMARK_OS_MACOSX)
mach_msg_type_number_t count = THREAD_BASIC_INFO_COUNT;
thread_basic_info_data_t info;
mach_port_t thread = mach_thread_self();
kern_return_t kr = thread_info(thread, THREAD_BASIC_INFO, (thread_info_t) &info, &count);
if (kr == KERN_SUCCESS && (info.flags & TH_FLAGS_IDLE) == 0) {
double t = MakeTime(info);
mach_port_deallocate(mach_task_self(), thread);
return t;
} else {
std::cerr << "Could not retrieve thread info" << std::endl;
std::exit(EXIT_FAILURE);
}
#else
#error Per-thread timing is not available on your system.
#endif
Expand Down

0 comments on commit ef4640b

Please sign in to comment.