blob: 009afc3c5681c2d5727554b96783f09412d8e521 [file] [log] [blame]
Yabin Cui323e9452015-04-20 18:07:17 -07001/*
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 Cui621a5332015-06-15 16:17:20 -070018#include <signal.h>
Yabin Cui323e9452015-04-20 18:07:17 -070019#include <stdio.h>
Yabin Cui04d08a32015-08-19 15:01:12 -070020#include <string.h>
Yabin Cui987d9ab2016-07-15 14:08:48 -070021#include <sys/prctl.h>
Yabin Cui04d08a32015-08-19 15:01:12 -070022
23#include <algorithm>
Yabin Cui323e9452015-04-20 18:07:17 -070024#include <chrono>
Yabin Cuif53f7162020-06-19 15:16:31 -070025#include <optional>
Yabin Cuib032de72015-06-17 21:15:09 -070026#include <set>
Yabin Cui323e9452015-04-20 18:07:17 -070027#include <string>
Yabin Cui117caa42019-07-15 10:39:15 -070028#include <string_view>
Yabin Cui323e9452015-04-20 18:07:17 -070029#include <vector>
30
Yabin Cuia80f8f72017-07-12 15:50:20 -070031#include <android-base/file.h>
Elliott Hughes66dd09e2015-12-04 14:00:57 -080032#include <android-base/logging.h>
33#include <android-base/strings.h>
Yabin Cui1a30a582019-01-10 15:35:39 -080034#include <android-base/unique_fd.h>
Yabin Cui323e9452015-04-20 18:07:17 -070035
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +020036#include "IOEventLoop.h"
Yabin Cuib3ed39b2020-03-26 14:00:49 -070037#include "cmd_stat_impl.h"
Yabin Cui323e9452015-04-20 18:07:17 -070038#include "command.h"
39#include "environment.h"
Yabin Cui9fd3cc12015-06-25 17:42:23 -070040#include "event_attr.h"
41#include "event_fd.h"
Yabin Cui9759e1b2015-04-28 15:54:13 -070042#include "event_selection_set.h"
Yabin Cui323e9452015-04-20 18:07:17 -070043#include "event_type.h"
Yabin Cui621a5332015-06-15 16:17:20 -070044#include "utils.h"
Yabin Cui323e9452015-04-20 18:07:17 -070045#include "workload.h"
46
Yabin Cui5271aa72020-03-31 16:59:33 -070047namespace simpleperf {
Yabin Cui877751b2016-06-13 18:03:47 -070048
Yabin Cuifaa7b922021-01-11 17:35:57 -080049using android::base::Split;
50
Yabin Cui323e9452015-04-20 18:07:17 -070051static std::vector<std::string> default_measured_event_types{
Yabin Cuif569b472015-04-30 09:43:26 -070052 "cpu-cycles", "stalled-cycles-frontend", "stalled-cycles-backend",
53 "instructions", "branch-instructions", "branch-misses",
54 "task-clock", "context-switches", "page-faults",
Yabin Cui323e9452015-04-20 18:07:17 -070055};
56
Yabin Cui117caa42019-07-15 10:39:15 -070057static 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
63static 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 Cui5271aa72020-03-31 16:59:33 -070094const 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 Cui0a45adf2016-06-22 20:58:52 -0700100 }
Yabin Cui0a45adf2016-06-22 20:58:52 -0700101 }
Yabin Cui5271aa72020-03-31 16:59:33 -0700102 return nullptr;
103}
Yabin Cui0a45adf2016-06-22 20:58:52 -0700104
Yabin Cui5271aa72020-03-31 16:59:33 -0700105void 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 Cui6922c072020-03-31 18:04:59 -0700113 s.count + other->count, s.runtime_in_ns, s.scale, true, csv_);
Yabin Cui0a45adf2016-06-22 20:58:52 -0700114 }
115 }
116 }
117 }
Yabin Cui5271aa72020-03-31 16:59:33 -0700118}
Yabin Cui0a45adf2016-06-22 20:58:52 -0700119
Yabin Cui5271aa72020-03-31 16:59:33 -0700120void CounterSummaries::GenerateComments(double duration_in_sec) {
121 for (auto& s : summaries_) {
122 s.comment = GetCommentForSummary(s, duration_in_sec);
123 }
124}
125
126void CounterSummaries::Show(FILE* fp) {
Yabin Cuif53f7162020-06-19 15:16:31 -0700127 bool show_thread = !summaries_.empty() && summaries_[0].thread != nullptr;
128 bool show_cpu = !summaries_.empty() && summaries_[0].cpu != -1;
Yabin Cui5271aa72020-03-31 16:59:33 -0700129 if (csv_) {
Yabin Cuif53f7162020-06-19 15:16:31 -0700130 ShowCSV(fp, show_thread, show_cpu);
Yabin Cui5271aa72020-03-31 16:59:33 -0700131 } else {
Yabin Cuif53f7162020-06-19 15:16:31 -0700132 ShowText(fp, show_thread, show_cpu);
Yabin Cui5271aa72020-03-31 16:59:33 -0700133 }
134}
135
Yabin Cuif53f7162020-06-19 15:16:31 -0700136void CounterSummaries::ShowCSV(FILE* fp, bool show_thread, bool show_cpu) {
Yabin Cui5271aa72020-03-31 16:59:33 -0700137 for (auto& s : summaries_) {
Yabin Cuif53f7162020-06-19 15:16:31 -0700138 if (show_thread) {
Yabin Cui5271aa72020-03-31 16:59:33 -0700139 fprintf(fp, "%s,%d,%d,", s.thread->name.c_str(), s.thread->pid, s.thread->tid);
Yabin Cui0a45adf2016-06-22 20:58:52 -0700140 }
Yabin Cuif53f7162020-06-19 15:16:31 -0700141 if (show_cpu) {
142 fprintf(fp, "%d,", s.cpu);
143 }
Yabin Cuiabc8ee22022-08-25 11:06:39 -0700144 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 Cui5271aa72020-03-31 16:59:33 -0700146 }
147}
148
Yabin Cuif53f7162020-06-19 15:16:31 -0700149void CounterSummaries::ShowText(FILE* fp, bool show_thread, bool show_cpu) {
Yabin Cui5271aa72020-03-31 16:59:33 -0700150 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");
Yabin4ed8e112022-08-23 11:43:01 -0700160 titles.emplace_back(" # count / runtime");
Yabin Cui5271aa72020-03-31 16:59:33 -0700161
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 Cui0a45adf2016-06-22 20:58:52 -0700169 }
170
Yabin Cui5271aa72020-03-31 16:59:33 -0700171 for (auto& s : summaries_) {
172 size_t i = 0;
Yabin Cuidc2708c2020-01-10 15:33:11 -0800173 if (show_thread) {
Yabin Cui5271aa72020-03-31 16:59:33 -0700174 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 Cui8ab56212020-01-27 15:23:46 -0800177 }
178 if (show_cpu) {
Yabin Cui5271aa72020-03-31 16:59:33 -0700179 adjust_width(width[i++], std::to_string(s.cpu).size());
Yabin Cui8ab56212020-01-27 15:23:46 -0800180 }
Yabin Cui5271aa72020-03-31 16:59:33 -0700181 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 Cui0a45adf2016-06-22 20:58:52 -0700184 }
185
Yabin Cui5271aa72020-03-31 16:59:33 -0700186 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 Zhu0979ba82016-07-09 19:38:08 -0700190 } else {
Yabin Cui5271aa72020-03-31 16:59:33 -0700191 fprintf(fp, "%-*s", static_cast<int>(width[i]), titles[i].c_str());
Zhiting Zhu0979ba82016-07-09 19:38:08 -0700192 }
Yabin Cui5271aa72020-03-31 16:59:33 -0700193 if (i + 1 < titles.size()) {
194 fprintf(fp, " ");
Yabin Cui0a45adf2016-06-22 20:58:52 -0700195 }
Yabin Cui5271aa72020-03-31 16:59:33 -0700196 }
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 Cui0a45adf2016-06-22 20:58:52 -0700205 }
Yabin Cui5271aa72020-03-31 16:59:33 -0700206 if (show_cpu) {
207 fprintf(fp, " %-*d", static_cast<int>(width[i++]), s.cpu);
Yabin Cui0a45adf2016-06-22 20:58:52 -0700208 }
Yabin4ed8e112022-08-23 11:43:01 -0700209 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 Cui5271aa72020-03-31 16:59:33 -0700212 }
213}
214
215std::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 Cui6922c072020-03-31 18:04:59 -0700225 double used_cpus = run_sec / duration_in_sec;
226 return android::base::StringPrintf("%f%ccpus used", used_cpus, sap_mid);
Yabin Cui5271aa72020-03-31 16:59:33 -0700227 }
228 if (s.type_name == "cpu-clock") {
229 return "";
230 }
231 if (s.type_name == "cpu-cycles") {
Yabin Cui6922c072020-03-31 18:04:59 -0700232 if (s.runtime_in_ns == 0) {
Yabin Cui900ea072018-03-06 11:35:22 -0800233 return "";
234 }
Yabin Cui6922c072020-03-31 18:04:59 -0700235 double ghz = static_cast<double>(s.count) / s.runtime_in_ns;
236 return android::base::StringPrintf("%f%cGHz", ghz, sap_mid);
Yabin Cui0a45adf2016-06-22 20:58:52 -0700237 }
Yabin Cui5271aa72020-03-31 16:59:33 -0700238 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 Cui6922c072020-03-31 18:04:59 -0700242 return android::base::StringPrintf("%f%ccycles per instruction", cpi, sap_mid);
Yabin Cui5271aa72020-03-31 16:59:33 -0700243 }
244 }
245 std::string rate_comment = GetRateComment(s, sap_mid);
246 if (!rate_comment.empty()) {
247 return rate_comment;
248 }
Yabin Cui6922c072020-03-31 18:04:59 -0700249 if (s.runtime_in_ns == 0) {
Yabin Cui5271aa72020-03-31 16:59:33 -0700250 return "";
251 }
Yabin Cui6922c072020-03-31 18:04:59 -0700252 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 Cui5271aa72020-03-31 16:59:33 -0700256 }
Yabin Cui6922c072020-03-31 18:04:59 -0700257 if (rate >= 1e6 - 1e2) {
258 return android::base::StringPrintf("%.3f%cM/sec", rate / 1e6, sap_mid);
Yabin Cui5271aa72020-03-31 16:59:33 -0700259 }
Yabin Cui6922c072020-03-31 18:04:59 -0700260 if (rate >= 1e3) {
261 return android::base::StringPrintf("%.3f%cK/sec", rate / 1e3, sap_mid);
Yabin Cui5271aa72020-03-31 16:59:33 -0700262 }
Yabin Cui6922c072020-03-31 18:04:59 -0700263 return android::base::StringPrintf("%.3f%c/sec", rate, sap_mid);
Yabin Cui5271aa72020-03-31 16:59:33 -0700264}
Yabin Cui0a45adf2016-06-22 20:58:52 -0700265
Yabin Cui5271aa72020-03-31 16:59:33 -0700266std::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 Cui38076912021-08-16 16:59:09 -0700274 if (event_name.empty() && (GetTargetArch() == ARCH_ARM || GetTargetArch() == ARCH_ARM64)) {
Yabin Cui5271aa72020-03-31 16:59:33 -0700275 if (auto it = ARM_EVENT_RATE_MAP.find(miss_event_name); it != ARM_EVENT_RATE_MAP.end()) {
Yabin Cui117caa42019-07-15 10:39:15 -0700276 event_name = it->second.first;
277 rate_desc = it->second.second;
278 }
Yabin Cui117caa42019-07-15 10:39:15 -0700279 }
Yabin Cui5271aa72020-03-31 16:59:33 -0700280 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 Cui900ea072018-03-06 11:35:22 -0800283 }
Yabin Cui5271aa72020-03-31 16:59:33 -0700284 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 Cui900ea072018-03-06 11:35:22 -0800293
Yabin Cui5271aa72020-03-31 16:59:33 -0700294namespace {
Yabin Cui0a45adf2016-06-22 20:58:52 -0700295
Yabin Cui52f32a42019-08-12 16:53:46 -0700296// 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.
299class 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 Cuif79f07e2015-06-01 11:21:37 -0700338class StatCommand : public Command {
Yabin Cui323e9452015-04-20 18:07:17 -0700339 public:
Yabin Cuif79f07e2015-06-01 11:21:37 -0700340 StatCommand()
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +0200341 : Command(
342 "stat", "gather performance counter information",
343 // clang-format off
Yabin Cui877751b2016-06-13 18:03:47 -0700344"Usage: simpleperf stat [options] [command [command-args]]\n"
Yabin Cui8a599d72016-07-21 18:32:53 -0700345" 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 Cui877751b2016-06-13 18:03:47 -0700347"-a Collect system-wide information.\n"
Yabin Cuia80f8f72017-07-12 15:50:20 -0700348#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 Cui877751b2016-06-13 18:03:47 -0700353"--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 Zhu0979ba82016-07-09 19:38:08 -0700356"--csv Write report in comma separate form.\n"
Yabin Cui8a599d72016-07-21 18:32:53 -0700357"--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 Wang539fb882016-09-28 14:42:02 -0700360"--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 Cui9267db22018-04-18 11:17:19 -0700362" 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 Cui877751b2016-06-13 18:03:47 -0700366"-e event1[:modifier1],event2[:modifier2],...\n"
Yabin Cui5e8ee332018-05-10 15:38:00 -0700367" 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 Cui877751b2016-06-13 18:03:47 -0700373" 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 Cui0a45adf2016-06-22 20:58:52 -0700380"-o output_filename Write report to output_filename instead of standard output.\n"
Yabin Cui8ab56212020-01-27 15:23:46 -0800381"--per-core Print counters for each cpu core.\n"
Yabin Cuidc2708c2020-01-10 15:33:11 -0800382"--per-thread Print counters for each thread.\n"
Yabin Cui1c6be752023-02-28 11:46:37 -0800383"-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 Cuif1a1e482021-12-22 14:54:20 -0800387"--print-hw-counter Test and print CPU PMU hardware counters available on the device.\n"
Yabin Cuif53f7162020-06-19 15:16:31 -0700388"--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 Cui52f32a42019-08-12 16:53:46 -0700400#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 Cui877751b2016-06-13 18:03:47 -0700408"--verbose Show result in verbose mode.\n"
Yabin Cuia80f8f72017-07-12 15:50:20 -0700409#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 Cuif8974522017-07-17 14:36:37 -0700412"--tracepoint-events file_name Read tracepoint events from [file_name] instead of tracefs.\n"
Yabin Cui1a30a582019-01-10 15:35:39 -0800413"--out-fd <fd> Write output to a file descriptor.\n"
414"--stop-signal-fd <fd> Stop stating when fd is readable.\n"
Yabin Cuia80f8f72017-07-12 15:50:20 -0700415#endif
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +0200416 // clang-format on
417 ),
Yabin Cuif79f07e2015-06-01 11:21:37 -0700418 verbose_mode_(false),
Yabin Cui4be41262015-06-22 14:23:01 -0700419 system_wide_collection_(false),
Zhiting Zhu0979ba82016-07-09 19:38:08 -0700420 child_inherit_(true),
Yabin Cui3e4c5952016-07-26 15:03:27 -0700421 duration_in_sec_(0),
Wei Wang539fb882016-09-28 14:42:02 -0700422 interval_in_ms_(0),
Yabin Cui9267db22018-04-18 11:17:19 -0700423 interval_only_values_(false),
Yabin Cui4cf37d12016-08-19 15:42:39 -0700424 event_selection_set_(true),
Yabin Cuia80f8f72017-07-12 15:50:20 -0700425 csv_(false),
426 in_app_context_(false) {
Yabin Cui987d9ab2016-07-15 14:08:48 -0700427 // Die if parent exits.
428 prctl(PR_SET_PDEATHSIG, SIGHUP, 0, 0, 0);
Yabin Cuif53f7162020-06-19 15:16:31 -0700429 // Set default sort keys. Full key list is in BuildSummaryComparator().
430 sort_keys_ = {"count_per_thread", "tid", "cpu", "count"};
Yabin Cui323e9452015-04-20 18:07:17 -0700431 }
432
433 bool Run(const std::vector<std::string>& args);
434
435 private:
Yabin Cui877751b2016-06-13 18:03:47 -0700436 bool ParseOptions(const std::vector<std::string>& args,
437 std::vector<std::string>* non_option_args);
Yabin Cui5aded992022-08-25 10:51:11 -0700438 void PrintHardwareCounters();
Yabin Cui323e9452015-04-20 18:07:17 -0700439 bool AddDefaultMeasuredEventTypes();
Yabin Cui877751b2016-06-13 18:03:47 -0700440 void SetEventSelectionFlags();
Yabin Cuidc2708c2020-01-10 15:33:11 -0800441 void MonitorEachThread();
Yabin Cui8ab56212020-01-27 15:23:46 -0800442 void AdjustToIntervalOnlyValues(std::vector<CountersInfo>& counters);
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +0200443 bool ShowCounters(const std::vector<CountersInfo>& counters, double duration_in_sec, FILE* fp);
Yabin4ed8e112022-08-23 11:43:01 -0700444 void CheckHardwareCounterMultiplexing();
Yabin Cui6a2fda42022-09-01 11:52:51 -0700445 void PrintWarningForInaccurateEvents();
Yabin Cui323e9452015-04-20 18:07:17 -0700446
Yabin Cui323e9452015-04-20 18:07:17 -0700447 bool verbose_mode_;
448 bool system_wide_collection_;
Yabin Cui4be41262015-06-22 14:23:01 -0700449 bool child_inherit_;
Yabin Cui3e4c5952016-07-26 15:03:27 -0700450 double duration_in_sec_;
Wei Wang539fb882016-09-28 14:42:02 -0700451 double interval_in_ms_;
Yabin Cui9267db22018-04-18 11:17:19 -0700452 bool interval_only_values_;
Yabin Cui8ab56212020-01-27 15:23:46 -0800453 std::vector<std::vector<CounterSum>> last_sum_values_;
Yabin Cuicb4c17e2015-10-26 16:15:29 -0700454 std::vector<int> cpus_;
Yabin Cui4be41262015-06-22 14:23:01 -0700455 EventSelectionSet event_selection_set_;
Yabin Cui0a45adf2016-06-22 20:58:52 -0700456 std::string output_filename_;
Yabin Cui1a30a582019-01-10 15:35:39 -0800457 android::base::unique_fd out_fd_;
Zhiting Zhu0979ba82016-07-09 19:38:08 -0700458 bool csv_;
Yabin Cuia80f8f72017-07-12 15:50:20 -0700459 std::string app_package_name_;
460 bool in_app_context_;
Yabin Cui1a30a582019-01-10 15:35:39 -0800461 android::base::unique_fd stop_signal_fd_;
Yabin Cui52f32a42019-08-12 16:53:46 -0700462 bool use_devfreq_counters_ = false;
Yabin Cuidc2708c2020-01-10 15:33:11 -0800463
Yabin Cui8ab56212020-01-27 15:23:46 -0800464 bool report_per_core_ = false;
Yabin Cuidc2708c2020-01-10 15:33:11 -0800465 bool report_per_thread_ = false;
Yabin Cui8ab56212020-01-27 15:23:46 -0800466 // used to report event count for each thread
Yabin Cuidc2708c2020-01-10 15:33:11 -0800467 std::unordered_map<pid_t, ThreadInfo> thread_info_;
Yabin Cuif53f7162020-06-19 15:16:31 -0700468 // used to sort report
469 std::vector<std::string> sort_keys_;
470 std::optional<SummaryComparator> summary_comparator_;
Yabin Cuif1a1e482021-12-22 14:54:20 -0800471 bool print_hw_counter_ = false;
Yabin Cui323e9452015-04-20 18:07:17 -0700472};
473
Yabin Cuif79f07e2015-06-01 11:21:37 -0700474bool StatCommand::Run(const std::vector<std::string>& args) {
Yabin Cuiebf79f32016-06-01 15:39:39 -0700475 if (!CheckPerfEventLimit()) {
476 return false;
477 }
Yabin Cuidc2708c2020-01-10 15:33:11 -0800478 AllowMoreOpenedFiles();
Yabin Cuiebf79f32016-06-01 15:39:39 -0700479
Yabin Cui4be41262015-06-22 14:23:01 -0700480 // 1. Parse options, and use default measured event types if not given.
Yabin Cui323e9452015-04-20 18:07:17 -0700481 std::vector<std::string> workload_args;
482 if (!ParseOptions(args, &workload_args)) {
483 return false;
484 }
Yabin Cuif1a1e482021-12-22 14:54:20 -0800485 if (print_hw_counter_) {
Yabin Cui5aded992022-08-25 10:51:11 -0700486 PrintHardwareCounters();
487 return true;
Yabin Cuif1a1e482021-12-22 14:54:20 -0800488 }
Yabin Cuia80f8f72017-07-12 15:50:20 -0700489 if (!app_package_name_.empty() && !in_app_context_) {
490 if (!IsRoot()) {
491 return RunInAppContext(app_package_name_, "stat", args, workload_args.size(),
Yabin Cuif8974522017-07-17 14:36:37 -0700492 output_filename_, !event_selection_set_.GetTracepointEvents().empty());
Yabin Cuia80f8f72017-07-12 15:50:20 -0700493 }
494 }
Yabin Cui52f32a42019-08-12 16:53:46 -0700495 DevfreqCounters devfreq_counters;
496 if (use_devfreq_counters_) {
497 if (!devfreq_counters.Use()) {
498 return false;
499 }
500 }
Yabin Cui877751b2016-06-13 18:03:47 -0700501 if (event_selection_set_.empty()) {
Yabin Cui323e9452015-04-20 18:07:17 -0700502 if (!AddDefaultMeasuredEventTypes()) {
503 return false;
504 }
505 }
Yabin Cui877751b2016-06-13 18:03:47 -0700506 SetEventSelectionFlags();
Yabin Cui323e9452015-04-20 18:07:17 -0700507
Yabin Cui4be41262015-06-22 14:23:01 -0700508 // 2. Create workload.
Yabin Cuib032de72015-06-17 21:15:09 -0700509 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 Cui323e9452015-04-20 18:07:17 -0700515 }
Yabin Cui5f43fc42016-12-13 13:47:49 -0800516 bool need_to_check_targets = false;
Yabin Cuibc2a1022016-08-29 12:33:17 -0700517 if (system_wide_collection_) {
Yabin Cuidc2708c2020-01-10 15:33:11 -0800518 if (report_per_thread_) {
519 event_selection_set_.AddMonitoredProcesses(GetAllProcesses());
520 } else {
521 event_selection_set_.AddMonitoredThreads({-1});
522 }
Yabin Cuibc2a1022016-08-29 12:33:17 -0700523 } else if (!event_selection_set_.HasMonitoredTarget()) {
Yabin Cuib032de72015-06-17 21:15:09 -0700524 if (workload != nullptr) {
Yabin Cuibc2a1022016-08-29 12:33:17 -0700525 event_selection_set_.AddMonitoredProcesses({workload->GetPid()});
Yabin Cuib032de72015-06-17 21:15:09 -0700526 event_selection_set_.SetEnableOnExec(true);
Yabin Cuia80f8f72017-07-12 15:50:20 -0700527 } else if (!app_package_name_.empty()) {
Yabin Cui7cb6f292017-08-28 14:49:04 -0700528 std::set<pid_t> pids = WaitForAppProcesses(app_package_name_);
529 event_selection_set_.AddMonitoredProcesses(pids);
Yabin Cuib032de72015-06-17 21:15:09 -0700530 } else {
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +0200531 LOG(ERROR) << "No threads to monitor. Try `simpleperf help stat` for help\n";
Yabin Cuib032de72015-06-17 21:15:09 -0700532 return false;
533 }
Yabin Cui5f43fc42016-12-13 13:47:49 -0800534 } else {
535 need_to_check_targets = true;
Yabin Cui323e9452015-04-20 18:07:17 -0700536 }
537
Yabin Cuidc2708c2020-01-10 15:33:11 -0800538 if (report_per_thread_) {
539 MonitorEachThread();
540 }
541
Wei Wang539fb882016-09-28 14:42:02 -0700542 // 3. Open perf_event_files and output file if defined.
Yabin Cuibc2a1022016-08-29 12:33:17 -0700543 if (!event_selection_set_.OpenEventFiles(cpus_)) {
544 return false;
Yabin Cui323e9452015-04-20 18:07:17 -0700545 }
Wei Wang539fb882016-09-28 14:42:02 -0700546 std::unique_ptr<FILE, decltype(&fclose)> fp_holder(nullptr, fclose);
Wei Wang539fb882016-09-28 14:42:02 -0700547 if (!output_filename_.empty()) {
Yabin Cui1a30a582019-01-10 15:35:39 -0800548 fp_holder.reset(fopen(output_filename_.c_str(), "we"));
Wei Wang539fb882016-09-28 14:42:02 -0700549 if (fp_holder == nullptr) {
550 PLOG(ERROR) << "failed to open " << output_filename_;
551 return false;
552 }
Yabin Cui1a30a582019-01-10 15:35:39 -0800553 } 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 Wang539fb882016-09-28 14:42:02 -0700559 }
Yabin Cui1a30a582019-01-10 15:35:39 -0800560 FILE* fp = fp_holder ? fp_holder.get() : stdout;
Yabin Cui323e9452015-04-20 18:07:17 -0700561
Yabin Cui26968e62017-01-30 11:34:24 -0800562 // 4. Add signal/periodic Events.
Yabin Cuic29e1722018-02-14 13:41:03 -0800563 IOEventLoop* loop = event_selection_set_.GetIOEventLoop();
564 if (interval_in_ms_ != 0) {
565 if (!loop->UsePreciseTimer()) {
566 return false;
567 }
568 }
Wei Wang539fb882016-09-28 14:42:02 -0700569 std::chrono::time_point<std::chrono::steady_clock> start_time;
570 std::vector<CountersInfo> counters;
Yabin Cui5f43fc42016-12-13 13:47:49 -0800571 if (need_to_check_targets && !event_selection_set_.StopWhenNoMoreTargets()) {
572 return false;
573 }
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +0200574 auto exit_loop_callback = [loop]() { return loop->ExitLoop(); };
Yabin Cui1a30a582019-01-10 15:35:39 -0800575 if (!loop->AddSignalEvents({SIGCHLD, SIGINT, SIGTERM, SIGHUP}, exit_loop_callback)) {
Yabin Cui3e4c5952016-07-26 15:03:27 -0700576 return false;
577 }
Yabin Cui1a30a582019-01-10 15:35:39 -0800578 if (stop_signal_fd_ != -1) {
579 if (!loop->AddReadEvent(stop_signal_fd_, exit_loop_callback)) {
580 return false;
581 }
582 }
Yabin Cui3e4c5952016-07-26 15:03:27 -0700583 if (duration_in_sec_ != 0) {
Yabin Cui1a30a582019-01-10 15:35:39 -0800584 if (!loop->AddPeriodicEvent(SecondToTimeval(duration_in_sec_), exit_loop_callback)) {
Yabin Cui3e4c5952016-07-26 15:03:27 -0700585 return false;
586 }
587 }
Wei Wang539fb882016-09-28 14:42:02 -0700588 auto print_counters = [&]() {
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +0200589 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 Wang539fb882016-09-28 14:42:02 -0700602 };
603
604 if (interval_in_ms_ != 0) {
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +0200605 if (!loop->AddPeriodicEvent(SecondToTimeval(interval_in_ms_ / 1000.0), print_counters)) {
Wei Wang539fb882016-09-28 14:42:02 -0700606 return false;
607 }
608 }
Yabin Cui3e4c5952016-07-26 15:03:27 -0700609
610 // 5. Count events while workload running.
Wei Wang539fb882016-09-28 14:42:02 -0700611 start_time = std::chrono::steady_clock::now();
Yabin Cuib032de72015-06-17 21:15:09 -0700612 if (workload != nullptr && !workload->Start()) {
Yabin Cui323e9452015-04-20 18:07:17 -0700613 return false;
614 }
Yabin Cui26968e62017-01-30 11:34:24 -0800615 if (!loop->RunLoop()) {
Yabin Cui3e4c5952016-07-26 15:03:27 -0700616 return false;
Yabin Cui621a5332015-06-15 16:17:20 -0700617 }
Yabin Cui323e9452015-04-20 18:07:17 -0700618
Yabin Cui3e4c5952016-07-26 15:03:27 -0700619 // 6. Read and print counters.
Yabin Cui9267db22018-04-18 11:17:19 -0700620 if (interval_in_ms_ == 0) {
Yabin4ed8e112022-08-23 11:43:01 -0700621 if (!print_counters()) {
622 return false;
623 }
Yabin Cui9267db22018-04-18 11:17:19 -0700624 }
Yabin4ed8e112022-08-23 11:43:01 -0700625
Yabin Cui6a2fda42022-09-01 11:52:51 -0700626 // 7. Print warnings when needed.
Yabin4ed8e112022-08-23 11:43:01 -0700627 event_selection_set_.CloseEventFiles();
628 CheckHardwareCounterMultiplexing();
Yabin Cui6a2fda42022-09-01 11:52:51 -0700629 PrintWarningForInaccurateEvents();
Yabin4ed8e112022-08-23 11:43:01 -0700630
Yabin Cui9267db22018-04-18 11:17:19 -0700631 return true;
Yabin Cui323e9452015-04-20 18:07:17 -0700632}
633
Yabin Cuif79f07e2015-06-01 11:21:37 -0700634bool StatCommand::ParseOptions(const std::vector<std::string>& args,
635 std::vector<std::string>* non_option_args) {
Yabin Cui6f094672020-07-22 14:50:35 -0700636 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 Cuie3ca9982020-10-16 13:16:26 -0700651 if (auto cpus = GetCpusFromString(*value->str_value); cpus) {
652 cpus_.assign(cpus->begin(), cpus->end());
653 } else {
654 return false;
655 }
Yabin Cui6f094672020-07-22 14:50:35 -0700656 }
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 Cuie3ca9982020-10-16 13:16:26 -0700668 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 Cui323e9452015-04-20 18:07:17 -0700672 }
Yabin Cui6f094672020-07-22 14:50:35 -0700673 }
674 }
675
Yabin Cuie3ca9982020-10-16 13:16:26 -0700676 for (const OptionValue& value : options.PullValues("--group")) {
677 if (!event_selection_set_.AddEventGroup(Split(*value.str_value, ","))) {
678 return false;
Yabin Cui6f094672020-07-22 14:50:35 -0700679 }
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 Cui1c6be752023-02-28 11:46:37 -0800695 if (auto strs = options.PullStringValues("-p"); !strs.empty()) {
696 if (auto pids = GetPidsFromStrings(strs, true, true); pids) {
Yabin Cuie3ca9982020-10-16 13:16:26 -0700697 event_selection_set_.AddMonitoredProcesses(pids.value());
698 } else {
699 return false;
Yabin Cui6f094672020-07-22 14:50:35 -0700700 }
701 }
Yabin Cuif1a1e482021-12-22 14:54:20 -0800702 print_hw_counter_ = options.PullBoolValue("--print-hw-counter");
Yabin Cuif53f7162020-06-19 15:16:31 -0700703
Yabin Cui6f094672020-07-22 14:50:35 -0700704 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 Cuie3ca9982020-10-16 13:16:26 -0700712 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 Cui6f094672020-07-22 14:50:35 -0700717 }
718 }
719
720 if (auto value = options.PullValue("--tracepoint-events"); value) {
Yabin Cui16a6ace2020-10-01 14:56:32 -0700721 if (!EventTypeManager::Instance().ReadTracepointsFromFile(*value->str_value)) {
Yabin Cui323e9452015-04-20 18:07:17 -0700722 return false;
723 }
724 }
725
Yabin Cui6f094672020-07-22 14:50:35 -0700726 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 Cuibc2a1022016-08-29 12:33:17 -0700732 if (system_wide_collection_ && event_selection_set_.HasMonitoredTarget()) {
Yabin Cui877751b2016-06-13 18:03:47 -0700733 LOG(ERROR) << "Stat system wide and existing processes/threads can't be "
734 "used at the same time.";
Yabin Cuib032de72015-06-17 21:15:09 -0700735 return false;
736 }
Yabin Cui8a599d72016-07-21 18:32:53 -0700737 if (system_wide_collection_ && !IsRoot()) {
738 LOG(ERROR) << "System wide profiling needs root privilege.";
739 return false;
740 }
Yabin Cuib032de72015-06-17 21:15:09 -0700741
Yabin Cuif53f7162020-06-19 15:16:31 -0700742 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 Cui323e9452015-04-20 18:07:17 -0700748 return true;
749}
750
Yabin4ed8e112022-08-23 11:43:01 -0700751std::optional<bool> CheckHardwareCountersOnCpu(int cpu, size_t counters) {
Yabin Cuif1a1e482021-12-22 14:54:20 -0800752 const EventType* event = FindEventTypeByName("cpu-cycles", true);
753 if (event == nullptr) {
Yabin6b771a22022-08-18 17:01:54 -0700754 return std::nullopt;
Yabin Cuif1a1e482021-12-22 14:54:20 -0800755 }
756 perf_event_attr attr = CreateDefaultPerfEventAttr(*event);
Yabin4ed8e112022-08-23 11:43:01 -0700757 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 Cuif1a1e482021-12-22 14:54:20 -0800767 return false;
768 }
Yabin4ed8e112022-08-23 11:43:01 -0700769 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 Cuif1a1e482021-12-22 14:54:20 -0800778 }
Yabin4ed8e112022-08-23 11:43:01 -0700779 if (counter.time_enabled == 0 || counter.time_enabled > counter.time_running) {
780 return false;
781 }
782 }
783 return true;
784}
785
786std::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 Cuif1a1e482021-12-22 14:54:20 -0800794 break;
795 }
796 available_counters++;
797 }
Yabin6b771a22022-08-18 17:01:54 -0700798 return available_counters;
799}
800
Yabin Cui5aded992022-08-25 10:51:11 -0700801void StatCommand::PrintHardwareCounters() {
Yabin6b771a22022-08-18 17:01:54 -0700802 for (int cpu : GetOnlineCpus()) {
803 std::optional<size_t> counters = GetHardwareCountersOnCpu(cpu);
804 if (!counters) {
Yabin Cui5aded992022-08-25 10:51:11 -0700805 // 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;
Yabin6b771a22022-08-18 17:01:54 -0700809 }
810 printf("There are %zu CPU PMU hardware counters available on cpu %d.\n", counters.value(), cpu);
811 }
Yabin Cuif1a1e482021-12-22 14:54:20 -0800812}
813
Yabin Cuif79f07e2015-06-01 11:21:37 -0700814bool StatCommand::AddDefaultMeasuredEventTypes() {
Yabin Cui323e9452015-04-20 18:07:17 -0700815 for (auto& name : default_measured_event_types) {
Yabin Cui877751b2016-06-13 18:03:47 -0700816 // It is not an error when some event types in the default list are not
817 // supported by the kernel.
Yabin Cui9fd3cc12015-06-25 17:42:23 -0700818 const EventType* type = FindEventTypeByName(name);
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +0200819 if (type != nullptr && IsEventAttrSupported(CreateDefaultPerfEventAttr(*type), name)) {
Yabin Cui877751b2016-06-13 18:03:47 -0700820 if (!event_selection_set_.AddEventType(name)) {
821 return false;
822 }
Yabin Cui9fd3cc12015-06-25 17:42:23 -0700823 }
Yabin Cui323e9452015-04-20 18:07:17 -0700824 }
Yabin Cui877751b2016-06-13 18:03:47 -0700825 if (event_selection_set_.empty()) {
Yabin Cui323e9452015-04-20 18:07:17 -0700826 LOG(ERROR) << "Failed to add any supported default measured types";
827 return false;
828 }
829 return true;
830}
831
Yabin Cui877751b2016-06-13 18:03:47 -0700832void StatCommand::SetEventSelectionFlags() {
Yabin Cui4be41262015-06-22 14:23:01 -0700833 event_selection_set_.SetInherit(child_inherit_);
834}
835
Yabin Cuidc2708c2020-01-10 15:33:11 -0800836void 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 Cui8ab56212020-01-27 15:23:46 -0800861void 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 Cuib3ed39b2020-03-26 14:00:49 -0700874 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 Cui8ab56212020-01-27 15:23:46 -0800879 }
880 }
881}
882
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +0200883bool StatCommand::ShowCounters(const std::vector<CountersInfo>& counters, double duration_in_sec,
884 FILE* fp) {
Zhiting Zhu0979ba82016-07-09 19:38:08 -0700885 if (csv_) {
886 fprintf(fp, "Performance counter statistics,\n");
887 } else {
888 fprintf(fp, "Performance counter statistics:\n\n");
889 }
Yabin Cui323e9452015-04-20 18:07:17 -0700890
Yabin Cui04d08a32015-08-19 15:01:12 -0700891 if (verbose_mode_) {
892 for (auto& counters_info : counters) {
Yabin Cui04d08a32015-08-19 15:01:12 -0700893 for (auto& counter_info : counters_info.counters) {
Zhiting Zhu0979ba82016-07-09 19:38:08 -0700894 if (csv_) {
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +0200895 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 Zhu0979ba82016-07-09 19:38:08 -0700900 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 Weksteen4848ee02020-10-23 16:06:59 +0200905 counters_info.event_name.c_str(), counter_info.tid, counter_info.cpu,
906 counter_info.counter.value, counter_info.counter.time_enabled,
Zhiting Zhu0979ba82016-07-09 19:38:08 -0700907 counter_info.counter.time_running, counter_info.counter.id);
908 }
Yabin Cui323e9452015-04-20 18:07:17 -0700909 }
910 }
Yabin Cui04d08a32015-08-19 15:01:12 -0700911 }
Yabin Cui323e9452015-04-20 18:07:17 -0700912
Yabin Cuif53f7162020-06-19 15:16:31 -0700913 CounterSummaryBuilder builder(report_per_thread_, report_per_core_, csv_, thread_info_,
914 summary_comparator_);
Yabin Cui8ab56212020-01-27 15:23:46 -0800915 for (const auto& info : counters) {
Yabin Cuib3ed39b2020-03-26 14:00:49 -0700916 builder.AddCountersForOneEventType(info);
Yabin Cui323e9452015-04-20 18:07:17 -0700917 }
Yabin Cuib3ed39b2020-03-26 14:00:49 -0700918 CounterSummaries summaries(builder.Build(), csv_);
Yabin Cui0a45adf2016-06-22 20:58:52 -0700919 summaries.AutoGenerateSummaries();
920 summaries.GenerateComments(duration_in_sec);
921 summaries.Show(fp);
Yabin Cui04d08a32015-08-19 15:01:12 -0700922
Yabin4ed8e112022-08-23 11:43:01 -0700923 if (csv_) {
Zhiting Zhu0979ba82016-07-09 19:38:08 -0700924 fprintf(fp, "Total test time,%lf,seconds,\n", duration_in_sec);
Yabin Cui8ab56212020-01-27 15:23:46 -0800925 } else {
Yabin4ed8e112022-08-23 11:43:01 -0700926 fprintf(fp, "\nTotal test time: %lf seconds.\n", duration_in_sec);
Yabin Cui52f32a42019-08-12 16:53:46 -0700927 }
Yabin Cui323e9452015-04-20 18:07:17 -0700928 return true;
929}
930
Yabin4ed8e112022-08-23 11:43:01 -0700931void 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 Cui6a2fda42022-09-01 11:52:51 -0700962void 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 Cui877751b2016-06-13 18:03:47 -0700972} // namespace
973
Yabin Cuiff7465c2016-02-25 11:02:30 -0800974void RegisterStatCommand() {
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +0200975 RegisterCommand("stat", [] { return std::unique_ptr<Command>(new StatCommand); });
Yabin Cuif79f07e2015-06-01 11:21:37 -0700976}
Yabin Cuiacbdb242020-07-07 15:56:34 -0700977
978} // namespace simpleperf