blob: c76ee8b85c363963bfbd0966bc7b8ad9c036358b [file] [log] [blame]
Jens Elofssona9817a12021-05-18 11:30:35 +02001/*
Kristofer Jonsson265b7eb2022-09-29 12:01:28 +02002 * SPDX-FileCopyrightText: Copyright 2021-2022 Arm Limited and/or its affiliates <open-source-office@arm.com>
Jens Elofssona9817a12021-05-18 11:30:35 +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#ifndef ETHOSU_MONITOR_H
20#define ETHOSU_MONITOR_H
21
22#include "EventRecorder.h"
23#include "EventRecorderConf.h"
24#include <algorithm>
25#include <ethosu_driver.h>
26#include <pmu_ethosu.h>
27#include <stdint.h>
Jens Elofssonfdbf0ec2021-05-26 19:50:20 +020028#include <vector>
Jens Elofssona9817a12021-05-18 11:30:35 +020029
30class EthosUMonitor {
31
32public:
33 enum Backend { PRINTF, EVENT_RECORDER };
34
Kristofer Jonsson265b7eb2022-09-29 12:01:28 +020035 EthosUMonitor(Backend backend = PRINTF);
Jens Elofssona9817a12021-05-18 11:30:35 +020036
37 template <typename T>
38 void configure(ethosu_driver *drv, const T &eventIds) {
39 // Set event ids
40 numEvents = std::min(static_cast<size_t>(ETHOSU_PMU_NCOUNTERS), eventIds.size());
41 for (size_t i = 0; i < numEvents; i++) {
42 ethosuEventIds[i] = eventIds[i];
43 }
44
45 // config pmu and set driver
46 ETHOSU_PMU_Enable(drv);
47
48 for (size_t i = 0; i < ETHOSU_PMU_NCOUNTERS; i++) {
49 ETHOSU_PMU_Set_EVTYPER(drv, i, static_cast<ethosu_pmu_event_type>(ethosuEventIds[i]));
50 ETHOSU_PMU_CNTR_Enable(drv, 1 << i);
51 }
52
Kristofer Jonsson265b7eb2022-09-29 12:01:28 +020053 ETHOSU_PMU_CNTR_Enable(drv, ETHOSU_PMU_CCNT_Msk);
54
Jens Elofssona9817a12021-05-18 11:30:35 +020055 ETHOSU_PMU_EVCNTR_ALL_Reset(drv);
Kristofer Jonsson265b7eb2022-09-29 12:01:28 +020056 ETHOSU_PMU_CYCCNT_Reset(drv);
Jens Elofssona9817a12021-05-18 11:30:35 +020057 }
58
59 void release(ethosu_driver *drv);
60
61 void monitorSample(ethosu_driver *drv);
62
63private:
Kristofer Jonsson265b7eb2022-09-29 12:01:28 +020064 struct EthosuEventRecord {
65 uint64_t cycleCount;
66 uint32_t qread;
67 uint32_t status;
68 struct {
69 uint32_t eventConfig;
70 uint32_t eventCount;
71 } event[ETHOSU_PMU_NCOUNTERS];
72 };
73
74 static constexpr int32_t EthosuEventComponentNo = 0x00;
75
Jens Elofssona9817a12021-05-18 11:30:35 +020076 ethosu_pmu_event_type ethosuEventIds[ETHOSU_PMU_NCOUNTERS];
77 size_t numEvents;
Jens Elofsson036d1a82021-05-27 15:05:22 +020078 Backend backend;
Jens Elofssona9817a12021-05-18 11:30:35 +020079};
80
81#endif