diff options
author | Bruce Dawson <brucedawson@chromium.org> | 2022-02-17 15:01:50 -1000 |
---|---|---|
committer | Bruce Dawson <brucedawson@chromium.org> | 2022-02-17 15:01:50 -1000 |
commit | 40a57813bd9a2551d8377f085974074d1aefc6cb (patch) | |
tree | 35c7d4548d3163def3763cff9df093d00f0e88e3 /src | |
parent | 77e2a4298366442ad0fbfe84043914d1201cac3f (diff) | |
download | ninja-40a57813bd9a2551d8377f085974074d1aefc6cb.tar.gz |
Make chrono optional, and enable updated __cplusplus on MSVC
Diffstat (limited to 'src')
-rw-r--r-- | src/metrics.cc | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/src/metrics.cc b/src/metrics.cc index 9a4dd12..3b403a9 100644 --- a/src/metrics.cc +++ b/src/metrics.cc @@ -14,12 +14,22 @@ #include "metrics.h" +#if __cplusplus >= 201103L +// C++ 11 is not available on older Linux build machines. +#define USE_CHRONO +#endif + #include <errno.h> #include <stdio.h> #include <string.h> #include <algorithm> + +#if defined(USE_CHRONO) #include <chrono> +#else +#include <sys/time.h> +#endif #include "util.h" @@ -31,12 +41,20 @@ namespace { /// Compute a platform-specific high-res timer value that fits into an int64. int64_t HighResTimer() { +#if defined(USE_CHRONO) auto now = chrono::steady_clock::now(); return chrono::duration_cast<chrono::steady_clock::duration>( now.time_since_epoch()) .count(); +#else + timeval tv; + if (gettimeofday(&tv, NULL) < 0) + Fatal("gettimeofday: %s", strerror(errno)); + return (int64_t)tv.tv_sec * 1000*1000 + tv.tv_usec; +#endif } +#if defined(USE_CHRONO) constexpr int64_t GetFrequency() { // If numerator isn't 1 then we lose precision and that will need to be // assessed. @@ -45,15 +63,26 @@ constexpr int64_t GetFrequency() { return std::chrono::steady_clock::period::den / std::chrono::steady_clock::period::num; } +#endif int64_t TimerToMicros(int64_t dt) { +#if defined(USE_CHRONO) // dt is in ticks. We want microseconds. return (dt * 1000000) / GetFrequency(); +#else + // No conversion necessary. + return dt; +#endif } int64_t TimerToMicros(double dt) { +#if defined(USE_CHRONO) // dt is in ticks. We want microseconds. return (dt * 1000000) / GetFrequency(); +#else + // No conversion necessary. + return dt; +#endif } } // anonymous namespace |