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 <android-base/logging.h>
28 #include <android-base/stringprintf.h>
29 #include <android-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 ProcessRecord(std::unique_ptr<Record> record);
288 void ProcessSampleRecord(const SampleRecord& r);
289 bool ReadFeaturesFromRecordFile();
290 int CompareSampleEntry(const SampleEntry& sample1, const SampleEntry& sample2);
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);
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_;
313 bool ReportCommand::Run(const std::vector<std::string>& args) {
315 if (!ParseOptions(args)) {
319 // 2. Read record file and build SampleTree.
320 record_file_reader_ = RecordFileReader::CreateInstance(record_filename_);
321 if (record_file_reader_ == nullptr) {
324 if (!ReadEventAttrFromRecordFile()) {
327 // Read features first to prepare build ids used when building SampleTree.
328 if (!ReadFeaturesFromRecordFile()) {
331 ReadSampleTreeFromRecordFile();
333 // 3. Show collected information.
339 bool ReportCommand::ParseOptions(const std::vector<std::string>& args) {
340 bool demangle = true;
341 std::string symfs_dir;
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;
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)) {
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());
363 } else if (args[i] == "-g") {
364 print_callgraph_ = true;
365 accumulate_callchain_ = true;
366 if (i + 1 < args.size() && args[i + 1][0] != '-') {
368 if (args[i] == "callee") {
369 callgraph_show_callee_ = true;
370 } else if (args[i] == "caller") {
371 callgraph_show_callee_ = false;
373 LOG(ERROR) << "Unknown argument with -g option: " << args[i];
377 } else if (args[i] == "-i") {
378 if (!NextArgumentOrError(args, &i)) {
381 record_filename_ = args[i];
383 } else if (args[i] == "-n") {
384 print_sample_count = true;
386 } else if (args[i] == "--no-demangle") {
389 } else if (args[i] == "--pids" || args[i] == "--tids") {
390 if (!NextArgumentOrError(args, &i)) {
393 std::vector<std::string> strs = android::base::Split(args[i], ",");
394 std::vector<int> ids;
395 for (auto& s : strs) {
397 if (!StringToPid(s, &id)) {
398 LOG(ERROR) << "invalid id in " << args[i] << " option: " << s;
403 std::unordered_set<int>& filter = (args[i] == "--pids" ? pid_filter : tid_filter);
404 filter.insert(ids.begin(), ids.end());
406 } else if (args[i] == "--sort") {
407 if (!NextArgumentOrError(args, &i)) {
410 sort_keys = android::base::Split(args[i], ",");
411 } else if (args[i] == "--symfs") {
412 if (!NextArgumentOrError(args, &i)) {
417 } else if (args[i] == "--vmlinux") {
418 if (!NextArgumentOrError(args, &i)) {
423 ReportUnknownOption(args, i);
428 Dso::SetDemangle(demangle);
429 if (!Dso::SetSymFsDir(symfs_dir)) {
432 if (!vmlinux.empty()) {
433 Dso::SetVmlinux(vmlinux);
436 if (!accumulate_callchain_) {
437 displayable_items_.push_back(
438 std::unique_ptr<Displayable>(new SelfOverheadItem(*sample_tree_, "Overhead")));
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_)));
444 if (print_sample_count) {
445 displayable_items_.push_back(std::unique_ptr<Displayable>(new SampleCountItem));
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.";
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);
489 LOG(ERROR) << "Unknown sort key: " << key;
493 sample_tree_->SetFilters(pid_filter, tid_filter, comm_filter, dso_filter);
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";
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.";
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));
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()));
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);
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) {
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());
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());
560 std::vector<SampleEntry*> callchain;
561 callchain.push_back(sample);
563 bool first_ip = true;
564 for (auto& ip : ips) {
565 if (ip >= PERF_CONTEXT_MAX) {
567 case PERF_CONTEXT_KERNEL:
570 case PERF_CONTEXT_USER:
574 LOG(ERROR) << "Unexpected perf_context in callchain: " << ip;
579 // Remove duplication with sampled ip.
580 if (ip == r.ip_data.ip) {
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);
591 if (print_callgraph_) {
592 std::set<SampleEntry*> added_set;
593 if (!callgraph_show_callee_) {
594 std::reverse(callchain.begin(), callchain.end());
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()) {
603 added_set.insert(sample);
604 sample_tree_->InsertCallChainForSample(sample, callchain, r.period_data.period);
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));
617 Dso::SetBuildIds(build_ids);
619 std::string arch = record_file_reader_->ReadFeatureString(PerfFileFormat::FEAT_ARCH);
621 if (!SetCurrentArch(arch)) {
626 std::vector<std::string> cmdline = record_file_reader_->ReadCmdlineFeature();
627 if (!cmdline.empty()) {
628 record_cmdline_ = android::base::Join(cmdline, ' ');
633 int ReportCommand::CompareSampleEntry(const SampleEntry& sample1, const SampleEntry& sample2) {
634 for (auto& item : comparable_items_) {
635 int result = item->Compare(sample1, sample2);
643 void ReportCommand::PrintReport() {
644 PrintReportContext();
645 CollectReportWidth();
647 sample_tree_->VisitAllSamples(
648 std::bind(&ReportCommand::PrintReportEntry, this, std::placeholders::_1));
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;
659 android::base::StringPrintf("(type %u, config %llu)", event_attr_.type, event_attr_.config);
661 if (!record_cmdline_.empty()) {
662 printf("Cmdline: %s\n", record_cmdline_.c_str());
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());
669 void ReportCommand::CollectReportWidth() {
670 sample_tree_->VisitAllSamples(
671 std::bind(&ReportCommand::CollectReportEntryWidth, this, std::placeholders::_1));
674 void ReportCommand::CollectReportEntryWidth(const SampleEntry& sample) {
675 for (auto& item : displayable_items_) {
676 item->AdjustWidth(sample);
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());
686 printf("%s\n", item->Name().c_str());
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());
697 printf("%s\n", item->Show(sample).c_str());
700 if (print_callgraph_) {
701 PrintCallGraph(sample);
705 static void PrintCallGraphEntry(size_t depth, std::string prefix,
706 const std::unique_ptr<CallChainNode>& node, uint64_t parent_period,
709 LOG(WARNING) << "truncated callgraph at depth " << depth;
713 printf("%s\n", prefix.c_str());
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);
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());
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()));
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()));
745 __attribute__((constructor)) static void RegisterReportCommand() {
746 RegisterCommand("report", [] { return std::unique_ptr<Command>(new ReportCommand()); });