blob: d09cdf00a45f19449023a5871777e2255f6a4bb4 [file] [log] [blame]
Jens Elofsson955288a2021-04-22 20:57:15 +02001/*
Mikael Olsson939d0b72024-05-15 10:08:26 +02002 * SPDX-FileCopyrightText: Copyright 2021-2022, 2024 Arm Limited and/or its affiliates <open-source-office@arm.com>
Jens Elofsson955288a2021-04-22 20:57:15 +02003 * SPDX-License-Identifier: Apache-2.0
4 *
5 * Licensed under the Apache License, Version 2.0 (the License); you may
6 * not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#include "tensorflow/lite/kernels/internal/compatibility.h"
Kristofer Jonsson1fed1d52022-11-21 13:39:45 +010019#include "tensorflow/lite/micro/micro_log.h"
Jens Elofsson955288a2021-04-22 20:57:15 +020020#include "tensorflow/lite/micro/micro_time.h"
21
22#include <string.h>
23
24#include "arm_profiler.hpp"
25#include <inttypes.h>
26#include <stdio.h>
27
28namespace tflite {
29
Kristofer Jonsson44d6e222021-05-21 18:59:18 +020030ArmProfiler::ArmProfiler(size_t max_events) : max_events_(max_events), num_events_(0) {
Jens Elofsson955288a2021-04-22 20:57:15 +020031 tags_ = std::make_unique<const char *[]>(max_events_);
Mikael Olsson939d0b72024-05-15 10:08:26 +020032 start_ticks_ = std::make_unique<uint32_t[]>(max_events_);
33 end_ticks_ = std::make_unique<uint32_t[]>(max_events_);
Jens Elofsson955288a2021-04-22 20:57:15 +020034}
35
36uint32_t ArmProfiler::BeginEvent(const char *tag) {
37 if (num_events_ == max_events_) {
Kristofer Jonsson1fed1d52022-11-21 13:39:45 +010038 MicroPrintf("Profiling event overflow, max: %u events", max_events_);
Jens Elofsson955288a2021-04-22 20:57:15 +020039 num_events_ = 0;
40 }
Kristofer Jonsson44d6e222021-05-21 18:59:18 +020041
Jens Elofsson955288a2021-04-22 20:57:15 +020042 tags_[num_events_] = tag;
43 start_ticks_[num_events_] = GetCurrentTimeTicks();
44 end_ticks_[num_events_] = start_ticks_[num_events_] - 1;
Kristofer Jonsson44d6e222021-05-21 18:59:18 +020045
Jens Elofsson955288a2021-04-22 20:57:15 +020046 return num_events_++;
47}
48
49void ArmProfiler::EndEvent(uint32_t event_handle) {
50 TFLITE_DCHECK(event_handle < max_events_);
51 end_ticks_[event_handle] = GetCurrentTimeTicks();
Jens Elofsson955288a2021-04-22 20:57:15 +020052}
53
Kristofer Jonssona78c7a82022-02-10 14:17:24 +010054uint64_t ArmProfiler::GetTotalTicks() const {
55 uint64_t ticks = 0;
Kristofer Jonsson44d6e222021-05-21 18:59:18 +020056
57 for (size_t i = 0; i < num_events_; ++i) {
Jens Elofsson955288a2021-04-22 20:57:15 +020058 ticks += end_ticks_[i] - start_ticks_[i];
59 }
Kristofer Jonsson44d6e222021-05-21 18:59:18 +020060
Jens Elofsson955288a2021-04-22 20:57:15 +020061 return ticks;
62}
63
Jonny Svärd2ebaac72022-05-10 17:29:30 +020064void ArmProfiler::ReportResults() const {
Kristofer Jonsson1fed1d52022-11-21 13:39:45 +010065 MicroPrintf("Profiler report, CPU cycles per operator:");
Jonny Svärd2ebaac72022-05-10 17:29:30 +020066 for (size_t i = 0; i < num_events_; ++i) {
Kristofer Jonsson1fed1d52022-11-21 13:39:45 +010067 MicroPrintf("%s : cycle_cnt : %u cycles", tags_[i], end_ticks_[i] - start_ticks_[i]);
Jonny Svärd2ebaac72022-05-10 17:29:30 +020068 }
69}
70
Jens Elofsson955288a2021-04-22 20:57:15 +020071} // namespace tflite