blob: fa5a63fa390a713e08867cbecd2c24f5191dba5e [file] [log] [blame]
Jens Elofssona9817a12021-05-18 11:30:35 +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#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
Jens Elofssonfdbf0ec2021-05-26 19:50:20 +020035 EthosUMonitor(std::vector<int32_t> eventRecordIds, 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
53 ETHOSU_PMU_EVCNTR_ALL_Reset(drv);
54 }
55
56 void release(ethosu_driver *drv);
57
58 void monitorSample(ethosu_driver *drv);
59
60private:
61 ethosu_pmu_event_type ethosuEventIds[ETHOSU_PMU_NCOUNTERS];
62 size_t numEvents;
Jens Elofssona9817a12021-05-18 11:30:35 +020063 Backend backend;
Jens Elofssonfdbf0ec2021-05-26 19:50:20 +020064 std::vector<int32_t> eventRecordIds;
Jens Elofssona9817a12021-05-18 11:30:35 +020065};
66
67#endif