1 /* Copyright 2017 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_PLATFORM_PROFILE_UTILS_CLOCK_CYCLE_PROFILER_H_
17 #define TENSORFLOW_PLATFORM_PROFILE_UTILS_CLOCK_CYCLE_PROFILER_H_
18 
19 #include <algorithm>
20 
21 #include "tensorflow/core/platform/logging.h"
22 #include "tensorflow/core/platform/macros.h"
23 #include "tensorflow/core/platform/profile_utils/cpu_utils.h"
24 
25 namespace tensorflow {
26 
27 class ClockCycleProfiler {
28  public:
29   ClockCycleProfiler() = default;
30 
31   // Start counting clock cycle.
Start()32   inline void Start() {
33     CHECK(!IsStarted()) << "Profiler has been already started.";
34     start_clock_ = GetCurrentClockCycleInternal();
35   }
36 
37   // Stop counting clock cycle.
Stop()38   inline void Stop() {
39     CHECK(IsStarted()) << "Profiler is not started yet.";
40     AccumulateClockCycle();
41   }
42 
43   // Get how many times Start() is called.
GetCount()44   inline double GetCount() {
45     CHECK(!IsStarted());
46     return count_;
47   }
48 
49   // Get average clock cycle.
GetAverageClockCycle()50   inline double GetAverageClockCycle() {
51     CHECK(!IsStarted());
52     return average_clock_cycle_;
53   }
54 
55   // TODO(satok): Support more statistics (e.g. standard deviation)
56   // Get worst clock cycle.
GetWorstClockCycle()57   inline double GetWorstClockCycle() {
58     CHECK(!IsStarted());
59     return worst_clock_cycle_;
60   }
61 
62   // Dump statistics
63   void DumpStatistics(const string& tag);
64 
65  private:
GetCurrentClockCycleInternal()66   inline uint64 GetCurrentClockCycleInternal() {
67     const uint64 clockCycle = profile_utils::CpuUtils::GetCurrentClockCycle();
68     if (clockCycle <= 0) {
69       if (valid_) {
70         LOG(WARNING) << "GetCurrentClockCycle is not implemented."
71                      << " Return 1 instead.";
72         valid_ = false;
73       }
74       return 1;
75     } else {
76       return clockCycle;
77     }
78   }
79 
IsStarted()80   inline bool IsStarted() const { return start_clock_ > 0; }
81 
AccumulateClockCycle()82   inline void AccumulateClockCycle() {
83     const uint64 now = GetCurrentClockCycleInternal();
84     const double clock_diff = static_cast<double>(now - start_clock_);
85     const double next_count = count_ + 1.0;
86     const double next_count_inv = 1.0 / next_count;
87     const double next_ave_cpu_clock =
88         next_count_inv * (average_clock_cycle_ * count_ + clock_diff);
89     count_ = next_count;
90     average_clock_cycle_ = next_ave_cpu_clock;
91     worst_clock_cycle_ = std::max(worst_clock_cycle_, clock_diff);
92     start_clock_ = 0;
93   }
94 
95   uint64 start_clock_{0};
96   double count_{0.0};
97   double average_clock_cycle_{0.0};
98   double worst_clock_cycle_{0.0};
99   bool valid_{true};
100 
101   TF_DISALLOW_COPY_AND_ASSIGN(ClockCycleProfiler);
102 };
103 
104 }  // namespace tensorflow
105 
106 #endif  // TENSORFLOW_PLATFORM_PROFILE_UTILS_CLOCK_CYCLE_PROFILER_H_
107