From ee3cfca651deae5c8af3e873f5d085074cde9fad Mon Sep 17 00:00:00 2001 From: Tom Madams Date: Thu, 6 Jul 2017 15:59:13 -0700 Subject: [PATCH] Fix ThreadCPUUsage when running on RTEMS. (#414) Change ThreadCPUUsage to call ProcessCPUUsage if __rtems__ is defined. RTEMS real time OS doesn't support CLOCK_THREAD_CPUTIME_ID. See https://github.com/RTEMS/rtems/blob/master/cpukit/posix/src/clockgettime.c#L58-L59 Prior to this change, ThreadCPUUsage would fail when running on RTEMS with: ERROR: clock_gettime(CLOCK_THREAD_CPUTIME_ID, ...) failed --- CONTRIBUTORS | 1 + src/internal_macros.h | 2 ++ src/timers.cc | 4 ++++ 3 files changed, 7 insertions(+) diff --git a/CONTRIBUTORS b/CONTRIBUTORS index 58f81fd3..9abb6086 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -51,6 +51,7 @@ Pierre Phaneuf Radoslav Yovchev Ray Glover Shuo Chen +Tom Madams Yixuan Qiu Yusuke Suzuki Tobias Ulvgård diff --git a/src/internal_macros.h b/src/internal_macros.h index 8dd9f87b..94288745 100644 --- a/src/internal_macros.h +++ b/src/internal_macros.h @@ -45,6 +45,8 @@ #define BENCHMARK_OS_NACL 1 #elif defined(EMSCRIPTEN) #define BENCHMARK_OS_EMSCRIPTEN 1 +#elif defined(__rtems__) +#define BENCHMARK_OS_RTEMS 1 #endif #if !__has_feature(cxx_exceptions) && !defined(__cpp_exceptions) \ diff --git a/src/timers.cc b/src/timers.cc index 8d56e8ad..817272d0 100644 --- a/src/timers.cc +++ b/src/timers.cc @@ -158,6 +158,10 @@ double ThreadCPUUsage() { #elif defined(BENCHMARK_OS_EMSCRIPTEN) // Emscripten doesn't support traditional threads return ProcessCPUUsage(); +#elif defined(BENCHMARK_OS_RTEMS) + // RTEMS doesn't support CLOCK_THREAD_CPUTIME_ID. See + // https://github.com/RTEMS/rtems/blob/master/cpukit/posix/src/clockgettime.c + return ProcessCPUUsage(); #elif defined(CLOCK_THREAD_CPUTIME_ID) struct timespec ts; if (clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts) == 0) return MakeTime(ts);