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 { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 24 | Profiler::Profiler(hal_platform* platform, const char* name = "Unknown") |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 25 | : m_name(name) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 26 | { |
| 27 | if (platform && platform->inited) { |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 28 | this->m_pPlatform = platform; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 29 | this->Reset(); |
| 30 | } else { |
| 31 | printf_err("Profiler %s initialised with invalid platform\n", |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 32 | this->m_name.c_str()); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 33 | } |
| 34 | } |
| 35 | |
| 36 | bool Profiler::StartProfiling(const char* name) |
| 37 | { |
| 38 | if (name) { |
| 39 | this->SetName(name); |
| 40 | } |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 41 | if (this->m_pPlatform && !this->m_started) { |
| 42 | this->m_pPlatform->timer->reset(); |
Kshitij Sisodia | da2ec06 | 2022-04-01 14:43:53 +0100 | [diff] [blame^] | 43 | this->m_tstampSt = this->m_pPlatform->timer->get_counters(); |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 44 | this->m_started = true; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 45 | return true; |
| 46 | } |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 47 | printf_err("Failed to start profiler %s\n", this->m_name.c_str()); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 48 | return false; |
| 49 | } |
| 50 | |
| 51 | bool Profiler::StopProfiling() |
| 52 | { |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 53 | if (this->m_pPlatform && this->m_started) { |
Kshitij Sisodia | da2ec06 | 2022-04-01 14:43:53 +0100 | [diff] [blame^] | 54 | this->m_tstampEnd = this->m_pPlatform->timer->get_counters(); |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 55 | this->m_started = false; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 56 | |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 57 | this->AddProfilingUnit(this->m_tstampSt, this->m_tstampEnd, this->m_name); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 58 | |
| 59 | return true; |
| 60 | } |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 61 | printf_err("Failed to stop profiler %s\n", this->m_name.c_str()); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 62 | return false; |
| 63 | } |
| 64 | |
| 65 | bool Profiler::StopProfilingAndReset() |
| 66 | { |
| 67 | if (this->StopProfiling()) { |
| 68 | this->Reset(); |
| 69 | return true; |
| 70 | } |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 71 | printf_err("Failed to stop profiler %s\n", this->m_name.c_str()); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 72 | return false; |
| 73 | } |
| 74 | |
| 75 | void Profiler::Reset() |
| 76 | { |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 77 | this->m_started = false; |
| 78 | this->m_series.clear(); |
| 79 | memset(&this->m_tstampSt, 0, sizeof(this->m_tstampSt)); |
| 80 | memset(&this->m_tstampEnd, 0, sizeof(this->m_tstampEnd)); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Isabella Gottardi | 8df12f3 | 2021-04-07 17:15:31 +0100 | [diff] [blame] | 83 | void calcProfilingStat(uint64_t currentValue, |
| 84 | Statistics& data, |
| 85 | uint32_t samples) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 86 | { |
Isabella Gottardi | 8df12f3 | 2021-04-07 17:15:31 +0100 | [diff] [blame] | 87 | data.total += currentValue; |
| 88 | data.min = std::min(data.min, currentValue); |
| 89 | data.max = std::max(data.max, currentValue); |
| 90 | data.avrg = ((double)data.total / samples); |
| 91 | } |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 92 | |
Isabella Gottardi | 8df12f3 | 2021-04-07 17:15:31 +0100 | [diff] [blame] | 93 | void Profiler::GetAllResultsAndReset(std::vector<ProfileResult>& results) |
| 94 | { |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 95 | for (const auto& item: this->m_series) { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 96 | auto name = item.first; |
| 97 | ProfilingSeries series = item.second; |
Isabella Gottardi | 8df12f3 | 2021-04-07 17:15:31 +0100 | [diff] [blame] | 98 | ProfileResult result{}; |
| 99 | result.name = item.first; |
| 100 | result.samplesNum = series.size(); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 101 | |
Kshitij Sisodia | da2ec06 | 2022-04-01 14:43:53 +0100 | [diff] [blame^] | 102 | std::vector<Statistics> stats(series[0].counters.num_counters); |
| 103 | for (size_t i = 0; i < stats.size(); ++i) { |
| 104 | stats[i].name = series[0].counters.counters[i].name; |
| 105 | stats[i].unit = series[0].counters.counters[i].unit; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 106 | } |
Kshitij Sisodia | da2ec06 | 2022-04-01 14:43:53 +0100 | [diff] [blame^] | 107 | |
| 108 | for(ProfilingUnit& unit: series) { |
| 109 | for (size_t i = 0; i < stats.size(); ++i) { |
| 110 | calcProfilingStat( |
| 111 | unit.counters.counters[i].value, |
| 112 | stats[i], |
| 113 | result.samplesNum); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | for (Statistics& stat : stats) { |
| 118 | result.data.emplace_back(stat); |
| 119 | } |
| 120 | |
| 121 | results.emplace_back(result); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 122 | } |
Kshitij Sisodia | da2ec06 | 2022-04-01 14:43:53 +0100 | [diff] [blame^] | 123 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 124 | this->Reset(); |
Isabella Gottardi | 8df12f3 | 2021-04-07 17:15:31 +0100 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | void printStatisticsHeader(uint32_t samplesNum) { |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 128 | info("Number of samples: %" PRIu32 "\n", samplesNum); |
Isabella Gottardi | 8df12f3 | 2021-04-07 17:15:31 +0100 | [diff] [blame] | 129 | info("%s\n", "Total / Avg./ Min / Max"); |
| 130 | } |
| 131 | |
| 132 | void Profiler::PrintProfilingResult(bool printFullStat) { |
| 133 | std::vector<ProfileResult> results{}; |
| 134 | GetAllResultsAndReset(results); |
| 135 | for(ProfileResult& result: results) { |
Kshitij Sisodia | da2ec06 | 2022-04-01 14:43:53 +0100 | [diff] [blame^] | 136 | if (result.data.size()) { |
| 137 | info("Profile for %s:\n", result.name.c_str()); |
| 138 | if (printFullStat) { |
| 139 | printStatisticsHeader(result.samplesNum); |
| 140 | } |
Isabella Gottardi | 8df12f3 | 2021-04-07 17:15:31 +0100 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | for (Statistics &stat: result.data) { |
| 144 | if (printFullStat) { |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 145 | info("%s %s: %" PRIu64 "/ %.0f / %" PRIu64 " / %" PRIu64 " \n", |
| 146 | stat.name.c_str(), stat.unit.c_str(), |
Isabella Gottardi | 8df12f3 | 2021-04-07 17:15:31 +0100 | [diff] [blame] | 147 | stat.total, stat.avrg, stat.min, stat.max); |
| 148 | } else { |
Kshitij Sisodia | da2ec06 | 2022-04-01 14:43:53 +0100 | [diff] [blame^] | 149 | 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] | 150 | } |
| 151 | } |
| 152 | } |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | void Profiler::SetName(const char* str) |
| 156 | { |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 157 | this->m_name = std::string(str); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 158 | } |
| 159 | |
Kshitij Sisodia | da2ec06 | 2022-04-01 14:43:53 +0100 | [diff] [blame^] | 160 | void Profiler::AddProfilingUnit(pmu_counters start, pmu_counters end, |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 161 | const std::string& name) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 162 | { |
Kshitij Sisodia | 14ab8d4 | 2021-10-22 17:35:01 +0100 | [diff] [blame] | 163 | if (!this->m_pPlatform) { |
| 164 | printf_err("Invalid platform\n"); |
| 165 | return; |
| 166 | } |
| 167 | |
Kshitij Sisodia | da2ec06 | 2022-04-01 14:43:53 +0100 | [diff] [blame^] | 168 | struct ProfilingUnit unit = { |
| 169 | .counters = end |
| 170 | }; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 171 | |
Kshitij Sisodia | da2ec06 | 2022-04-01 14:43:53 +0100 | [diff] [blame^] | 172 | if (end.num_counters != start.num_counters || |
| 173 | true != end.initialised || true != start.initialised) { |
| 174 | printf_err("Invalid start or end counters\n"); |
| 175 | return; |
| 176 | } |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 177 | |
Kshitij Sisodia | da2ec06 | 2022-04-01 14:43:53 +0100 | [diff] [blame^] | 178 | for (size_t i = 0; i < unit.counters.num_counters; ++i) { |
| 179 | if (unit.counters.counters[i].value < start.counters[i].value) { |
| 180 | warn("Overflow detected for %s\n", unit.counters.counters[i].name); |
| 181 | unit.counters.counters[i].value = 0; |
| 182 | } else { |
| 183 | unit.counters.counters[i].value -= start.counters[i].value; |
Isabella Gottardi | 8df12f3 | 2021-04-07 17:15:31 +0100 | [diff] [blame] | 184 | } |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 187 | this->m_series[name].emplace_back(unit); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | } /* namespace app */ |
Kshitij Sisodia | 14ab8d4 | 2021-10-22 17:35:01 +0100 | [diff] [blame] | 191 | } /* namespace arm */ |