blob: 2bdeb4a62167d70da1db2a25d364818712a1b43a [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 Jonsson36426952022-12-05 14:50:45 +010035 /**
36 * @param backend Select which backend to output performance data to.
37 * @param merge Merge performance samples if QREAD or STATUS has not changed
38 */
39 EthosUMonitor(Backend backend = PRINTF, bool merge = true);
Jens Elofssona9817a12021-05-18 11:30:35 +020040
41 template <typename T>
42 void configure(ethosu_driver *drv, const T &eventIds) {
Kristofer Jonsson36426952022-12-05 14:50:45 +010043 // Reset previous record
44 prevRecord.qread = -1;
45 prevRecord.status = -1;
46 mergeCount = 0;
47
Jens Elofssona9817a12021-05-18 11:30:35 +020048 // Set event ids
49 numEvents = std::min(static_cast<size_t>(ETHOSU_PMU_NCOUNTERS), eventIds.size());
50 for (size_t i = 0; i < numEvents; i++) {
51 ethosuEventIds[i] = eventIds[i];
52 }
53
54 // config pmu and set driver
55 ETHOSU_PMU_Enable(drv);
56
57 for (size_t i = 0; i < ETHOSU_PMU_NCOUNTERS; i++) {
58 ETHOSU_PMU_Set_EVTYPER(drv, i, static_cast<ethosu_pmu_event_type>(ethosuEventIds[i]));
59 ETHOSU_PMU_CNTR_Enable(drv, 1 << i);
60 }
61
Kristofer Jonsson265b7eb2022-09-29 12:01:28 +020062 ETHOSU_PMU_CNTR_Enable(drv, ETHOSU_PMU_CCNT_Msk);
63
Jens Elofssona9817a12021-05-18 11:30:35 +020064 ETHOSU_PMU_EVCNTR_ALL_Reset(drv);
Kristofer Jonsson265b7eb2022-09-29 12:01:28 +020065 ETHOSU_PMU_CYCCNT_Reset(drv);
Jens Elofssona9817a12021-05-18 11:30:35 +020066 }
67
68 void release(ethosu_driver *drv);
69
70 void monitorSample(ethosu_driver *drv);
71
Kristofer Jonsson36426952022-12-05 14:50:45 +010072 size_t getMergeCount() const;
73
Jens Elofssona9817a12021-05-18 11:30:35 +020074private:
Kristofer Jonsson265b7eb2022-09-29 12:01:28 +020075 struct EthosuEventRecord {
76 uint64_t cycleCount;
77 uint32_t qread;
78 uint32_t status;
79 struct {
80 uint32_t eventConfig;
81 uint32_t eventCount;
82 } event[ETHOSU_PMU_NCOUNTERS];
83 };
84
85 static constexpr int32_t EthosuEventComponentNo = 0x00;
86
Jens Elofssona9817a12021-05-18 11:30:35 +020087 ethosu_pmu_event_type ethosuEventIds[ETHOSU_PMU_NCOUNTERS];
88 size_t numEvents;
Kristofer Jonsson36426952022-12-05 14:50:45 +010089 const Backend backend;
90 const bool merge;
91 size_t mergeCount;
92 EthosuEventRecord prevRecord;
Jens Elofssona9817a12021-05-18 11:30:35 +020093};
94
95#endif