OSDN Git Service

Merge "Error correction: Add libfec to read encoded data"
[android-x86/system-extras.git] / simpleperf / cmd_report.cpp
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 <algorithm>
19 #include <functional>
20 #include <map>
21 #include <set>
22 #include <string>
23 #include <unordered_map>
24 #include <unordered_set>
25 #include <vector>
26
27 #include <base/logging.h>
28 #include <base/stringprintf.h>
29 #include <base/strings.h>
30
31 #include "command.h"
32 #include "dwarf_unwind.h"
33 #include "environment.h"
34 #include "event_attr.h"
35 #include "event_type.h"
36 #include "perf_regs.h"
37 #include "record.h"
38 #include "record_file.h"
39 #include "sample_tree.h"
40 #include "thread_tree.h"
41 #include "utils.h"
42
43 class Displayable {
44  public:
45   Displayable(const std::string& name) : name_(name), width_(name.size()) {
46   }
47
48   virtual ~Displayable() {
49   }
50
51   const std::string& Name() const {
52     return name_;
53   }
54   size_t Width() const {
55     return width_;
56   }
57
58   virtual std::string Show(const SampleEntry& sample) const = 0;
59   void AdjustWidth(const SampleEntry& sample) {
60     size_t size = Show(sample).size();
61     width_ = std::max(width_, size);
62   }
63
64  private:
65   const std::string name_;
66   size_t width_;
67 };
68
69 class AccumulatedOverheadItem : public Displayable {
70  public:
71   AccumulatedOverheadItem(const SampleTree& sample_tree)
72       : Displayable("Children"), sample_tree_(sample_tree) {
73   }
74
75   std::string Show(const SampleEntry& sample) const override {
76     uint64_t period = sample.period + sample.accumulated_period;
77     uint64_t total_period = sample_tree_.TotalPeriod();
78     double percentage = (total_period != 0) ? 100.0 * period / total_period : 0.0;
79     return android::base::StringPrintf("%.2lf%%", percentage);
80   }
81
82  private:
83   const SampleTree& sample_tree_;
84 };
85
86 class SelfOverheadItem : public Displayable {
87  public:
88   SelfOverheadItem(const SampleTree& sample_tree, const std::string& name = "Self")
89       : Displayable(name), sample_tree_(sample_tree) {
90   }
91
92   std::string Show(const SampleEntry& sample) const override {
93     uint64_t period = sample.period;
94     uint64_t total_period = sample_tree_.TotalPeriod();
95     double percentage = (total_period != 0) ? 100.0 * period / total_period : 0.0;
96     return android::base::StringPrintf("%.2lf%%", percentage);
97   }
98
99  private:
100   const SampleTree& sample_tree_;
101 };
102
103 class SampleCountItem : public Displayable {
104  public:
105   SampleCountItem() : Displayable("Sample") {
106   }
107
108   std::string Show(const SampleEntry& sample) const override {
109     return android::base::StringPrintf("%" PRId64, sample.sample_count);
110   }
111 };
112
113 class Comparable {
114  public:
115   virtual ~Comparable() {
116   }
117
118   virtual int Compare(const SampleEntry& sample1, const SampleEntry& sample2) const = 0;
119 };
120
121 class PidItem : public Displayable, public Comparable {
122  public:
123   PidItem() : Displayable("Pid") {
124   }
125
126   int Compare(const SampleEntry& sample1, const SampleEntry& sample2) const override {
127     return sample1.thread->pid - sample2.thread->pid;
128   }
129
130   std::string Show(const SampleEntry& sample) const override {
131     return android::base::StringPrintf("%d", sample.thread->pid);
132   }
133 };
134
135 class TidItem : public Displayable, public Comparable {
136  public:
137   TidItem() : Displayable("Tid") {
138   }
139
140   int Compare(const SampleEntry& sample1, const SampleEntry& sample2) const override {
141     return sample1.thread->tid - sample2.thread->tid;
142   }
143
144   std::string Show(const SampleEntry& sample) const override {
145     return android::base::StringPrintf("%d", sample.thread->tid);
146   }
147 };
148
149 class CommItem : public Displayable, public Comparable {
150  public:
151   CommItem() : Displayable("Command") {
152   }
153
154   int Compare(const SampleEntry& sample1, const SampleEntry& sample2) const override {
155     return strcmp(sample1.thread_comm, sample2.thread_comm);
156   }
157
158   std::string Show(const SampleEntry& sample) const override {
159     return sample.thread_comm;
160   }
161 };
162
163 class DsoItem : public Displayable, public Comparable {
164  public:
165   DsoItem(const std::string& name = "Shared Object") : Displayable(name) {
166   }
167
168   int Compare(const SampleEntry& sample1, const SampleEntry& sample2) const override {
169     return strcmp(sample1.map->dso->Path().c_str(), sample2.map->dso->Path().c_str());
170   }
171
172   std::string Show(const SampleEntry& sample) const override {
173     return sample.map->dso->Path();
174   }
175 };
176
177 class SymbolItem : public Displayable, public Comparable {
178  public:
179   SymbolItem(const std::string& name = "Symbol") : Displayable(name) {
180   }
181
182   int Compare(const SampleEntry& sample1, const SampleEntry& sample2) const override {
183     return strcmp(sample1.symbol->DemangledName(), sample2.symbol->DemangledName());
184   }
185
186   std::string Show(const SampleEntry& sample) const override {
187     return sample.symbol->DemangledName();
188   }
189 };
190
191 class DsoFromItem : public Displayable, public Comparable {
192  public:
193   DsoFromItem() : Displayable("Source Shared Object") {
194   }
195
196   int Compare(const SampleEntry& sample1, const SampleEntry& sample2) const override {
197     return strcmp(sample1.branch_from.map->dso->Path().c_str(),
198                   sample2.branch_from.map->dso->Path().c_str());
199   }
200
201   std::string Show(const SampleEntry& sample) const override {
202     return sample.branch_from.map->dso->Path();
203   }
204 };
205
206 class DsoToItem : public DsoItem {
207  public:
208   DsoToItem() : DsoItem("Target Shared Object") {
209   }
210 };
211
212 class SymbolFromItem : public Displayable, public Comparable {
213  public:
214   SymbolFromItem() : Displayable("Source Symbol") {
215   }
216
217   int Compare(const SampleEntry& sample1, const SampleEntry& sample2) const override {
218     return strcmp(sample1.branch_from.symbol->DemangledName(),
219                   sample2.branch_from.symbol->DemangledName());
220   }
221
222   std::string Show(const SampleEntry& sample) const override {
223     return sample.branch_from.symbol->DemangledName();
224   }
225 };
226
227 class SymbolToItem : public SymbolItem {
228  public:
229   SymbolToItem() : SymbolItem("Target Symbol") {
230   }
231 };
232
233 static std::set<std::string> branch_sort_keys = {
234     "dso_from", "dso_to", "symbol_from", "symbol_to",
235 };
236
237 class ReportCommand : public Command {
238  public:
239   ReportCommand()
240       : Command(
241             "report", "report sampling information in perf.data",
242             "Usage: simpleperf report [options]\n"
243             "    -b            Use the branch-to addresses in sampled take branches instead of\n"
244             "                  the instruction addresses. Only valid for perf.data recorded with\n"
245             "                  -b/-j option.\n"
246             "    --children    Print the overhead accumulated by appearing in the callchain.\n"
247             "    --comms comm1,comm2,...\n"
248             "                  Report only for selected comms.\n"
249             "    --dsos dso1,dso2,...\n"
250             "                  Report only for selected dsos.\n"
251             "    -g [callee|caller]\n"
252             "                  Print call graph. If callee mode is used, the graph shows how\n"
253             "                  functions are called from others. Otherwise, the graph shows how\n"
254             "                  functions call others. Default is callee mode.\n"
255             "    -i <file>     Specify path of record file, default is perf.data.\n"
256             "    -n            Print the sample count for each item.\n"
257             "    --no-demangle        Don't demangle symbol names.\n"
258             "    --pid pid1,pid2,...\n"
259             "                  Report only for selected pids.\n"
260             "    --sort key1,key2,...\n"
261             "                  Select the keys to sort and print the report. Possible keys\n"
262             "                  include pid, tid, comm, dso, symbol, dso_from, dso_to, symbol_from\n"
263             "                  symbol_to. dso_from, dso_to, symbol_from, symbol_to can only be\n"
264             "                  used with -b option. Default keys are \"comm,pid,tid,dso,symbol\"\n"
265             "    --symfs <dir> Look for files with symbols relative to this directory.\n"
266             "    --tids tid1,tid2,...\n"
267             "                  Report only for selected tids.\n"
268             "    --vmlinux <file>\n"
269             "                  Parse kernel symbols from <file>.\n"),
270         record_filename_("perf.data"),
271         use_branch_address_(false),
272         accumulate_callchain_(false),
273         print_callgraph_(false),
274         callgraph_show_callee_(true) {
275     compare_sample_func_t compare_sample_callback = std::bind(
276         &ReportCommand::CompareSampleEntry, this, std::placeholders::_1, std::placeholders::_2);
277     sample_tree_ =
278         std::unique_ptr<SampleTree>(new SampleTree(&thread_tree_, compare_sample_callback));
279   }
280
281   bool Run(const std::vector<std::string>& args);
282
283  private:
284   bool ParseOptions(const std::vector<std::string>& args);
285   bool ReadEventAttrFromRecordFile();
286   void ReadSampleTreeFromRecordFile();
287   void ProcessSampleRecord(const SampleRecord& r);
288   bool ReadFeaturesFromRecordFile();
289   int CompareSampleEntry(const SampleEntry& sample1, const SampleEntry& sample2);
290   void PrintReport();
291   void PrintReportContext();
292   void CollectReportWidth();
293   void CollectReportEntryWidth(const SampleEntry& sample);
294   void PrintReportHeader();
295   void PrintReportEntry(const SampleEntry& sample);
296   void PrintCallGraph(const SampleEntry& sample);
297
298   std::string record_filename_;
299   std::unique_ptr<RecordFileReader> record_file_reader_;
300   perf_event_attr event_attr_;
301   std::vector<std::unique_ptr<Displayable>> displayable_items_;
302   std::vector<Comparable*> comparable_items_;
303   ThreadTree thread_tree_;
304   std::unique_ptr<SampleTree> sample_tree_;
305   bool use_branch_address_;
306   std::string record_cmdline_;
307   bool accumulate_callchain_;
308   bool print_callgraph_;
309   bool callgraph_show_callee_;
310 };
311
312 bool ReportCommand::Run(const std::vector<std::string>& args) {
313   // 1. Parse options.
314   if (!ParseOptions(args)) {
315     return false;
316   }
317
318   // 2. Read record file and build SampleTree.
319   record_file_reader_ = RecordFileReader::CreateInstance(record_filename_);
320   if (record_file_reader_ == nullptr) {
321     return false;
322   }
323   if (!ReadEventAttrFromRecordFile()) {
324     return false;
325   }
326   // Read features first to prepare build ids used when building SampleTree.
327   if (!ReadFeaturesFromRecordFile()) {
328     return false;
329   }
330   ReadSampleTreeFromRecordFile();
331
332   // 3. Show collected information.
333   PrintReport();
334
335   return true;
336 }
337
338 bool ReportCommand::ParseOptions(const std::vector<std::string>& args) {
339   bool demangle = true;
340   std::string symfs_dir;
341   std::string vmlinux;
342   bool print_sample_count = false;
343   std::vector<std::string> sort_keys = {"comm", "pid", "tid", "dso", "symbol"};
344   std::unordered_set<std::string> comm_filter;
345   std::unordered_set<std::string> dso_filter;
346   std::unordered_set<int> pid_filter;
347   std::unordered_set<int> tid_filter;
348
349   for (size_t i = 0; i < args.size(); ++i) {
350     if (args[i] == "-b") {
351       use_branch_address_ = true;
352     } else if (args[i] == "--children") {
353       accumulate_callchain_ = true;
354     } else if (args[i] == "--comms" || args[i] == "--dsos") {
355       if (!NextArgumentOrError(args, &i)) {
356         return false;
357       }
358       std::vector<std::string> strs = android::base::Split(args[i], ",");
359       std::unordered_set<std::string>& filter = (args[i] == "--comms" ? comm_filter : dso_filter);
360       filter.insert(strs.begin(), strs.end());
361
362     } else if (args[i] == "-g") {
363       print_callgraph_ = true;
364       accumulate_callchain_ = true;
365       if (i + 1 < args.size() && args[i + 1][0] != '-') {
366         ++i;
367         if (args[i] == "callee") {
368           callgraph_show_callee_ = true;
369         } else if (args[i] == "caller") {
370           callgraph_show_callee_ = false;
371         } else {
372           LOG(ERROR) << "Unknown argument with -g option: " << args[i];
373           return false;
374         }
375       }
376     } else if (args[i] == "-i") {
377       if (!NextArgumentOrError(args, &i)) {
378         return false;
379       }
380       record_filename_ = args[i];
381
382     } else if (args[i] == "-n") {
383       print_sample_count = true;
384
385     } else if (args[i] == "--no-demangle") {
386       demangle = false;
387
388     } else if (args[i] == "--pids" || args[i] == "--tids") {
389       if (!NextArgumentOrError(args, &i)) {
390         return false;
391       }
392       std::vector<std::string> strs = android::base::Split(args[i], ",");
393       std::vector<int> ids;
394       for (auto& s : strs) {
395         int id;
396         if (!StringToPid(s, &id)) {
397           LOG(ERROR) << "invalid id in " << args[i] << " option: " << s;
398           return false;
399         }
400         ids.push_back(id);
401       }
402       std::unordered_set<int>& filter = (args[i] == "--pids" ? pid_filter : tid_filter);
403       filter.insert(ids.begin(), ids.end());
404
405     } else if (args[i] == "--sort") {
406       if (!NextArgumentOrError(args, &i)) {
407         return false;
408       }
409       sort_keys = android::base::Split(args[i], ",");
410     } else if (args[i] == "--symfs") {
411       if (!NextArgumentOrError(args, &i)) {
412         return false;
413       }
414       symfs_dir = args[i];
415
416     } else if (args[i] == "--vmlinux") {
417       if (!NextArgumentOrError(args, &i)) {
418         return false;
419       }
420       vmlinux = args[i];
421     } else {
422       ReportUnknownOption(args, i);
423       return false;
424     }
425   }
426
427   Dso::SetDemangle(demangle);
428   if (!Dso::SetSymFsDir(symfs_dir)) {
429     return false;
430   }
431   if (!vmlinux.empty()) {
432     Dso::SetVmlinux(vmlinux);
433   }
434
435   if (!accumulate_callchain_) {
436     displayable_items_.push_back(
437         std::unique_ptr<Displayable>(new SelfOverheadItem(*sample_tree_, "Overhead")));
438   } else {
439     displayable_items_.push_back(
440         std::unique_ptr<Displayable>(new AccumulatedOverheadItem(*sample_tree_)));
441     displayable_items_.push_back(std::unique_ptr<Displayable>(new SelfOverheadItem(*sample_tree_)));
442   }
443   if (print_sample_count) {
444     displayable_items_.push_back(std::unique_ptr<Displayable>(new SampleCountItem));
445   }
446   for (auto& key : sort_keys) {
447     if (!use_branch_address_ && branch_sort_keys.find(key) != branch_sort_keys.end()) {
448       LOG(ERROR) << "sort key '" << key << "' can only be used with -b option.";
449       return false;
450     }
451     if (key == "pid") {
452       PidItem* item = new PidItem;
453       displayable_items_.push_back(std::unique_ptr<Displayable>(item));
454       comparable_items_.push_back(item);
455     } else if (key == "tid") {
456       TidItem* item = new TidItem;
457       displayable_items_.push_back(std::unique_ptr<Displayable>(item));
458       comparable_items_.push_back(item);
459     } else if (key == "comm") {
460       CommItem* item = new CommItem;
461       displayable_items_.push_back(std::unique_ptr<Displayable>(item));
462       comparable_items_.push_back(item);
463     } else if (key == "dso") {
464       DsoItem* item = new DsoItem;
465       displayable_items_.push_back(std::unique_ptr<Displayable>(item));
466       comparable_items_.push_back(item);
467     } else if (key == "symbol") {
468       SymbolItem* item = new SymbolItem;
469       displayable_items_.push_back(std::unique_ptr<Displayable>(item));
470       comparable_items_.push_back(item);
471     } else if (key == "dso_from") {
472       DsoFromItem* item = new DsoFromItem;
473       displayable_items_.push_back(std::unique_ptr<Displayable>(item));
474       comparable_items_.push_back(item);
475     } else if (key == "dso_to") {
476       DsoToItem* item = new DsoToItem;
477       displayable_items_.push_back(std::unique_ptr<Displayable>(item));
478       comparable_items_.push_back(item);
479     } else if (key == "symbol_from") {
480       SymbolFromItem* item = new SymbolFromItem;
481       displayable_items_.push_back(std::unique_ptr<Displayable>(item));
482       comparable_items_.push_back(item);
483     } else if (key == "symbol_to") {
484       SymbolToItem* item = new SymbolToItem;
485       displayable_items_.push_back(std::unique_ptr<Displayable>(item));
486       comparable_items_.push_back(item);
487     } else {
488       LOG(ERROR) << "Unknown sort key: " << key;
489       return false;
490     }
491   }
492   sample_tree_->SetFilters(pid_filter, tid_filter, comm_filter, dso_filter);
493   return true;
494 }
495
496 bool ReportCommand::ReadEventAttrFromRecordFile() {
497   std::vector<const PerfFileFormat::FileAttr*> attrs = record_file_reader_->AttrSection();
498   if (attrs.size() != 1) {
499     LOG(ERROR) << "record file contains " << attrs.size() << " attrs";
500     return false;
501   }
502   event_attr_ = attrs[0]->attr;
503   if (use_branch_address_ && (event_attr_.sample_type & PERF_SAMPLE_BRANCH_STACK) == 0) {
504     LOG(ERROR) << record_filename_ << " is not recorded with branch stack sampling option.";
505     return false;
506   }
507   return true;
508 }
509
510 void ReportCommand::ReadSampleTreeFromRecordFile() {
511   std::vector<std::unique_ptr<Record>> records = record_file_reader_->DataSection();
512   thread_tree_.AddThread(0, 0, "swapper");
513   for (auto& record : records) {
514     BuildThreadTree(*record, &thread_tree_);
515     if (record->header.type == PERF_RECORD_SAMPLE) {
516       ProcessSampleRecord(*static_cast<const SampleRecord*>(record.get()));
517     }
518   }
519 }
520
521 void ReportCommand::ProcessSampleRecord(const SampleRecord& r) {
522   if (use_branch_address_ && (r.sample_type & PERF_SAMPLE_BRANCH_STACK)) {
523     for (auto& item : r.branch_stack_data.stack) {
524       if (item.from != 0 && item.to != 0) {
525         sample_tree_->AddBranchSample(r.tid_data.pid, r.tid_data.tid, item.from, item.to,
526                                       item.flags, r.time_data.time, r.period_data.period);
527       }
528     }
529   } else {
530     bool in_kernel = (r.header.misc & PERF_RECORD_MISC_CPUMODE_MASK) == PERF_RECORD_MISC_KERNEL;
531     SampleEntry* sample = sample_tree_->AddSample(r.tid_data.pid, r.tid_data.tid, r.ip_data.ip,
532                                                   r.time_data.time, r.period_data.period, in_kernel);
533     if (sample == nullptr) {
534       return;
535     }
536     if (accumulate_callchain_) {
537       std::vector<uint64_t> ips;
538       if (r.sample_type & PERF_SAMPLE_CALLCHAIN) {
539         ips.insert(ips.end(), r.callchain_data.ips.begin(), r.callchain_data.ips.end());
540       }
541       // Use stack_user_data.data.size() instead of stack_user_data.dyn_size, to make up for
542       // the missing kernel patch in N9. See b/22612370.
543       if ((r.sample_type & PERF_SAMPLE_REGS_USER) && (r.regs_user_data.reg_mask != 0) &&
544           (r.sample_type & PERF_SAMPLE_STACK_USER) && (!r.stack_user_data.data.empty())) {
545         RegSet regs = CreateRegSet(r.regs_user_data.reg_mask, r.regs_user_data.regs);
546         std::vector<char> stack(r.stack_user_data.data.begin(),
547                                 r.stack_user_data.data.begin() + r.stack_user_data.data.size());
548         std::vector<uint64_t> unwind_ips = UnwindCallChain(*sample->thread, regs, stack);
549         if (!unwind_ips.empty()) {
550           ips.push_back(PERF_CONTEXT_USER);
551           ips.insert(ips.end(), unwind_ips.begin(), unwind_ips.end());
552         }
553       }
554
555       std::vector<SampleEntry*> callchain;
556       callchain.push_back(sample);
557
558       bool first_ip = true;
559       for (auto& ip : ips) {
560         if (ip >= PERF_CONTEXT_MAX) {
561           switch (ip) {
562             case PERF_CONTEXT_KERNEL:
563               in_kernel = true;
564               break;
565             case PERF_CONTEXT_USER:
566               in_kernel = false;
567               break;
568             default:
569               LOG(ERROR) << "Unexpected perf_context in callchain: " << ip;
570           }
571         } else {
572           if (first_ip) {
573             first_ip = false;
574             // Remove duplication with sampled ip.
575             if (ip == r.ip_data.ip) {
576               continue;
577             }
578           }
579           SampleEntry* sample =
580               sample_tree_->AddCallChainSample(r.tid_data.pid, r.tid_data.tid, ip, r.time_data.time,
581                                                r.period_data.period, in_kernel, callchain);
582           callchain.push_back(sample);
583         }
584       }
585
586       if (print_callgraph_) {
587         std::set<SampleEntry*> added_set;
588         if (!callgraph_show_callee_) {
589           std::reverse(callchain.begin(), callchain.end());
590         }
591         while (callchain.size() >= 2) {
592           SampleEntry* sample = callchain[0];
593           callchain.erase(callchain.begin());
594           // Add only once for recursive calls on callchain.
595           if (added_set.find(sample) != added_set.end()) {
596             continue;
597           }
598           added_set.insert(sample);
599           sample_tree_->InsertCallChainForSample(sample, callchain, r.period_data.period);
600         }
601       }
602     }
603   }
604 }
605
606 bool ReportCommand::ReadFeaturesFromRecordFile() {
607   std::vector<BuildIdRecord> records = record_file_reader_->ReadBuildIdFeature();
608   std::vector<std::pair<std::string, BuildId>> build_ids;
609   for (auto& r : records) {
610     build_ids.push_back(std::make_pair(r.filename, r.build_id));
611   }
612   Dso::SetBuildIds(build_ids);
613
614   std::string arch = record_file_reader_->ReadFeatureString(PerfFileFormat::FEAT_ARCH);
615   if (!arch.empty()) {
616     if (!SetCurrentArch(arch)) {
617       return false;
618     }
619   }
620
621   std::vector<std::string> cmdline = record_file_reader_->ReadCmdlineFeature();
622   if (!cmdline.empty()) {
623     record_cmdline_ = android::base::Join(cmdline, ' ');
624   }
625   return true;
626 }
627
628 int ReportCommand::CompareSampleEntry(const SampleEntry& sample1, const SampleEntry& sample2) {
629   for (auto& item : comparable_items_) {
630     int result = item->Compare(sample1, sample2);
631     if (result != 0) {
632       return result;
633     }
634   }
635   return 0;
636 }
637
638 void ReportCommand::PrintReport() {
639   PrintReportContext();
640   CollectReportWidth();
641   PrintReportHeader();
642   sample_tree_->VisitAllSamples(
643       std::bind(&ReportCommand::PrintReportEntry, this, std::placeholders::_1));
644   fflush(stdout);
645 }
646
647 void ReportCommand::PrintReportContext() {
648   const EventType* event_type = FindEventTypeByConfig(event_attr_.type, event_attr_.config);
649   std::string event_type_name;
650   if (event_type != nullptr) {
651     event_type_name = event_type->name;
652   } else {
653     event_type_name =
654         android::base::StringPrintf("(type %u, config %llu)", event_attr_.type, event_attr_.config);
655   }
656   if (!record_cmdline_.empty()) {
657     printf("Cmdline: %s\n", record_cmdline_.c_str());
658   }
659   printf("Samples: %" PRIu64 " of event '%s'\n", sample_tree_->TotalSamples(),
660          event_type_name.c_str());
661   printf("Event count: %" PRIu64 "\n\n", sample_tree_->TotalPeriod());
662 }
663
664 void ReportCommand::CollectReportWidth() {
665   sample_tree_->VisitAllSamples(
666       std::bind(&ReportCommand::CollectReportEntryWidth, this, std::placeholders::_1));
667 }
668
669 void ReportCommand::CollectReportEntryWidth(const SampleEntry& sample) {
670   for (auto& item : displayable_items_) {
671     item->AdjustWidth(sample);
672   }
673 }
674
675 void ReportCommand::PrintReportHeader() {
676   for (size_t i = 0; i < displayable_items_.size(); ++i) {
677     auto& item = displayable_items_[i];
678     if (i != displayable_items_.size() - 1) {
679       printf("%-*s  ", static_cast<int>(item->Width()), item->Name().c_str());
680     } else {
681       printf("%s\n", item->Name().c_str());
682     }
683   }
684 }
685
686 void ReportCommand::PrintReportEntry(const SampleEntry& sample) {
687   for (size_t i = 0; i < displayable_items_.size(); ++i) {
688     auto& item = displayable_items_[i];
689     if (i != displayable_items_.size() - 1) {
690       printf("%-*s  ", static_cast<int>(item->Width()), item->Show(sample).c_str());
691     } else {
692       printf("%s\n", item->Show(sample).c_str());
693     }
694   }
695   if (print_callgraph_) {
696     PrintCallGraph(sample);
697   }
698 }
699
700 static void PrintCallGraphEntry(size_t depth, std::string prefix,
701                                 const std::unique_ptr<CallChainNode>& node, uint64_t parent_period,
702                                 bool last) {
703   if (depth > 20) {
704     LOG(WARNING) << "truncated callgraph at depth " << depth;
705     return;
706   }
707   prefix += "|";
708   printf("%s\n", prefix.c_str());
709   if (last) {
710     prefix.back() = ' ';
711   }
712   std::string percentage_s = "-- ";
713   if (node->period + node->children_period != parent_period) {
714     double percentage = 100.0 * (node->period + node->children_period) / parent_period;
715     percentage_s = android::base::StringPrintf("--%.2lf%%-- ", percentage);
716   }
717   printf("%s%s%s\n", prefix.c_str(), percentage_s.c_str(), node->chain[0]->symbol->DemangledName());
718   prefix.append(percentage_s.size(), ' ');
719   for (size_t i = 1; i < node->chain.size(); ++i) {
720     printf("%s%s\n", prefix.c_str(), node->chain[i]->symbol->DemangledName());
721   }
722
723   for (size_t i = 0; i < node->children.size(); ++i) {
724     PrintCallGraphEntry(depth + 1, prefix, node->children[i], node->children_period,
725                         (i + 1 == node->children.size()));
726   }
727 }
728
729 void ReportCommand::PrintCallGraph(const SampleEntry& sample) {
730   std::string prefix = "       ";
731   printf("%s|\n", prefix.c_str());
732   printf("%s-- %s\n", prefix.c_str(), sample.symbol->DemangledName());
733   prefix.append(3, ' ');
734   for (size_t i = 0; i < sample.callchain.children.size(); ++i) {
735     PrintCallGraphEntry(1, prefix, sample.callchain.children[i], sample.callchain.children_period,
736                         (i + 1 == sample.callchain.children.size()));
737   }
738 }
739
740 __attribute__((constructor)) static void RegisterReportCommand() {
741   RegisterCommand("report", [] { return std::unique_ptr<Command>(new ReportCommand()); });
742 }