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