blob: 29774744fefcc3177c35499cf5c22a3ced6d72a0 [file] [log] [blame]
Jens Elofsson955288a2021-04-22 20:57:15 +02001/*
Kristofer Jonssona78c7a82022-02-10 14:17:24 +01002 * Copyright (c) 2021-2022 Arm Limited. All rights reserved.
Jens Elofsson955288a2021-04-22 20:57:15 +02003 *
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"
Kristofer Jonsson1fed1d52022-11-21 13:39:45 +010020#include "tensorflow/lite/micro/micro_log.h"
Jens Elofsson955288a2021-04-22 20:57:15 +020021#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
Kristofer Jonsson44d6e222021-05-21 18:59:18 +020031ArmProfiler::ArmProfiler(size_t max_events) : max_events_(max_events), num_events_(0) {
Jens Elofsson955288a2021-04-22 20:57:15 +020032 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_) {
Kristofer Jonsson1fed1d52022-11-21 13:39:45 +010039 MicroPrintf("Profiling event overflow, max: %u events", max_events_);
Jens Elofsson955288a2021-04-22 20:57:15 +020040 num_events_ = 0;
41 }
Kristofer Jonsson44d6e222021-05-21 18:59:18 +020042
Jens Elofsson955288a2021-04-22 20:57:15 +020043 tags_[num_events_] = tag;
44 start_ticks_[num_events_] = GetCurrentTimeTicks();
45 end_ticks_[num_events_] = start_ticks_[num_events_] - 1;
Kristofer Jonsson44d6e222021-05-21 18:59:18 +020046
Jens Elofsson955288a2021-04-22 20:57:15 +020047 return num_events_++;
48}
49
50void ArmProfiler::EndEvent(uint32_t event_handle) {
51 TFLITE_DCHECK(event_handle < max_events_);
52 end_ticks_[event_handle] = GetCurrentTimeTicks();
Jens Elofsson955288a2021-04-22 20:57:15 +020053}
54
Kristofer Jonssona78c7a82022-02-10 14:17:24 +010055uint64_t ArmProfiler::GetTotalTicks() const {
56 uint64_t ticks = 0;
Kristofer Jonsson44d6e222021-05-21 18:59:18 +020057
58 for (size_t i = 0; i < num_events_; ++i) {
Jens Elofsson955288a2021-04-22 20:57:15 +020059 ticks += end_ticks_[i] - start_ticks_[i];
60 }
Kristofer Jonsson44d6e222021-05-21 18:59:18 +020061
Jens Elofsson955288a2021-04-22 20:57:15 +020062 return ticks;
63}
64
Jonny Svärd2ebaac72022-05-10 17:29:30 +020065void ArmProfiler::ReportResults() const {
Kristofer Jonsson1fed1d52022-11-21 13:39:45 +010066 MicroPrintf("Profiler report, CPU cycles per operator:");
Jonny Svärd2ebaac72022-05-10 17:29:30 +020067 for (size_t i = 0; i < num_events_; ++i) {
Kristofer Jonsson1fed1d52022-11-21 13:39:45 +010068 MicroPrintf("%s : cycle_cnt : %u cycles", tags_[i], end_ticks_[i] - start_ticks_[i]);
Jonny Svärd2ebaac72022-05-10 17:29:30 +020069 }
70}
71
Jens Elofsson955288a2021-04-22 20:57:15 +020072} // namespace tflite