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 #include "tensorflow/lite/micro/micro_profiler.h" 16 17 #include <cstdint> 18 19 #include "tensorflow/lite/kernels/internal/compatibility.h" 20 #include "tensorflow/lite/micro/micro_error_reporter.h" 21 #include "tensorflow/lite/micro/micro_time.h" 22 23 namespace tflite { 24 BeginEvent(const char * tag)25uint32_t MicroProfiler::BeginEvent(const char* tag) { 26 if (num_events_ == kMaxEvents) { 27 num_events_ = 0; 28 } 29 30 tags_[num_events_] = tag; 31 start_ticks_[num_events_] = GetCurrentTimeTicks(); 32 end_ticks_[num_events_] = start_ticks_[num_events_] - 1; 33 return num_events_++; 34 } 35 EndEvent(uint32_t event_handle)36void MicroProfiler::EndEvent(uint32_t event_handle) { 37 TFLITE_DCHECK(event_handle < kMaxEvents); 38 end_ticks_[event_handle] = GetCurrentTimeTicks(); 39 } 40 GetTotalTicks() const41int32_t MicroProfiler::GetTotalTicks() const { 42 int32_t ticks = 0; 43 for (int i = 0; i < num_events_; ++i) { 44 ticks += end_ticks_[i] - start_ticks_[i]; 45 } 46 return ticks; 47 } 48 Log() const49void MicroProfiler::Log() const { 50 #if !defined(TF_LITE_STRIP_ERROR_STRINGS) 51 for (int i = 0; i < num_events_; ++i) { 52 int32_t ticks = end_ticks_[i] - start_ticks_[i]; 53 MicroPrintf("%s took %d ticks (%d ms).", tags_[i], ticks, TicksToMs(ticks)); 54 } 55 #endif 56 } 57 58 } // namespace tflite 59