diff options
author | Jiri Simsa <jsimsa@google.com> | 2018-07-31 13:04:40 -0700 |
---|---|---|
committer | TensorFlower Gardener <gardener@tensorflow.org> | 2018-07-31 13:08:58 -0700 |
commit | 20f56d1057aaacd6f2cc2601793689e00bc9561b (patch) | |
tree | 9bbd37b88532ec7f4a89ecc62149dcd2a7f2e82d | |
parent | c5fcae3d5c632069a57ef1374e628d25be90e563 (diff) |
Adding `NowNanos()` to `tensorflow::Env`.
PiperOrigin-RevId: 206812942
-rw-r--r-- | tensorflow/core/platform/cloud/gcs_throttle_test.cc | 8 | ||||
-rw-r--r-- | tensorflow/core/platform/env_time.h | 14 | ||||
-rw-r--r-- | tensorflow/core/platform/posix/env_time.cc | 9 | ||||
-rw-r--r-- | tensorflow/core/platform/windows/env_time.cc | 25 |
4 files changed, 35 insertions, 21 deletions
diff --git a/tensorflow/core/platform/cloud/gcs_throttle_test.cc b/tensorflow/core/platform/cloud/gcs_throttle_test.cc index 57193ac405..8f962b92b8 100644 --- a/tensorflow/core/platform/cloud/gcs_throttle_test.cc +++ b/tensorflow/core/platform/cloud/gcs_throttle_test.cc @@ -24,14 +24,14 @@ namespace { class TestTime : public EnvTime { public: - uint64 NowMicros() override { return now_; } + uint64 NowNanos() override { return now_micros_ * kMicrosToNanos; } - void SetTime(uint64 now_micros) { now_ = now_micros; } + void SetTime(uint64 now_micros) { now_micros_ = now_micros; } - void AdvanceSeconds(int64 secs) { now_ += secs * 1000000L; } + void AdvanceSeconds(int64 secs) { now_micros_ += secs * kSecondsToMicros; } private: - uint64 now_ = 1234567890000000ULL; + uint64 now_micros_ = 1234567890000000ULL; }; class GcsThrottleTest : public ::testing::Test { diff --git a/tensorflow/core/platform/env_time.h b/tensorflow/core/platform/env_time.h index 23dbedd60d..b4756ed209 100644 --- a/tensorflow/core/platform/env_time.h +++ b/tensorflow/core/platform/env_time.h @@ -25,6 +25,13 @@ namespace tensorflow { /// access timer related operations. class EnvTime { public: + static constexpr uint64 kMicrosToNanos = 1000ULL; + static constexpr uint64 kMillisToMicros = 1000ULL; + static constexpr uint64 kMillisToNanos = 1000ULL * 1000ULL; + static constexpr uint64 kSecondsToMillis = 1000ULL; + static constexpr uint64 kSecondsToMicros = 1000ULL * 1000ULL; + static constexpr uint64 kSecondsToNanos = 1000ULL * 1000ULL * 1000ULL; + EnvTime(); virtual ~EnvTime() = default; @@ -34,11 +41,14 @@ class EnvTime { /// The result of Default() belongs to this library and must never be deleted. static EnvTime* Default(); + /// \brief Returns the number of nano-seconds since the Unix epoch. + virtual uint64 NowNanos() = 0; + /// \brief Returns the number of micro-seconds since the Unix epoch. - virtual uint64 NowMicros() = 0; + virtual uint64 NowMicros() { return NowNanos() / kMicrosToNanos; } /// \brief Returns the number of seconds since the Unix epoch. - virtual uint64 NowSeconds() { return NowMicros() / 1000000L; } + virtual uint64 NowSeconds() { return NowNanos() / kSecondsToNanos; } }; } // namespace tensorflow diff --git a/tensorflow/core/platform/posix/env_time.cc b/tensorflow/core/platform/posix/env_time.cc index 341c585a9e..59a67b17aa 100644 --- a/tensorflow/core/platform/posix/env_time.cc +++ b/tensorflow/core/platform/posix/env_time.cc @@ -26,10 +26,11 @@ class PosixEnvTime : public EnvTime { public: PosixEnvTime() {} - uint64 NowMicros() override { - struct timeval tv; - gettimeofday(&tv, nullptr); - return static_cast<uint64>(tv.tv_sec) * 1000000 + tv.tv_usec; + uint64 NowNanos() override { + struct timespec ts; + clock_gettime(CLOCK_REALTIME, &ts); + return (static_cast<uint64>(ts.tv_sec) * kSecondsToNanos + + static_cast<uint64>(ts.tv_nsec)); } }; diff --git a/tensorflow/core/platform/windows/env_time.cc b/tensorflow/core/platform/windows/env_time.cc index 16cc9dc675..b1713f695c 100644 --- a/tensorflow/core/platform/windows/env_time.cc +++ b/tensorflow/core/platform/windows/env_time.cc @@ -19,6 +19,10 @@ limitations under the License. #include <windows.h> #include <chrono> +using std::chrono::duration_cast; +using std::chrono::nanoseconds; +using std::chrono::system_clock; + namespace tensorflow { namespace { @@ -38,18 +42,17 @@ class WindowsEnvTime : public EnvTime { } } - uint64 NowMicros() override { + uint64 NowNanos() { if (GetSystemTimePreciseAsFileTime_ != NULL) { // GetSystemTimePreciseAsFileTime function is only available in latest // versions of Windows, so we need to check for its existence here. - // All std::chrono clocks on Windows proved to return - // values that may repeat, which is not good enough for some uses. + // All std::chrono clocks on Windows proved to return values that may + // repeat, which is not good enough for some uses. constexpr int64_t kUnixEpochStartTicks = 116444736000000000i64; - constexpr int64_t kFtToMicroSec = 10; - // This interface needs to return system time and not - // just any microseconds because it is often used as an argument - // to TimedWait() on condition variable + // This interface needs to return system time and not just any time + // because it is often used as an argument to TimedWait() on condition + // variable. FILETIME system_time; GetSystemTimePreciseAsFileTime_(&system_time); @@ -58,12 +61,12 @@ class WindowsEnvTime : public EnvTime { li.HighPart = system_time.dwHighDateTime; // Subtract unix epoch start li.QuadPart -= kUnixEpochStartTicks; - // Convert to microsecs - li.QuadPart /= kFtToMicroSec; + + constexpr int64_t kFtToNanoSec = 100; + li.QuadPart *= kFtToNanoSec; return li.QuadPart; } - using namespace std::chrono; - return duration_cast<microseconds>(system_clock::now().time_since_epoch()) + return duration_cast<nanoseconds>(system_clock::now().time_since_epoch()) .count(); } |