blob: c58037fbd548f9eaba11f9877b4b2acc0da8fc71 [file] [log] [blame]
Jens Elofsson955288a2021-04-22 20:57:15 +02001/*
2 * Copyright (c) 2021 Arm Limited. All rights reserved.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Licensed under the Apache License, Version 2.0 (the License); you may
7 * not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
14 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
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#include <string.h>
24
25#include "arm_profiler.hpp"
26#include <inttypes.h>
27#include <stdio.h>
28
29namespace tflite {
30
31ArmProfiler::ArmProfiler(size_t max_events) : max_events_(max_events) {
32 tags_ = std::make_unique<const char *[]>(max_events_);
33 start_ticks_ = std::make_unique<int32_t[]>(max_events_);
34 end_ticks_ = std::make_unique<int32_t[]>(max_events_);
35}
36
37uint32_t ArmProfiler::BeginEvent(const char *tag) {
38 if (num_events_ == max_events_) {
39 tflite::GetMicroErrorReporter()->Report("Profiling event overflow, max: %u events", max_events_);
40 num_events_ = 0;
41 }
42 tags_[num_events_] = tag;
43 start_ticks_[num_events_] = GetCurrentTimeTicks();
44 end_ticks_[num_events_] = start_ticks_[num_events_] - 1;
45 return num_events_++;
46}
47
48void ArmProfiler::EndEvent(uint32_t event_handle) {
49 TFLITE_DCHECK(event_handle < max_events_);
50 end_ticks_[event_handle] = GetCurrentTimeTicks();
51 tflite::GetMicroErrorReporter()->Report(
52 "%s : cycle_cnt : %u cycles", tags_[event_handle], end_ticks_[event_handle] - start_ticks_[event_handle]);
53}
54
55int32_t ArmProfiler::GetTotalTicks() const {
56 int32_t ticks = 0;
57 for (int i = 0; i < num_events_; ++i) {
58 ticks += end_ticks_[i] - start_ticks_[i];
59 }
60 return ticks;
61}
62
63} // namespace tflite