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 #include <string> 22 #include <utility> 23 #include <vector> 24 25 #include "tensorflow/lite/core/api/profiler.h" 26 #include "tensorflow/lite/profiling/memory_info.h" 27 #include "tensorflow/lite/profiling/time.h" 28 29 namespace tflite { 30 namespace profiling { 31 32 constexpr uint32_t kInvalidEventHandle = static_cast<uint32_t>(~0) - 1; 33 34 // A profiling event. 35 struct ProfileEvent { 36 // Describes the type of event. 37 // The event_metadata field may contain additional data for interpreting 38 // the event. 39 using EventType = tflite::Profiler::EventType; 40 41 // Label of the event. This usually describes the event. 42 std::string tag; 43 // Timestamp in microseconds when the event began. 44 uint64_t begin_timestamp_us; 45 // Event processing time in microseconds. 46 uint64_t elapsed_time; 47 48 // The memory usage when the event begins. 49 memory::MemoryUsage begin_mem_usage; 50 // The memory usage when the event ends. 51 memory::MemoryUsage end_mem_usage; 52 53 // The field containing the type of event. This must be one of the event types 54 // in EventType. 55 EventType event_type; 56 // Meta data associated w/ the event. 57 int64_t event_metadata; 58 // Note: if this is an OPERATOR_INVOKE_EVENT, 'extra_event_metadata' will 59 // represent the index of the subgraph that this event comes from. 60 int64_t extra_event_metadata; 61 }; 62 63 // A buffer of profile events. In general, the buffer works like a ring buffer. 64 // However, when 'allow_dynamic_expansion' is set, a unlimitted number of buffer 65 // entries is allowed and more profiling overhead could occur. 66 // This class is *not thread safe*. 67 class ProfileBuffer { 68 public: 69 ProfileBuffer(uint32_t max_num_entries, bool enabled, 70 bool allow_dynamic_expansion = false) enabled_(enabled)71 : enabled_(enabled), 72 current_index_(0), 73 event_buffer_(max_num_entries), 74 allow_dynamic_expansion_(allow_dynamic_expansion) {} 75 76 // Adds an event to the buffer with begin timestamp set to the current 77 // timestamp. Returns a handle to event that can be used to call EndEvent. If 78 // buffer is disabled this has no affect. 79 // The tag of the event should remain valid till the buffer is valid. BeginEvent(const char * tag,ProfileEvent::EventType event_type,int64_t event_metadata1,int64_t event_metadata2)80 uint32_t BeginEvent(const char* tag, ProfileEvent::EventType event_type, 81 int64_t event_metadata1, int64_t event_metadata2) { 82 if (!enabled_) { 83 return kInvalidEventHandle; 84 } 85 uint64_t timestamp = time::NowMicros(); 86 const auto next_index = GetNextEntryIndex(); 87 if (next_index.second) { 88 return next_index.first; 89 } 90 const int index = next_index.first; 91 event_buffer_[index].tag = tag; 92 event_buffer_[index].event_type = event_type; 93 event_buffer_[index].event_metadata = event_metadata1; 94 event_buffer_[index].extra_event_metadata = event_metadata2; 95 event_buffer_[index].begin_timestamp_us = timestamp; 96 event_buffer_[index].elapsed_time = 0; 97 if (event_type != Profiler::EventType::OPERATOR_INVOKE_EVENT) { 98 event_buffer_[index].begin_mem_usage = memory::GetMemoryUsage(); 99 } 100 current_index_++; 101 return index; 102 } 103 104 // Sets the enabled state of buffer to |enabled| SetEnabled(bool enabled)105 void SetEnabled(bool enabled) { enabled_ = enabled; } 106 107 // Sets the end timestamp for event for the handle to current time. 108 // If the buffer is disabled or previous event has been overwritten this 109 // operation has not effect. 110 void EndEvent(uint32_t event_handle, const int64_t* event_metadata1 = nullptr, 111 const int64_t* event_metadata2 = nullptr) { 112 if (!enabled_ || event_handle == kInvalidEventHandle || 113 event_handle > current_index_) { 114 return; 115 } 116 const uint32_t max_size = event_buffer_.size(); 117 if (current_index_ > (max_size + event_handle)) { 118 // Ignore, buffer has already overflowed. 119 fprintf(stderr, "Warning: Dropping ProfileBuffer event.\n"); 120 return; 121 } 122 123 int event_index = event_handle % max_size; 124 event_buffer_[event_index].elapsed_time = 125 time::NowMicros() - event_buffer_[event_index].begin_timestamp_us; 126 if (event_buffer_[event_index].event_type != 127 Profiler::EventType::OPERATOR_INVOKE_EVENT) { 128 event_buffer_[event_index].end_mem_usage = memory::GetMemoryUsage(); 129 } 130 if (event_metadata1) { 131 event_buffer_[event_index].event_metadata = *event_metadata1; 132 } 133 if (event_metadata2) { 134 event_buffer_[event_index].extra_event_metadata = *event_metadata2; 135 } 136 } 137 AddEvent(const char * tag,ProfileEvent::EventType event_type,uint64_t elapsed_time,int64_t event_metadata1,int64_t event_metadata2)138 void AddEvent(const char* tag, ProfileEvent::EventType event_type, 139 uint64_t elapsed_time, int64_t event_metadata1, 140 int64_t event_metadata2) { 141 if (!enabled_) { 142 return; 143 } 144 const auto next_index = GetNextEntryIndex(); 145 if (next_index.second) { 146 return; 147 } 148 const int index = next_index.first; 149 event_buffer_[index].tag = tag; 150 event_buffer_[index].event_type = event_type; 151 event_buffer_[index].event_metadata = event_metadata1; 152 event_buffer_[index].extra_event_metadata = event_metadata2; 153 event_buffer_[index].begin_timestamp_us = 0; 154 event_buffer_[index].elapsed_time = elapsed_time; 155 current_index_++; 156 } 157 158 // Returns the size of the buffer. Size()159 size_t Size() const { 160 return (current_index_ >= event_buffer_.size()) ? event_buffer_.size() 161 : current_index_; 162 } 163 164 // Resets the buffer. Reset()165 void Reset() { 166 enabled_ = false; 167 current_index_ = 0; 168 } 169 170 // Returns the profile event at the given index. If the index is invalid a 171 // nullptr is returned. The return event may get overwritten if more events 172 // are added to buffer. At(size_t index)173 const struct ProfileEvent* At(size_t index) const { 174 size_t size = Size(); 175 if (index >= size) { 176 return nullptr; 177 } 178 const uint32_t max_size = event_buffer_.size(); 179 uint32_t start = 180 (current_index_ > max_size) ? current_index_ % max_size : max_size; 181 index = (index + start) % max_size; 182 return &event_buffer_[index]; 183 } 184 185 private: 186 // Returns a pair of values. The 1st element refers to the next buffer id, 187 // the 2nd element refers to whether the buffer reaches its allowed capacity. GetNextEntryIndex()188 std::pair<int, bool> GetNextEntryIndex() { 189 int index = current_index_ % event_buffer_.size(); 190 if (current_index_ == 0 || index != 0) { 191 return std::make_pair(index, false); 192 } 193 194 // Current buffer is full 195 if (!allow_dynamic_expansion_) { 196 fprintf(stderr, "Warning: Dropping ProfileBuffer event.\n"); 197 return std::make_pair(current_index_, true); 198 } else { 199 fprintf(stderr, "Warning: Doubling internal profiling buffer.\n"); 200 event_buffer_.resize(current_index_ * 2); 201 return std::make_pair(current_index_, false); 202 } 203 } 204 205 bool enabled_; 206 uint32_t current_index_; 207 std::vector<ProfileEvent> event_buffer_; 208 const bool allow_dynamic_expansion_; 209 }; 210 211 } // namespace profiling 212 } // namespace tflite 213 214 #endif // TENSORFLOW_LITE_PROFILING_PROFILE_BUFFER_H_ 215