From d3e0671a879f8ec5ced7018eb338cb7eb6d191a3 Mon Sep 17 00:00:00 2001 From: Eric Fiselier Date: Thu, 26 Mar 2015 17:52:28 -0400 Subject: [PATCH] finish selection of clock with debug information --- CMakeLists.txt | 1 + cmake/steady_clock.cpp | 7 +++++++ src/reporter.cc | 2 +- src/walltime.cc | 46 +++++++++++++++++++++++++++++++++--------- 4 files changed, 46 insertions(+), 10 deletions(-) create mode 100644 cmake/steady_clock.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index b3c8a308..8944d446 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -54,6 +54,7 @@ endif() cxx_feature_check(STD_REGEX) cxx_feature_check(GNU_POSIX_REGEX) cxx_feature_check(POSIX_REGEX) +cxx_feature_check(STEADY_CLOCK) # Set up directories include_directories(${PROJECT_SOURCE_DIR}/include) diff --git a/cmake/steady_clock.cpp b/cmake/steady_clock.cpp new file mode 100644 index 00000000..4aa72a6e --- /dev/null +++ b/cmake/steady_clock.cpp @@ -0,0 +1,7 @@ +#include + +int main() { + typedef std::chrono::steady_clock Clock; + Clock::time_point tp = Clock::now(); + ((void)tp); +} \ No newline at end of file diff --git a/src/reporter.cc b/src/reporter.cc index 96108da8..1d168e08 100644 --- a/src/reporter.cc +++ b/src/reporter.cc @@ -103,7 +103,7 @@ bool ConsoleReporter::ReportContext(const Context& context) { if (context.cpu_scaling_enabled) { fprintf(stdout, "***WARNING*** CPU scaling is enabled, the benchmark " - "timings may be noisy\n"); + "timings may be noisy and will incure extra overhead.\n"); } #ifndef NDEBUG diff --git a/src/walltime.cc b/src/walltime.cc index 3a477991..325040b0 100644 --- a/src/walltime.cc +++ b/src/walltime.cc @@ -29,6 +29,7 @@ #include "arraysize.h" #include "check.h" #include "cycleclock.h" +#include "log.h" #include "sysinfo.h" namespace benchmark { @@ -36,6 +37,26 @@ namespace walltime { namespace { +#if defined(HAVE_STEADY_CLOCK) +template +struct ChooseSteadyClock { + typedef std::chrono::high_resolution_clock type; +}; + +template <> +struct ChooseSteadyClock { + typedef std::chrono::steady_clock type; +}; +#endif + +struct ChooseClockType { +#if defined(HAVE_STEADY_CLOCK) + typedef typename ChooseSteadyClock<>::type type; +#else + typedef std::chrono::high_resolution_clock type; +#endif +}; + class WallTimeImp { public: @@ -151,7 +172,7 @@ WallTime CPUWalltimeNow() { } WallTime ChronoWalltimeNow() { - typedef std::chrono::system_clock Clock; + typedef ChooseClockType::type Clock; typedef std::chrono::duration FPSeconds; static_assert(std::chrono::treat_as_floating_point::value, @@ -160,13 +181,23 @@ WallTime ChronoWalltimeNow() { return std::chrono::duration_cast(now).count(); } +bool UseCpuCycleClock() { + bool useWallTime = !CpuScalingEnabled(); + if (useWallTime) { + VLOG(1) << "Using the CPU cycle clock to provide walltime::Now().\n"; + } else { + VLOG(1) << "Using std::chrono to provide walltime::Now().\n"; + } + return useWallTime; +} + // WallTimeImp doesn't work when CPU Scaling is enabled. If CPU Scaling is // enabled at the start of the program then std::chrono::system_clock is used // instead. WallTime Now() { - static bool useWallTime = !CpuScalingEnabled(); - if (useWallTime) { + static bool useCPUClock = UseCpuCycleClock(); + if (useCPUClock) { return CPUWalltimeNow(); } else { return ChronoWalltimeNow(); @@ -182,14 +213,11 @@ std::string DateTimeString(bool local) { char storage[128]; std::tm timeinfo; + std::memset(&timeinfo, 0, sizeof(std::tm)); if (local) { - std::tm* ret = std::localtime(&now); - CHECK(ret != nullptr); - timeinfo = *ret; + ::localtime_r(&now, &timeinfo); } else { - std::tm* ret = std::gmtime(&now); - CHECK(ret != nullptr); - timeinfo = *ret; + ::gmtime_r(&now, &timeinfo); } std::size_t written = std::strftime(storage, sizeof(storage), "%F %T", &timeinfo); CHECK(written < arraysize(storage));