Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <inttypes.h> |
Yabin Cui | 621a533 | 2015-06-15 16:17:20 -0700 | [diff] [blame] | 18 | #include <signal.h> |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 19 | #include <stdio.h> |
Yabin Cui | 04d08a3 | 2015-08-19 15:01:12 -0700 | [diff] [blame] | 20 | #include <string.h> |
Yabin Cui | 987d9ab | 2016-07-15 14:08:48 -0700 | [diff] [blame] | 21 | #include <sys/prctl.h> |
Yabin Cui | 04d08a3 | 2015-08-19 15:01:12 -0700 | [diff] [blame] | 22 | |
| 23 | #include <algorithm> |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 24 | #include <chrono> |
Yabin Cui | f53f716 | 2020-06-19 15:16:31 -0700 | [diff] [blame] | 25 | #include <optional> |
Yabin Cui | b032de7 | 2015-06-17 21:15:09 -0700 | [diff] [blame] | 26 | #include <set> |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 27 | #include <string> |
Yabin Cui | 117caa4 | 2019-07-15 10:39:15 -0700 | [diff] [blame] | 28 | #include <string_view> |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 29 | #include <vector> |
| 30 | |
Yabin Cui | a80f8f7 | 2017-07-12 15:50:20 -0700 | [diff] [blame] | 31 | #include <android-base/file.h> |
Elliott Hughes | 66dd09e | 2015-12-04 14:00:57 -0800 | [diff] [blame] | 32 | #include <android-base/logging.h> |
| 33 | #include <android-base/strings.h> |
Yabin Cui | 1a30a58 | 2019-01-10 15:35:39 -0800 | [diff] [blame] | 34 | #include <android-base/unique_fd.h> |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 35 | |
Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 36 | #include "IOEventLoop.h" |
Yabin Cui | 61b4dac | 2023-11-03 15:19:14 -0700 | [diff] [blame] | 37 | #include "ProbeEvents.h" |
Yabin Cui | b3ed39b | 2020-03-26 14:00:49 -0700 | [diff] [blame] | 38 | #include "cmd_stat_impl.h" |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 39 | #include "command.h" |
| 40 | #include "environment.h" |
Yabin Cui | 9fd3cc1 | 2015-06-25 17:42:23 -0700 | [diff] [blame] | 41 | #include "event_attr.h" |
| 42 | #include "event_fd.h" |
Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 43 | #include "event_selection_set.h" |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 44 | #include "event_type.h" |
Yabin Cui | 621a533 | 2015-06-15 16:17:20 -0700 | [diff] [blame] | 45 | #include "utils.h" |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 46 | #include "workload.h" |
| 47 | |
Yabin Cui | 5271aa7 | 2020-03-31 16:59:33 -0700 | [diff] [blame] | 48 | namespace simpleperf { |
Yabin Cui | 877751b | 2016-06-13 18:03:47 -0700 | [diff] [blame] | 49 | |
Yabin Cui | faa7b92 | 2021-01-11 17:35:57 -0800 | [diff] [blame] | 50 | using android::base::Split; |
| 51 | |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 52 | static std::vector<std::string> default_measured_event_types{ |
Yabin Cui | f569b47 | 2015-04-30 09:43:26 -0700 | [diff] [blame] | 53 | "cpu-cycles", "stalled-cycles-frontend", "stalled-cycles-backend", |
| 54 | "instructions", "branch-instructions", "branch-misses", |
| 55 | "task-clock", "context-switches", "page-faults", |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 56 | }; |
| 57 | |
Yabin Cui | 117caa4 | 2019-07-15 10:39:15 -0700 | [diff] [blame] | 58 | static const std::unordered_map<std::string_view, std::pair<std::string_view, std::string_view>> |
| 59 | COMMON_EVENT_RATE_MAP = { |
| 60 | {"cache-misses", {"cache-references", "miss rate"}}, |
| 61 | {"branch-misses", {"branch-instructions", "miss rate"}}, |
| 62 | }; |
| 63 | |
| 64 | static const std::unordered_map<std::string_view, std::pair<std::string_view, std::string_view>> |
| 65 | ARM_EVENT_RATE_MAP = { |
| 66 | // Refer to "D6.10.5 Meaningful ratios between common microarchitectural events" in ARMv8 |
| 67 | // specification. |
| 68 | {"raw-l1i-cache-refill", {"raw-l1i-cache", "level 1 instruction cache refill rate"}}, |
| 69 | {"raw-l1i-tlb-refill", {"raw-l1i-tlb", "level 1 instruction TLB refill rate"}}, |
| 70 | {"raw-l1d-cache-refill", {"raw-l1d-cache", "level 1 data or unified cache refill rate"}}, |
| 71 | {"raw-l1d-tlb-refill", {"raw-l1d-tlb", "level 1 data or unified TLB refill rate"}}, |
| 72 | {"raw-l2d-cache-refill", {"raw-l2d-cache", "level 2 data or unified cache refill rate"}}, |
| 73 | {"raw-l2i-cache-refill", {"raw-l2i-cache", "level 2 instruction cache refill rate"}}, |
| 74 | {"raw-l3d-cache-refill", {"raw-l3d-cache", "level 3 data or unified cache refill rate"}}, |
| 75 | {"raw-l2d-tlb-refill", {"raw-l2d-tlb", "level 2 data or unified TLB refill rate"}}, |
| 76 | {"raw-l2i-tlb-refill", {"raw-l2i-tlb", "level 2 instruction TLB refill rate"}}, |
| 77 | {"raw-bus-access", {"raw-bus-cycles", "bus accesses per cycle"}}, |
| 78 | {"raw-ll-cache-miss", {"raw-ll-cache", "last level data or unified cache refill rate"}}, |
| 79 | {"raw-dtlb-walk", {"raw-l1d-tlb", "data TLB miss rate"}}, |
| 80 | {"raw-itlb-walk", {"raw-l1i-tlb", "instruction TLB miss rate"}}, |
| 81 | {"raw-ll-cache-miss-rd", {"raw-ll-cache-rd", "memory read operation miss rate"}}, |
| 82 | {"raw-remote-access-rd", |
| 83 | {"raw-remote-access", "read accesses to another socket in a multi-socket system"}}, |
| 84 | // Refer to "Table K3-2 Relationship between REFILL events and associated access events" in |
| 85 | // ARMv8 specification. |
| 86 | {"raw-l1d-cache-refill-rd", {"raw-l1d-cache-rd", "level 1 cache refill rate, read"}}, |
| 87 | {"raw-l1d-cache-refill-wr", {"raw-l1d-cache-wr", "level 1 cache refill rate, write"}}, |
| 88 | {"raw-l1d-tlb-refill-rd", {"raw-l1d-tlb-rd", "level 1 TLB refill rate, read"}}, |
| 89 | {"raw-l1d-tlb-refill-wr", {"raw-l1d-tlb-wr", "level 1 TLB refill rate, write"}}, |
| 90 | {"raw-l2d-cache-refill-rd", {"raw-l2d-cache-rd", "level 2 data cache refill rate, read"}}, |
| 91 | {"raw-l2d-cache-refill-wr", {"raw-l2d-cache-wr", "level 2 data cache refill rate, write"}}, |
| 92 | {"raw-l2d-tlb-refill-rd", {"raw-l2d-tlb-rd", "level 2 data TLB refill rate, read"}}, |
| 93 | }; |
| 94 | |
Yabin Cui | bb9a0d1 | 2023-08-31 15:53:31 -0700 | [diff] [blame] | 95 | std::string CounterSummary::ReadableCountValue(bool csv) { |
| 96 | if (type_name == "cpu-clock" || type_name == "task-clock") { |
| 97 | // Convert nanoseconds to milliseconds. |
| 98 | double value = count / 1e6; |
| 99 | return android::base::StringPrintf("%lf(ms)", value); |
| 100 | } |
| 101 | if (csv) { |
| 102 | return android::base::StringPrintf("%" PRIu64, count); |
| 103 | } |
| 104 | return ReadableCount(count); |
| 105 | } |
| 106 | |
Yabin Cui | 5271aa7 | 2020-03-31 16:59:33 -0700 | [diff] [blame] | 107 | const CounterSummary* CounterSummaries::FindSummary(const std::string& type_name, |
| 108 | const std::string& modifier, |
| 109 | const ThreadInfo* thread, int cpu) { |
| 110 | for (const auto& s : summaries_) { |
| 111 | if (s.type_name == type_name && s.modifier == modifier && s.thread == thread && s.cpu == cpu) { |
| 112 | return &s; |
Yabin Cui | 0a45adf | 2016-06-22 20:58:52 -0700 | [diff] [blame] | 113 | } |
Yabin Cui | 0a45adf | 2016-06-22 20:58:52 -0700 | [diff] [blame] | 114 | } |
Yabin Cui | 5271aa7 | 2020-03-31 16:59:33 -0700 | [diff] [blame] | 115 | return nullptr; |
| 116 | } |
Yabin Cui | 0a45adf | 2016-06-22 20:58:52 -0700 | [diff] [blame] | 117 | |
Yabin Cui | 5271aa7 | 2020-03-31 16:59:33 -0700 | [diff] [blame] | 118 | void CounterSummaries::AutoGenerateSummaries() { |
| 119 | for (size_t i = 0; i < summaries_.size(); ++i) { |
| 120 | const CounterSummary& s = summaries_[i]; |
| 121 | if (s.modifier == "u") { |
| 122 | const CounterSummary* other = FindSummary(s.type_name, "k", s.thread, s.cpu); |
| 123 | if (other != nullptr && other->IsMonitoredAtTheSameTime(s)) { |
| 124 | if (FindSummary(s.type_name, "", s.thread, s.cpu) == nullptr) { |
| 125 | summaries_.emplace_back(s.type_name, "", s.group_id, s.thread, s.cpu, |
Yabin Cui | 6922c07 | 2020-03-31 18:04:59 -0700 | [diff] [blame] | 126 | s.count + other->count, s.runtime_in_ns, s.scale, true, csv_); |
Yabin Cui | 0a45adf | 2016-06-22 20:58:52 -0700 | [diff] [blame] | 127 | } |
| 128 | } |
| 129 | } |
| 130 | } |
Yabin Cui | 5271aa7 | 2020-03-31 16:59:33 -0700 | [diff] [blame] | 131 | } |
Yabin Cui | 0a45adf | 2016-06-22 20:58:52 -0700 | [diff] [blame] | 132 | |
Yabin Cui | 5271aa7 | 2020-03-31 16:59:33 -0700 | [diff] [blame] | 133 | void CounterSummaries::GenerateComments(double duration_in_sec) { |
| 134 | for (auto& s : summaries_) { |
| 135 | s.comment = GetCommentForSummary(s, duration_in_sec); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | void CounterSummaries::Show(FILE* fp) { |
Yabin Cui | f53f716 | 2020-06-19 15:16:31 -0700 | [diff] [blame] | 140 | bool show_thread = !summaries_.empty() && summaries_[0].thread != nullptr; |
| 141 | bool show_cpu = !summaries_.empty() && summaries_[0].cpu != -1; |
Yabin Cui | 5271aa7 | 2020-03-31 16:59:33 -0700 | [diff] [blame] | 142 | if (csv_) { |
Yabin Cui | f53f716 | 2020-06-19 15:16:31 -0700 | [diff] [blame] | 143 | ShowCSV(fp, show_thread, show_cpu); |
Yabin Cui | 5271aa7 | 2020-03-31 16:59:33 -0700 | [diff] [blame] | 144 | } else { |
Yabin Cui | f53f716 | 2020-06-19 15:16:31 -0700 | [diff] [blame] | 145 | ShowText(fp, show_thread, show_cpu); |
Yabin Cui | 5271aa7 | 2020-03-31 16:59:33 -0700 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | |
Yabin Cui | f53f716 | 2020-06-19 15:16:31 -0700 | [diff] [blame] | 149 | void CounterSummaries::ShowCSV(FILE* fp, bool show_thread, bool show_cpu) { |
Yabin Cui | 5271aa7 | 2020-03-31 16:59:33 -0700 | [diff] [blame] | 150 | for (auto& s : summaries_) { |
Yabin Cui | f53f716 | 2020-06-19 15:16:31 -0700 | [diff] [blame] | 151 | if (show_thread) { |
Yabin Cui | 5271aa7 | 2020-03-31 16:59:33 -0700 | [diff] [blame] | 152 | fprintf(fp, "%s,%d,%d,", s.thread->name.c_str(), s.thread->pid, s.thread->tid); |
Yabin Cui | 0a45adf | 2016-06-22 20:58:52 -0700 | [diff] [blame] | 153 | } |
Yabin Cui | f53f716 | 2020-06-19 15:16:31 -0700 | [diff] [blame] | 154 | if (show_cpu) { |
| 155 | fprintf(fp, "%d,", s.cpu); |
| 156 | } |
Yabin Cui | abc8ee2 | 2022-08-25 11:06:39 -0700 | [diff] [blame] | 157 | fprintf(fp, "%s,%s,%s,%s\n", s.readable_count.c_str(), s.Name().c_str(), s.comment.c_str(), |
| 158 | (s.auto_generated ? "(generated)," : "")); |
Yabin Cui | 5271aa7 | 2020-03-31 16:59:33 -0700 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | |
Yabin Cui | f53f716 | 2020-06-19 15:16:31 -0700 | [diff] [blame] | 162 | void CounterSummaries::ShowText(FILE* fp, bool show_thread, bool show_cpu) { |
Yabin Cui | 5271aa7 | 2020-03-31 16:59:33 -0700 | [diff] [blame] | 163 | std::vector<std::string> titles; |
| 164 | |
| 165 | if (show_thread) { |
| 166 | titles = {"thread_name", "pid", "tid"}; |
| 167 | } |
| 168 | if (show_cpu) { |
| 169 | titles.emplace_back("cpu"); |
| 170 | } |
| 171 | titles.emplace_back("count"); |
| 172 | titles.emplace_back("event_name"); |
Yabin | 4ed8e11 | 2022-08-23 11:43:01 -0700 | [diff] [blame] | 173 | titles.emplace_back(" # count / runtime"); |
Yabin Cui | 5271aa7 | 2020-03-31 16:59:33 -0700 | [diff] [blame] | 174 | |
| 175 | std::vector<size_t> width(titles.size(), 0); |
| 176 | |
| 177 | auto adjust_width = [](size_t& w, size_t size) { w = std::max(w, size); }; |
| 178 | |
| 179 | // The last title is too long. Don't include it for width adjustment. |
| 180 | for (size_t i = 0; i + 1 < titles.size(); i++) { |
| 181 | adjust_width(width[i], titles[i].size()); |
Yabin Cui | 0a45adf | 2016-06-22 20:58:52 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Yabin Cui | 5271aa7 | 2020-03-31 16:59:33 -0700 | [diff] [blame] | 184 | for (auto& s : summaries_) { |
| 185 | size_t i = 0; |
Yabin Cui | dc2708c | 2020-01-10 15:33:11 -0800 | [diff] [blame] | 186 | if (show_thread) { |
Yabin Cui | 5271aa7 | 2020-03-31 16:59:33 -0700 | [diff] [blame] | 187 | adjust_width(width[i++], s.thread->name.size()); |
| 188 | adjust_width(width[i++], std::to_string(s.thread->pid).size()); |
| 189 | adjust_width(width[i++], std::to_string(s.thread->tid).size()); |
Yabin Cui | 8ab5621 | 2020-01-27 15:23:46 -0800 | [diff] [blame] | 190 | } |
| 191 | if (show_cpu) { |
Yabin Cui | 5271aa7 | 2020-03-31 16:59:33 -0700 | [diff] [blame] | 192 | adjust_width(width[i++], std::to_string(s.cpu).size()); |
Yabin Cui | 8ab5621 | 2020-01-27 15:23:46 -0800 | [diff] [blame] | 193 | } |
Yabin Cui | 5271aa7 | 2020-03-31 16:59:33 -0700 | [diff] [blame] | 194 | adjust_width(width[i++], s.readable_count.size()); |
| 195 | adjust_width(width[i++], s.Name().size()); |
| 196 | adjust_width(width[i++], s.comment.size()); |
Yabin Cui | 0a45adf | 2016-06-22 20:58:52 -0700 | [diff] [blame] | 197 | } |
| 198 | |
Yabin Cui | 5271aa7 | 2020-03-31 16:59:33 -0700 | [diff] [blame] | 199 | fprintf(fp, "# "); |
| 200 | for (size_t i = 0; i < titles.size(); i++) { |
| 201 | if (titles[i] == "count") { |
| 202 | fprintf(fp, "%*s", static_cast<int>(width[i]), titles[i].c_str()); |
Zhiting Zhu | 0979ba8 | 2016-07-09 19:38:08 -0700 | [diff] [blame] | 203 | } else { |
Yabin Cui | 5271aa7 | 2020-03-31 16:59:33 -0700 | [diff] [blame] | 204 | fprintf(fp, "%-*s", static_cast<int>(width[i]), titles[i].c_str()); |
Zhiting Zhu | 0979ba8 | 2016-07-09 19:38:08 -0700 | [diff] [blame] | 205 | } |
Yabin Cui | 5271aa7 | 2020-03-31 16:59:33 -0700 | [diff] [blame] | 206 | if (i + 1 < titles.size()) { |
| 207 | fprintf(fp, " "); |
Yabin Cui | 0a45adf | 2016-06-22 20:58:52 -0700 | [diff] [blame] | 208 | } |
Yabin Cui | 5271aa7 | 2020-03-31 16:59:33 -0700 | [diff] [blame] | 209 | } |
| 210 | fprintf(fp, "\n"); |
| 211 | |
| 212 | for (auto& s : summaries_) { |
| 213 | size_t i = 0; |
| 214 | if (show_thread) { |
| 215 | fprintf(fp, " %-*s", static_cast<int>(width[i++]), s.thread->name.c_str()); |
| 216 | fprintf(fp, " %-*d", static_cast<int>(width[i++]), s.thread->pid); |
| 217 | fprintf(fp, " %-*d", static_cast<int>(width[i++]), s.thread->tid); |
Yabin Cui | 0a45adf | 2016-06-22 20:58:52 -0700 | [diff] [blame] | 218 | } |
Yabin Cui | 5271aa7 | 2020-03-31 16:59:33 -0700 | [diff] [blame] | 219 | if (show_cpu) { |
| 220 | fprintf(fp, " %-*d", static_cast<int>(width[i++]), s.cpu); |
Yabin Cui | 0a45adf | 2016-06-22 20:58:52 -0700 | [diff] [blame] | 221 | } |
Yabin | 4ed8e11 | 2022-08-23 11:43:01 -0700 | [diff] [blame] | 222 | fprintf(fp, " %*s %-*s # %-*s%s\n", static_cast<int>(width[i]), s.readable_count.c_str(), |
| 223 | static_cast<int>(width[i + 1]), s.Name().c_str(), static_cast<int>(width[i + 2]), |
| 224 | s.comment.c_str(), (s.auto_generated ? " (generated)" : "")); |
Yabin Cui | 5271aa7 | 2020-03-31 16:59:33 -0700 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | |
| 228 | std::string CounterSummaries::GetCommentForSummary(const CounterSummary& s, |
| 229 | double duration_in_sec) { |
| 230 | char sap_mid; |
| 231 | if (csv_) { |
| 232 | sap_mid = ','; |
| 233 | } else { |
| 234 | sap_mid = ' '; |
| 235 | } |
| 236 | if (s.type_name == "task-clock") { |
| 237 | double run_sec = s.count / 1e9; |
Yabin Cui | 6922c07 | 2020-03-31 18:04:59 -0700 | [diff] [blame] | 238 | double used_cpus = run_sec / duration_in_sec; |
| 239 | return android::base::StringPrintf("%f%ccpus used", used_cpus, sap_mid); |
Yabin Cui | 5271aa7 | 2020-03-31 16:59:33 -0700 | [diff] [blame] | 240 | } |
| 241 | if (s.type_name == "cpu-clock") { |
| 242 | return ""; |
| 243 | } |
| 244 | if (s.type_name == "cpu-cycles") { |
Yabin Cui | 6922c07 | 2020-03-31 18:04:59 -0700 | [diff] [blame] | 245 | if (s.runtime_in_ns == 0) { |
Yabin Cui | 900ea07 | 2018-03-06 11:35:22 -0800 | [diff] [blame] | 246 | return ""; |
| 247 | } |
Yabin Cui | 6922c07 | 2020-03-31 18:04:59 -0700 | [diff] [blame] | 248 | double ghz = static_cast<double>(s.count) / s.runtime_in_ns; |
| 249 | return android::base::StringPrintf("%f%cGHz", ghz, sap_mid); |
Yabin Cui | 0a45adf | 2016-06-22 20:58:52 -0700 | [diff] [blame] | 250 | } |
Yabin Cui | 5271aa7 | 2020-03-31 16:59:33 -0700 | [diff] [blame] | 251 | if (s.type_name == "instructions" && s.count != 0) { |
| 252 | const CounterSummary* other = FindSummary("cpu-cycles", s.modifier, s.thread, s.cpu); |
| 253 | if (other != nullptr && other->IsMonitoredAtTheSameTime(s)) { |
| 254 | double cpi = static_cast<double>(other->count) / s.count; |
Yabin Cui | 6922c07 | 2020-03-31 18:04:59 -0700 | [diff] [blame] | 255 | return android::base::StringPrintf("%f%ccycles per instruction", cpi, sap_mid); |
Yabin Cui | 5271aa7 | 2020-03-31 16:59:33 -0700 | [diff] [blame] | 256 | } |
| 257 | } |
| 258 | std::string rate_comment = GetRateComment(s, sap_mid); |
| 259 | if (!rate_comment.empty()) { |
| 260 | return rate_comment; |
| 261 | } |
Yabin Cui | 6922c07 | 2020-03-31 18:04:59 -0700 | [diff] [blame] | 262 | if (s.runtime_in_ns == 0) { |
Yabin Cui | 5271aa7 | 2020-03-31 16:59:33 -0700 | [diff] [blame] | 263 | return ""; |
| 264 | } |
Yabin Cui | 6922c07 | 2020-03-31 18:04:59 -0700 | [diff] [blame] | 265 | double runtime_in_sec = static_cast<double>(s.runtime_in_ns) / 1e9; |
| 266 | double rate = s.count / runtime_in_sec; |
| 267 | if (rate >= 1e9 - 1e5) { |
| 268 | return android::base::StringPrintf("%.3f%cG/sec", rate / 1e9, sap_mid); |
Yabin Cui | 5271aa7 | 2020-03-31 16:59:33 -0700 | [diff] [blame] | 269 | } |
Yabin Cui | 6922c07 | 2020-03-31 18:04:59 -0700 | [diff] [blame] | 270 | if (rate >= 1e6 - 1e2) { |
| 271 | return android::base::StringPrintf("%.3f%cM/sec", rate / 1e6, sap_mid); |
Yabin Cui | 5271aa7 | 2020-03-31 16:59:33 -0700 | [diff] [blame] | 272 | } |
Yabin Cui | 6922c07 | 2020-03-31 18:04:59 -0700 | [diff] [blame] | 273 | if (rate >= 1e3) { |
| 274 | return android::base::StringPrintf("%.3f%cK/sec", rate / 1e3, sap_mid); |
Yabin Cui | 5271aa7 | 2020-03-31 16:59:33 -0700 | [diff] [blame] | 275 | } |
Yabin Cui | 6922c07 | 2020-03-31 18:04:59 -0700 | [diff] [blame] | 276 | return android::base::StringPrintf("%.3f%c/sec", rate, sap_mid); |
Yabin Cui | 5271aa7 | 2020-03-31 16:59:33 -0700 | [diff] [blame] | 277 | } |
Yabin Cui | 0a45adf | 2016-06-22 20:58:52 -0700 | [diff] [blame] | 278 | |
Yabin Cui | 5271aa7 | 2020-03-31 16:59:33 -0700 | [diff] [blame] | 279 | std::string CounterSummaries::GetRateComment(const CounterSummary& s, char sep) { |
| 280 | std::string_view miss_event_name = s.type_name; |
| 281 | std::string event_name; |
| 282 | std::string rate_desc; |
| 283 | if (auto it = COMMON_EVENT_RATE_MAP.find(miss_event_name); it != COMMON_EVENT_RATE_MAP.end()) { |
| 284 | event_name = it->second.first; |
| 285 | rate_desc = it->second.second; |
| 286 | } |
Yabin Cui | 3807691 | 2021-08-16 16:59:09 -0700 | [diff] [blame] | 287 | if (event_name.empty() && (GetTargetArch() == ARCH_ARM || GetTargetArch() == ARCH_ARM64)) { |
Yabin Cui | 5271aa7 | 2020-03-31 16:59:33 -0700 | [diff] [blame] | 288 | if (auto it = ARM_EVENT_RATE_MAP.find(miss_event_name); it != ARM_EVENT_RATE_MAP.end()) { |
Yabin Cui | 117caa4 | 2019-07-15 10:39:15 -0700 | [diff] [blame] | 289 | event_name = it->second.first; |
| 290 | rate_desc = it->second.second; |
| 291 | } |
Yabin Cui | 117caa4 | 2019-07-15 10:39:15 -0700 | [diff] [blame] | 292 | } |
Yabin Cui | 5271aa7 | 2020-03-31 16:59:33 -0700 | [diff] [blame] | 293 | if (event_name.empty() && android::base::ConsumeSuffix(&miss_event_name, "-misses")) { |
| 294 | event_name = std::string(miss_event_name) + "s"; |
| 295 | rate_desc = "miss rate"; |
Yabin Cui | 900ea07 | 2018-03-06 11:35:22 -0800 | [diff] [blame] | 296 | } |
Yabin Cui | 5271aa7 | 2020-03-31 16:59:33 -0700 | [diff] [blame] | 297 | if (!event_name.empty()) { |
| 298 | const CounterSummary* other = FindSummary(event_name, s.modifier, s.thread, s.cpu); |
| 299 | if (other != nullptr && other->IsMonitoredAtTheSameTime(s) && other->count != 0) { |
| 300 | double miss_rate = static_cast<double>(s.count) / other->count; |
| 301 | return android::base::StringPrintf("%f%%%c%s", miss_rate * 100, sep, rate_desc.c_str()); |
| 302 | } |
| 303 | } |
| 304 | return ""; |
| 305 | } |
Yabin Cui | 900ea07 | 2018-03-06 11:35:22 -0800 | [diff] [blame] | 306 | |
Yabin Cui | 5271aa7 | 2020-03-31 16:59:33 -0700 | [diff] [blame] | 307 | namespace { |
Yabin Cui | 0a45adf | 2016-06-22 20:58:52 -0700 | [diff] [blame] | 308 | |
Yabin Cui | 52f32a4 | 2019-08-12 16:53:46 -0700 | [diff] [blame] | 309 | // devfreq may use performance counters to calculate memory latency (as in |
| 310 | // drivers/devfreq/arm-memlat-mon.c). Hopefully we can get more available counters by asking devfreq |
| 311 | // to not use the memory latency governor temporarily. |
| 312 | class DevfreqCounters { |
| 313 | public: |
| 314 | bool Use() { |
| 315 | if (!IsRoot()) { |
| 316 | LOG(ERROR) << "--use-devfreq-counters needs root permission to set devfreq governors"; |
| 317 | return false; |
| 318 | } |
| 319 | std::string devfreq_dir = "/sys/class/devfreq/"; |
| 320 | for (auto& name : GetSubDirs(devfreq_dir)) { |
| 321 | std::string governor_path = devfreq_dir + name + "/governor"; |
| 322 | if (IsRegularFile(governor_path)) { |
| 323 | std::string governor; |
| 324 | if (!android::base::ReadFileToString(governor_path, &governor)) { |
| 325 | LOG(ERROR) << "failed to read " << governor_path; |
| 326 | return false; |
| 327 | } |
| 328 | governor = android::base::Trim(governor); |
| 329 | if (governor == "mem_latency") { |
| 330 | if (!android::base::WriteStringToFile("performance", governor_path)) { |
| 331 | PLOG(ERROR) << "failed to write " << governor_path; |
| 332 | return false; |
| 333 | } |
| 334 | mem_latency_governor_paths_.emplace_back(std::move(governor_path)); |
| 335 | } |
| 336 | } |
| 337 | } |
| 338 | return true; |
| 339 | } |
| 340 | |
| 341 | ~DevfreqCounters() { |
| 342 | for (auto& path : mem_latency_governor_paths_) { |
| 343 | android::base::WriteStringToFile("mem_latency", path); |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | private: |
| 348 | std::vector<std::string> mem_latency_governor_paths_; |
| 349 | }; |
| 350 | |
Yabin Cui | f79f07e | 2015-06-01 11:21:37 -0700 | [diff] [blame] | 351 | class StatCommand : public Command { |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 352 | public: |
Yabin Cui | f79f07e | 2015-06-01 11:21:37 -0700 | [diff] [blame] | 353 | StatCommand() |
Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 354 | : Command( |
| 355 | "stat", "gather performance counter information", |
| 356 | // clang-format off |
Yabin Cui | 877751b | 2016-06-13 18:03:47 -0700 | [diff] [blame] | 357 | "Usage: simpleperf stat [options] [command [command-args]]\n" |
Yabin Cui | 8a599d7 | 2016-07-21 18:32:53 -0700 | [diff] [blame] | 358 | " Gather performance counter information of running [command].\n" |
| 359 | " And -a/-p/-t option can be used to change target of counter information.\n" |
Yabin Cui | 877751b | 2016-06-13 18:03:47 -0700 | [diff] [blame] | 360 | "-a Collect system-wide information.\n" |
Yabin Cui | a80f8f7 | 2017-07-12 15:50:20 -0700 | [diff] [blame] | 361 | #if defined(__ANDROID__) |
| 362 | "--app package_name Profile the process of an Android application.\n" |
| 363 | " On non-rooted devices, the app must be debuggable,\n" |
| 364 | " because we use run-as to switch to the app's context.\n" |
| 365 | #endif |
Yabin Cui | dbf8280 | 2023-10-19 13:28:42 -0700 | [diff] [blame] | 366 | "--cpu cpu_item1,cpu_item2,... Monitor events on selected cpus. cpu_item can be a number like\n" |
| 367 | " 1, or a range like 0-3. A --cpu option affects all event types\n" |
| 368 | " following it until meeting another --cpu option.\n" |
Zhiting Zhu | 0979ba8 | 2016-07-09 19:38:08 -0700 | [diff] [blame] | 369 | "--csv Write report in comma separate form.\n" |
Yabin Cui | 8a599d7 | 2016-07-21 18:32:53 -0700 | [diff] [blame] | 370 | "--duration time_in_sec Monitor for time_in_sec seconds instead of running\n" |
| 371 | " [command]. Here time_in_sec may be any positive\n" |
| 372 | " floating point number.\n" |
Wei Wang | 539fb88 | 2016-09-28 14:42:02 -0700 | [diff] [blame] | 373 | "--interval time_in_ms Print stat for every time_in_ms milliseconds.\n" |
| 374 | " Here time_in_ms may be any positive floating point\n" |
Yabin Cui | 9267db2 | 2018-04-18 11:17:19 -0700 | [diff] [blame] | 375 | " number. Simpleperf prints total values from the\n" |
| 376 | " starting point. But this can be changed by\n" |
| 377 | " --interval-only-values.\n" |
| 378 | "--interval-only-values Print numbers of events happened in each interval.\n" |
Yabin Cui | 877751b | 2016-06-13 18:03:47 -0700 | [diff] [blame] | 379 | "-e event1[:modifier1],event2[:modifier2],...\n" |
Yabin Cui | 5e8ee33 | 2018-05-10 15:38:00 -0700 | [diff] [blame] | 380 | " Select a list of events to count. An event can be:\n" |
| 381 | " 1) an event name listed in `simpleperf list`;\n" |
| 382 | " 2) a raw PMU event in rN format. N is a hex number.\n" |
| 383 | " For example, r1b selects event number 0x1b.\n" |
| 384 | " Modifiers can be added to define how the event should be\n" |
| 385 | " monitored. Possible modifiers are:\n" |
Yabin Cui | 877751b | 2016-06-13 18:03:47 -0700 | [diff] [blame] | 386 | " u - monitor user space events only\n" |
| 387 | " k - monitor kernel space events only\n" |
| 388 | "--group event1[:modifier],event2[:modifier2],...\n" |
| 389 | " Similar to -e option. But events specified in the same --group\n" |
| 390 | " option are monitored as a group, and scheduled in and out at the\n" |
| 391 | " same time.\n" |
Yabin Cui | 61b4dac | 2023-11-03 15:19:14 -0700 | [diff] [blame] | 392 | "--kprobe kprobe_event1,kprobe_event2,...\n" |
| 393 | " Add kprobe events during stating. The kprobe_event format is in\n" |
| 394 | " Documentation/trace/kprobetrace.rst in the kernel. Examples:\n" |
| 395 | " 'p:myprobe do_sys_openat2 $arg2:string' - add event kprobes:myprobe\n" |
| 396 | " 'r:myretprobe do_sys_openat2 $retval:s64' - add event kprobes:myretprobe\n" |
Yabin Cui | 877751b | 2016-06-13 18:03:47 -0700 | [diff] [blame] | 397 | "--no-inherit Don't stat created child threads/processes.\n" |
Yabin Cui | 0a45adf | 2016-06-22 20:58:52 -0700 | [diff] [blame] | 398 | "-o output_filename Write report to output_filename instead of standard output.\n" |
Yabin Cui | 8ab5621 | 2020-01-27 15:23:46 -0800 | [diff] [blame] | 399 | "--per-core Print counters for each cpu core.\n" |
Yabin Cui | dc2708c | 2020-01-10 15:33:11 -0800 | [diff] [blame] | 400 | "--per-thread Print counters for each thread.\n" |
Yabin Cui | 1c6be75 | 2023-02-28 11:46:37 -0800 | [diff] [blame] | 401 | "-p pid_or_process_name_regex1,pid_or_process_name_regex2,...\n" |
| 402 | " Stat events on existing processes. Processes are searched either by pid\n" |
| 403 | " or process name regex. Mutually exclusive with -a.\n" |
| 404 | "-t tid1,tid2,... Stat events on existing threads. Mutually exclusive with -a.\n" |
Yabin Cui | 6de79a0 | 2023-11-03 16:36:56 -0700 | [diff] [blame] | 405 | "--tp-filter filter_string Set filter_string for the previous tracepoint event.\n" |
| 406 | " Format is in Documentation/trace/events.rst in the kernel.\n" |
| 407 | " An example: 'prev_comm != \"simpleperf\" && (prev_pid > 1)'.\n" |
Yabin Cui | f1a1e48 | 2021-12-22 14:54:20 -0800 | [diff] [blame] | 408 | "--print-hw-counter Test and print CPU PMU hardware counters available on the device.\n" |
Yabin Cui | f53f716 | 2020-06-19 15:16:31 -0700 | [diff] [blame] | 409 | "--sort key1,key2,... Select keys used to sort the report, used when --per-thread\n" |
| 410 | " or --per-core appears. The appearance order of keys decides\n" |
| 411 | " the order of keys used to sort the report.\n" |
| 412 | " Possible keys include:\n" |
| 413 | " count -- event count for each entry\n" |
| 414 | " count_per_thread -- event count for a thread on all cpus\n" |
| 415 | " cpu -- cpu id\n" |
| 416 | " pid -- process id\n" |
| 417 | " tid -- thread id\n" |
| 418 | " comm -- thread name\n" |
| 419 | " The default sort keys are:\n" |
| 420 | " count_per_thread,tid,cpu,count\n" |
Yabin Cui | 52f32a4 | 2019-08-12 16:53:46 -0700 | [diff] [blame] | 421 | #if defined(__ANDROID__) |
| 422 | "--use-devfreq-counters On devices with Qualcomm SOCs, some hardware counters may be used\n" |
| 423 | " to monitor memory latency (in drivers/devfreq/arm-memlat-mon.c),\n" |
| 424 | " making fewer counters available to users. This option asks devfreq\n" |
| 425 | " to temporarily release counters by replacing memory-latency governor\n" |
| 426 | " with performance governor. It affects memory latency during profiling,\n" |
| 427 | " and may cause wedged power if simpleperf is killed in between.\n" |
| 428 | #endif |
Yabin Cui | 877751b | 2016-06-13 18:03:47 -0700 | [diff] [blame] | 429 | "--verbose Show result in verbose mode.\n" |
Yabin Cui | a80f8f7 | 2017-07-12 15:50:20 -0700 | [diff] [blame] | 430 | #if 0 |
| 431 | // Below options are only used internally and shouldn't be visible to the public. |
| 432 | "--in-app We are already running in the app's context.\n" |
Yabin Cui | f897452 | 2017-07-17 14:36:37 -0700 | [diff] [blame] | 433 | "--tracepoint-events file_name Read tracepoint events from [file_name] instead of tracefs.\n" |
Yabin Cui | 1a30a58 | 2019-01-10 15:35:39 -0800 | [diff] [blame] | 434 | "--out-fd <fd> Write output to a file descriptor.\n" |
| 435 | "--stop-signal-fd <fd> Stop stating when fd is readable.\n" |
Yabin Cui | a80f8f7 | 2017-07-12 15:50:20 -0700 | [diff] [blame] | 436 | #endif |
Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 437 | // clang-format on |
| 438 | ), |
Yabin Cui | f79f07e | 2015-06-01 11:21:37 -0700 | [diff] [blame] | 439 | verbose_mode_(false), |
Yabin Cui | 4be4126 | 2015-06-22 14:23:01 -0700 | [diff] [blame] | 440 | system_wide_collection_(false), |
Zhiting Zhu | 0979ba8 | 2016-07-09 19:38:08 -0700 | [diff] [blame] | 441 | child_inherit_(true), |
Yabin Cui | 3e4c595 | 2016-07-26 15:03:27 -0700 | [diff] [blame] | 442 | duration_in_sec_(0), |
Wei Wang | 539fb88 | 2016-09-28 14:42:02 -0700 | [diff] [blame] | 443 | interval_in_ms_(0), |
Yabin Cui | 9267db2 | 2018-04-18 11:17:19 -0700 | [diff] [blame] | 444 | interval_only_values_(false), |
Yabin Cui | 4cf37d1 | 2016-08-19 15:42:39 -0700 | [diff] [blame] | 445 | event_selection_set_(true), |
Yabin Cui | a80f8f7 | 2017-07-12 15:50:20 -0700 | [diff] [blame] | 446 | csv_(false), |
| 447 | in_app_context_(false) { |
Yabin Cui | 987d9ab | 2016-07-15 14:08:48 -0700 | [diff] [blame] | 448 | // Die if parent exits. |
| 449 | prctl(PR_SET_PDEATHSIG, SIGHUP, 0, 0, 0); |
Yabin Cui | f53f716 | 2020-06-19 15:16:31 -0700 | [diff] [blame] | 450 | // Set default sort keys. Full key list is in BuildSummaryComparator(). |
| 451 | sort_keys_ = {"count_per_thread", "tid", "cpu", "count"}; |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | bool Run(const std::vector<std::string>& args); |
| 455 | |
| 456 | private: |
Yabin Cui | 61b4dac | 2023-11-03 15:19:14 -0700 | [diff] [blame] | 457 | bool ParseOptions(const std::vector<std::string>& args, std::vector<std::string>* non_option_args, |
| 458 | ProbeEvents& probe_events); |
Yabin Cui | 5aded99 | 2022-08-25 10:51:11 -0700 | [diff] [blame] | 459 | void PrintHardwareCounters(); |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 460 | bool AddDefaultMeasuredEventTypes(); |
Yabin Cui | 877751b | 2016-06-13 18:03:47 -0700 | [diff] [blame] | 461 | void SetEventSelectionFlags(); |
Yabin Cui | dc2708c | 2020-01-10 15:33:11 -0800 | [diff] [blame] | 462 | void MonitorEachThread(); |
Yabin Cui | 8ab5621 | 2020-01-27 15:23:46 -0800 | [diff] [blame] | 463 | void AdjustToIntervalOnlyValues(std::vector<CountersInfo>& counters); |
Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 464 | bool ShowCounters(const std::vector<CountersInfo>& counters, double duration_in_sec, FILE* fp); |
Yabin | 4ed8e11 | 2022-08-23 11:43:01 -0700 | [diff] [blame] | 465 | void CheckHardwareCounterMultiplexing(); |
Yabin Cui | 6a2fda4 | 2022-09-01 11:52:51 -0700 | [diff] [blame] | 466 | void PrintWarningForInaccurateEvents(); |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 467 | |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 468 | bool verbose_mode_; |
| 469 | bool system_wide_collection_; |
Yabin Cui | 4be4126 | 2015-06-22 14:23:01 -0700 | [diff] [blame] | 470 | bool child_inherit_; |
Yabin Cui | 3e4c595 | 2016-07-26 15:03:27 -0700 | [diff] [blame] | 471 | double duration_in_sec_; |
Wei Wang | 539fb88 | 2016-09-28 14:42:02 -0700 | [diff] [blame] | 472 | double interval_in_ms_; |
Yabin Cui | 9267db2 | 2018-04-18 11:17:19 -0700 | [diff] [blame] | 473 | bool interval_only_values_; |
Yabin Cui | 8ab5621 | 2020-01-27 15:23:46 -0800 | [diff] [blame] | 474 | std::vector<std::vector<CounterSum>> last_sum_values_; |
Yabin Cui | 4be4126 | 2015-06-22 14:23:01 -0700 | [diff] [blame] | 475 | EventSelectionSet event_selection_set_; |
Yabin Cui | 0a45adf | 2016-06-22 20:58:52 -0700 | [diff] [blame] | 476 | std::string output_filename_; |
Yabin Cui | 1a30a58 | 2019-01-10 15:35:39 -0800 | [diff] [blame] | 477 | android::base::unique_fd out_fd_; |
Zhiting Zhu | 0979ba8 | 2016-07-09 19:38:08 -0700 | [diff] [blame] | 478 | bool csv_; |
Yabin Cui | a80f8f7 | 2017-07-12 15:50:20 -0700 | [diff] [blame] | 479 | std::string app_package_name_; |
| 480 | bool in_app_context_; |
Yabin Cui | 1a30a58 | 2019-01-10 15:35:39 -0800 | [diff] [blame] | 481 | android::base::unique_fd stop_signal_fd_; |
Yabin Cui | 52f32a4 | 2019-08-12 16:53:46 -0700 | [diff] [blame] | 482 | bool use_devfreq_counters_ = false; |
Yabin Cui | dc2708c | 2020-01-10 15:33:11 -0800 | [diff] [blame] | 483 | |
Yabin Cui | 8ab5621 | 2020-01-27 15:23:46 -0800 | [diff] [blame] | 484 | bool report_per_core_ = false; |
Yabin Cui | dc2708c | 2020-01-10 15:33:11 -0800 | [diff] [blame] | 485 | bool report_per_thread_ = false; |
Yabin Cui | 8ab5621 | 2020-01-27 15:23:46 -0800 | [diff] [blame] | 486 | // used to report event count for each thread |
Yabin Cui | dc2708c | 2020-01-10 15:33:11 -0800 | [diff] [blame] | 487 | std::unordered_map<pid_t, ThreadInfo> thread_info_; |
Yabin Cui | f53f716 | 2020-06-19 15:16:31 -0700 | [diff] [blame] | 488 | // used to sort report |
| 489 | std::vector<std::string> sort_keys_; |
| 490 | std::optional<SummaryComparator> summary_comparator_; |
Yabin Cui | f1a1e48 | 2021-12-22 14:54:20 -0800 | [diff] [blame] | 491 | bool print_hw_counter_ = false; |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 492 | }; |
| 493 | |
Yabin Cui | f79f07e | 2015-06-01 11:21:37 -0700 | [diff] [blame] | 494 | bool StatCommand::Run(const std::vector<std::string>& args) { |
Yabin Cui | ebf79f3 | 2016-06-01 15:39:39 -0700 | [diff] [blame] | 495 | if (!CheckPerfEventLimit()) { |
| 496 | return false; |
| 497 | } |
Yabin Cui | dc2708c | 2020-01-10 15:33:11 -0800 | [diff] [blame] | 498 | AllowMoreOpenedFiles(); |
Yabin Cui | ebf79f3 | 2016-06-01 15:39:39 -0700 | [diff] [blame] | 499 | |
Yabin Cui | 4be4126 | 2015-06-22 14:23:01 -0700 | [diff] [blame] | 500 | // 1. Parse options, and use default measured event types if not given. |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 501 | std::vector<std::string> workload_args; |
Yabin Cui | 61b4dac | 2023-11-03 15:19:14 -0700 | [diff] [blame] | 502 | ProbeEvents probe_events(event_selection_set_); |
| 503 | if (!ParseOptions(args, &workload_args, probe_events)) { |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 504 | return false; |
| 505 | } |
Yabin Cui | f1a1e48 | 2021-12-22 14:54:20 -0800 | [diff] [blame] | 506 | if (print_hw_counter_) { |
Yabin Cui | 5aded99 | 2022-08-25 10:51:11 -0700 | [diff] [blame] | 507 | PrintHardwareCounters(); |
| 508 | return true; |
Yabin Cui | f1a1e48 | 2021-12-22 14:54:20 -0800 | [diff] [blame] | 509 | } |
Yabin Cui | a80f8f7 | 2017-07-12 15:50:20 -0700 | [diff] [blame] | 510 | if (!app_package_name_.empty() && !in_app_context_) { |
| 511 | if (!IsRoot()) { |
| 512 | return RunInAppContext(app_package_name_, "stat", args, workload_args.size(), |
Yabin Cui | f897452 | 2017-07-17 14:36:37 -0700 | [diff] [blame] | 513 | output_filename_, !event_selection_set_.GetTracepointEvents().empty()); |
Yabin Cui | a80f8f7 | 2017-07-12 15:50:20 -0700 | [diff] [blame] | 514 | } |
| 515 | } |
Yabin Cui | 52f32a4 | 2019-08-12 16:53:46 -0700 | [diff] [blame] | 516 | DevfreqCounters devfreq_counters; |
| 517 | if (use_devfreq_counters_) { |
| 518 | if (!devfreq_counters.Use()) { |
| 519 | return false; |
| 520 | } |
| 521 | } |
Yabin Cui | 877751b | 2016-06-13 18:03:47 -0700 | [diff] [blame] | 522 | if (event_selection_set_.empty()) { |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 523 | if (!AddDefaultMeasuredEventTypes()) { |
| 524 | return false; |
| 525 | } |
| 526 | } |
Yabin Cui | 877751b | 2016-06-13 18:03:47 -0700 | [diff] [blame] | 527 | SetEventSelectionFlags(); |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 528 | |
Yabin Cui | 4be4126 | 2015-06-22 14:23:01 -0700 | [diff] [blame] | 529 | // 2. Create workload. |
Yabin Cui | b032de7 | 2015-06-17 21:15:09 -0700 | [diff] [blame] | 530 | std::unique_ptr<Workload> workload; |
| 531 | if (!workload_args.empty()) { |
| 532 | workload = Workload::CreateWorkload(workload_args); |
| 533 | if (workload == nullptr) { |
| 534 | return false; |
| 535 | } |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 536 | } |
Yabin Cui | 5f43fc4 | 2016-12-13 13:47:49 -0800 | [diff] [blame] | 537 | bool need_to_check_targets = false; |
Yabin Cui | bc2a102 | 2016-08-29 12:33:17 -0700 | [diff] [blame] | 538 | if (system_wide_collection_) { |
Yabin Cui | dc2708c | 2020-01-10 15:33:11 -0800 | [diff] [blame] | 539 | if (report_per_thread_) { |
| 540 | event_selection_set_.AddMonitoredProcesses(GetAllProcesses()); |
| 541 | } else { |
| 542 | event_selection_set_.AddMonitoredThreads({-1}); |
| 543 | } |
Yabin Cui | bc2a102 | 2016-08-29 12:33:17 -0700 | [diff] [blame] | 544 | } else if (!event_selection_set_.HasMonitoredTarget()) { |
Yabin Cui | b032de7 | 2015-06-17 21:15:09 -0700 | [diff] [blame] | 545 | if (workload != nullptr) { |
Yabin Cui | bc2a102 | 2016-08-29 12:33:17 -0700 | [diff] [blame] | 546 | event_selection_set_.AddMonitoredProcesses({workload->GetPid()}); |
Yabin Cui | e964a02 | 2023-11-22 14:26:49 -0800 | [diff] [blame] | 547 | event_selection_set_.SetEnableCondition(false, true); |
Yabin Cui | a80f8f7 | 2017-07-12 15:50:20 -0700 | [diff] [blame] | 548 | } else if (!app_package_name_.empty()) { |
Yabin Cui | 7cb6f29 | 2017-08-28 14:49:04 -0700 | [diff] [blame] | 549 | std::set<pid_t> pids = WaitForAppProcesses(app_package_name_); |
| 550 | event_selection_set_.AddMonitoredProcesses(pids); |
Yabin Cui | b032de7 | 2015-06-17 21:15:09 -0700 | [diff] [blame] | 551 | } else { |
Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 552 | LOG(ERROR) << "No threads to monitor. Try `simpleperf help stat` for help\n"; |
Yabin Cui | b032de7 | 2015-06-17 21:15:09 -0700 | [diff] [blame] | 553 | return false; |
| 554 | } |
Yabin Cui | 5f43fc4 | 2016-12-13 13:47:49 -0800 | [diff] [blame] | 555 | } else { |
| 556 | need_to_check_targets = true; |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 557 | } |
| 558 | |
Yabin Cui | dc2708c | 2020-01-10 15:33:11 -0800 | [diff] [blame] | 559 | if (report_per_thread_) { |
| 560 | MonitorEachThread(); |
| 561 | } |
| 562 | |
Wei Wang | 539fb88 | 2016-09-28 14:42:02 -0700 | [diff] [blame] | 563 | // 3. Open perf_event_files and output file if defined. |
Yabin Cui | dbf8280 | 2023-10-19 13:28:42 -0700 | [diff] [blame] | 564 | if (!event_selection_set_.OpenEventFiles()) { |
Yabin Cui | bc2a102 | 2016-08-29 12:33:17 -0700 | [diff] [blame] | 565 | return false; |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 566 | } |
Wei Wang | 539fb88 | 2016-09-28 14:42:02 -0700 | [diff] [blame] | 567 | std::unique_ptr<FILE, decltype(&fclose)> fp_holder(nullptr, fclose); |
Wei Wang | 539fb88 | 2016-09-28 14:42:02 -0700 | [diff] [blame] | 568 | if (!output_filename_.empty()) { |
Yabin Cui | 1a30a58 | 2019-01-10 15:35:39 -0800 | [diff] [blame] | 569 | fp_holder.reset(fopen(output_filename_.c_str(), "we")); |
Wei Wang | 539fb88 | 2016-09-28 14:42:02 -0700 | [diff] [blame] | 570 | if (fp_holder == nullptr) { |
| 571 | PLOG(ERROR) << "failed to open " << output_filename_; |
| 572 | return false; |
| 573 | } |
Yabin Cui | 1a30a58 | 2019-01-10 15:35:39 -0800 | [diff] [blame] | 574 | } else if (out_fd_ != -1) { |
| 575 | fp_holder.reset(fdopen(out_fd_.release(), "we")); |
| 576 | if (fp_holder == nullptr) { |
| 577 | PLOG(ERROR) << "failed to write output."; |
| 578 | return false; |
| 579 | } |
Wei Wang | 539fb88 | 2016-09-28 14:42:02 -0700 | [diff] [blame] | 580 | } |
Yabin Cui | 1a30a58 | 2019-01-10 15:35:39 -0800 | [diff] [blame] | 581 | FILE* fp = fp_holder ? fp_holder.get() : stdout; |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 582 | |
Yabin Cui | 26968e6 | 2017-01-30 11:34:24 -0800 | [diff] [blame] | 583 | // 4. Add signal/periodic Events. |
Yabin Cui | c29e172 | 2018-02-14 13:41:03 -0800 | [diff] [blame] | 584 | IOEventLoop* loop = event_selection_set_.GetIOEventLoop(); |
Wei Wang | 539fb88 | 2016-09-28 14:42:02 -0700 | [diff] [blame] | 585 | std::chrono::time_point<std::chrono::steady_clock> start_time; |
| 586 | std::vector<CountersInfo> counters; |
Yabin Cui | 5f43fc4 | 2016-12-13 13:47:49 -0800 | [diff] [blame] | 587 | if (need_to_check_targets && !event_selection_set_.StopWhenNoMoreTargets()) { |
| 588 | return false; |
| 589 | } |
Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 590 | auto exit_loop_callback = [loop]() { return loop->ExitLoop(); }; |
Yabin Cui | 1a30a58 | 2019-01-10 15:35:39 -0800 | [diff] [blame] | 591 | if (!loop->AddSignalEvents({SIGCHLD, SIGINT, SIGTERM, SIGHUP}, exit_loop_callback)) { |
Yabin Cui | 3e4c595 | 2016-07-26 15:03:27 -0700 | [diff] [blame] | 592 | return false; |
| 593 | } |
Yabin Cui | 1a30a58 | 2019-01-10 15:35:39 -0800 | [diff] [blame] | 594 | if (stop_signal_fd_ != -1) { |
| 595 | if (!loop->AddReadEvent(stop_signal_fd_, exit_loop_callback)) { |
| 596 | return false; |
| 597 | } |
| 598 | } |
Yabin Cui | 3e4c595 | 2016-07-26 15:03:27 -0700 | [diff] [blame] | 599 | if (duration_in_sec_ != 0) { |
Yabin Cui | 1a30a58 | 2019-01-10 15:35:39 -0800 | [diff] [blame] | 600 | if (!loop->AddPeriodicEvent(SecondToTimeval(duration_in_sec_), exit_loop_callback)) { |
Yabin Cui | 3e4c595 | 2016-07-26 15:03:27 -0700 | [diff] [blame] | 601 | return false; |
| 602 | } |
| 603 | } |
Wei Wang | 539fb88 | 2016-09-28 14:42:02 -0700 | [diff] [blame] | 604 | auto print_counters = [&]() { |
Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 605 | auto end_time = std::chrono::steady_clock::now(); |
| 606 | if (!event_selection_set_.ReadCounters(&counters)) { |
| 607 | return false; |
| 608 | } |
| 609 | double duration_in_sec = |
| 610 | std::chrono::duration_cast<std::chrono::duration<double>>(end_time - start_time).count(); |
| 611 | if (interval_only_values_) { |
| 612 | AdjustToIntervalOnlyValues(counters); |
| 613 | } |
| 614 | if (!ShowCounters(counters, duration_in_sec, fp)) { |
| 615 | return false; |
| 616 | } |
| 617 | return true; |
Wei Wang | 539fb88 | 2016-09-28 14:42:02 -0700 | [diff] [blame] | 618 | }; |
| 619 | |
| 620 | if (interval_in_ms_ != 0) { |
Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 621 | if (!loop->AddPeriodicEvent(SecondToTimeval(interval_in_ms_ / 1000.0), print_counters)) { |
Wei Wang | 539fb88 | 2016-09-28 14:42:02 -0700 | [diff] [blame] | 622 | return false; |
| 623 | } |
| 624 | } |
Yabin Cui | 3e4c595 | 2016-07-26 15:03:27 -0700 | [diff] [blame] | 625 | |
| 626 | // 5. Count events while workload running. |
Wei Wang | 539fb88 | 2016-09-28 14:42:02 -0700 | [diff] [blame] | 627 | start_time = std::chrono::steady_clock::now(); |
Yabin Cui | b032de7 | 2015-06-17 21:15:09 -0700 | [diff] [blame] | 628 | if (workload != nullptr && !workload->Start()) { |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 629 | return false; |
| 630 | } |
Yabin Cui | 26968e6 | 2017-01-30 11:34:24 -0800 | [diff] [blame] | 631 | if (!loop->RunLoop()) { |
Yabin Cui | 3e4c595 | 2016-07-26 15:03:27 -0700 | [diff] [blame] | 632 | return false; |
Yabin Cui | 621a533 | 2015-06-15 16:17:20 -0700 | [diff] [blame] | 633 | } |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 634 | |
Yabin Cui | 3e4c595 | 2016-07-26 15:03:27 -0700 | [diff] [blame] | 635 | // 6. Read and print counters. |
Yabin Cui | 9267db2 | 2018-04-18 11:17:19 -0700 | [diff] [blame] | 636 | if (interval_in_ms_ == 0) { |
Yabin | 4ed8e11 | 2022-08-23 11:43:01 -0700 | [diff] [blame] | 637 | if (!print_counters()) { |
| 638 | return false; |
| 639 | } |
Yabin Cui | 9267db2 | 2018-04-18 11:17:19 -0700 | [diff] [blame] | 640 | } |
Yabin | 4ed8e11 | 2022-08-23 11:43:01 -0700 | [diff] [blame] | 641 | |
Yabin Cui | 6a2fda4 | 2022-09-01 11:52:51 -0700 | [diff] [blame] | 642 | // 7. Print warnings when needed. |
Yabin | 4ed8e11 | 2022-08-23 11:43:01 -0700 | [diff] [blame] | 643 | event_selection_set_.CloseEventFiles(); |
| 644 | CheckHardwareCounterMultiplexing(); |
Yabin Cui | 6a2fda4 | 2022-09-01 11:52:51 -0700 | [diff] [blame] | 645 | PrintWarningForInaccurateEvents(); |
Yabin | 4ed8e11 | 2022-08-23 11:43:01 -0700 | [diff] [blame] | 646 | |
Yabin Cui | 9267db2 | 2018-04-18 11:17:19 -0700 | [diff] [blame] | 647 | return true; |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 648 | } |
| 649 | |
Yabin Cui | f79f07e | 2015-06-01 11:21:37 -0700 | [diff] [blame] | 650 | bool StatCommand::ParseOptions(const std::vector<std::string>& args, |
Yabin Cui | 61b4dac | 2023-11-03 15:19:14 -0700 | [diff] [blame] | 651 | std::vector<std::string>* non_option_args, |
| 652 | ProbeEvents& probe_events) { |
Yabin Cui | 6f09467 | 2020-07-22 14:50:35 -0700 | [diff] [blame] | 653 | OptionValueMap options; |
| 654 | std::vector<std::pair<OptionName, OptionValue>> ordered_options; |
| 655 | |
| 656 | if (!PreprocessOptions(args, GetStatCmdOptionFormats(), &options, &ordered_options, |
| 657 | non_option_args)) { |
| 658 | return false; |
| 659 | } |
| 660 | |
| 661 | // Process options. |
| 662 | system_wide_collection_ = options.PullBoolValue("-a"); |
| 663 | |
| 664 | if (auto value = options.PullValue("--app"); value) { |
| 665 | app_package_name_ = *value->str_value; |
| 666 | } |
Yabin Cui | 6f09467 | 2020-07-22 14:50:35 -0700 | [diff] [blame] | 667 | csv_ = options.PullBoolValue("--csv"); |
| 668 | |
| 669 | if (!options.PullDoubleValue("--duration", &duration_in_sec_, 1e-9)) { |
| 670 | return false; |
| 671 | } |
| 672 | if (!options.PullDoubleValue("--interval", &interval_in_ms_, 1e-9)) { |
| 673 | return false; |
| 674 | } |
| 675 | interval_only_values_ = options.PullBoolValue("--interval-only-values"); |
| 676 | |
Yabin Cui | 6f09467 | 2020-07-22 14:50:35 -0700 | [diff] [blame] | 677 | in_app_context_ = options.PullBoolValue("--in-app"); |
Yabin Cui | 61b4dac | 2023-11-03 15:19:14 -0700 | [diff] [blame] | 678 | for (const OptionValue& value : options.PullValues("--kprobe")) { |
| 679 | for (const auto& cmd : Split(*value.str_value, ",")) { |
| 680 | if (!probe_events.AddKprobe(cmd)) { |
| 681 | return false; |
| 682 | } |
| 683 | } |
| 684 | } |
Yabin Cui | 6f09467 | 2020-07-22 14:50:35 -0700 | [diff] [blame] | 685 | child_inherit_ = !options.PullBoolValue("--no-inherit"); |
| 686 | |
| 687 | if (auto value = options.PullValue("-o"); value) { |
| 688 | output_filename_ = *value->str_value; |
| 689 | } |
| 690 | if (auto value = options.PullValue("--out-fd"); value) { |
| 691 | out_fd_.reset(static_cast<int>(value->uint_value)); |
| 692 | } |
| 693 | |
| 694 | report_per_core_ = options.PullBoolValue("--per-core"); |
| 695 | report_per_thread_ = options.PullBoolValue("--per-thread"); |
| 696 | |
Yabin Cui | 1c6be75 | 2023-02-28 11:46:37 -0800 | [diff] [blame] | 697 | if (auto strs = options.PullStringValues("-p"); !strs.empty()) { |
| 698 | if (auto pids = GetPidsFromStrings(strs, true, true); pids) { |
Yabin Cui | e3ca998 | 2020-10-16 13:16:26 -0700 | [diff] [blame] | 699 | event_selection_set_.AddMonitoredProcesses(pids.value()); |
| 700 | } else { |
| 701 | return false; |
Yabin Cui | 6f09467 | 2020-07-22 14:50:35 -0700 | [diff] [blame] | 702 | } |
| 703 | } |
Yabin Cui | f1a1e48 | 2021-12-22 14:54:20 -0800 | [diff] [blame] | 704 | print_hw_counter_ = options.PullBoolValue("--print-hw-counter"); |
Yabin Cui | f53f716 | 2020-06-19 15:16:31 -0700 | [diff] [blame] | 705 | |
Yabin Cui | 6f09467 | 2020-07-22 14:50:35 -0700 | [diff] [blame] | 706 | if (auto value = options.PullValue("--sort"); value) { |
| 707 | sort_keys_ = Split(*value->str_value, ","); |
| 708 | } |
| 709 | |
| 710 | if (auto value = options.PullValue("--stop-signal-fd"); value) { |
| 711 | stop_signal_fd_.reset(static_cast<int>(value->uint_value)); |
| 712 | } |
| 713 | |
Yabin Cui | e3ca998 | 2020-10-16 13:16:26 -0700 | [diff] [blame] | 714 | for (const OptionValue& value : options.PullValues("-t")) { |
| 715 | if (auto tids = GetTidsFromString(*value.str_value, true); tids) { |
| 716 | event_selection_set_.AddMonitoredThreads(tids.value()); |
| 717 | } else { |
| 718 | return false; |
Yabin Cui | 6f09467 | 2020-07-22 14:50:35 -0700 | [diff] [blame] | 719 | } |
| 720 | } |
| 721 | |
| 722 | if (auto value = options.PullValue("--tracepoint-events"); value) { |
Yabin Cui | 16a6ace | 2020-10-01 14:56:32 -0700 | [diff] [blame] | 723 | if (!EventTypeManager::Instance().ReadTracepointsFromFile(*value->str_value)) { |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 724 | return false; |
| 725 | } |
| 726 | } |
| 727 | |
Yabin Cui | 6f09467 | 2020-07-22 14:50:35 -0700 | [diff] [blame] | 728 | use_devfreq_counters_ = options.PullBoolValue("--use-devfreq-counters"); |
| 729 | verbose_mode_ = options.PullBoolValue("--verbose"); |
| 730 | |
| 731 | CHECK(options.values.empty()); |
Yabin Cui | dbf8280 | 2023-10-19 13:28:42 -0700 | [diff] [blame] | 732 | |
| 733 | // Process ordered options. |
| 734 | for (const auto& pair : ordered_options) { |
| 735 | const OptionName& name = pair.first; |
| 736 | const OptionValue& value = pair.second; |
| 737 | |
| 738 | if (name == "--cpu") { |
| 739 | if (auto v = GetCpusFromString(*value.str_value); v) { |
| 740 | std::set<int>& cpus = v.value(); |
| 741 | event_selection_set_.SetCpusForNewEvents(std::vector<int>(cpus.begin(), cpus.end())); |
| 742 | } else { |
| 743 | return false; |
| 744 | } |
| 745 | } else if (name == "-e") { |
| 746 | for (const auto& event_type : Split(*value.str_value, ",")) { |
Yabin Cui | 61b4dac | 2023-11-03 15:19:14 -0700 | [diff] [blame] | 747 | if (!probe_events.CreateProbeEventIfNotExist(event_type)) { |
| 748 | return false; |
| 749 | } |
Yabin Cui | dbf8280 | 2023-10-19 13:28:42 -0700 | [diff] [blame] | 750 | if (!event_selection_set_.AddEventType(event_type)) { |
| 751 | return false; |
| 752 | } |
| 753 | } |
| 754 | } else if (name == "--group") { |
Yabin Cui | 61b4dac | 2023-11-03 15:19:14 -0700 | [diff] [blame] | 755 | std::vector<std::string> event_types = Split(*value.str_value, ","); |
| 756 | for (const auto& event_type : event_types) { |
| 757 | if (!probe_events.CreateProbeEventIfNotExist(event_type)) { |
| 758 | return false; |
| 759 | } |
| 760 | } |
| 761 | if (!event_selection_set_.AddEventGroup(event_types)) { |
Yabin Cui | dbf8280 | 2023-10-19 13:28:42 -0700 | [diff] [blame] | 762 | return false; |
| 763 | } |
Yabin Cui | 6de79a0 | 2023-11-03 16:36:56 -0700 | [diff] [blame] | 764 | } else if (name == "--tp-filter") { |
| 765 | if (!event_selection_set_.SetTracepointFilter(*value.str_value)) { |
| 766 | return false; |
| 767 | } |
| 768 | } else { |
| 769 | LOG(ERROR) << "unprocessed option: " << name; |
| 770 | return false; |
Yabin Cui | dbf8280 | 2023-10-19 13:28:42 -0700 | [diff] [blame] | 771 | } |
| 772 | } |
Yabin Cui | 6f09467 | 2020-07-22 14:50:35 -0700 | [diff] [blame] | 773 | |
Yabin Cui | bc2a102 | 2016-08-29 12:33:17 -0700 | [diff] [blame] | 774 | if (system_wide_collection_ && event_selection_set_.HasMonitoredTarget()) { |
Yabin Cui | 877751b | 2016-06-13 18:03:47 -0700 | [diff] [blame] | 775 | LOG(ERROR) << "Stat system wide and existing processes/threads can't be " |
| 776 | "used at the same time."; |
Yabin Cui | b032de7 | 2015-06-17 21:15:09 -0700 | [diff] [blame] | 777 | return false; |
| 778 | } |
Yabin Cui | 8a599d7 | 2016-07-21 18:32:53 -0700 | [diff] [blame] | 779 | if (system_wide_collection_ && !IsRoot()) { |
| 780 | LOG(ERROR) << "System wide profiling needs root privilege."; |
| 781 | return false; |
| 782 | } |
Yabin Cui | b032de7 | 2015-06-17 21:15:09 -0700 | [diff] [blame] | 783 | |
Yabin Cui | f53f716 | 2020-06-19 15:16:31 -0700 | [diff] [blame] | 784 | if (report_per_core_ || report_per_thread_) { |
| 785 | summary_comparator_ = BuildSummaryComparator(sort_keys_, report_per_thread_, report_per_core_); |
| 786 | if (!summary_comparator_) { |
| 787 | return false; |
| 788 | } |
| 789 | } |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 790 | return true; |
| 791 | } |
| 792 | |
Yabin | 4ed8e11 | 2022-08-23 11:43:01 -0700 | [diff] [blame] | 793 | std::optional<bool> CheckHardwareCountersOnCpu(int cpu, size_t counters) { |
Yabin Cui | dbf8280 | 2023-10-19 13:28:42 -0700 | [diff] [blame] | 794 | if (counters == 0) { |
| 795 | return true; |
| 796 | } |
Yabin Cui | f1a1e48 | 2021-12-22 14:54:20 -0800 | [diff] [blame] | 797 | const EventType* event = FindEventTypeByName("cpu-cycles", true); |
| 798 | if (event == nullptr) { |
Yabin | 6b771a2 | 2022-08-18 17:01:54 -0700 | [diff] [blame] | 799 | return std::nullopt; |
Yabin Cui | f1a1e48 | 2021-12-22 14:54:20 -0800 | [diff] [blame] | 800 | } |
| 801 | perf_event_attr attr = CreateDefaultPerfEventAttr(*event); |
Yabin | 4ed8e11 | 2022-08-23 11:43:01 -0700 | [diff] [blame] | 802 | auto workload = Workload::CreateWorkload({"sleep", "0.1"}); |
| 803 | if (!workload || !workload->SetCpuAffinity(cpu)) { |
| 804 | return std::nullopt; |
| 805 | } |
| 806 | std::vector<std::unique_ptr<EventFd>> event_fds; |
| 807 | for (size_t i = 0; i < counters; i++) { |
| 808 | EventFd* group_event_fd = event_fds.empty() ? nullptr : event_fds[0].get(); |
| 809 | auto event_fd = |
| 810 | EventFd::OpenEventFile(attr, workload->GetPid(), cpu, group_event_fd, "cpu-cycles", false); |
| 811 | if (!event_fd) { |
Yabin Cui | f1a1e48 | 2021-12-22 14:54:20 -0800 | [diff] [blame] | 812 | return false; |
| 813 | } |
Yabin | 4ed8e11 | 2022-08-23 11:43:01 -0700 | [diff] [blame] | 814 | event_fds.emplace_back(std::move(event_fd)); |
| 815 | } |
| 816 | if (!workload->Start() || !workload->WaitChildProcess(true, nullptr)) { |
| 817 | return std::nullopt; |
| 818 | } |
| 819 | for (auto& event_fd : event_fds) { |
| 820 | PerfCounter counter; |
| 821 | if (!event_fd->ReadCounter(&counter)) { |
| 822 | return std::nullopt; |
Yabin Cui | f1a1e48 | 2021-12-22 14:54:20 -0800 | [diff] [blame] | 823 | } |
Yabin | 4ed8e11 | 2022-08-23 11:43:01 -0700 | [diff] [blame] | 824 | if (counter.time_enabled == 0 || counter.time_enabled > counter.time_running) { |
| 825 | return false; |
| 826 | } |
| 827 | } |
| 828 | return true; |
| 829 | } |
| 830 | |
| 831 | std::optional<size_t> GetHardwareCountersOnCpu(int cpu) { |
| 832 | size_t available_counters = 0; |
| 833 | while (true) { |
| 834 | std::optional<bool> result = CheckHardwareCountersOnCpu(cpu, available_counters + 1); |
| 835 | if (!result.has_value()) { |
| 836 | return std::nullopt; |
| 837 | } |
| 838 | if (!result.value()) { |
Yabin Cui | f1a1e48 | 2021-12-22 14:54:20 -0800 | [diff] [blame] | 839 | break; |
| 840 | } |
| 841 | available_counters++; |
| 842 | } |
Yabin | 6b771a2 | 2022-08-18 17:01:54 -0700 | [diff] [blame] | 843 | return available_counters; |
| 844 | } |
| 845 | |
Yabin Cui | 5aded99 | 2022-08-25 10:51:11 -0700 | [diff] [blame] | 846 | void StatCommand::PrintHardwareCounters() { |
Yabin | 6b771a2 | 2022-08-18 17:01:54 -0700 | [diff] [blame] | 847 | for (int cpu : GetOnlineCpus()) { |
| 848 | std::optional<size_t> counters = GetHardwareCountersOnCpu(cpu); |
| 849 | if (!counters) { |
Yabin Cui | 5aded99 | 2022-08-25 10:51:11 -0700 | [diff] [blame] | 850 | // When built as a 32-bit program, we can't set sched_affinity to a 64-bit only CPU. So we |
| 851 | // may not be able to get hardware counters on that CPU. |
| 852 | LOG(WARNING) << "Failed to get CPU PMU hardware counters on cpu " << cpu; |
| 853 | continue; |
Yabin | 6b771a2 | 2022-08-18 17:01:54 -0700 | [diff] [blame] | 854 | } |
| 855 | printf("There are %zu CPU PMU hardware counters available on cpu %d.\n", counters.value(), cpu); |
| 856 | } |
Yabin Cui | f1a1e48 | 2021-12-22 14:54:20 -0800 | [diff] [blame] | 857 | } |
| 858 | |
Yabin Cui | f79f07e | 2015-06-01 11:21:37 -0700 | [diff] [blame] | 859 | bool StatCommand::AddDefaultMeasuredEventTypes() { |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 860 | for (auto& name : default_measured_event_types) { |
Yabin Cui | 877751b | 2016-06-13 18:03:47 -0700 | [diff] [blame] | 861 | // It is not an error when some event types in the default list are not |
| 862 | // supported by the kernel. |
Yabin Cui | 9fd3cc1 | 2015-06-25 17:42:23 -0700 | [diff] [blame] | 863 | const EventType* type = FindEventTypeByName(name); |
Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 864 | if (type != nullptr && IsEventAttrSupported(CreateDefaultPerfEventAttr(*type), name)) { |
Yabin Cui | 877751b | 2016-06-13 18:03:47 -0700 | [diff] [blame] | 865 | if (!event_selection_set_.AddEventType(name)) { |
| 866 | return false; |
| 867 | } |
Yabin Cui | 9fd3cc1 | 2015-06-25 17:42:23 -0700 | [diff] [blame] | 868 | } |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 869 | } |
Yabin Cui | 877751b | 2016-06-13 18:03:47 -0700 | [diff] [blame] | 870 | if (event_selection_set_.empty()) { |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 871 | LOG(ERROR) << "Failed to add any supported default measured types"; |
| 872 | return false; |
| 873 | } |
| 874 | return true; |
| 875 | } |
| 876 | |
Yabin Cui | 877751b | 2016-06-13 18:03:47 -0700 | [diff] [blame] | 877 | void StatCommand::SetEventSelectionFlags() { |
Yabin Cui | 4be4126 | 2015-06-22 14:23:01 -0700 | [diff] [blame] | 878 | event_selection_set_.SetInherit(child_inherit_); |
| 879 | } |
| 880 | |
Yabin Cui | dc2708c | 2020-01-10 15:33:11 -0800 | [diff] [blame] | 881 | void StatCommand::MonitorEachThread() { |
| 882 | std::vector<pid_t> threads; |
| 883 | for (auto pid : event_selection_set_.GetMonitoredProcesses()) { |
| 884 | for (auto tid : GetThreadsInProcess(pid)) { |
| 885 | ThreadInfo info; |
| 886 | if (GetThreadName(tid, &info.name)) { |
| 887 | info.tid = tid; |
| 888 | info.pid = pid; |
| 889 | thread_info_[tid] = std::move(info); |
| 890 | threads.push_back(tid); |
| 891 | } |
| 892 | } |
| 893 | } |
| 894 | for (auto tid : event_selection_set_.GetMonitoredThreads()) { |
| 895 | ThreadInfo info; |
| 896 | if (ReadThreadNameAndPid(tid, &info.name, &info.pid)) { |
| 897 | info.tid = tid; |
| 898 | thread_info_[tid] = std::move(info); |
| 899 | threads.push_back(tid); |
| 900 | } |
| 901 | } |
| 902 | event_selection_set_.ClearMonitoredTargets(); |
| 903 | event_selection_set_.AddMonitoredThreads(threads); |
| 904 | } |
| 905 | |
Yabin Cui | 8ab5621 | 2020-01-27 15:23:46 -0800 | [diff] [blame] | 906 | void StatCommand::AdjustToIntervalOnlyValues(std::vector<CountersInfo>& counters) { |
| 907 | if (last_sum_values_.size() < counters.size()) { |
| 908 | last_sum_values_.resize(counters.size()); |
| 909 | } |
| 910 | for (size_t i = 0; i < counters.size(); i++) { |
| 911 | std::vector<CounterInfo>& counters_per_event = counters[i].counters; |
| 912 | std::vector<CounterSum>& last_sum = last_sum_values_[i]; |
| 913 | |
| 914 | if (last_sum.size() < counters_per_event.size()) { |
| 915 | last_sum.resize(counters_per_event.size()); |
| 916 | } |
| 917 | for (size_t j = 0; j < counters_per_event.size(); j++) { |
| 918 | PerfCounter& counter = counters_per_event[j].counter; |
Yabin Cui | b3ed39b | 2020-03-26 14:00:49 -0700 | [diff] [blame] | 919 | CounterSum new_sum; |
| 920 | new_sum.FromCounter(counter); |
| 921 | CounterSum delta = new_sum - last_sum[j]; |
| 922 | delta.ToCounter(counter); |
| 923 | last_sum[j] = new_sum; |
Yabin Cui | 8ab5621 | 2020-01-27 15:23:46 -0800 | [diff] [blame] | 924 | } |
| 925 | } |
| 926 | } |
| 927 | |
Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 928 | bool StatCommand::ShowCounters(const std::vector<CountersInfo>& counters, double duration_in_sec, |
| 929 | FILE* fp) { |
Zhiting Zhu | 0979ba8 | 2016-07-09 19:38:08 -0700 | [diff] [blame] | 930 | if (csv_) { |
| 931 | fprintf(fp, "Performance counter statistics,\n"); |
| 932 | } else { |
| 933 | fprintf(fp, "Performance counter statistics:\n\n"); |
| 934 | } |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 935 | |
Yabin Cui | 04d08a3 | 2015-08-19 15:01:12 -0700 | [diff] [blame] | 936 | if (verbose_mode_) { |
| 937 | for (auto& counters_info : counters) { |
Yabin Cui | 04d08a3 | 2015-08-19 15:01:12 -0700 | [diff] [blame] | 938 | for (auto& counter_info : counters_info.counters) { |
Zhiting Zhu | 0979ba8 | 2016-07-09 19:38:08 -0700 | [diff] [blame] | 939 | if (csv_) { |
Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 940 | fprintf(fp, |
| 941 | "%s,tid,%d,cpu,%d,count,%" PRIu64 ",time_enabled,%" PRIu64 |
| 942 | ",time running,%" PRIu64 ",id,%" PRIu64 ",\n", |
| 943 | counters_info.event_name.c_str(), counter_info.tid, counter_info.cpu, |
| 944 | counter_info.counter.value, counter_info.counter.time_enabled, |
Zhiting Zhu | 0979ba8 | 2016-07-09 19:38:08 -0700 | [diff] [blame] | 945 | counter_info.counter.time_running, counter_info.counter.id); |
| 946 | } else { |
| 947 | fprintf(fp, |
| 948 | "%s(tid %d, cpu %d): count %" PRIu64 ", time_enabled %" PRIu64 |
| 949 | ", time running %" PRIu64 ", id %" PRIu64 "\n", |
Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 950 | counters_info.event_name.c_str(), counter_info.tid, counter_info.cpu, |
| 951 | counter_info.counter.value, counter_info.counter.time_enabled, |
Zhiting Zhu | 0979ba8 | 2016-07-09 19:38:08 -0700 | [diff] [blame] | 952 | counter_info.counter.time_running, counter_info.counter.id); |
| 953 | } |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 954 | } |
| 955 | } |
Yabin Cui | 04d08a3 | 2015-08-19 15:01:12 -0700 | [diff] [blame] | 956 | } |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 957 | |
Yabin Cui | f53f716 | 2020-06-19 15:16:31 -0700 | [diff] [blame] | 958 | CounterSummaryBuilder builder(report_per_thread_, report_per_core_, csv_, thread_info_, |
| 959 | summary_comparator_); |
Yabin Cui | 8ab5621 | 2020-01-27 15:23:46 -0800 | [diff] [blame] | 960 | for (const auto& info : counters) { |
Yabin Cui | b3ed39b | 2020-03-26 14:00:49 -0700 | [diff] [blame] | 961 | builder.AddCountersForOneEventType(info); |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 962 | } |
Yabin Cui | b3ed39b | 2020-03-26 14:00:49 -0700 | [diff] [blame] | 963 | CounterSummaries summaries(builder.Build(), csv_); |
Yabin Cui | 0a45adf | 2016-06-22 20:58:52 -0700 | [diff] [blame] | 964 | summaries.AutoGenerateSummaries(); |
| 965 | summaries.GenerateComments(duration_in_sec); |
| 966 | summaries.Show(fp); |
Yabin Cui | 04d08a3 | 2015-08-19 15:01:12 -0700 | [diff] [blame] | 967 | |
Yabin | 4ed8e11 | 2022-08-23 11:43:01 -0700 | [diff] [blame] | 968 | if (csv_) { |
Zhiting Zhu | 0979ba8 | 2016-07-09 19:38:08 -0700 | [diff] [blame] | 969 | fprintf(fp, "Total test time,%lf,seconds,\n", duration_in_sec); |
Yabin Cui | 8ab5621 | 2020-01-27 15:23:46 -0800 | [diff] [blame] | 970 | } else { |
Yabin | 4ed8e11 | 2022-08-23 11:43:01 -0700 | [diff] [blame] | 971 | fprintf(fp, "\nTotal test time: %lf seconds.\n", duration_in_sec); |
Yabin Cui | 52f32a4 | 2019-08-12 16:53:46 -0700 | [diff] [blame] | 972 | } |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 973 | return true; |
| 974 | } |
| 975 | |
Yabin | 4ed8e11 | 2022-08-23 11:43:01 -0700 | [diff] [blame] | 976 | void StatCommand::CheckHardwareCounterMultiplexing() { |
Yabin Cui | dbf8280 | 2023-10-19 13:28:42 -0700 | [diff] [blame] | 977 | for (const auto& [cpu, hardware_events] : event_selection_set_.GetHardwareCountersForCpus()) { |
Yabin | 4ed8e11 | 2022-08-23 11:43:01 -0700 | [diff] [blame] | 978 | std::optional<bool> result = CheckHardwareCountersOnCpu(cpu, hardware_events); |
| 979 | if (result.has_value() && !result.value()) { |
| 980 | LOG(WARNING) << "It seems the number of hardware events are more than the number of\n" |
| 981 | << "available CPU PMU hardware counters. That will trigger hardware counter\n" |
| 982 | << "multiplexing. As a result, events are not counted all the time processes\n" |
| 983 | << "running, and event counts are smaller than what really happen.\n" |
| 984 | << "Use --print-hw-counter to show available hardware counters.\n" |
| 985 | #if defined(__ANDROID__) |
| 986 | << "If on a rooted device, try --use-devfreq-counters to get more counters.\n" |
| 987 | #endif |
| 988 | ; |
| 989 | break; |
| 990 | } |
| 991 | } |
| 992 | } |
| 993 | |
Yabin Cui | 6a2fda4 | 2022-09-01 11:52:51 -0700 | [diff] [blame] | 994 | void StatCommand::PrintWarningForInaccurateEvents() { |
| 995 | for (const EventType* event : event_selection_set_.GetEvents()) { |
| 996 | if (event->name == "raw-l3d-cache-lmiss-rd") { |
| 997 | LOG(WARNING) << "PMU event L3D_CACHE_LMISS_RD might undercount on A510. Please use " |
| 998 | "L3D_CACHE_REFILL_RD instead."; |
| 999 | break; |
| 1000 | } |
| 1001 | } |
| 1002 | } |
| 1003 | |
Yabin Cui | 877751b | 2016-06-13 18:03:47 -0700 | [diff] [blame] | 1004 | } // namespace |
| 1005 | |
Yabin Cui | ff7465c | 2016-02-25 11:02:30 -0800 | [diff] [blame] | 1006 | void RegisterStatCommand() { |
Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 1007 | RegisterCommand("stat", [] { return std::unique_ptr<Command>(new StatCommand); }); |
Yabin Cui | f79f07e | 2015-06-01 11:21:37 -0700 | [diff] [blame] | 1008 | } |
Yabin Cui | acbdb24 | 2020-07-07 15:56:34 -0700 | [diff] [blame] | 1009 | |
| 1010 | } // namespace simpleperf |