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