2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 #include <unordered_map>
24 #include <unordered_set>
27 #include <base/logging.h>
28 #include <base/stringprintf.h>
29 #include <base/strings.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"
38 #include "record_file.h"
39 #include "sample_tree.h"
40 #include "thread_tree.h"
45 Displayable(const std::string& name) : name_(name), width_(name.size()) {
48 virtual ~Displayable() {
51 const std::string& Name() const {
54 size_t Width() const {
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);
65 const std::string name_;
69 class AccumulatedOverheadItem : public Displayable {
71 AccumulatedOverheadItem(const SampleTree& sample_tree)
72 : Displayable("Children"), sample_tree_(sample_tree) {
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);
83 const SampleTree& sample_tree_;
86 class SelfOverheadItem : public Displayable {
88 SelfOverheadItem(const SampleTree& sample_tree, const std::string& name = "Self")
89 : Displayable(name), sample_tree_(sample_tree) {
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);
100 const SampleTree& sample_tree_;
103 class SampleCountItem : public Displayable {
105 SampleCountItem() : Displayable("Sample") {
108 std::string Show(const SampleEntry& sample) const override {
109 return android::base::StringPrintf("%" PRId64, sample.sample_count);
115 virtual ~Comparable() {
118 virtual int Compare(const SampleEntry& sample1, const SampleEntry& sample2) const = 0;
121 class PidItem : public Displayable, public Comparable {
123 PidItem() : Displayable("Pid") {
126 int Compare(const SampleEntry& sample1, const SampleEntry& sample2) const override {
127 return sample1.thread->pid - sample2.thread->pid;
130 std::string Show(const SampleEntry& sample) const override {
131 return android::base::StringPrintf("%d", sample.thread->pid);
135 class TidItem : public Displayable, public Comparable {
137 TidItem() : Displayable("Tid") {
140 int Compare(const SampleEntry& sample1, const SampleEntry& sample2) const override {
141 return sample1.thread->tid - sample2.thread->tid;
144 std::string Show(const SampleEntry& sample) const override {
145 return android::base::StringPrintf("%d", sample.thread->tid);
149 class CommItem : public Displayable, public Comparable {
151 CommItem() : Displayable("Command") {
154 int Compare(const SampleEntry& sample1, const SampleEntry& sample2) const override {
155 return strcmp(sample1.thread_comm, sample2.thread_comm);
158 std::string Show(const SampleEntry& sample) const override {
159 return sample.thread_comm;
163 class DsoItem : public Displayable, public Comparable {
165 DsoItem(const std::string& name = "Shared Object") : Displayable(name) {
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());
172 std::string Show(const SampleEntry& sample) const override {
173 return sample.map->dso->Path();
177 class SymbolItem : public Displayable, public Comparable {
179 SymbolItem(const std::string& name = "Symbol") : Displayable(name) {
182 int Compare(const SampleEntry& sample1, const SampleEntry& sample2) const override {
183 return strcmp(sample1.symbol->DemangledName(), sample2.symbol->DemangledName());
186 std::string Show(const SampleEntry& sample) const override {
187 return sample.symbol->DemangledName();
191 class DsoFromItem : public Displayable, public Comparable {
193 DsoFromItem() : Displayable("Source Shared Object") {
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());
201 std::string Show(const SampleEntry& sample) const override {
202 return sample.branch_from.map->dso->Path();
206 class DsoToItem : public DsoItem {
208 DsoToItem() : DsoItem("Target Shared Object") {
212 class SymbolFromItem : public Displayable, public Comparable {
214 SymbolFromItem() : Displayable("Source Symbol") {
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());
222 std::string Show(const SampleEntry& sample) const override {
223 return sample.branch_from.symbol->DemangledName();
227 class SymbolToItem : public SymbolItem {
229 SymbolToItem() : SymbolItem("Target Symbol") {
233 static std::set<std::string> branch_sort_keys = {
234 "dso_from", "dso_to", "symbol_from", "symbol_to",
237 class ReportCommand : public 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"
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);
278 std::unique_ptr<SampleTree>(new SampleTree(&thread_tree_, compare_sample_callback));
281 bool Run(const std::vector<std::string>& args);
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);
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);
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_;
312 bool ReportCommand::Run(const std::vector<std::string>& args) {
314 if (!ParseOptions(args)) {
318 // 2. Read record file and build SampleTree.
319 record_file_reader_ = RecordFileReader::CreateInstance(record_filename_);
320 if (record_file_reader_ == nullptr) {
323 if (!ReadEventAttrFromRecordFile()) {
326 // Read features first to prepare build ids used when building SampleTree.
327 if (!ReadFeaturesFromRecordFile()) {
330 ReadSampleTreeFromRecordFile();
332 // 3. Show collected information.
338 bool ReportCommand::ParseOptions(const std::vector<std::string>& args) {
339 bool demangle = true;
340 std::string symfs_dir;
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;
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)) {
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());
362 } else if (args[i] == "-g") {
363 print_callgraph_ = true;
364 accumulate_callchain_ = true;
365 if (i + 1 < args.size() && args[i + 1][0] != '-') {
367 if (args[i] == "callee") {
368 callgraph_show_callee_ = true;
369 } else if (args[i] == "caller") {
370 callgraph_show_callee_ = false;
372 LOG(ERROR) << "Unknown argument with -g option: " << args[i];
376 } else if (args[i] == "-i") {
377 if (!NextArgumentOrError(args, &i)) {
380 record_filename_ = args[i];
382 } else if (args[i] == "-n") {
383 print_sample_count = true;
385 } else if (args[i] == "--no-demangle") {
388 } else if (args[i] == "--pids" || args[i] == "--tids") {
389 if (!NextArgumentOrError(args, &i)) {
392 std::vector<std::string> strs = android::base::Split(args[i], ",");
393 std::vector<int> ids;
394 for (auto& s : strs) {
396 if (!StringToPid(s, &id)) {
397 LOG(ERROR) << "invalid id in " << args[i] << " option: " << s;
402 std::unordered_set<int>& filter = (args[i] == "--pids" ? pid_filter : tid_filter);
403 filter.insert(ids.begin(), ids.end());
405 } else if (args[i] == "--sort") {
406 if (!NextArgumentOrError(args, &i)) {
409 sort_keys = android::base::Split(args[i], ",");
410 } else if (args[i] == "--symfs") {
411 if (!NextArgumentOrError(args, &i)) {
416 } else if (args[i] == "--vmlinux") {
417 if (!NextArgumentOrError(args, &i)) {
422 ReportUnknownOption(args, i);
427 Dso::SetDemangle(demangle);
428 if (!Dso::SetSymFsDir(symfs_dir)) {
431 if (!vmlinux.empty()) {
432 Dso::SetVmlinux(vmlinux);
435 if (!accumulate_callchain_) {
436 displayable_items_.push_back(
437 std::unique_ptr<Displayable>(new SelfOverheadItem(*sample_tree_, "Overhead")));
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_)));
443 if (print_sample_count) {
444 displayable_items_.push_back(std::unique_ptr<Displayable>(new SampleCountItem));
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.";
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);
488 LOG(ERROR) << "Unknown sort key: " << key;
492 sample_tree_->SetFilters(pid_filter, tid_filter, comm_filter, dso_filter);
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";
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.";
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()));
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);
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) {
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());
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());
555 std::vector<SampleEntry*> callchain;
556 callchain.push_back(sample);
558 bool first_ip = true;
559 for (auto& ip : ips) {
560 if (ip >= PERF_CONTEXT_MAX) {
562 case PERF_CONTEXT_KERNEL:
565 case PERF_CONTEXT_USER:
569 LOG(ERROR) << "Unexpected perf_context in callchain: " << ip;
574 // Remove duplication with sampled ip.
575 if (ip == r.ip_data.ip) {
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);
586 if (print_callgraph_) {
587 std::set<SampleEntry*> added_set;
588 if (!callgraph_show_callee_) {
589 std::reverse(callchain.begin(), callchain.end());
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()) {
598 added_set.insert(sample);
599 sample_tree_->InsertCallChainForSample(sample, callchain, r.period_data.period);
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));
612 Dso::SetBuildIds(build_ids);
614 std::string arch = record_file_reader_->ReadFeatureString(PerfFileFormat::FEAT_ARCH);
616 if (!SetCurrentArch(arch)) {
621 std::vector<std::string> cmdline = record_file_reader_->ReadCmdlineFeature();
622 if (!cmdline.empty()) {
623 record_cmdline_ = android::base::Join(cmdline, ' ');
628 int ReportCommand::CompareSampleEntry(const SampleEntry& sample1, const SampleEntry& sample2) {
629 for (auto& item : comparable_items_) {
630 int result = item->Compare(sample1, sample2);
638 void ReportCommand::PrintReport() {
639 PrintReportContext();
640 CollectReportWidth();
642 sample_tree_->VisitAllSamples(
643 std::bind(&ReportCommand::PrintReportEntry, this, std::placeholders::_1));
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;
654 android::base::StringPrintf("(type %u, config %llu)", event_attr_.type, event_attr_.config);
656 if (!record_cmdline_.empty()) {
657 printf("Cmdline: %s\n", record_cmdline_.c_str());
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());
664 void ReportCommand::CollectReportWidth() {
665 sample_tree_->VisitAllSamples(
666 std::bind(&ReportCommand::CollectReportEntryWidth, this, std::placeholders::_1));
669 void ReportCommand::CollectReportEntryWidth(const SampleEntry& sample) {
670 for (auto& item : displayable_items_) {
671 item->AdjustWidth(sample);
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());
681 printf("%s\n", item->Name().c_str());
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());
692 printf("%s\n", item->Show(sample).c_str());
695 if (print_callgraph_) {
696 PrintCallGraph(sample);
700 static void PrintCallGraphEntry(size_t depth, std::string prefix,
701 const std::unique_ptr<CallChainNode>& node, uint64_t parent_period,
704 LOG(WARNING) << "truncated callgraph at depth " << depth;
708 printf("%s\n", prefix.c_str());
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);
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());
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()));
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()));
740 __attribute__((constructor)) static void RegisterReportCommand() {
741 RegisterCommand("report", [] { return std::unique_ptr<Command>(new ReportCommand()); });