blob: bf4aae795ab84e616753c609d042c7ae43fbc31e [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_profiler.h"
22#include "tensorflow/lite/micro/micro_time.h"
23
24#include <string.h>
25
26#include "ethosu_profiler.hpp"
27#include <ethosu_driver.h>
28#include <inttypes.h>
29#include <stdio.h>
30
31namespace {
32
33uint64_t GetCurrentEthosuTicks(struct ethosu_driver *drv) {
Anton Moberg596a4662021-05-24 09:16:07 +020034 return ETHOSU_PMU_Get_CCNTR(drv);
Jens Elofsson955288a2021-04-22 20:57:15 +020035}
36
Jens Elofsson955288a2021-04-22 20:57:15 +020037} // namespace
38
39namespace tflite {
40
Jens Elofssonde044c32021-05-06 16:21:29 +020041EthosUProfiler::EthosUProfiler(size_t max_events) : max_events_(max_events) {
Jens Elofsson955288a2021-04-22 20:57:15 +020042 tags_ = std::make_unique<const char *[]>(max_events_);
43 start_ticks_ = std::make_unique<uint64_t[]>(max_events_);
44 end_ticks_ = std::make_unique<uint64_t[]>(max_events_);
45
Jens Elofssonde044c32021-05-06 16:21:29 +020046 struct ethosu_driver *drv = ethosu_reserve_driver();
Anton Moberg596a4662021-05-24 09:16:07 +020047 ETHOSU_PMU_CNTR_Enable(drv, ETHOSU_PMU_CCNT_Msk);
48 ETHOSU_PMU_CYCCNT_Reset(drv);
Jens Elofssonde044c32021-05-06 16:21:29 +020049 ethosu_release_driver(drv);
Jens Elofsson955288a2021-04-22 20:57:15 +020050}
51
52// NOTE: THIS PROFILER ONLY WORKS ON SYSTEMS WITH 1 NPU
53uint32_t EthosUProfiler::BeginEvent(const char *tag) {
54 if (num_events_ == max_events_) {
55 tflite::GetMicroErrorReporter()->Report("Profiling event overflow, max: %u events", max_events_);
56 num_events_ = 0;
57 }
58
59 tags_[num_events_] = tag;
60
61 if (strcmp("ethos-u", tag) == 0) {
62 struct ethosu_driver *ethosu_drv = ethosu_reserve_driver();
Anton Moberg596a4662021-05-24 09:16:07 +020063 ETHOSU_PMU_CYCCNT_Reset(ethosu_drv);
64 ETHOSU_PMU_PMCCNTR_CFG_Set_Start_Event(ethosu_drv, ETHOSU_PMU_NPU_ACTIVE);
65 ETHOSU_PMU_PMCCNTR_CFG_Set_Stop_Event(ethosu_drv, ETHOSU_PMU_NPU_IDLE);
Jens Elofsson955288a2021-04-22 20:57:15 +020066 start_ticks_[num_events_] = GetCurrentEthosuTicks(ethosu_drv);
Jens Elofsson955288a2021-04-22 20:57:15 +020067 ethosu_release_driver(ethosu_drv);
68 } else {
69 start_ticks_[num_events_] = GetCurrentTimeTicks();
70 }
71
72 end_ticks_[num_events_] = start_ticks_[num_events_] - 1;
73 return num_events_++;
74}
75
76// NOTE: THIS PROFILER ONLY WORKS ON SYSTEMS WITH 1 NPU
77void EthosUProfiler::EndEvent(uint32_t event_handle) {
78 TFLITE_DCHECK(event_handle < max_events_);
79
80 if (strcmp("ethos-u", tags_[event_handle]) == 0) {
81 struct ethosu_driver *ethosu_drv = ethosu_reserve_driver();
82 end_ticks_[event_handle] = GetCurrentEthosuTicks(ethosu_drv);
Jens Elofsson955288a2021-04-22 20:57:15 +020083 ethosu_release_driver(ethosu_drv);
Jens Elofsson955288a2021-04-22 20:57:15 +020084 } else {
85 end_ticks_[event_handle] = GetCurrentTimeTicks();
Jens Elofsson955288a2021-04-22 20:57:15 +020086 }
Jens Elofssonde044c32021-05-06 16:21:29 +020087
88 printf("%s : cycle_cnt : %" PRIu64 " cycles\n",
89 tags_[event_handle],
90 end_ticks_[event_handle] - start_ticks_[event_handle]);
Jens Elofsson955288a2021-04-22 20:57:15 +020091}
92
93uint64_t EthosUProfiler::GetTotalTicks() const {
94 uint64_t ticks = 0;
95 for (int i = 0; i < num_events_; ++i) {
96 ticks += end_ticks_[i] - start_ticks_[i];
97 }
98
99 return ticks;
100}
101
102void EthosUProfiler::Log() const {
103#if !defined(TF_LITE_STRIP_ERROR_STRINGS)
104 for (int i = 0; i < num_events_; ++i) {
105 uint64_t ticks = end_ticks_[i] - start_ticks_[i];
106 printf("%s took %" PRIu64 " cycles\n", tags_[i], ticks);
107 }
108#endif
109}
110
Jens Elofsson955288a2021-04-22 20:57:15 +0200111} // namespace tflite