OSDN Git Service

Simpleperf: check value returned by fopen.
[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 <android-base/logging.h>
28 #include <android-base/stringprintf.h>
29 #include <android-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 ProcessRecord(std::unique_ptr<Record> record);
288   void ProcessSampleRecord(const SampleRecord& r);
289   bool ReadFeaturesFromRecordFile();
290   int CompareSampleEntry(const SampleEntry& sample1, const SampleEntry& sample2);
291   void PrintReport();
292   void PrintReportContext();
293   void CollectReportWidth();
294   void CollectReportEntryWidth(const SampleEntry& sample);
295   void PrintReportHeader();
296   void PrintReportEntry(const SampleEntry& sample);
297   void PrintCallGraph(const SampleEntry& sample);
298
299   std::string record_filename_;
300   std::unique_ptr<RecordFileReader> record_file_reader_;
301   perf_event_attr event_attr_;
302   std::vector<std::unique_ptr<Displayable>> displayable_items_;
303   std::vector<Comparable*> comparable_items_;
304   ThreadTree thread_tree_;
305   std::unique_ptr<SampleTree> sample_tree_;
306   bool use_branch_address_;
307   std::string record_cmdline_;
308   bool accumulate_callchain_;
309   bool print_callgraph_;
310   bool callgraph_show_callee_;
311 };
312
313 bool ReportCommand::Run(const std::vector<std::string>& args) {
314   // 1. Parse options.
315   if (!ParseOptions(args)) {
316     return false;
317   }
318
319   // 2. Read record file and build SampleTree.
320   record_file_reader_ = RecordFileReader::CreateInstance(record_filename_);
321   if (record_file_reader_ == nullptr) {
322     return false;
323   }
324   if (!ReadEventAttrFromRecordFile()) {
325     return false;
326   }
327   // Read features first to prepare build ids used when building SampleTree.
328   if (!ReadFeaturesFromRecordFile()) {
329     return false;
330   }
331   ReadSampleTreeFromRecordFile();
332
333   // 3. Show collected information.
334   PrintReport();
335
336   return true;
337 }
338
339 bool ReportCommand::ParseOptions(const std::vector<std::string>& args) {
340   bool demangle = true;
341   std::string symfs_dir;
342   std::string vmlinux;
343   bool print_sample_count = false;
344   std::vector<std::string> sort_keys = {"comm", "pid", "tid", "dso", "symbol"};
345   std::unordered_set<std::string> comm_filter;
346   std::unordered_set<std::string> dso_filter;
347   std::unordered_set<int> pid_filter;
348   std::unordered_set<int> tid_filter;
349
350   for (size_t i = 0; i < args.size(); ++i) {
351     if (args[i] == "-b") {
352       use_branch_address_ = true;
353     } else if (args[i] == "--children") {
354       accumulate_callchain_ = true;
355     } else if (args[i] == "--comms" || args[i] == "--dsos") {
356       if (!NextArgumentOrError(args, &i)) {
357         return false;
358       }
359       std::vector<std::string> strs = android::base::Split(args[i], ",");
360       std::unordered_set<std::string>& filter = (args[i] == "--comms" ? comm_filter : dso_filter);
361       filter.insert(strs.begin(), strs.end());
362
363     } else if (args[i] == "-g") {
364       print_callgraph_ = true;
365       accumulate_callchain_ = true;
366       if (i + 1 < args.size() && args[i + 1][0] != '-') {
367         ++i;
368         if (args[i] == "callee") {
369           callgraph_show_callee_ = true;
370         } else if (args[i] == "caller") {
371           callgraph_show_callee_ = false;
372         } else {
373           LOG(ERROR) << "Unknown argument with -g option: " << args[i];
374           return false;
375         }
376       }
377     } else if (args[i] == "-i") {
378       if (!NextArgumentOrError(args, &i)) {
379         return false;
380       }
381       record_filename_ = args[i];
382
383     } else if (args[i] == "-n") {
384       print_sample_count = true;
385
386     } else if (args[i] == "--no-demangle") {
387       demangle = false;
388
389     } else if (args[i] == "--pids" || args[i] == "--tids") {
390       if (!NextArgumentOrError(args, &i)) {
391         return false;
392       }
393       std::vector<std::string> strs = android::base::Split(args[i], ",");
394       std::vector<int> ids;
395       for (auto& s : strs) {
396         int id;
397         if (!StringToPid(s, &id)) {
398           LOG(ERROR) << "invalid id in " << args[i] << " option: " << s;
399           return false;
400         }
401         ids.push_back(id);
402       }
403       std::unordered_set<int>& filter = (args[i] == "--pids" ? pid_filter : tid_filter);
404       filter.insert(ids.begin(), ids.end());
405
406     } else if (args[i] == "--sort") {
407       if (!NextArgumentOrError(args, &i)) {
408         return false;
409       }
410       sort_keys = android::base::Split(args[i], ",");
411     } else if (args[i] == "--symfs") {
412       if (!NextArgumentOrError(args, &i)) {
413         return false;
414       }
415       symfs_dir = args[i];
416
417     } else if (args[i] == "--vmlinux") {
418       if (!NextArgumentOrError(args, &i)) {
419         return false;
420       }
421       vmlinux = args[i];
422     } else {
423       ReportUnknownOption(args, i);
424       return false;
425     }
426   }
427
428   Dso::SetDemangle(demangle);
429   if (!Dso::SetSymFsDir(symfs_dir)) {
430     return false;
431   }
432   if (!vmlinux.empty()) {
433     Dso::SetVmlinux(vmlinux);
434   }
435
436   if (!accumulate_callchain_) {
437     displayable_items_.push_back(
438         std::unique_ptr<Displayable>(new SelfOverheadItem(*sample_tree_, "Overhead")));
439   } else {
440     displayable_items_.push_back(
441         std::unique_ptr<Displayable>(new AccumulatedOverheadItem(*sample_tree_)));
442     displayable_items_.push_back(std::unique_ptr<Displayable>(new SelfOverheadItem(*sample_tree_)));
443   }
444   if (print_sample_count) {
445     displayable_items_.push_back(std::unique_ptr<Displayable>(new SampleCountItem));
446   }
447   for (auto& key : sort_keys) {
448     if (!use_branch_address_ && branch_sort_keys.find(key) != branch_sort_keys.end()) {
449       LOG(ERROR) << "sort key '" << key << "' can only be used with -b option.";
450       return false;
451     }
452     if (key == "pid") {
453       PidItem* item = new PidItem;
454       displayable_items_.push_back(std::unique_ptr<Displayable>(item));
455       comparable_items_.push_back(item);
456     } else if (key == "tid") {
457       TidItem* item = new TidItem;
458       displayable_items_.push_back(std::unique_ptr<Displayable>(item));
459       comparable_items_.push_back(item);
460     } else if (key == "comm") {
461       CommItem* item = new CommItem;
462       displayable_items_.push_back(std::unique_ptr<Displayable>(item));
463       comparable_items_.push_back(item);
464     } else if (key == "dso") {
465       DsoItem* item = new DsoItem;
466       displayable_items_.push_back(std::unique_ptr<Displayable>(item));
467       comparable_items_.push_back(item);
468     } else if (key == "symbol") {
469       SymbolItem* item = new SymbolItem;
470       displayable_items_.push_back(std::unique_ptr<Displayable>(item));
471       comparable_items_.push_back(item);
472     } else if (key == "dso_from") {
473       DsoFromItem* item = new DsoFromItem;
474       displayable_items_.push_back(std::unique_ptr<Displayable>(item));
475       comparable_items_.push_back(item);
476     } else if (key == "dso_to") {
477       DsoToItem* item = new DsoToItem;
478       displayable_items_.push_back(std::unique_ptr<Displayable>(item));
479       comparable_items_.push_back(item);
480     } else if (key == "symbol_from") {
481       SymbolFromItem* item = new SymbolFromItem;
482       displayable_items_.push_back(std::unique_ptr<Displayable>(item));
483       comparable_items_.push_back(item);
484     } else if (key == "symbol_to") {
485       SymbolToItem* item = new SymbolToItem;
486       displayable_items_.push_back(std::unique_ptr<Displayable>(item));
487       comparable_items_.push_back(item);
488     } else {
489       LOG(ERROR) << "Unknown sort key: " << key;
490       return false;
491     }
492   }
493   sample_tree_->SetFilters(pid_filter, tid_filter, comm_filter, dso_filter);
494   return true;
495 }
496
497 bool ReportCommand::ReadEventAttrFromRecordFile() {
498   const std::vector<PerfFileFormat::FileAttr>& attrs = record_file_reader_->AttrSection();
499   if (attrs.size() != 1) {
500     LOG(ERROR) << "record file contains " << attrs.size() << " attrs";
501     return false;
502   }
503   event_attr_ = attrs[0].attr;
504   if (use_branch_address_ && (event_attr_.sample_type & PERF_SAMPLE_BRANCH_STACK) == 0) {
505     LOG(ERROR) << record_filename_ << " is not recorded with branch stack sampling option.";
506     return false;
507   }
508   return true;
509 }
510
511 void ReportCommand::ReadSampleTreeFromRecordFile() {
512   thread_tree_.AddThread(0, 0, "swapper");
513   record_file_reader_->ReadDataSection([this](std::unique_ptr<Record> record) {
514     ProcessRecord(std::move(record));
515     return true;
516   });
517 }
518
519 void ReportCommand::ProcessRecord(std::unique_ptr<Record> record) {
520   BuildThreadTree(*record, &thread_tree_);
521   if (record->header.type == PERF_RECORD_SAMPLE) {
522     ProcessSampleRecord(*static_cast<const SampleRecord*>(record.get()));
523   }
524 }
525
526 void ReportCommand::ProcessSampleRecord(const SampleRecord& r) {
527   if (use_branch_address_ && (r.sample_type & PERF_SAMPLE_BRANCH_STACK)) {
528     for (auto& item : r.branch_stack_data.stack) {
529       if (item.from != 0 && item.to != 0) {
530         sample_tree_->AddBranchSample(r.tid_data.pid, r.tid_data.tid, item.from, item.to,
531                                       item.flags, r.time_data.time, r.period_data.period);
532       }
533     }
534   } else {
535     bool in_kernel = (r.header.misc & PERF_RECORD_MISC_CPUMODE_MASK) == PERF_RECORD_MISC_KERNEL;
536     SampleEntry* sample = sample_tree_->AddSample(r.tid_data.pid, r.tid_data.tid, r.ip_data.ip,
537                                                   r.time_data.time, r.period_data.period, in_kernel);
538     if (sample == nullptr) {
539       return;
540     }
541     if (accumulate_callchain_) {
542       std::vector<uint64_t> ips;
543       if (r.sample_type & PERF_SAMPLE_CALLCHAIN) {
544         ips.insert(ips.end(), r.callchain_data.ips.begin(), r.callchain_data.ips.end());
545       }
546       // Use stack_user_data.data.size() instead of stack_user_data.dyn_size, to make up for
547       // the missing kernel patch in N9. See b/22612370.
548       if ((r.sample_type & PERF_SAMPLE_REGS_USER) && (r.regs_user_data.reg_mask != 0) &&
549           (r.sample_type & PERF_SAMPLE_STACK_USER) && (!r.stack_user_data.data.empty())) {
550         RegSet regs = CreateRegSet(r.regs_user_data.reg_mask, r.regs_user_data.regs);
551         std::vector<char> stack(r.stack_user_data.data.begin(),
552                                 r.stack_user_data.data.begin() + r.stack_user_data.data.size());
553         std::vector<uint64_t> unwind_ips = UnwindCallChain(*sample->thread, regs, stack);
554         if (!unwind_ips.empty()) {
555           ips.push_back(PERF_CONTEXT_USER);
556           ips.insert(ips.end(), unwind_ips.begin(), unwind_ips.end());
557         }
558       }
559
560       std::vector<SampleEntry*> callchain;
561       callchain.push_back(sample);
562
563       bool first_ip = true;
564       for (auto& ip : ips) {
565         if (ip >= PERF_CONTEXT_MAX) {
566           switch (ip) {
567             case PERF_CONTEXT_KERNEL:
568               in_kernel = true;
569               break;
570             case PERF_CONTEXT_USER:
571               in_kernel = false;
572               break;
573             default:
574               LOG(ERROR) << "Unexpected perf_context in callchain: " << ip;
575           }
576         } else {
577           if (first_ip) {
578             first_ip = false;
579             // Remove duplication with sampled ip.
580             if (ip == r.ip_data.ip) {
581               continue;
582             }
583           }
584           SampleEntry* sample =
585               sample_tree_->AddCallChainSample(r.tid_data.pid, r.tid_data.tid, ip, r.time_data.time,
586                                                r.period_data.period, in_kernel, callchain);
587           callchain.push_back(sample);
588         }
589       }
590
591       if (print_callgraph_) {
592         std::set<SampleEntry*> added_set;
593         if (!callgraph_show_callee_) {
594           std::reverse(callchain.begin(), callchain.end());
595         }
596         while (callchain.size() >= 2) {
597           SampleEntry* sample = callchain[0];
598           callchain.erase(callchain.begin());
599           // Add only once for recursive calls on callchain.
600           if (added_set.find(sample) != added_set.end()) {
601             continue;
602           }
603           added_set.insert(sample);
604           sample_tree_->InsertCallChainForSample(sample, callchain, r.period_data.period);
605         }
606       }
607     }
608   }
609 }
610
611 bool ReportCommand::ReadFeaturesFromRecordFile() {
612   std::vector<BuildIdRecord> records = record_file_reader_->ReadBuildIdFeature();
613   std::vector<std::pair<std::string, BuildId>> build_ids;
614   for (auto& r : records) {
615     build_ids.push_back(std::make_pair(r.filename, r.build_id));
616   }
617   Dso::SetBuildIds(build_ids);
618
619   std::string arch = record_file_reader_->ReadFeatureString(PerfFileFormat::FEAT_ARCH);
620   if (!arch.empty()) {
621     if (!SetCurrentArch(arch)) {
622       return false;
623     }
624   }
625
626   std::vector<std::string> cmdline = record_file_reader_->ReadCmdlineFeature();
627   if (!cmdline.empty()) {
628     record_cmdline_ = android::base::Join(cmdline, ' ');
629   }
630   return true;
631 }
632
633 int ReportCommand::CompareSampleEntry(const SampleEntry& sample1, const SampleEntry& sample2) {
634   for (auto& item : comparable_items_) {
635     int result = item->Compare(sample1, sample2);
636     if (result != 0) {
637       return result;
638     }
639   }
640   return 0;
641 }
642
643 void ReportCommand::PrintReport() {
644   PrintReportContext();
645   CollectReportWidth();
646   PrintReportHeader();
647   sample_tree_->VisitAllSamples(
648       std::bind(&ReportCommand::PrintReportEntry, this, std::placeholders::_1));
649   fflush(stdout);
650 }
651
652 void ReportCommand::PrintReportContext() {
653   const EventType* event_type = FindEventTypeByConfig(event_attr_.type, event_attr_.config);
654   std::string event_type_name;
655   if (event_type != nullptr) {
656     event_type_name = event_type->name;
657   } else {
658     event_type_name =
659         android::base::StringPrintf("(type %u, config %llu)", event_attr_.type, event_attr_.config);
660   }
661   if (!record_cmdline_.empty()) {
662     printf("Cmdline: %s\n", record_cmdline_.c_str());
663   }
664   printf("Samples: %" PRIu64 " of event '%s'\n", sample_tree_->TotalSamples(),
665          event_type_name.c_str());
666   printf("Event count: %" PRIu64 "\n\n", sample_tree_->TotalPeriod());
667 }
668
669 void ReportCommand::CollectReportWidth() {
670   sample_tree_->VisitAllSamples(
671       std::bind(&ReportCommand::CollectReportEntryWidth, this, std::placeholders::_1));
672 }
673
674 void ReportCommand::CollectReportEntryWidth(const SampleEntry& sample) {
675   for (auto& item : displayable_items_) {
676     item->AdjustWidth(sample);
677   }
678 }
679
680 void ReportCommand::PrintReportHeader() {
681   for (size_t i = 0; i < displayable_items_.size(); ++i) {
682     auto& item = displayable_items_[i];
683     if (i != displayable_items_.size() - 1) {
684       printf("%-*s  ", static_cast<int>(item->Width()), item->Name().c_str());
685     } else {
686       printf("%s\n", item->Name().c_str());
687     }
688   }
689 }
690
691 void ReportCommand::PrintReportEntry(const SampleEntry& sample) {
692   for (size_t i = 0; i < displayable_items_.size(); ++i) {
693     auto& item = displayable_items_[i];
694     if (i != displayable_items_.size() - 1) {
695       printf("%-*s  ", static_cast<int>(item->Width()), item->Show(sample).c_str());
696     } else {
697       printf("%s\n", item->Show(sample).c_str());
698     }
699   }
700   if (print_callgraph_) {
701     PrintCallGraph(sample);
702   }
703 }
704
705 static void PrintCallGraphEntry(size_t depth, std::string prefix,
706                                 const std::unique_ptr<CallChainNode>& node, uint64_t parent_period,
707                                 bool last) {
708   if (depth > 20) {
709     LOG(WARNING) << "truncated callgraph at depth " << depth;
710     return;
711   }
712   prefix += "|";
713   printf("%s\n", prefix.c_str());
714   if (last) {
715     prefix.back() = ' ';
716   }
717   std::string percentage_s = "-- ";
718   if (node->period + node->children_period != parent_period) {
719     double percentage = 100.0 * (node->period + node->children_period) / parent_period;
720     percentage_s = android::base::StringPrintf("--%.2lf%%-- ", percentage);
721   }
722   printf("%s%s%s\n", prefix.c_str(), percentage_s.c_str(), node->chain[0]->symbol->DemangledName());
723   prefix.append(percentage_s.size(), ' ');
724   for (size_t i = 1; i < node->chain.size(); ++i) {
725     printf("%s%s\n", prefix.c_str(), node->chain[i]->symbol->DemangledName());
726   }
727
728   for (size_t i = 0; i < node->children.size(); ++i) {
729     PrintCallGraphEntry(depth + 1, prefix, node->children[i], node->children_period,
730                         (i + 1 == node->children.size()));
731   }
732 }
733
734 void ReportCommand::PrintCallGraph(const SampleEntry& sample) {
735   std::string prefix = "       ";
736   printf("%s|\n", prefix.c_str());
737   printf("%s-- %s\n", prefix.c_str(), sample.symbol->DemangledName());
738   prefix.append(3, ' ');
739   for (size_t i = 0; i < sample.callchain.children.size(); ++i) {
740     PrintCallGraphEntry(1, prefix, sample.callchain.children[i], sample.callchain.children_period,
741                         (i + 1 == sample.callchain.children.size()));
742   }
743 }
744
745 __attribute__((constructor)) static void RegisterReportCommand() {
746   RegisterCommand("report", [] { return std::unique_ptr<Command>(new ReportCommand()); });
747 }