C++ refactor: time getters now return milliseconds Previously they returned nanoseconds but all call sites converted those to milliseconds. This is not only a simplification of the semantics and renaming of the methods to make the returned units and the purpose clear, but also preparation for the Windows/MSVC implementations of these methods. -- MOS_MIGRATED_REVID=138383956
diff --git a/src/main/cpp/blaze.cc b/src/main/cpp/blaze.cc index 26053f6..da7b100 100644 --- a/src/main/cpp/blaze.cc +++ b/src/main/cpp/blaze.cc
@@ -655,7 +655,7 @@ } // Wall clock time since process startup. - globals->startup_time = ProcessClock() / 1000000LL; + globals->startup_time = GetMillisecondsSinceProcessStart(); if (VerboseLogging()) { fprintf(stderr, "Starting %s in batch mode.\n", @@ -985,15 +985,15 @@ // If the install dir doesn't exist, create it, if it does, we know it's good. struct stat buf; if (stat(globals->options->install_base.c_str(), &buf) == -1) { - uint64_t st = MonotonicClock(); + uint64_t st = GetMillisecondsMonotonic(); // Work in a temp dir to avoid races. string tmp_install = globals->options->install_base + ".tmp." + ToString(getpid()); string tmp_binaries = tmp_install + "/_embedded_binaries"; ActuallyExtractData(self_path, tmp_binaries); - uint64_t et = MonotonicClock(); - globals->extract_data_time = (et - st) / 1000000LL; + uint64_t et = GetMillisecondsMonotonic(); + globals->extract_data_time = et - st; // Now rename the completed installation to its final name. If this // fails due to an ENOTEMPTY then we assume another good @@ -1239,7 +1239,7 @@ } // Wall clock time since process startup. - globals->startup_time = ProcessClock() / 1000000LL; + globals->startup_time = GetMillisecondsSinceProcessStart(); // Unblock all signals. sigset_t sigset;
diff --git a/src/main/cpp/blaze_util.cc b/src/main/cpp/blaze_util.cc index 7990286..b93c699 100644 --- a/src/main/cpp/blaze_util.cc +++ b/src/main/cpp/blaze_util.cc
@@ -401,7 +401,7 @@ fflush(stderr); // Take a clock sample for that start of the waiting time - uint64_t st = MonotonicClock(); + uint64_t st = GetMillisecondsMonotonic(); // Try to take the lock again (blocking). int r; do { @@ -413,8 +413,8 @@ "couldn't acquire file lock"); } // Take another clock sample, calculate elapsed - uint64_t et = MonotonicClock(); - wait_time = (et - st) / 1000000LL; + uint64_t et = GetMillisecondsMonotonic(); + wait_time = et - st; } // Identify ourselves in the lockfile.
diff --git a/src/main/cpp/blaze_util_darwin.cc b/src/main/cpp/blaze_util_darwin.cc index 93645b1..c4a35c0 100644 --- a/src/main/cpp/blaze_util_darwin.cc +++ b/src/main/cpp/blaze_util_darwin.cc
@@ -123,16 +123,16 @@ return string(pathbuf, len); } -uint64_t MonotonicClock() { +uint64_t GetMillisecondsMonotonic() { struct timeval ts = {}; if (gettimeofday(&ts, NULL) < 0) { pdie(blaze_exit_code::INTERNAL_ERROR, "error calling gettimeofday"); } - return ts.tv_sec * 1000000000LL + ts.tv_usec * 1000; + return ts.tv_sec * 1000LL + ts.tv_usec / 1000LL; } -uint64_t ProcessClock() { - return clock() * (1000000000LL / CLOCKS_PER_SEC); +uint64_t GetMillisecondsSinceProcessStart() { + return (clock() * 1000LL) / CLOCKS_PER_SEC; } void SetScheduling(bool batch_cpu_scheduling, int io_nice_level) {
diff --git a/src/main/cpp/blaze_util_freebsd.cc b/src/main/cpp/blaze_util_freebsd.cc index d7b7354..c4ac11a 100644 --- a/src/main/cpp/blaze_util_freebsd.cc +++ b/src/main/cpp/blaze_util_freebsd.cc
@@ -86,16 +86,16 @@ return string(buffer); } -uint64_t MonotonicClock() { +uint64_t GetMillisecondsMonotonic() { struct timespec ts = {}; clock_gettime(CLOCK_MONOTONIC, &ts); - return ts.tv_sec * 1000000000LL + ts.tv_nsec; + return ts.tv_sec * 1000LL + (ts.tv_nsec / 1000000LL); } -uint64_t ProcessClock() { +uint64_t GetMillisecondsSinceProcessStart() { struct timespec ts = {}; clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts); - return ts.tv_sec * 1000000000LL + ts.tv_nsec; + return ts.tv_sec * 1000LL + (ts.tv_nsec / 1000000LL); } void SetScheduling(bool batch_cpu_scheduling, int io_nice_level) {
diff --git a/src/main/cpp/blaze_util_linux.cc b/src/main/cpp/blaze_util_linux.cc index f53e193..c270821 100644 --- a/src/main/cpp/blaze_util_linux.cc +++ b/src/main/cpp/blaze_util_linux.cc
@@ -95,16 +95,16 @@ return string(buffer); } -uint64_t MonotonicClock() { +uint64_t GetMillisecondsMonotonic() { struct timespec ts = {}; clock_gettime(CLOCK_MONOTONIC, &ts); - return ts.tv_sec * 1000000000LL + ts.tv_nsec; + return ts.tv_sec * 1000LL + (ts.tv_nsec / 1000000LL); } -uint64_t ProcessClock() { +uint64_t GetMillisecondsSinceProcessStart() { struct timespec ts = {}; clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts); - return ts.tv_sec * 1000000000LL + ts.tv_nsec; + return ts.tv_sec * 1000LL + (ts.tv_nsec / 1000000LL); } void SetScheduling(bool batch_cpu_scheduling, int io_nice_level) {
diff --git a/src/main/cpp/blaze_util_mingw.cc b/src/main/cpp/blaze_util_mingw.cc index 9136c61..c94ff89 100644 --- a/src/main/cpp/blaze_util_mingw.cc +++ b/src/main/cpp/blaze_util_mingw.cc
@@ -83,16 +83,16 @@ } } -uint64_t MonotonicClock() { +uint64_t GetMillisecondsMonotonic() { struct timespec ts = {}; clock_gettime(CLOCK_MONOTONIC, &ts); - return ts.tv_sec * 1000000000LL + ts.tv_nsec; + return ts.tv_sec * 1000LL + (ts.tv_nsec / 1000000LL); } -uint64_t ProcessClock() { +uint64_t GetMillisecondsSinceProcessStart() { struct timespec ts = {}; clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts); - return ts.tv_sec * 1000000000LL + ts.tv_nsec; + return ts.tv_sec * 1000LL + (ts.tv_nsec / 1000000LL); } void SetScheduling(bool batch_cpu_scheduling, int io_nice_level) {
diff --git a/src/main/cpp/blaze_util_msvc.cc b/src/main/cpp/blaze_util_msvc.cc index 9136c61..c94ff89 100644 --- a/src/main/cpp/blaze_util_msvc.cc +++ b/src/main/cpp/blaze_util_msvc.cc
@@ -83,16 +83,16 @@ } } -uint64_t MonotonicClock() { +uint64_t GetMillisecondsMonotonic() { struct timespec ts = {}; clock_gettime(CLOCK_MONOTONIC, &ts); - return ts.tv_sec * 1000000000LL + ts.tv_nsec; + return ts.tv_sec * 1000LL + (ts.tv_nsec / 1000000LL); } -uint64_t ProcessClock() { +uint64_t GetMillisecondsSinceProcessStart() { struct timespec ts = {}; clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts); - return ts.tv_sec * 1000000000LL + ts.tv_nsec; + return ts.tv_sec * 1000LL + (ts.tv_nsec / 1000000LL); } void SetScheduling(bool batch_cpu_scheduling, int io_nice_level) {
diff --git a/src/main/cpp/blaze_util_platform.h b/src/main/cpp/blaze_util_platform.h index 0c47920..5892b0c 100644 --- a/src/main/cpp/blaze_util_platform.h +++ b/src/main/cpp/blaze_util_platform.h
@@ -30,13 +30,13 @@ // Warn about dubious filesystem types, such as NFS, case-insensitive (?). void WarnFilesystemType(const std::string& output_base); -// Wrapper around clock_gettime(CLOCK_MONOTONIC) that returns the time -// as a uint64_t nanoseconds since epoch. -uint64_t MonotonicClock(); +// Returns elapsed milliseconds since some unspecified start of time. +// The results are monotonic, i.e. subsequent calls to this method never return +// a value less than a previous result. +uint64_t GetMillisecondsMonotonic(); -// Wrapper around clock_gettime(CLOCK_PROCESS_CPUTIME_ID) that returns the -// nanoseconds consumed by the current process since it started. -uint64_t ProcessClock(); +// Returns elapsed milliseconds since the process started. +uint64_t GetMillisecondsSinceProcessStart(); // Set cpu and IO scheduling properties. Note that this can take ~50ms // on Linux, so it should only be called when necessary.