• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2020 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 #include "tensorflow/core/profiler/utils/time_utils.h"
17 
18 #include "absl/time/clock.h"
19 #include "absl/time/time.h"
20 
21 namespace tensorflow {
22 namespace profiler {
23 
GetCurrentTimeNanos()24 int64 GetCurrentTimeNanos() {
25   // absl::GetCurrentTimeNanos() is much faster than EnvTime::NowNanos().
26   // It is wrapped under tensorflow::profiler::GetCurrentTimeNanos to avoid ODR
27   // violation and to allow switching to yet another implementation if required.
28   return absl::GetCurrentTimeNanos();
29 }
30 
SleepForNanos(int64 ns)31 void SleepForNanos(int64 ns) { absl::SleepFor(absl::Nanoseconds(ns)); }
32 
SpinForNanos(int64 ns)33 void SpinForNanos(int64 ns) {
34   if (ns <= 0) return;
35   int64 deadline = GetCurrentTimeNanos() + ns;
36   while (GetCurrentTimeNanos() < deadline) {
37   }
38 }
39 
40 }  // namespace profiler
41 }  // namespace tensorflow
42