alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 1 | /* |
Isabella Gottardi | c64f506 | 2022-01-21 15:27:13 +0000 | [diff] [blame] | 2 | * Copyright (c) 2022 Arm Limited. All rights reserved. |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 3 | * SPDX-License-Identifier: Apache-2.0 |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | #include "Profiler.hpp" |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 18 | #include "log_macros.h" |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 19 | |
| 20 | #include <cstring> |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 21 | |
| 22 | namespace arm { |
| 23 | namespace app { |
Kshitij Sisodia | 4cc4021 | 2022-04-08 09:54:53 +0100 | [diff] [blame^] | 24 | Profiler::Profiler() |
| 25 | : Profiler("Unknown") |
| 26 | {} |
| 27 | |
| 28 | Profiler::Profiler(const char* name) |
| 29 | : m_name(name) |
| 30 | {} |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 31 | |
| 32 | bool Profiler::StartProfiling(const char* name) |
| 33 | { |
| 34 | if (name) { |
| 35 | this->SetName(name); |
| 36 | } |
Kshitij Sisodia | 4cc4021 | 2022-04-08 09:54:53 +0100 | [diff] [blame^] | 37 | if (!this->m_started) { |
| 38 | hal_pmu_reset(); |
| 39 | this->m_tstampSt.initialised = false; |
| 40 | hal_pmu_get_counters(&this->m_tstampSt); |
| 41 | if (this->m_tstampSt.initialised) { |
| 42 | this->m_started = true; |
| 43 | return true; |
| 44 | } |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 45 | } |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 46 | printf_err("Failed to start profiler %s\n", this->m_name.c_str()); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 47 | return false; |
| 48 | } |
| 49 | |
| 50 | bool Profiler::StopProfiling() |
| 51 | { |
Kshitij Sisodia | 4cc4021 | 2022-04-08 09:54:53 +0100 | [diff] [blame^] | 52 | if (this->m_started) { |
| 53 | this->m_tstampEnd.initialised = false; |
| 54 | hal_pmu_get_counters(&this->m_tstampEnd); |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 55 | this->m_started = false; |
Kshitij Sisodia | 4cc4021 | 2022-04-08 09:54:53 +0100 | [diff] [blame^] | 56 | if (this->m_tstampEnd.initialised) { |
| 57 | this->AddProfilingUnit( |
| 58 | this->m_tstampSt, |
| 59 | this->m_tstampEnd, |
| 60 | this->m_name); |
| 61 | return true; |
| 62 | } |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 63 | } |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 64 | printf_err("Failed to stop profiler %s\n", this->m_name.c_str()); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 65 | return false; |
| 66 | } |
| 67 | |
| 68 | bool Profiler::StopProfilingAndReset() |
| 69 | { |
| 70 | if (this->StopProfiling()) { |
| 71 | this->Reset(); |
| 72 | return true; |
| 73 | } |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 74 | printf_err("Failed to stop profiler %s\n", this->m_name.c_str()); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 75 | return false; |
| 76 | } |
| 77 | |
| 78 | void Profiler::Reset() |
| 79 | { |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 80 | this->m_started = false; |
| 81 | this->m_series.clear(); |
| 82 | memset(&this->m_tstampSt, 0, sizeof(this->m_tstampSt)); |
| 83 | memset(&this->m_tstampEnd, 0, sizeof(this->m_tstampEnd)); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Isabella Gottardi | 8df12f3 | 2021-04-07 17:15:31 +0100 | [diff] [blame] | 86 | void calcProfilingStat(uint64_t currentValue, |
| 87 | Statistics& data, |
| 88 | uint32_t samples) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 89 | { |
Isabella Gottardi | 8df12f3 | 2021-04-07 17:15:31 +0100 | [diff] [blame] | 90 | data.total += currentValue; |
| 91 | data.min = std::min(data.min, currentValue); |
| 92 | data.max = std::max(data.max, currentValue); |
| 93 | data.avrg = ((double)data.total / samples); |
| 94 | } |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 95 | |
Isabella Gottardi | 8df12f3 | 2021-04-07 17:15:31 +0100 | [diff] [blame] | 96 | void Profiler::GetAllResultsAndReset(std::vector<ProfileResult>& results) |
| 97 | { |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 98 | for (const auto& item: this->m_series) { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 99 | auto name = item.first; |
| 100 | ProfilingSeries series = item.second; |
Isabella Gottardi | 8df12f3 | 2021-04-07 17:15:31 +0100 | [diff] [blame] | 101 | ProfileResult result{}; |
| 102 | result.name = item.first; |
| 103 | result.samplesNum = series.size(); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 104 | |
Kshitij Sisodia | da2ec06 | 2022-04-01 14:43:53 +0100 | [diff] [blame] | 105 | std::vector<Statistics> stats(series[0].counters.num_counters); |
| 106 | for (size_t i = 0; i < stats.size(); ++i) { |
| 107 | stats[i].name = series[0].counters.counters[i].name; |
| 108 | stats[i].unit = series[0].counters.counters[i].unit; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 109 | } |
Kshitij Sisodia | da2ec06 | 2022-04-01 14:43:53 +0100 | [diff] [blame] | 110 | |
| 111 | for(ProfilingUnit& unit: series) { |
| 112 | for (size_t i = 0; i < stats.size(); ++i) { |
| 113 | calcProfilingStat( |
| 114 | unit.counters.counters[i].value, |
| 115 | stats[i], |
| 116 | result.samplesNum); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | for (Statistics& stat : stats) { |
| 121 | result.data.emplace_back(stat); |
| 122 | } |
| 123 | |
| 124 | results.emplace_back(result); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 125 | } |
Kshitij Sisodia | da2ec06 | 2022-04-01 14:43:53 +0100 | [diff] [blame] | 126 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 127 | this->Reset(); |
Isabella Gottardi | 8df12f3 | 2021-04-07 17:15:31 +0100 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | void printStatisticsHeader(uint32_t samplesNum) { |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 131 | info("Number of samples: %" PRIu32 "\n", samplesNum); |
Isabella Gottardi | 8df12f3 | 2021-04-07 17:15:31 +0100 | [diff] [blame] | 132 | info("%s\n", "Total / Avg./ Min / Max"); |
| 133 | } |
| 134 | |
| 135 | void Profiler::PrintProfilingResult(bool printFullStat) { |
| 136 | std::vector<ProfileResult> results{}; |
| 137 | GetAllResultsAndReset(results); |
| 138 | for(ProfileResult& result: results) { |
Kshitij Sisodia | da2ec06 | 2022-04-01 14:43:53 +0100 | [diff] [blame] | 139 | if (result.data.size()) { |
| 140 | info("Profile for %s:\n", result.name.c_str()); |
| 141 | if (printFullStat) { |
| 142 | printStatisticsHeader(result.samplesNum); |
| 143 | } |
Isabella Gottardi | 8df12f3 | 2021-04-07 17:15:31 +0100 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | for (Statistics &stat: result.data) { |
| 147 | if (printFullStat) { |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 148 | info("%s %s: %" PRIu64 "/ %.0f / %" PRIu64 " / %" PRIu64 " \n", |
| 149 | stat.name.c_str(), stat.unit.c_str(), |
Isabella Gottardi | 8df12f3 | 2021-04-07 17:15:31 +0100 | [diff] [blame] | 150 | stat.total, stat.avrg, stat.min, stat.max); |
| 151 | } else { |
Kshitij Sisodia | da2ec06 | 2022-04-01 14:43:53 +0100 | [diff] [blame] | 152 | info("%s: %.0f %s\n", stat.name.c_str(), stat.avrg, stat.unit.c_str()); |
Isabella Gottardi | 8df12f3 | 2021-04-07 17:15:31 +0100 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | } |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | void Profiler::SetName(const char* str) |
| 159 | { |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 160 | this->m_name = std::string(str); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 161 | } |
| 162 | |
Kshitij Sisodia | da2ec06 | 2022-04-01 14:43:53 +0100 | [diff] [blame] | 163 | void Profiler::AddProfilingUnit(pmu_counters start, pmu_counters end, |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 164 | const std::string& name) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 165 | { |
Kshitij Sisodia | da2ec06 | 2022-04-01 14:43:53 +0100 | [diff] [blame] | 166 | struct ProfilingUnit unit = { |
| 167 | .counters = end |
| 168 | }; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 169 | |
Kshitij Sisodia | da2ec06 | 2022-04-01 14:43:53 +0100 | [diff] [blame] | 170 | if (end.num_counters != start.num_counters || |
| 171 | true != end.initialised || true != start.initialised) { |
| 172 | printf_err("Invalid start or end counters\n"); |
| 173 | return; |
| 174 | } |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 175 | |
Kshitij Sisodia | da2ec06 | 2022-04-01 14:43:53 +0100 | [diff] [blame] | 176 | for (size_t i = 0; i < unit.counters.num_counters; ++i) { |
| 177 | if (unit.counters.counters[i].value < start.counters[i].value) { |
| 178 | warn("Overflow detected for %s\n", unit.counters.counters[i].name); |
| 179 | unit.counters.counters[i].value = 0; |
| 180 | } else { |
| 181 | unit.counters.counters[i].value -= start.counters[i].value; |
Isabella Gottardi | 8df12f3 | 2021-04-07 17:15:31 +0100 | [diff] [blame] | 182 | } |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 185 | this->m_series[name].emplace_back(unit); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | } /* namespace app */ |
Kshitij Sisodia | 14ab8d4 | 2021-10-22 17:35:01 +0100 | [diff] [blame] | 189 | } /* namespace arm */ |