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 <set>
26 #include <string>
27 #include <string_view>
28 #include <vector>
29 
30 #include <android-base/file.h>
31 #include <android-base/logging.h>
32 #include <android-base/strings.h>
33 #include <android-base/unique_fd.h>
34 
35 #include "command.h"
36 #include "environment.h"
37 #include "event_attr.h"
38 #include "event_fd.h"
39 #include "event_selection_set.h"
40 #include "event_type.h"
41 #include "IOEventLoop.h"
42 #include "utils.h"
43 #include "workload.h"
44 
45 namespace {
46 
47 static std::vector<std::string> default_measured_event_types{
48     "cpu-cycles",   "stalled-cycles-frontend", "stalled-cycles-backend",
49     "instructions", "branch-instructions",     "branch-misses",
50     "task-clock",   "context-switches",        "page-faults",
51 };
52 
53 struct CounterSum {
54   uint64_t value = 0;
55   uint64_t time_enabled = 0;
56   uint64_t time_running = 0;
57 };
58 
59 struct CounterSummary {
60   std::string type_name;
61   std::string modifier;
62   uint32_t group_id;
63   uint64_t count;
64   double scale;
65   std::string readable_count;
66   std::string comment;
67   bool auto_generated;
68 
CounterSummary__anond81d63db0111::CounterSummary69   CounterSummary(const std::string& type_name, const std::string& modifier,
70                  uint32_t group_id, uint64_t count, double scale,
71                  bool auto_generated, bool csv)
72       : type_name(type_name),
73         modifier(modifier),
74         group_id(group_id),
75         count(count),
76         scale(scale),
77         auto_generated(auto_generated) {
78     readable_count = ReadableCountValue(csv);
79   }
80 
IsMonitoredAtTheSameTime__anond81d63db0111::CounterSummary81   bool IsMonitoredAtTheSameTime(const CounterSummary& other) const {
82     // Two summaries are monitored at the same time if they are in the same
83     // group or are monitored all the time.
84     if (group_id == other.group_id) {
85       return true;
86     }
87     return IsMonitoredAllTheTime() && other.IsMonitoredAllTheTime();
88   }
89 
Name__anond81d63db0111::CounterSummary90   std::string Name() const {
91     if (modifier.empty()) {
92       return type_name;
93     }
94     return type_name + ":" + modifier;
95   }
96 
IsMonitoredAllTheTime__anond81d63db0111::CounterSummary97   bool IsMonitoredAllTheTime() const {
98     // If an event runs all the time it is enabled (by not sharing hardware
99     // counters with other events), the scale of its summary is usually within
100     // [1, 1 + 1e-5]. By setting SCALE_ERROR_LIMIT to 1e-5, We can identify
101     // events monitored all the time in most cases while keeping the report
102     // error rate <= 1e-5.
103     constexpr double SCALE_ERROR_LIMIT = 1e-5;
104     return (fabs(scale - 1.0) < SCALE_ERROR_LIMIT);
105   }
106 
107  private:
ReadableCountValue__anond81d63db0111::CounterSummary108   std::string ReadableCountValue(bool csv) {
109     if (type_name == "cpu-clock" || type_name == "task-clock") {
110       // Convert nanoseconds to milliseconds.
111       double value = count / 1e6;
112       return android::base::StringPrintf("%lf(ms)", value);
113     } else {
114       // Convert big numbers to human friendly mode. For example,
115       // 1000000 will be converted to 1,000,000.
116       std::string s = android::base::StringPrintf("%" PRIu64, count);
117       if (csv) {
118         return s;
119       } else {
120         for (size_t i = s.size() - 1, j = 1; i > 0; --i, ++j) {
121           if (j == 3) {
122             s.insert(s.begin() + i, ',');
123             j = 0;
124           }
125         }
126         return s;
127       }
128     }
129   }
130 };
131 
132 static const std::unordered_map<std::string_view, std::pair<std::string_view, std::string_view>>
133     COMMON_EVENT_RATE_MAP = {
134         {"cache-misses", {"cache-references", "miss rate"}},
135         {"branch-misses", {"branch-instructions", "miss rate"}},
136 };
137 
138 static const std::unordered_map<std::string_view, std::pair<std::string_view, std::string_view>>
139     ARM_EVENT_RATE_MAP = {
140         // Refer to "D6.10.5 Meaningful ratios between common microarchitectural events" in ARMv8
141         // specification.
142         {"raw-l1i-cache-refill", {"raw-l1i-cache", "level 1 instruction cache refill rate"}},
143         {"raw-l1i-tlb-refill", {"raw-l1i-tlb", "level 1 instruction TLB refill rate"}},
144         {"raw-l1d-cache-refill", {"raw-l1d-cache", "level 1 data or unified cache refill rate"}},
145         {"raw-l1d-tlb-refill", {"raw-l1d-tlb", "level 1 data or unified TLB refill rate"}},
146         {"raw-l2d-cache-refill", {"raw-l2d-cache", "level 2 data or unified cache refill rate"}},
147         {"raw-l2i-cache-refill", {"raw-l2i-cache", "level 2 instruction cache refill rate"}},
148         {"raw-l3d-cache-refill", {"raw-l3d-cache", "level 3 data or unified cache refill rate"}},
149         {"raw-l2d-tlb-refill", {"raw-l2d-tlb", "level 2 data or unified TLB refill rate"}},
150         {"raw-l2i-tlb-refill", {"raw-l2i-tlb", "level 2 instruction TLB refill rate"}},
151         {"raw-bus-access", {"raw-bus-cycles", "bus accesses per cycle"}},
152         {"raw-ll-cache-miss", {"raw-ll-cache", "last level data or unified cache refill rate"}},
153         {"raw-dtlb-walk", {"raw-l1d-tlb", "data TLB miss rate"}},
154         {"raw-itlb-walk", {"raw-l1i-tlb", "instruction TLB miss rate"}},
155         {"raw-ll-cache-miss-rd", {"raw-ll-cache-rd", "memory read operation miss rate"}},
156         {"raw-remote-access-rd",
157          {"raw-remote-access", "read accesses to another socket in a multi-socket system"}},
158         // Refer to "Table K3-2 Relationship between REFILL events and associated access events" in
159         // ARMv8 specification.
160         {"raw-l1d-cache-refill-rd", {"raw-l1d-cache-rd", "level 1 cache refill rate, read"}},
161         {"raw-l1d-cache-refill-wr", {"raw-l1d-cache-wr", "level 1 cache refill rate, write"}},
162         {"raw-l1d-tlb-refill-rd", {"raw-l1d-tlb-rd", "level 1 TLB refill rate, read"}},
163         {"raw-l1d-tlb-refill-wr", {"raw-l1d-tlb-wr", "level 1 TLB refill rate, write"}},
164         {"raw-l2d-cache-refill-rd", {"raw-l2d-cache-rd", "level 2 data cache refill rate, read"}},
165         {"raw-l2d-cache-refill-wr", {"raw-l2d-cache-wr", "level 2 data cache refill rate, write"}},
166         {"raw-l2d-tlb-refill-rd", {"raw-l2d-tlb-rd", "level 2 data TLB refill rate, read"}},
167 };
168 
169 class CounterSummaries {
170  public:
CounterSummaries(bool csv)171   explicit CounterSummaries(bool csv) : csv_(csv) {}
Summaries()172   std::vector<CounterSummary>& Summaries() { return summaries_; }
173 
FindSummary(const std::string & type_name,const std::string & modifier)174   const CounterSummary* FindSummary(const std::string& type_name,
175                                     const std::string& modifier) {
176     for (const auto& s : summaries_) {
177       if (s.type_name == type_name && s.modifier == modifier) {
178         return &s;
179       }
180     }
181     return nullptr;
182   }
183 
184   // If we have two summaries monitoring the same event type at the same time,
185   // that one is for user space only, and the other is for kernel space only;
186   // then we can automatically generate a summary combining the two results.
187   // For example, a summary of branch-misses:u and a summary for branch-misses:k
188   // can generate a summary of branch-misses.
AutoGenerateSummaries()189   void AutoGenerateSummaries() {
190     for (size_t i = 0; i < summaries_.size(); ++i) {
191       const CounterSummary& s = summaries_[i];
192       if (s.modifier == "u") {
193         const CounterSummary* other = FindSummary(s.type_name, "k");
194         if (other != nullptr && other->IsMonitoredAtTheSameTime(s)) {
195           if (FindSummary(s.type_name, "") == nullptr) {
196             Summaries().emplace_back(s.type_name, "", s.group_id, s.count + other->count, s.scale,
197                                      true, csv_);
198           }
199         }
200       }
201     }
202   }
203 
GenerateComments(double duration_in_sec)204   void GenerateComments(double duration_in_sec) {
205     for (auto& s : summaries_) {
206       s.comment = GetCommentForSummary(s, duration_in_sec);
207     }
208   }
209 
Show(FILE * fp)210   void Show(FILE* fp) {
211     size_t count_column_width = 0;
212     size_t name_column_width = 0;
213     size_t comment_column_width = 0;
214     for (auto& s : summaries_) {
215       count_column_width =
216           std::max(count_column_width, s.readable_count.size());
217       name_column_width = std::max(name_column_width, s.Name().size());
218       comment_column_width = std::max(comment_column_width, s.comment.size());
219     }
220 
221     for (auto& s : summaries_) {
222       if (csv_) {
223         fprintf(fp, "%s,%s,%s,(%.0lf%%)%s\n", s.readable_count.c_str(),
224                 s.Name().c_str(), s.comment.c_str(), 1.0 / s.scale * 100,
225                 (s.auto_generated ? " (generated)," : ","));
226       } else {
227         fprintf(fp, "  %*s  %-*s   # %-*s  (%.0lf%%)%s\n",
228                 static_cast<int>(count_column_width), s.readable_count.c_str(),
229                 static_cast<int>(name_column_width), s.Name().c_str(),
230                 static_cast<int>(comment_column_width), s.comment.c_str(),
231                 1.0 / s.scale * 100, (s.auto_generated ? " (generated)" : ""));
232       }
233     }
234   }
235 
236  private:
GetCommentForSummary(const CounterSummary & s,double duration_in_sec)237   std::string GetCommentForSummary(const CounterSummary& s,
238                                    double duration_in_sec) {
239     char sap_mid;
240     if (csv_) {
241       sap_mid = ',';
242     } else {
243       sap_mid = ' ';
244     }
245     if (s.type_name == "task-clock") {
246       double run_sec = s.count / 1e9;
247       double used_cpus = run_sec / (duration_in_sec / s.scale);
248       return android::base::StringPrintf("%lf%ccpus used", used_cpus, sap_mid);
249     }
250     if (s.type_name == "cpu-clock") {
251       return "";
252     }
253     if (s.type_name == "cpu-cycles") {
254       double running_time_in_sec;
255       if (!FindRunningTimeForSummary(s, &running_time_in_sec)) {
256         return "";
257       }
258       double hz = s.count / (running_time_in_sec / s.scale);
259       return android::base::StringPrintf("%lf%cGHz", hz / 1e9, sap_mid);
260     }
261     if (s.type_name == "instructions" && s.count != 0) {
262       const CounterSummary* other = FindSummary("cpu-cycles", s.modifier);
263       if (other != nullptr && other->IsMonitoredAtTheSameTime(s)) {
264         double cpi = static_cast<double>(other->count) / s.count;
265         return android::base::StringPrintf("%lf%ccycles per instruction", cpi,
266                                            sap_mid);
267       }
268     }
269     std::string rate_comment = GetRateComment(s, sap_mid);
270     if (!rate_comment.empty()) {
271       return rate_comment;
272     }
273     double running_time_in_sec;
274     if (!FindRunningTimeForSummary(s, &running_time_in_sec)) {
275       return "";
276     }
277     double rate = s.count / (running_time_in_sec / s.scale);
278     if (rate > 1e9) {
279       return android::base::StringPrintf("%.3lf%cG/sec", rate / 1e9, sap_mid);
280     }
281     if (rate > 1e6) {
282       return android::base::StringPrintf("%.3lf%cM/sec", rate / 1e6, sap_mid);
283     }
284     if (rate > 1e3) {
285       return android::base::StringPrintf("%.3lf%cK/sec", rate / 1e3, sap_mid);
286     }
287     return android::base::StringPrintf("%.3lf%c/sec", rate, sap_mid);
288   }
289 
GetRateComment(const CounterSummary & s,char sep)290   std::string GetRateComment(const CounterSummary& s, char sep) {
291     std::string_view miss_event_name = s.type_name;
292     std::string event_name;
293     std::string rate_desc;
294     if (auto it = COMMON_EVENT_RATE_MAP.find(miss_event_name); it != COMMON_EVENT_RATE_MAP.end()) {
295       event_name = it->second.first;
296       rate_desc = it->second.second;
297     }
298     if (event_name.empty() && (GetBuildArch() == ARCH_ARM || GetBuildArch() == ARCH_ARM64)) {
299       if (auto it = ARM_EVENT_RATE_MAP.find(miss_event_name); it != ARM_EVENT_RATE_MAP.end()) {
300         event_name = it->second.first;
301         rate_desc = it->second.second;
302       }
303     }
304     if (event_name.empty() && android::base::ConsumeSuffix(&miss_event_name, "-misses")) {
305       event_name = std::string(miss_event_name) + "s";
306       rate_desc = "miss rate";
307     }
308     if (!event_name.empty()) {
309       const CounterSummary* other = FindSummary(event_name, s.modifier);
310       if (other != nullptr && other->IsMonitoredAtTheSameTime(s) && other->count != 0) {
311         double miss_rate = static_cast<double>(s.count) / other->count;
312         return android::base::StringPrintf("%f%%%c%s", miss_rate * 100, sep, rate_desc.c_str());
313       }
314     }
315     return "";
316   }
317 
FindRunningTimeForSummary(const CounterSummary & summary,double * running_time_in_sec)318   bool FindRunningTimeForSummary(const CounterSummary& summary, double* running_time_in_sec) {
319     for (auto& s : summaries_) {
320       if ((s.type_name == "task-clock" || s.type_name == "cpu-clock") &&
321           s.IsMonitoredAtTheSameTime(summary) && s.count != 0u) {
322         *running_time_in_sec = s.count / 1e9;
323         return true;
324       }
325     }
326     return false;
327   }
328 
329  private:
330   std::vector<CounterSummary> summaries_;
331   bool csv_;
332 };
333 
334 // devfreq may use performance counters to calculate memory latency (as in
335 // drivers/devfreq/arm-memlat-mon.c). Hopefully we can get more available counters by asking devfreq
336 // to not use the memory latency governor temporarily.
337 class DevfreqCounters {
338  public:
Use()339   bool Use() {
340     if (!IsRoot()) {
341       LOG(ERROR) << "--use-devfreq-counters needs root permission to set devfreq governors";
342       return false;
343     }
344     std::string devfreq_dir = "/sys/class/devfreq/";
345     for (auto& name : GetSubDirs(devfreq_dir)) {
346       std::string governor_path = devfreq_dir + name + "/governor";
347       if (IsRegularFile(governor_path)) {
348         std::string governor;
349         if (!android::base::ReadFileToString(governor_path, &governor)) {
350           LOG(ERROR) << "failed to read " << governor_path;
351           return false;
352         }
353         governor = android::base::Trim(governor);
354         if (governor == "mem_latency") {
355           if (!android::base::WriteStringToFile("performance", governor_path)) {
356             PLOG(ERROR) << "failed to write " << governor_path;
357             return false;
358           }
359           mem_latency_governor_paths_.emplace_back(std::move(governor_path));
360         }
361       }
362     }
363     return true;
364   }
365 
~DevfreqCounters()366   ~DevfreqCounters() {
367     for (auto& path : mem_latency_governor_paths_) {
368       android::base::WriteStringToFile("mem_latency", path);
369     }
370   }
371 
372  private:
373   std::vector<std::string> mem_latency_governor_paths_;
374 };
375 
376 class StatCommand : public Command {
377  public:
StatCommand()378   StatCommand()
379       : Command("stat", "gather performance counter information",
380                 // clang-format off
381 "Usage: simpleperf stat [options] [command [command-args]]\n"
382 "       Gather performance counter information of running [command].\n"
383 "       And -a/-p/-t option can be used to change target of counter information.\n"
384 "-a           Collect system-wide information.\n"
385 #if defined(__ANDROID__)
386 "--app package_name    Profile the process of an Android application.\n"
387 "                      On non-rooted devices, the app must be debuggable,\n"
388 "                      because we use run-as to switch to the app's context.\n"
389 #endif
390 "--cpu cpu_item1,cpu_item2,...\n"
391 "                 Collect information only on the selected cpus. cpu_item can\n"
392 "                 be a cpu number like 1, or a cpu range like 0-3.\n"
393 "--csv            Write report in comma separate form.\n"
394 "--duration time_in_sec  Monitor for time_in_sec seconds instead of running\n"
395 "                        [command]. Here time_in_sec may be any positive\n"
396 "                        floating point number.\n"
397 "--interval time_in_ms   Print stat for every time_in_ms milliseconds.\n"
398 "                        Here time_in_ms may be any positive floating point\n"
399 "                        number. Simpleperf prints total values from the\n"
400 "                        starting point. But this can be changed by\n"
401 "                        --interval-only-values.\n"
402 "--interval-only-values  Print numbers of events happened in each interval.\n"
403 "-e event1[:modifier1],event2[:modifier2],...\n"
404 "                 Select a list of events to count. An event can be:\n"
405 "                   1) an event name listed in `simpleperf list`;\n"
406 "                   2) a raw PMU event in rN format. N is a hex number.\n"
407 "                      For example, r1b selects event number 0x1b.\n"
408 "                 Modifiers can be added to define how the event should be\n"
409 "                 monitored. Possible modifiers are:\n"
410 "                   u - monitor user space events only\n"
411 "                   k - monitor kernel space events only\n"
412 "--group event1[:modifier],event2[:modifier2],...\n"
413 "             Similar to -e option. But events specified in the same --group\n"
414 "             option are monitored as a group, and scheduled in and out at the\n"
415 "             same time.\n"
416 "--no-inherit     Don't stat created child threads/processes.\n"
417 "-o output_filename  Write report to output_filename instead of standard output.\n"
418 "-p pid1,pid2,... Stat events on existing processes. Mutually exclusive with -a.\n"
419 "-t tid1,tid2,... Stat events on existing threads. Mutually exclusive with -a.\n"
420 #if defined(__ANDROID__)
421 "--use-devfreq-counters    On devices with Qualcomm SOCs, some hardware counters may be used\n"
422 "                          to monitor memory latency (in drivers/devfreq/arm-memlat-mon.c),\n"
423 "                          making fewer counters available to users. This option asks devfreq\n"
424 "                          to temporarily release counters by replacing memory-latency governor\n"
425 "                          with performance governor. It affects memory latency during profiling,\n"
426 "                          and may cause wedged power if simpleperf is killed in between.\n"
427 #endif
428 "--verbose        Show result in verbose mode.\n"
429 #if 0
430 // Below options are only used internally and shouldn't be visible to the public.
431 "--in-app         We are already running in the app's context.\n"
432 "--tracepoint-events file_name   Read tracepoint events from [file_name] instead of tracefs.\n"
433 "--out-fd <fd>    Write output to a file descriptor.\n"
434 "--stop-signal-fd <fd>   Stop stating when fd is readable.\n"
435 #endif
436                 // clang-format on
437                 ),
438         verbose_mode_(false),
439         system_wide_collection_(false),
440         child_inherit_(true),
441         duration_in_sec_(0),
442         interval_in_ms_(0),
443         interval_only_values_(false),
444         event_selection_set_(true),
445         csv_(false),
446         in_app_context_(false) {
447     // Die if parent exits.
448     prctl(PR_SET_PDEATHSIG, SIGHUP, 0, 0, 0);
449   }
450 
451   bool Run(const std::vector<std::string>& args);
452 
453  private:
454   bool ParseOptions(const std::vector<std::string>& args,
455                     std::vector<std::string>* non_option_args);
456   bool AddDefaultMeasuredEventTypes();
457   void SetEventSelectionFlags();
458   bool ShowCounters(const std::vector<CountersInfo>& counters,
459                     double duration_in_sec, FILE* fp);
460 
461   bool verbose_mode_;
462   bool system_wide_collection_;
463   bool child_inherit_;
464   double duration_in_sec_;
465   double interval_in_ms_;
466   bool interval_only_values_;
467   std::vector<CounterSum> last_sum_values_;
468   std::vector<int> cpus_;
469   EventSelectionSet event_selection_set_;
470   std::string output_filename_;
471   android::base::unique_fd out_fd_;
472   bool csv_;
473   std::string app_package_name_;
474   bool in_app_context_;
475   android::base::unique_fd stop_signal_fd_;
476   bool use_devfreq_counters_ = false;
477 };
478 
Run(const std::vector<std::string> & args)479 bool StatCommand::Run(const std::vector<std::string>& args) {
480   if (!CheckPerfEventLimit()) {
481     return false;
482   }
483 
484   // 1. Parse options, and use default measured event types if not given.
485   std::vector<std::string> workload_args;
486   if (!ParseOptions(args, &workload_args)) {
487     return false;
488   }
489   if (!app_package_name_.empty() && !in_app_context_) {
490     if (!IsRoot()) {
491       return RunInAppContext(app_package_name_, "stat", args, workload_args.size(),
492                              output_filename_, !event_selection_set_.GetTracepointEvents().empty());
493     }
494   }
495   DevfreqCounters devfreq_counters;
496   if (use_devfreq_counters_) {
497     if (!devfreq_counters.Use()) {
498       return false;
499     }
500   }
501   if (event_selection_set_.empty()) {
502     if (!AddDefaultMeasuredEventTypes()) {
503       return false;
504     }
505   }
506   SetEventSelectionFlags();
507 
508   // 2. Create workload.
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     }
515   }
516   bool need_to_check_targets = false;
517   if (system_wide_collection_) {
518     event_selection_set_.AddMonitoredThreads({-1});
519   } else if (!event_selection_set_.HasMonitoredTarget()) {
520     if (workload != nullptr) {
521       event_selection_set_.AddMonitoredProcesses({workload->GetPid()});
522       event_selection_set_.SetEnableOnExec(true);
523     } else if (!app_package_name_.empty()) {
524       std::set<pid_t> pids = WaitForAppProcesses(app_package_name_);
525       event_selection_set_.AddMonitoredProcesses(pids);
526     } else {
527       LOG(ERROR)
528           << "No threads to monitor. Try `simpleperf help stat` for help\n";
529       return false;
530     }
531   } else {
532     need_to_check_targets = true;
533   }
534 
535   // 3. Open perf_event_files and output file if defined.
536   if (!system_wide_collection_ && cpus_.empty()) {
537     cpus_.push_back(-1);  // Monitor on all cpus.
538   }
539   if (!event_selection_set_.OpenEventFiles(cpus_)) {
540     return false;
541   }
542   std::unique_ptr<FILE, decltype(&fclose)> fp_holder(nullptr, fclose);
543   if (!output_filename_.empty()) {
544     fp_holder.reset(fopen(output_filename_.c_str(), "we"));
545     if (fp_holder == nullptr) {
546       PLOG(ERROR) << "failed to open " << output_filename_;
547       return false;
548     }
549   } else if (out_fd_ != -1) {
550     fp_holder.reset(fdopen(out_fd_.release(), "we"));
551     if (fp_holder == nullptr) {
552       PLOG(ERROR) << "failed to write output.";
553       return false;
554     }
555   }
556   FILE* fp = fp_holder ? fp_holder.get() : stdout;
557 
558   // 4. Add signal/periodic Events.
559   IOEventLoop* loop = event_selection_set_.GetIOEventLoop();
560   if (interval_in_ms_ != 0) {
561     if (!loop->UsePreciseTimer()) {
562       return false;
563     }
564   }
565   std::chrono::time_point<std::chrono::steady_clock> start_time;
566   std::vector<CountersInfo> counters;
567   if (system_wide_collection_ || (!cpus_.empty() && cpus_[0] != -1)) {
568     if (!event_selection_set_.HandleCpuHotplugEvents(cpus_)) {
569       return false;
570     }
571   }
572   if (need_to_check_targets && !event_selection_set_.StopWhenNoMoreTargets()) {
573     return false;
574   }
575   auto exit_loop_callback = [loop]() {
576     return loop->ExitLoop();
577   };
578   if (!loop->AddSignalEvents({SIGCHLD, SIGINT, SIGTERM, SIGHUP}, exit_loop_callback)) {
579     return false;
580   }
581   if (stop_signal_fd_ != -1) {
582     if (!loop->AddReadEvent(stop_signal_fd_, exit_loop_callback)) {
583       return false;
584     }
585   }
586   if (duration_in_sec_ != 0) {
587     if (!loop->AddPeriodicEvent(SecondToTimeval(duration_in_sec_), exit_loop_callback)) {
588       return false;
589     }
590   }
591   auto print_counters = [&]() {
592       auto end_time = std::chrono::steady_clock::now();
593       if (!event_selection_set_.ReadCounters(&counters)) {
594         return false;
595       }
596       double duration_in_sec =
597       std::chrono::duration_cast<std::chrono::duration<double>>(end_time -
598                                                                 start_time)
599       .count();
600       if (!ShowCounters(counters, duration_in_sec, fp)) {
601         return false;
602       }
603       return true;
604   };
605 
606   if (interval_in_ms_ != 0) {
607     if (!loop->AddPeriodicEvent(SecondToTimeval(interval_in_ms_ / 1000.0),
608                                 print_counters)) {
609       return false;
610     }
611   }
612 
613   // 5. Count events while workload running.
614   start_time = std::chrono::steady_clock::now();
615   if (workload != nullptr && !workload->Start()) {
616     return false;
617   }
618   if (!loop->RunLoop()) {
619     return false;
620   }
621 
622   // 6. Read and print counters.
623   if (interval_in_ms_ == 0) {
624     return print_counters();
625   }
626   return true;
627 }
628 
ParseOptions(const std::vector<std::string> & args,std::vector<std::string> * non_option_args)629 bool StatCommand::ParseOptions(const std::vector<std::string>& args,
630                                std::vector<std::string>* non_option_args) {
631   std::set<pid_t> tid_set;
632   size_t i;
633   for (i = 0; i < args.size() && args[i].size() > 0 && args[i][0] == '-'; ++i) {
634     if (args[i] == "-a") {
635       system_wide_collection_ = true;
636     } else if (args[i] == "--app") {
637       if (!NextArgumentOrError(args, &i)) {
638         return false;
639       }
640       app_package_name_ = args[i];
641     } else if (args[i] == "--cpu") {
642       if (!NextArgumentOrError(args, &i)) {
643         return false;
644       }
645       cpus_ = GetCpusFromString(args[i]);
646     } else if (args[i] == "--csv") {
647       csv_ = true;
648     } else if (args[i] == "--duration") {
649       if (!GetDoubleOption(args, &i, &duration_in_sec_, 1e-9)) {
650         return false;
651       }
652     } else if (args[i] == "--interval") {
653       if (!GetDoubleOption(args, &i, &interval_in_ms_, 1e-9)) {
654         return false;
655       }
656     } else if (args[i] == "--interval-only-values") {
657       interval_only_values_ = true;
658     } else if (args[i] == "-e") {
659       if (!NextArgumentOrError(args, &i)) {
660         return false;
661       }
662       std::vector<std::string> event_types = android::base::Split(args[i], ",");
663       for (auto& event_type : event_types) {
664         if (!event_selection_set_.AddEventType(event_type)) {
665           return false;
666         }
667       }
668     } else if (args[i] == "--group") {
669       if (!NextArgumentOrError(args, &i)) {
670         return false;
671       }
672       std::vector<std::string> event_types = android::base::Split(args[i], ",");
673       if (!event_selection_set_.AddEventGroup(event_types)) {
674         return false;
675       }
676     } else if (args[i] == "--in-app") {
677       in_app_context_ = true;
678     } else if (args[i] == "--no-inherit") {
679       child_inherit_ = false;
680     } else if (args[i] == "-o") {
681       if (!NextArgumentOrError(args, &i)) {
682         return false;
683       }
684       output_filename_ = args[i];
685     } else if (args[i] == "--out-fd") {
686       int fd;
687       if (!GetUintOption(args, &i, &fd)) {
688         return false;
689       }
690       out_fd_.reset(fd);
691     } else if (args[i] == "-p") {
692       if (!NextArgumentOrError(args, &i)) {
693         return false;
694       }
695       std::set<pid_t> pids;
696       if (!GetValidThreadsFromThreadString(args[i], &pids)) {
697         return false;
698       }
699       event_selection_set_.AddMonitoredProcesses(pids);
700     } else if (args[i] == "--stop-signal-fd") {
701       int fd;
702       if (!GetUintOption(args, &i, &fd)) {
703         return false;
704       }
705       stop_signal_fd_.reset(fd);
706     } else if (args[i] == "-t") {
707       if (!NextArgumentOrError(args, &i)) {
708         return false;
709       }
710       std::set<pid_t> tids;
711       if (!GetValidThreadsFromThreadString(args[i], &tids)) {
712         return false;
713       }
714       event_selection_set_.AddMonitoredThreads(tids);
715     } else if (args[i] == "--tracepoint-events") {
716       if (!NextArgumentOrError(args, &i)) {
717         return false;
718       }
719       if (!SetTracepointEventsFilePath(args[i])) {
720         return false;
721       }
722 #if defined(__ANDROID__)
723     } else if (args[i] == "--use-devfreq-counters") {
724       use_devfreq_counters_ = true;
725 #endif
726     } else if (args[i] == "--verbose") {
727       verbose_mode_ = true;
728     } else {
729       ReportUnknownOption(args, i);
730       return false;
731     }
732   }
733 
734   if (system_wide_collection_ && event_selection_set_.HasMonitoredTarget()) {
735     LOG(ERROR) << "Stat system wide and existing processes/threads can't be "
736                   "used at the same time.";
737     return false;
738   }
739   if (system_wide_collection_ && !IsRoot()) {
740     LOG(ERROR) << "System wide profiling needs root privilege.";
741     return false;
742   }
743 
744   non_option_args->clear();
745   for (; i < args.size(); ++i) {
746     non_option_args->push_back(args[i]);
747   }
748   return true;
749 }
750 
AddDefaultMeasuredEventTypes()751 bool StatCommand::AddDefaultMeasuredEventTypes() {
752   for (auto& name : default_measured_event_types) {
753     // It is not an error when some event types in the default list are not
754     // supported by the kernel.
755     const EventType* type = FindEventTypeByName(name);
756     if (type != nullptr &&
757         IsEventAttrSupported(CreateDefaultPerfEventAttr(*type))) {
758       if (!event_selection_set_.AddEventType(name)) {
759         return false;
760       }
761     }
762   }
763   if (event_selection_set_.empty()) {
764     LOG(ERROR) << "Failed to add any supported default measured types";
765     return false;
766   }
767   return true;
768 }
769 
SetEventSelectionFlags()770 void StatCommand::SetEventSelectionFlags() {
771   event_selection_set_.SetInherit(child_inherit_);
772 }
773 
ShowCounters(const std::vector<CountersInfo> & counters,double duration_in_sec,FILE * fp)774 bool StatCommand::ShowCounters(const std::vector<CountersInfo>& counters,
775                                double duration_in_sec, FILE* fp) {
776   if (csv_) {
777     fprintf(fp, "Performance counter statistics,\n");
778   } else {
779     fprintf(fp, "Performance counter statistics:\n\n");
780   }
781 
782   if (verbose_mode_) {
783     for (auto& counters_info : counters) {
784       for (auto& counter_info : counters_info.counters) {
785         if (csv_) {
786           fprintf(fp, "%s,tid,%d,cpu,%d,count,%" PRIu64 ",time_enabled,%" PRIu64
787                       ",time running,%" PRIu64 ",id,%" PRIu64 ",\n",
788                   counters_info.event_name.c_str(), counter_info.tid,
789                   counter_info.cpu, counter_info.counter.value,
790                   counter_info.counter.time_enabled,
791                   counter_info.counter.time_running, counter_info.counter.id);
792         } else {
793           fprintf(fp,
794                   "%s(tid %d, cpu %d): count %" PRIu64 ", time_enabled %" PRIu64
795                   ", time running %" PRIu64 ", id %" PRIu64 "\n",
796                   counters_info.event_name.c_str(), counter_info.tid,
797                   counter_info.cpu, counter_info.counter.value,
798                   counter_info.counter.time_enabled,
799                   counter_info.counter.time_running, counter_info.counter.id);
800         }
801       }
802     }
803   }
804 
805   bool counters_always_available = true;
806   CounterSummaries summaries(csv_);
807   for (size_t i = 0; i < counters.size(); ++i) {
808     const CountersInfo& counters_info = counters[i];
809     CounterSum sum;
810     for (auto& counter_info : counters_info.counters) {
811       sum.value += counter_info.counter.value;
812       sum.time_enabled += counter_info.counter.time_enabled;
813       sum.time_running += counter_info.counter.time_running;
814     }
815     if (interval_only_values_) {
816       if (last_sum_values_.size() < counters.size()) {
817         last_sum_values_.resize(counters.size());
818       }
819       CounterSum tmp = sum;
820       sum.value -= last_sum_values_[i].value;
821       sum.time_enabled -= last_sum_values_[i].time_enabled;
822       sum.time_running -= last_sum_values_[i].time_running;
823       last_sum_values_[i] = tmp;
824     }
825 
826     double scale = 1.0;
827     if (sum.time_running < sum.time_enabled && sum.time_running != 0) {
828       scale = static_cast<double>(sum.time_enabled) / sum.time_running;
829     }
830     summaries.Summaries().emplace_back(counters_info.event_name, counters_info.event_modifier,
831                                        counters_info.group_id, sum.value, scale, false, csv_);
832     counters_always_available &= summaries.Summaries().back().IsMonitoredAllTheTime();
833   }
834   summaries.AutoGenerateSummaries();
835   summaries.GenerateComments(duration_in_sec);
836   summaries.Show(fp);
837 
838   if (csv_)
839     fprintf(fp, "Total test time,%lf,seconds,\n", duration_in_sec);
840   else
841     fprintf(fp, "\nTotal test time: %lf seconds.\n", duration_in_sec);
842 
843   if (!counters_always_available) {
844     LOG(WARNING) << "Some hardware counters are not always available (scale < 100%). "
845                  << "Try --use-devfreq-counters if on a rooted device.";
846   }
847   return true;
848 }
849 
850 }  // namespace
851 
RegisterStatCommand()852 void RegisterStatCommand() {
853   RegisterCommand("stat",
854                   [] { return std::unique_ptr<Command>(new StatCommand); });
855 }
856