1/* Copyright 2015 The TensorFlow Authors. All Rights Reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations under the License.
14==============================================================================*/
15#ifndef TENSORFLOW_TSL_PLATFORM_ENV_TIME_H_
16#define TENSORFLOW_TSL_PLATFORM_ENV_TIME_H_
17
18#include <stdint.h>
19
20#include "tensorflow/tsl/platform/types.h"
21
22namespace tsl {
23
24/// \brief An interface used by the tsl implementation to
25/// access timer related operations.
26class EnvTime {
27 public:
28 static constexpr uint64 kMicrosToPicos = 1000ULL * 1000ULL;
29 static constexpr uint64 kMicrosToNanos = 1000ULL;
30 static constexpr uint64 kMillisToMicros = 1000ULL;
31 static constexpr uint64 kMillisToNanos = 1000ULL * 1000ULL;
32 static constexpr uint64 kNanosToPicos = 1000ULL;
33 static constexpr uint64 kSecondsToMillis = 1000ULL;
34 static constexpr uint64 kSecondsToMicros = 1000ULL * 1000ULL;
35 static constexpr uint64 kSecondsToNanos = 1000ULL * 1000ULL * 1000ULL;
36
37 EnvTime() = default;
38 virtual ~EnvTime() = default;
39
40 /// \brief Returns the number of nano-seconds since the Unix epoch.
41 static uint64 NowNanos();
42
43 /// \brief Returns the number of micro-seconds since the Unix epoch.
44 static uint64 NowMicros() { return NowNanos() / kMicrosToNanos; }
45
46 /// \brief Returns the number of seconds since the Unix epoch.
47 static uint64 NowSeconds() { return NowNanos() / kSecondsToNanos; }
48
49 /// \brief A version of NowNanos() that may be overridden by a subclass.
50 virtual uint64 GetOverridableNowNanos() const { return NowNanos(); }
51
52 /// \brief A version of NowMicros() that may be overridden by a subclass.
53 virtual uint64 GetOverridableNowMicros() const {
54 return GetOverridableNowNanos() / kMicrosToNanos;
55 }
56
57 /// \brief A version of NowSeconds() that may be overridden by a subclass.
58 virtual uint64 GetOverridableNowSeconds() const {
59 return GetOverridableNowNanos() / kSecondsToNanos;
60 }
61};
62
63} // namespace tsl
64
65#endif // TENSORFLOW_TSL_PLATFORM_ENV_TIME_H_
66