• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2018 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 #ifndef TENSORFLOW_LITE_PROFILING_PROFILE_BUFFER_H_
16 #define TENSORFLOW_LITE_PROFILING_PROFILE_BUFFER_H_
17 
18 #include <cstddef>
19 #include <cstdint>
20 #include <cstdio>
21 
22 #include "tensorflow/lite/profiling/time.h"
23 
24 namespace tflite {
25 namespace profiling {
26 
27 // A profiling event.
28 struct ProfileEvent {
29   // Describes the type of event.
30   // The event_metadata field may contain additional data for interpreting
31   // the event.
32   enum class EventType {
33     // Default event type, the metadata field has no special significance.
34     DEFAULT = 0,
35     // The event is an operator invocation and the event_metadata field is the
36     // index of operator node.
37     OPERATOR_INVOKE_EVENT = 1
38   };
39 
40   // Label of the event. This usually describes the event.
41   const char* tag;
42   // Timestamp in microseconds when the event began.
43   uint64_t begin_timestamp_us;
44   // Timestamp in microseconds when the event ended.
45   uint64_t end_timestamp_us;
46   // The field containing the type of event. This must be one of the event types
47   // in EventType.
48   EventType event_type;
49   // Extra data describing the details of the event.
50   uint32_t event_metadata;
51 };
52 }  // namespace profiling
53 }  // namespace tflite
54 
55 #ifdef TFLITE_PROFILING_ENABLED
56 
57 #include <sys/time.h>
58 #include <vector>
59 
60 namespace tflite {
61 namespace profiling {
62 constexpr uint32_t kInvalidEventHandle = static_cast<uint32_t>(~0) - 1;
63 
64 // A ring buffer of profile events.
65 // This class is not thread safe.
66 class ProfileBuffer {
67  public:
ProfileBuffer(uint32_t max_num_entries,bool enabled)68   ProfileBuffer(uint32_t max_num_entries, bool enabled)
69       : enabled_(enabled), current_index_(0), event_buffer_(max_num_entries) {}
70 
71   // Adds an event to the buffer with begin timestamp set to the current
72   // timestamp. Returns a handle to event that can be used to call EndEvent. If
73   // buffer is disabled this has no affect.
74   // The tag of the event should remain valid till the buffer is valid.
BeginEvent(const char * tag,ProfileEvent::EventType event_type,uint32_t event_metadata)75   uint32_t BeginEvent(const char* tag, ProfileEvent::EventType event_type,
76                       uint32_t event_metadata) {
77     if (!enabled_) {
78       return kInvalidEventHandle;
79     }
80     uint64_t timestamp = time::NowMicros();
81     int index = current_index_ % event_buffer_.size();
82     if (current_index_ != 0 && index == 0) {
83       fprintf(stderr, "Warning: ProfileBuffer wrapping.\n");
84     }
85     event_buffer_[index].tag = tag;
86     event_buffer_[index].event_type = event_type;
87     event_buffer_[index].event_metadata = event_metadata;
88     event_buffer_[index].begin_timestamp_us = timestamp;
89     event_buffer_[index].end_timestamp_us = 0;
90     current_index_++;
91     return index;
92   }
93 
94   // Sets the enabled state of buffer to |enabled|
SetEnabled(bool enabled)95   void SetEnabled(bool enabled) { enabled_ = enabled; }
96 
97   // Sets the end timestamp for event for the handle to current time.
98   // If the buffer is disabled or previous event has been overwritten this
99   // operation has not effect.
EndEvent(uint32_t event_handle)100   void EndEvent(uint32_t event_handle) {
101     if (!enabled_ || event_handle == kInvalidEventHandle ||
102         event_handle > current_index_) {
103       return;
104     }
105     const uint32_t max_size = event_buffer_.size();
106     if (current_index_ > (max_size + event_handle)) {
107       // Ignore, buffer has already overflowed.
108       fprintf(stderr, "Warning: Dropping ProfileBuffer event.\n");
109       return;
110     }
111 
112     int event_index = event_handle % max_size;
113     event_buffer_[event_index].end_timestamp_us = time::NowMicros();
114   }
115 
116   // Returns the size of the buffer.
Size()117   size_t Size() const {
118     return (current_index_ >= event_buffer_.size()) ? event_buffer_.size()
119                                                     : current_index_;
120   }
121 
122   // Resets the buffer.
Reset()123   void Reset() {
124     enabled_ = false;
125     current_index_ = 0;
126   }
127 
128   // Returns the profile event at the given index. If the index is invalid a
129   // nullptr is returned. The return event may get overwritten if more events
130   // are added to buffer.
At(size_t index)131   const struct ProfileEvent* const At(size_t index) const {
132     size_t size = Size();
133     if (index >= size) {
134       return nullptr;
135     }
136     const uint32_t max_size = event_buffer_.size();
137     uint32_t start =
138         (current_index_ > max_size) ? current_index_ % max_size : max_size;
139     index = (index + start) % max_size;
140     return &event_buffer_[index];
141   }
142 
143  private:
144   bool enabled_;
145   uint32_t current_index_;
146   std::vector<ProfileEvent> event_buffer_;
147 };
148 }  // namespace profiling
149 }  // namespace tflite
150 #endif  // TFLITE_PROFILING_ENABLED
151 #endif  // TENSORFLOW_LITE_PROFILING_PROFILE_BUFFER_H_
152