Jens Elofsson | a9817a1 | 2021-05-18 11:30:35 +0200 | [diff] [blame] | 1 | /* |
Kristofer Jonsson | 265b7eb | 2022-09-29 12:01:28 +0200 | [diff] [blame^] | 2 | * SPDX-FileCopyrightText: Copyright 2021-2022 Arm Limited and/or its affiliates <open-source-office@arm.com> |
Jens Elofsson | a9817a1 | 2021-05-18 11:30:35 +0200 | [diff] [blame] | 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 Elofsson | fdbf0ec | 2021-05-26 19:50:20 +0200 | [diff] [blame] | 28 | #include <vector> |
Jens Elofsson | a9817a1 | 2021-05-18 11:30:35 +0200 | [diff] [blame] | 29 | |
| 30 | class EthosUMonitor { |
| 31 | |
| 32 | public: |
| 33 | enum Backend { PRINTF, EVENT_RECORDER }; |
| 34 | |
Kristofer Jonsson | 265b7eb | 2022-09-29 12:01:28 +0200 | [diff] [blame^] | 35 | EthosUMonitor(Backend backend = PRINTF); |
Jens Elofsson | a9817a1 | 2021-05-18 11:30:35 +0200 | [diff] [blame] | 36 | |
| 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 Jonsson | 265b7eb | 2022-09-29 12:01:28 +0200 | [diff] [blame^] | 53 | ETHOSU_PMU_CNTR_Enable(drv, ETHOSU_PMU_CCNT_Msk); |
| 54 | |
Jens Elofsson | a9817a1 | 2021-05-18 11:30:35 +0200 | [diff] [blame] | 55 | ETHOSU_PMU_EVCNTR_ALL_Reset(drv); |
Kristofer Jonsson | 265b7eb | 2022-09-29 12:01:28 +0200 | [diff] [blame^] | 56 | ETHOSU_PMU_CYCCNT_Reset(drv); |
Jens Elofsson | a9817a1 | 2021-05-18 11:30:35 +0200 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | void release(ethosu_driver *drv); |
| 60 | |
| 61 | void monitorSample(ethosu_driver *drv); |
| 62 | |
| 63 | private: |
Kristofer Jonsson | 265b7eb | 2022-09-29 12:01:28 +0200 | [diff] [blame^] | 64 | 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 Elofsson | a9817a1 | 2021-05-18 11:30:35 +0200 | [diff] [blame] | 76 | ethosu_pmu_event_type ethosuEventIds[ETHOSU_PMU_NCOUNTERS]; |
| 77 | size_t numEvents; |
Jens Elofsson | 036d1a8 | 2021-05-27 15:05:22 +0200 | [diff] [blame] | 78 | Backend backend; |
Jens Elofsson | a9817a1 | 2021-05-18 11:30:35 +0200 | [diff] [blame] | 79 | }; |
| 80 | |
| 81 | #endif |