1 /* Copyright 2019 The TensorFlow Authors. All Rights Reserved. 2 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9 Unless required by applicable law or agreed to in writing, software 10 distributed under the License is distributed on an "AS IS" BASIS, 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 See the License for the specific language governing permissions and 13 limitations under the License. 14 ==============================================================================*/ 15 16 #ifndef TENSORFLOW_CORE_PROFILER_UTILS_TIME_UTILS_H_ 17 #define TENSORFLOW_CORE_PROFILER_UTILS_TIME_UTILS_H_ 18 19 #include "tensorflow/core/platform/types.h" 20 21 namespace tensorflow { 22 namespace profiler { 23 24 // Converts among different time units. 25 // NOTE: We use uint64 for picoseconds and nanoseconds, which are used in 26 // storage, and double for other units that are used in the UI. PicosToNanos(uint64 ps)27inline double PicosToNanos(uint64 ps) { return ps / 1E3; } PicosToMicros(uint64 ps)28inline double PicosToMicros(uint64 ps) { return ps / 1E6; } PicosToMillis(uint64 ps)29inline double PicosToMillis(uint64 ps) { return ps / 1E9; } PicosToSeconds(uint64 ps)30inline double PicosToSeconds(uint64 ps) { return ps / 1E12; } NanosToPicos(uint64 ns)31inline uint64 NanosToPicos(uint64 ns) { return ns * 1000; } NanosToMicros(uint64 ns)32inline double NanosToMicros(uint64 ns) { return ns / 1E3; } MicrosToNanos(double us)33inline uint64 MicrosToNanos(double us) { return us * 1E3; } MicrosToMillis(double us)34inline double MicrosToMillis(double us) { return us / 1E3; } MillisToPicos(double ms)35inline uint64 MillisToPicos(double ms) { return ms * 1E9; } MillisToNanos(double ms)36inline uint64 MillisToNanos(double ms) { return ms * 1E6; } MillisToSeconds(double ms)37inline double MillisToSeconds(double ms) { return ms / 1E3; } SecondsToNanos(double s)38inline uint64 SecondsToNanos(double s) { return s * 1E9; } 39 40 // Returns the current CPU wallclock time in nanoseconds. 41 int64 GetCurrentTimeNanos(); 42 43 // Sleeps for the specified duration. 44 void SleepForNanos(int64 ns); SleepForMicros(int64 us)45inline void SleepForMicros(int64 us) { SleepForNanos(us * 1000); } SleepForMillis(int64 ms)46inline void SleepForMillis(int64 ms) { SleepForNanos(ms * 1000000); } SleepForSeconds(int64 s)47inline void SleepForSeconds(int64 s) { SleepForNanos(s * 1000000000); } 48 49 // Spins to simulate doing some work instead of sleeping, because sleep 50 // precision is poor. For testing only. 51 void SpinForNanos(int64 ns); SpinForMicros(int64 us)52inline void SpinForMicros(int64 us) { SpinForNanos(us * 1000); } 53 54 } // namespace profiler 55 } // namespace tensorflow 56 57 #endif // TENSORFLOW_CORE_PROFILER_UTILS_TIME_UTILS_H_ 58