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>
18 #include <signal.h>
19 #include <stdio.h>
20 #include <string.h>
21 #include <sys/prctl.h>
22 
23 #include <algorithm>
24 #include <chrono>
25 #include <optional>
26 #include <set>
27 #include <string>
28 #include <string_view>
29 #include <vector>
30 
31 #include <android-base/file.h>
32 #include <android-base/logging.h>
33 #include <android-base/strings.h>
34 #include <android-base/unique_fd.h>
35 
36 #include "IOEventLoop.h"
37 #include "cmd_stat_impl.h"
38 #include "command.h"
39 #include "environment.h"
40 #include "event_attr.h"
41 #include "event_fd.h"
42 #include "event_selection_set.h"
43 #include "event_type.h"
44 #include "utils.h"
45 #include "workload.h"
46 
47 namespace simpleperf {
48 
49 using android::base::Split;
50 
51 static std::vector<std::string> default_measured_event_types{
52     "cpu-cycles",   "stalled-cycles-frontend", "stalled-cycles-backend",
53     "instructions", "branch-instructions",     "branch-misses",
54     "task-clock",   "context-switches",        "page-faults",
55 };
56 
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 
FindSummary(const std::string & type_name,const std::string & modifier,const ThreadInfo * thread,int cpu)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;
100     }
101   }
102   return nullptr;
103 }
104 
AutoGenerateSummaries()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,
113                                   s.count + other->count, s.runtime_in_ns, s.scale, true, csv_);
114         }
115       }
116     }
117   }
118 }
119 
GenerateComments(double duration_in_sec)120 void CounterSummaries::GenerateComments(double duration_in_sec) {
121   for (auto& s : summaries_) {
122     s.comment = GetCommentForSummary(s, duration_in_sec);
123   }
124 }
125 
Show(FILE * fp)126 void CounterSummaries::Show(FILE* fp) {
127   bool show_thread = !summaries_.empty() && summaries_[0].thread != nullptr;
128   bool show_cpu = !summaries_.empty() && summaries_[0].cpu != -1;
129   if (csv_) {
130     ShowCSV(fp, show_thread, show_cpu);
131   } else {
132     ShowText(fp, show_thread, show_cpu);
133   }
134 }
135 
ShowCSV(FILE * fp,bool show_thread,bool show_cpu)136 void CounterSummaries::ShowCSV(FILE* fp, bool show_thread, bool show_cpu) {
137   for (auto& s : summaries_) {
138     if (show_thread) {
139       fprintf(fp, "%s,%d,%d,", s.thread->name.c_str(), s.thread->pid, s.thread->tid);
140     }
141     if (show_cpu) {
142       fprintf(fp, "%d,", s.cpu);
143     }
144     fprintf(fp, "%s,%s,%s,(%.0f%%)%s\n", s.readable_count.c_str(), s.Name().c_str(),
145             s.comment.c_str(), 1.0 / s.scale * 100, (s.auto_generated ? " (generated)," : ","));
146   }
147 }
148 
ShowText(FILE * fp,bool show_thread,bool show_cpu)149 void CounterSummaries::ShowText(FILE* fp, bool show_thread, bool show_cpu) {
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");
160   titles.emplace_back(" # count / runtime,  runtime / enabled_time");
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());
169   }
170 
171   for (auto& s : summaries_) {
172     size_t i = 0;
173     if (show_thread) {
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());
177     }
178     if (show_cpu) {
179       adjust_width(width[i++], std::to_string(s.cpu).size());
180     }
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());
184   }
185 
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());
190     } else {
191       fprintf(fp, "%-*s", static_cast<int>(width[i]), titles[i].c_str());
192     }
193     if (i + 1 < titles.size()) {
194       fprintf(fp, "  ");
195     }
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);
205     }
206     if (show_cpu) {
207       fprintf(fp, "  %-*d", static_cast<int>(width[i++]), s.cpu);
208     }
209     fprintf(fp, "  %*s  %-*s   # %-*s  (%.0f%%)%s\n", static_cast<int>(width[i]),
210             s.readable_count.c_str(), static_cast<int>(width[i + 1]), s.Name().c_str(),
211             static_cast<int>(width[i + 2]), s.comment.c_str(), 1.0 / s.scale * 100,
212             (s.auto_generated ? " (generated)" : ""));
213   }
214 }
215 
GetCommentForSummary(const CounterSummary & s,double duration_in_sec)216 std::string CounterSummaries::GetCommentForSummary(const CounterSummary& s,
217                                                    double duration_in_sec) {
218   char sap_mid;
219   if (csv_) {
220     sap_mid = ',';
221   } else {
222     sap_mid = ' ';
223   }
224   if (s.type_name == "task-clock") {
225     double run_sec = s.count / 1e9;
226     double used_cpus = run_sec / duration_in_sec;
227     return android::base::StringPrintf("%f%ccpus used", used_cpus, sap_mid);
228   }
229   if (s.type_name == "cpu-clock") {
230     return "";
231   }
232   if (s.type_name == "cpu-cycles") {
233     if (s.runtime_in_ns == 0) {
234       return "";
235     }
236     double ghz = static_cast<double>(s.count) / s.runtime_in_ns;
237     return android::base::StringPrintf("%f%cGHz", ghz, sap_mid);
238   }
239   if (s.type_name == "instructions" && s.count != 0) {
240     const CounterSummary* other = FindSummary("cpu-cycles", s.modifier, s.thread, s.cpu);
241     if (other != nullptr && other->IsMonitoredAtTheSameTime(s)) {
242       double cpi = static_cast<double>(other->count) / s.count;
243       return android::base::StringPrintf("%f%ccycles per instruction", cpi, sap_mid);
244     }
245   }
246   std::string rate_comment = GetRateComment(s, sap_mid);
247   if (!rate_comment.empty()) {
248     return rate_comment;
249   }
250   if (s.runtime_in_ns == 0) {
251     return "";
252   }
253   double runtime_in_sec = static_cast<double>(s.runtime_in_ns) / 1e9;
254   double rate = s.count / runtime_in_sec;
255   if (rate >= 1e9 - 1e5) {
256     return android::base::StringPrintf("%.3f%cG/sec", rate / 1e9, sap_mid);
257   }
258   if (rate >= 1e6 - 1e2) {
259     return android::base::StringPrintf("%.3f%cM/sec", rate / 1e6, sap_mid);
260   }
261   if (rate >= 1e3) {
262     return android::base::StringPrintf("%.3f%cK/sec", rate / 1e3, sap_mid);
263   }
264   return android::base::StringPrintf("%.3f%c/sec", rate, sap_mid);
265 }
266 
GetRateComment(const CounterSummary & s,char sep)267 std::string CounterSummaries::GetRateComment(const CounterSummary& s, char sep) {
268   std::string_view miss_event_name = s.type_name;
269   std::string event_name;
270   std::string rate_desc;
271   if (auto it = COMMON_EVENT_RATE_MAP.find(miss_event_name); it != COMMON_EVENT_RATE_MAP.end()) {
272     event_name = it->second.first;
273     rate_desc = it->second.second;
274   }
275   if (event_name.empty() && (GetBuildArch() == ARCH_ARM || GetBuildArch() == ARCH_ARM64)) {
276     if (auto it = ARM_EVENT_RATE_MAP.find(miss_event_name); it != ARM_EVENT_RATE_MAP.end()) {
277       event_name = it->second.first;
278       rate_desc = it->second.second;
279     }
280   }
281   if (event_name.empty() && android::base::ConsumeSuffix(&miss_event_name, "-misses")) {
282     event_name = std::string(miss_event_name) + "s";
283     rate_desc = "miss rate";
284   }
285   if (!event_name.empty()) {
286     const CounterSummary* other = FindSummary(event_name, s.modifier, s.thread, s.cpu);
287     if (other != nullptr && other->IsMonitoredAtTheSameTime(s) && other->count != 0) {
288       double miss_rate = static_cast<double>(s.count) / other->count;
289       return android::base::StringPrintf("%f%%%c%s", miss_rate * 100, sep, rate_desc.c_str());
290     }
291   }
292   return "";
293 }
294 
295 namespace {
296 
297 // devfreq may use performance counters to calculate memory latency (as in
298 // drivers/devfreq/arm-memlat-mon.c). Hopefully we can get more available counters by asking devfreq
299 // to not use the memory latency governor temporarily.
300 class DevfreqCounters {
301  public:
Use()302   bool Use() {
303     if (!IsRoot()) {
304       LOG(ERROR) << "--use-devfreq-counters needs root permission to set devfreq governors";
305       return false;
306     }
307     std::string devfreq_dir = "/sys/class/devfreq/";
308     for (auto& name : GetSubDirs(devfreq_dir)) {
309       std::string governor_path = devfreq_dir + name + "/governor";
310       if (IsRegularFile(governor_path)) {
311         std::string governor;
312         if (!android::base::ReadFileToString(governor_path, &governor)) {
313           LOG(ERROR) << "failed to read " << governor_path;
314           return false;
315         }
316         governor = android::base::Trim(governor);
317         if (governor == "mem_latency") {
318           if (!android::base::WriteStringToFile("performance", governor_path)) {
319             PLOG(ERROR) << "failed to write " << governor_path;
320             return false;
321           }
322           mem_latency_governor_paths_.emplace_back(std::move(governor_path));
323         }
324       }
325     }
326     return true;
327   }
328 
~DevfreqCounters()329   ~DevfreqCounters() {
330     for (auto& path : mem_latency_governor_paths_) {
331       android::base::WriteStringToFile("mem_latency", path);
332     }
333   }
334 
335  private:
336   std::vector<std::string> mem_latency_governor_paths_;
337 };
338 
339 class StatCommand : public Command {
340  public:
StatCommand()341   StatCommand()
342       : Command(
343             "stat", "gather performance counter information",
344             // clang-format off
345 "Usage: simpleperf stat [options] [command [command-args]]\n"
346 "       Gather performance counter information of running [command].\n"
347 "       And -a/-p/-t option can be used to change target of counter information.\n"
348 "-a           Collect system-wide information.\n"
349 #if defined(__ANDROID__)
350 "--app package_name    Profile the process of an Android application.\n"
351 "                      On non-rooted devices, the app must be debuggable,\n"
352 "                      because we use run-as to switch to the app's context.\n"
353 #endif
354 "--cpu cpu_item1,cpu_item2,...\n"
355 "                 Collect information only on the selected cpus. cpu_item can\n"
356 "                 be a cpu number like 1, or a cpu range like 0-3.\n"
357 "--csv            Write report in comma separate form.\n"
358 "--duration time_in_sec  Monitor for time_in_sec seconds instead of running\n"
359 "                        [command]. Here time_in_sec may be any positive\n"
360 "                        floating point number.\n"
361 "--interval time_in_ms   Print stat for every time_in_ms milliseconds.\n"
362 "                        Here time_in_ms may be any positive floating point\n"
363 "                        number. Simpleperf prints total values from the\n"
364 "                        starting point. But this can be changed by\n"
365 "                        --interval-only-values.\n"
366 "--interval-only-values  Print numbers of events happened in each interval.\n"
367 "-e event1[:modifier1],event2[:modifier2],...\n"
368 "                 Select a list of events to count. An event can be:\n"
369 "                   1) an event name listed in `simpleperf list`;\n"
370 "                   2) a raw PMU event in rN format. N is a hex number.\n"
371 "                      For example, r1b selects event number 0x1b.\n"
372 "                 Modifiers can be added to define how the event should be\n"
373 "                 monitored. Possible modifiers are:\n"
374 "                   u - monitor user space events only\n"
375 "                   k - monitor kernel space events only\n"
376 "--group event1[:modifier],event2[:modifier2],...\n"
377 "             Similar to -e option. But events specified in the same --group\n"
378 "             option are monitored as a group, and scheduled in and out at the\n"
379 "             same time.\n"
380 "--no-inherit     Don't stat created child threads/processes.\n"
381 "-o output_filename  Write report to output_filename instead of standard output.\n"
382 "--per-core       Print counters for each cpu core.\n"
383 "--per-thread     Print counters for each thread.\n"
384 "-p pid1,pid2,... Stat events on existing processes. Mutually exclusive with -a.\n"
385 "-t tid1,tid2,... Stat events on existing threads. Mutually exclusive with -a.\n"
386 "--sort key1,key2,...  Select keys used to sort the report, used when --per-thread\n"
387 "                      or --per-core appears. The appearance order of keys decides\n"
388 "                      the order of keys used to sort the report.\n"
389 "                      Possible keys include:\n"
390 "                        count             -- event count for each entry\n"
391 "                        count_per_thread  -- event count for a thread on all cpus\n"
392 "                        cpu               -- cpu id\n"
393 "                        pid               -- process id\n"
394 "                        tid               -- thread id\n"
395 "                        comm              -- thread name\n"
396 "                      The default sort keys are:\n"
397 "                        count_per_thread,tid,cpu,count\n"
398 #if defined(__ANDROID__)
399 "--use-devfreq-counters    On devices with Qualcomm SOCs, some hardware counters may be used\n"
400 "                          to monitor memory latency (in drivers/devfreq/arm-memlat-mon.c),\n"
401 "                          making fewer counters available to users. This option asks devfreq\n"
402 "                          to temporarily release counters by replacing memory-latency governor\n"
403 "                          with performance governor. It affects memory latency during profiling,\n"
404 "                          and may cause wedged power if simpleperf is killed in between.\n"
405 #endif
406 "--verbose        Show result in verbose mode.\n"
407 #if 0
408 // Below options are only used internally and shouldn't be visible to the public.
409 "--in-app         We are already running in the app's context.\n"
410 "--tracepoint-events file_name   Read tracepoint events from [file_name] instead of tracefs.\n"
411 "--out-fd <fd>    Write output to a file descriptor.\n"
412 "--stop-signal-fd <fd>   Stop stating when fd is readable.\n"
413 #endif
414             // clang-format on
415             ),
416         verbose_mode_(false),
417         system_wide_collection_(false),
418         child_inherit_(true),
419         duration_in_sec_(0),
420         interval_in_ms_(0),
421         interval_only_values_(false),
422         event_selection_set_(true),
423         csv_(false),
424         in_app_context_(false) {
425     // Die if parent exits.
426     prctl(PR_SET_PDEATHSIG, SIGHUP, 0, 0, 0);
427     // Set default sort keys. Full key list is in BuildSummaryComparator().
428     sort_keys_ = {"count_per_thread", "tid", "cpu", "count"};
429   }
430 
431   bool Run(const std::vector<std::string>& args);
432 
433  private:
434   bool ParseOptions(const std::vector<std::string>& args,
435                     std::vector<std::string>* non_option_args);
436   bool AddDefaultMeasuredEventTypes();
437   void SetEventSelectionFlags();
438   void MonitorEachThread();
439   void AdjustToIntervalOnlyValues(std::vector<CountersInfo>& counters);
440   bool ShowCounters(const std::vector<CountersInfo>& counters, double duration_in_sec, FILE* fp);
441 
442   bool verbose_mode_;
443   bool system_wide_collection_;
444   bool child_inherit_;
445   double duration_in_sec_;
446   double interval_in_ms_;
447   bool interval_only_values_;
448   std::vector<std::vector<CounterSum>> last_sum_values_;
449   std::vector<int> cpus_;
450   EventSelectionSet event_selection_set_;
451   std::string output_filename_;
452   android::base::unique_fd out_fd_;
453   bool csv_;
454   std::string app_package_name_;
455   bool in_app_context_;
456   android::base::unique_fd stop_signal_fd_;
457   bool use_devfreq_counters_ = false;
458 
459   bool report_per_core_ = false;
460   bool report_per_thread_ = false;
461   // used to report event count for each thread
462   std::unordered_map<pid_t, ThreadInfo> thread_info_;
463   // used to sort report
464   std::vector<std::string> sort_keys_;
465   std::optional<SummaryComparator> summary_comparator_;
466 };
467 
Run(const std::vector<std::string> & args)468 bool StatCommand::Run(const std::vector<std::string>& args) {
469   if (!CheckPerfEventLimit()) {
470     return false;
471   }
472   AllowMoreOpenedFiles();
473 
474   // 1. Parse options, and use default measured event types if not given.
475   std::vector<std::string> workload_args;
476   if (!ParseOptions(args, &workload_args)) {
477     return false;
478   }
479   if (!app_package_name_.empty() && !in_app_context_) {
480     if (!IsRoot()) {
481       return RunInAppContext(app_package_name_, "stat", args, workload_args.size(),
482                              output_filename_, !event_selection_set_.GetTracepointEvents().empty());
483     }
484   }
485   DevfreqCounters devfreq_counters;
486   if (use_devfreq_counters_) {
487     if (!devfreq_counters.Use()) {
488       return false;
489     }
490   }
491   if (event_selection_set_.empty()) {
492     if (!AddDefaultMeasuredEventTypes()) {
493       return false;
494     }
495   }
496   SetEventSelectionFlags();
497 
498   // 2. Create workload.
499   std::unique_ptr<Workload> workload;
500   if (!workload_args.empty()) {
501     workload = Workload::CreateWorkload(workload_args);
502     if (workload == nullptr) {
503       return false;
504     }
505   }
506   bool need_to_check_targets = false;
507   if (system_wide_collection_) {
508     if (report_per_thread_) {
509       event_selection_set_.AddMonitoredProcesses(GetAllProcesses());
510     } else {
511       event_selection_set_.AddMonitoredThreads({-1});
512     }
513   } else if (!event_selection_set_.HasMonitoredTarget()) {
514     if (workload != nullptr) {
515       event_selection_set_.AddMonitoredProcesses({workload->GetPid()});
516       event_selection_set_.SetEnableOnExec(true);
517     } else if (!app_package_name_.empty()) {
518       std::set<pid_t> pids = WaitForAppProcesses(app_package_name_);
519       event_selection_set_.AddMonitoredProcesses(pids);
520     } else {
521       LOG(ERROR) << "No threads to monitor. Try `simpleperf help stat` for help\n";
522       return false;
523     }
524   } else {
525     need_to_check_targets = true;
526   }
527 
528   if (report_per_thread_) {
529     MonitorEachThread();
530   }
531 
532   // 3. Open perf_event_files and output file if defined.
533   if (cpus_.empty() && !report_per_core_ && (report_per_thread_ || !system_wide_collection_)) {
534     cpus_.push_back(-1);  // Get event count for each thread on all cpus.
535   }
536   if (!event_selection_set_.OpenEventFiles(cpus_)) {
537     return false;
538   }
539   std::unique_ptr<FILE, decltype(&fclose)> fp_holder(nullptr, fclose);
540   if (!output_filename_.empty()) {
541     fp_holder.reset(fopen(output_filename_.c_str(), "we"));
542     if (fp_holder == nullptr) {
543       PLOG(ERROR) << "failed to open " << output_filename_;
544       return false;
545     }
546   } else if (out_fd_ != -1) {
547     fp_holder.reset(fdopen(out_fd_.release(), "we"));
548     if (fp_holder == nullptr) {
549       PLOG(ERROR) << "failed to write output.";
550       return false;
551     }
552   }
553   FILE* fp = fp_holder ? fp_holder.get() : stdout;
554 
555   // 4. Add signal/periodic Events.
556   IOEventLoop* loop = event_selection_set_.GetIOEventLoop();
557   if (interval_in_ms_ != 0) {
558     if (!loop->UsePreciseTimer()) {
559       return false;
560     }
561   }
562   std::chrono::time_point<std::chrono::steady_clock> start_time;
563   std::vector<CountersInfo> counters;
564   if (need_to_check_targets && !event_selection_set_.StopWhenNoMoreTargets()) {
565     return false;
566   }
567   auto exit_loop_callback = [loop]() { return loop->ExitLoop(); };
568   if (!loop->AddSignalEvents({SIGCHLD, SIGINT, SIGTERM, SIGHUP}, exit_loop_callback)) {
569     return false;
570   }
571   if (stop_signal_fd_ != -1) {
572     if (!loop->AddReadEvent(stop_signal_fd_, exit_loop_callback)) {
573       return false;
574     }
575   }
576   if (duration_in_sec_ != 0) {
577     if (!loop->AddPeriodicEvent(SecondToTimeval(duration_in_sec_), exit_loop_callback)) {
578       return false;
579     }
580   }
581   auto print_counters = [&]() {
582     auto end_time = std::chrono::steady_clock::now();
583     if (!event_selection_set_.ReadCounters(&counters)) {
584       return false;
585     }
586     double duration_in_sec =
587         std::chrono::duration_cast<std::chrono::duration<double>>(end_time - start_time).count();
588     if (interval_only_values_) {
589       AdjustToIntervalOnlyValues(counters);
590     }
591     if (!ShowCounters(counters, duration_in_sec, fp)) {
592       return false;
593     }
594     return true;
595   };
596 
597   if (interval_in_ms_ != 0) {
598     if (!loop->AddPeriodicEvent(SecondToTimeval(interval_in_ms_ / 1000.0), print_counters)) {
599       return false;
600     }
601   }
602 
603   // 5. Count events while workload running.
604   start_time = std::chrono::steady_clock::now();
605   if (workload != nullptr && !workload->Start()) {
606     return false;
607   }
608   if (!loop->RunLoop()) {
609     return false;
610   }
611 
612   // 6. Read and print counters.
613   if (interval_in_ms_ == 0) {
614     return print_counters();
615   }
616   return true;
617 }
618 
ParseOptions(const std::vector<std::string> & args,std::vector<std::string> * non_option_args)619 bool StatCommand::ParseOptions(const std::vector<std::string>& args,
620                                std::vector<std::string>* non_option_args) {
621   OptionValueMap options;
622   std::vector<std::pair<OptionName, OptionValue>> ordered_options;
623 
624   if (!PreprocessOptions(args, GetStatCmdOptionFormats(), &options, &ordered_options,
625                          non_option_args)) {
626     return false;
627   }
628 
629   // Process options.
630   system_wide_collection_ = options.PullBoolValue("-a");
631 
632   if (auto value = options.PullValue("--app"); value) {
633     app_package_name_ = *value->str_value;
634   }
635   if (auto value = options.PullValue("--cpu"); value) {
636     if (auto cpus = GetCpusFromString(*value->str_value); cpus) {
637       cpus_.assign(cpus->begin(), cpus->end());
638     } else {
639       return false;
640     }
641   }
642 
643   csv_ = options.PullBoolValue("--csv");
644 
645   if (!options.PullDoubleValue("--duration", &duration_in_sec_, 1e-9)) {
646     return false;
647   }
648   if (!options.PullDoubleValue("--interval", &interval_in_ms_, 1e-9)) {
649     return false;
650   }
651   interval_only_values_ = options.PullBoolValue("--interval-only-values");
652 
653   for (const OptionValue& value : options.PullValues("-e")) {
654     for (const auto& event_type : Split(*value.str_value, ",")) {
655       if (!event_selection_set_.AddEventType(event_type)) {
656         return false;
657       }
658     }
659   }
660 
661   for (const OptionValue& value : options.PullValues("--group")) {
662     if (!event_selection_set_.AddEventGroup(Split(*value.str_value, ","))) {
663       return false;
664     }
665   }
666 
667   in_app_context_ = options.PullBoolValue("--in-app");
668   child_inherit_ = !options.PullBoolValue("--no-inherit");
669 
670   if (auto value = options.PullValue("-o"); value) {
671     output_filename_ = *value->str_value;
672   }
673   if (auto value = options.PullValue("--out-fd"); value) {
674     out_fd_.reset(static_cast<int>(value->uint_value));
675   }
676 
677   report_per_core_ = options.PullBoolValue("--per-core");
678   report_per_thread_ = options.PullBoolValue("--per-thread");
679 
680   for (const OptionValue& value : options.PullValues("-p")) {
681     if (auto pids = GetTidsFromString(*value.str_value, true); pids) {
682       event_selection_set_.AddMonitoredProcesses(pids.value());
683     } else {
684       return false;
685     }
686   }
687 
688   if (auto value = options.PullValue("--sort"); value) {
689     sort_keys_ = Split(*value->str_value, ",");
690   }
691 
692   if (auto value = options.PullValue("--stop-signal-fd"); value) {
693     stop_signal_fd_.reset(static_cast<int>(value->uint_value));
694   }
695 
696   for (const OptionValue& value : options.PullValues("-t")) {
697     if (auto tids = GetTidsFromString(*value.str_value, true); tids) {
698       event_selection_set_.AddMonitoredThreads(tids.value());
699     } else {
700       return false;
701     }
702   }
703 
704   if (auto value = options.PullValue("--tracepoint-events"); value) {
705     if (!EventTypeManager::Instance().ReadTracepointsFromFile(*value->str_value)) {
706       return false;
707     }
708   }
709 
710   use_devfreq_counters_ = options.PullBoolValue("--use-devfreq-counters");
711   verbose_mode_ = options.PullBoolValue("--verbose");
712 
713   CHECK(options.values.empty());
714   CHECK(ordered_options.empty());
715 
716   if (system_wide_collection_ && event_selection_set_.HasMonitoredTarget()) {
717     LOG(ERROR) << "Stat system wide and existing processes/threads can't be "
718                   "used at the same time.";
719     return false;
720   }
721   if (system_wide_collection_ && !IsRoot()) {
722     LOG(ERROR) << "System wide profiling needs root privilege.";
723     return false;
724   }
725 
726   if (report_per_core_ || report_per_thread_) {
727     summary_comparator_ = BuildSummaryComparator(sort_keys_, report_per_thread_, report_per_core_);
728     if (!summary_comparator_) {
729       return false;
730     }
731   }
732   return true;
733 }
734 
AddDefaultMeasuredEventTypes()735 bool StatCommand::AddDefaultMeasuredEventTypes() {
736   for (auto& name : default_measured_event_types) {
737     // It is not an error when some event types in the default list are not
738     // supported by the kernel.
739     const EventType* type = FindEventTypeByName(name);
740     if (type != nullptr && IsEventAttrSupported(CreateDefaultPerfEventAttr(*type), name)) {
741       if (!event_selection_set_.AddEventType(name)) {
742         return false;
743       }
744     }
745   }
746   if (event_selection_set_.empty()) {
747     LOG(ERROR) << "Failed to add any supported default measured types";
748     return false;
749   }
750   return true;
751 }
752 
SetEventSelectionFlags()753 void StatCommand::SetEventSelectionFlags() {
754   event_selection_set_.SetInherit(child_inherit_);
755 }
756 
MonitorEachThread()757 void StatCommand::MonitorEachThread() {
758   std::vector<pid_t> threads;
759   for (auto pid : event_selection_set_.GetMonitoredProcesses()) {
760     for (auto tid : GetThreadsInProcess(pid)) {
761       ThreadInfo info;
762       if (GetThreadName(tid, &info.name)) {
763         info.tid = tid;
764         info.pid = pid;
765         thread_info_[tid] = std::move(info);
766         threads.push_back(tid);
767       }
768     }
769   }
770   for (auto tid : event_selection_set_.GetMonitoredThreads()) {
771     ThreadInfo info;
772     if (ReadThreadNameAndPid(tid, &info.name, &info.pid)) {
773       info.tid = tid;
774       thread_info_[tid] = std::move(info);
775       threads.push_back(tid);
776     }
777   }
778   event_selection_set_.ClearMonitoredTargets();
779   event_selection_set_.AddMonitoredThreads(threads);
780 }
781 
AdjustToIntervalOnlyValues(std::vector<CountersInfo> & counters)782 void StatCommand::AdjustToIntervalOnlyValues(std::vector<CountersInfo>& counters) {
783   if (last_sum_values_.size() < counters.size()) {
784     last_sum_values_.resize(counters.size());
785   }
786   for (size_t i = 0; i < counters.size(); i++) {
787     std::vector<CounterInfo>& counters_per_event = counters[i].counters;
788     std::vector<CounterSum>& last_sum = last_sum_values_[i];
789 
790     if (last_sum.size() < counters_per_event.size()) {
791       last_sum.resize(counters_per_event.size());
792     }
793     for (size_t j = 0; j < counters_per_event.size(); j++) {
794       PerfCounter& counter = counters_per_event[j].counter;
795       CounterSum new_sum;
796       new_sum.FromCounter(counter);
797       CounterSum delta = new_sum - last_sum[j];
798       delta.ToCounter(counter);
799       last_sum[j] = new_sum;
800     }
801   }
802 }
803 
ShowCounters(const std::vector<CountersInfo> & counters,double duration_in_sec,FILE * fp)804 bool StatCommand::ShowCounters(const std::vector<CountersInfo>& counters, double duration_in_sec,
805                                FILE* fp) {
806   if (csv_) {
807     fprintf(fp, "Performance counter statistics,\n");
808   } else {
809     fprintf(fp, "Performance counter statistics:\n\n");
810   }
811 
812   if (verbose_mode_) {
813     for (auto& counters_info : counters) {
814       for (auto& counter_info : counters_info.counters) {
815         if (csv_) {
816           fprintf(fp,
817                   "%s,tid,%d,cpu,%d,count,%" PRIu64 ",time_enabled,%" PRIu64
818                   ",time running,%" PRIu64 ",id,%" PRIu64 ",\n",
819                   counters_info.event_name.c_str(), counter_info.tid, counter_info.cpu,
820                   counter_info.counter.value, counter_info.counter.time_enabled,
821                   counter_info.counter.time_running, counter_info.counter.id);
822         } else {
823           fprintf(fp,
824                   "%s(tid %d, cpu %d): count %" PRIu64 ", time_enabled %" PRIu64
825                   ", time running %" PRIu64 ", id %" PRIu64 "\n",
826                   counters_info.event_name.c_str(), counter_info.tid, counter_info.cpu,
827                   counter_info.counter.value, counter_info.counter.time_enabled,
828                   counter_info.counter.time_running, counter_info.counter.id);
829         }
830       }
831     }
832   }
833 
834   CounterSummaryBuilder builder(report_per_thread_, report_per_core_, csv_, thread_info_,
835                                 summary_comparator_);
836   for (const auto& info : counters) {
837     builder.AddCountersForOneEventType(info);
838   }
839   CounterSummaries summaries(builder.Build(), csv_);
840   summaries.AutoGenerateSummaries();
841   summaries.GenerateComments(duration_in_sec);
842   summaries.Show(fp);
843 
844   if (csv_)
845     fprintf(fp, "Total test time,%lf,seconds,\n", duration_in_sec);
846   else
847     fprintf(fp, "\nTotal test time: %lf seconds.\n", duration_in_sec);
848 
849   const char* COUNTER_MULTIPLEX_INFO =
850       "probably caused by hardware counter multiplexing (less counters than events).\n"
851       "Try --use-devfreq-counters if on a rooted device.";
852 
853   if (cpus_ == std::vector<int>(1, -1) ||
854       event_selection_set_.GetMonitoredThreads() == std::set<pid_t>({-1})) {
855     // We either monitor a thread on all cpus, or monitor all threads on a cpu. In both cases,
856     // if percentages < 100%, probably it is caused by hardware counter multiplexing.
857     bool counters_always_available = true;
858     for (const auto& summary : summaries.Summaries()) {
859       if (!summary.IsMonitoredAllTheTime()) {
860         counters_always_available = false;
861         break;
862       }
863     }
864     if (!counters_always_available) {
865       LOG(WARNING) << "Percentages < 100% means some events only run a subset of enabled time,\n"
866                    << COUNTER_MULTIPLEX_INFO;
867     }
868   } else if (report_per_thread_) {
869     // We monitor each thread on each cpu.
870     LOG(INFO) << "A percentage represents runtime_on_a_cpu / runtime_on_all_cpus for each thread.\n"
871               << "If percentage sum of a thread < 99%, or report for a running thread is missing,\n"
872               << COUNTER_MULTIPLEX_INFO;
873   } else {
874     // We monitor some threads on each cpu.
875     LOG(INFO) << "A percentage represents runtime_on_a_cpu / runtime_on_all_cpus for monitored\n"
876               << "threads. If percentage sum < 99%, or report for an event is missing,\n"
877               << COUNTER_MULTIPLEX_INFO;
878   }
879   return true;
880 }
881 
882 }  // namespace
883 
RegisterStatCommand()884 void RegisterStatCommand() {
885   RegisterCommand("stat", [] { return std::unique_ptr<Command>(new StatCommand); });
886 }
887 
888 }  // namespace simpleperf
889