finish selection of clock with debug information
diff --git a/CMakeLists.txt b/CMakeLists.txt
index b3c8a30..8944d44 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -54,6 +54,7 @@
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 0000000..4aa72a6
--- /dev/null
+++ b/cmake/steady_clock.cpp
@@ -0,0 +1,7 @@
+#include <chrono>
+
+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 96108da..1d168e0 100644
--- a/src/reporter.cc
+++ b/src/reporter.cc
@@ -103,7 +103,7 @@
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 3a47799..325040b 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 {
+#if defined(HAVE_STEADY_CLOCK)
+template <bool HighResIsSteady = std::chrono::high_resolution_clock::is_steady>
+struct ChooseSteadyClock {
+ typedef std::chrono::high_resolution_clock type;
+};
+
+template <>
+struct ChooseSteadyClock<false> {
+ 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 ChronoWalltimeNow() {
- typedef std::chrono::system_clock Clock;
+ typedef ChooseClockType::type Clock;
typedef std::chrono::duration<WallTime, std::chrono::seconds::period>
FPSeconds;
static_assert(std::chrono::treat_as_floating_point<WallTime>::value,
@@ -160,13 +181,23 @@
return std::chrono::duration_cast<FPSeconds>(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 @@
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));