X-Git-Url: http://git.osdn.net/view?a=blobdiff_plain;f=simpleperf%2Fcmd_report.cpp;h=3d778abb75070168b8201f59857b21767a218260;hb=cb6b387ada3637832e188b63c811a2c298474140;hp=7fee516895ddaf11d0ce96a86ff67a61de13636b;hpb=3125605d93510b6620019d1303c42a469bd4d4a6;p=android-x86%2Fsystem-extras.git diff --git a/simpleperf/cmd_report.cpp b/simpleperf/cmd_report.cpp index 7fee5168..3d778abb 100644 --- a/simpleperf/cmd_report.cpp +++ b/simpleperf/cmd_report.cpp @@ -24,11 +24,13 @@ #include #include -#include -#include -#include +#include +#include +#include +#include #include "command.h" +#include "dwarf_unwind.h" #include "environment.h" #include "event_attr.h" #include "event_type.h" @@ -254,6 +256,7 @@ class ReportCommand : public Command { " -i Specify path of record file, default is perf.data.\n" " -n Print the sample count for each item.\n" " --no-demangle Don't demangle symbol names.\n" + " -o report_file_name Set report file name, default is stdout.\n" " --pid pid1,pid2,...\n" " Report only for selected pids.\n" " --sort key1,key2,...\n" @@ -267,10 +270,12 @@ class ReportCommand : public Command { " --vmlinux \n" " Parse kernel symbols from .\n"), record_filename_("perf.data"), + record_file_arch_(GetBuildArch()), use_branch_address_(false), accumulate_callchain_(false), print_callgraph_(false), - callgraph_show_callee_(true) { + callgraph_show_callee_(true), + report_fp_(nullptr) { compare_sample_func_t compare_sample_callback = std::bind( &ReportCommand::CompareSampleEntry, this, std::placeholders::_1, std::placeholders::_2); sample_tree_ = @@ -283,18 +288,22 @@ class ReportCommand : public Command { bool ParseOptions(const std::vector& args); bool ReadEventAttrFromRecordFile(); void ReadSampleTreeFromRecordFile(); + void ProcessRecord(std::unique_ptr record); void ProcessSampleRecord(const SampleRecord& r); bool ReadFeaturesFromRecordFile(); int CompareSampleEntry(const SampleEntry& sample1, const SampleEntry& sample2); - void PrintReport(); + bool PrintReport(); void PrintReportContext(); void CollectReportWidth(); void CollectReportEntryWidth(const SampleEntry& sample); void PrintReportHeader(); void PrintReportEntry(const SampleEntry& sample); void PrintCallGraph(const SampleEntry& sample); + void PrintCallGraphEntry(size_t depth, std::string prefix, const std::unique_ptr& node, + uint64_t parent_period, bool last); std::string record_filename_; + ArchType record_file_arch_; std::unique_ptr record_file_reader_; perf_event_attr event_attr_; std::vector> displayable_items_; @@ -306,6 +315,9 @@ class ReportCommand : public Command { bool accumulate_callchain_; bool print_callgraph_; bool callgraph_show_callee_; + + std::string report_filename_; + FILE* report_fp_; }; bool ReportCommand::Run(const std::vector& args) { @@ -326,10 +338,13 @@ bool ReportCommand::Run(const std::vector& args) { if (!ReadFeaturesFromRecordFile()) { return false; } + ScopedCurrentArch scoped_arch(record_file_arch_); ReadSampleTreeFromRecordFile(); // 3. Show collected information. - PrintReport(); + if (!PrintReport()) { + return false; + } return true; } @@ -351,11 +366,11 @@ bool ReportCommand::ParseOptions(const std::vector& args) { } else if (args[i] == "--children") { accumulate_callchain_ = true; } else if (args[i] == "--comms" || args[i] == "--dsos") { + std::unordered_set& filter = (args[i] == "--comms" ? comm_filter : dso_filter); if (!NextArgumentOrError(args, &i)) { return false; } std::vector strs = android::base::Split(args[i], ","); - std::unordered_set& filter = (args[i] == "--comms" ? comm_filter : dso_filter); filter.insert(strs.begin(), strs.end()); } else if (args[i] == "-g") { @@ -383,6 +398,11 @@ bool ReportCommand::ParseOptions(const std::vector& args) { } else if (args[i] == "--no-demangle") { demangle = false; + } else if (args[i] == "-o") { + if (!NextArgumentOrError(args, &i)) { + return false; + } + report_filename_ = args[i]; } else if (args[i] == "--pids" || args[i] == "--tids") { if (!NextArgumentOrError(args, &i)) { @@ -390,9 +410,9 @@ bool ReportCommand::ParseOptions(const std::vector& args) { } std::vector strs = android::base::Split(args[i], ","); std::vector ids; - for (auto& s : strs) { + for (const auto& s : strs) { int id; - if (!StringToPid(s, &id)) { + if (!android::base::ParseInt(s.c_str(), &id, 0)) { LOG(ERROR) << "invalid id in " << args[i] << " option: " << s; return false; } @@ -493,12 +513,12 @@ bool ReportCommand::ParseOptions(const std::vector& args) { } bool ReportCommand::ReadEventAttrFromRecordFile() { - std::vector attrs = record_file_reader_->AttrSection(); + const std::vector& attrs = record_file_reader_->AttrSection(); if (attrs.size() != 1) { LOG(ERROR) << "record file contains " << attrs.size() << " attrs"; return false; } - event_attr_ = attrs[0]->attr; + event_attr_ = attrs[0].attr; if (use_branch_address_ && (event_attr_.sample_type & PERF_SAMPLE_BRANCH_STACK) == 0) { LOG(ERROR) << record_filename_ << " is not recorded with branch stack sampling option."; return false; @@ -507,13 +527,17 @@ bool ReportCommand::ReadEventAttrFromRecordFile() { } void ReportCommand::ReadSampleTreeFromRecordFile() { - std::vector> records = record_file_reader_->DataSection(); thread_tree_.AddThread(0, 0, "swapper"); - for (auto& record : records) { - BuildThreadTree(*record, &thread_tree_); - if (record->header.type == PERF_RECORD_SAMPLE) { - ProcessSampleRecord(*static_cast(record.get())); - } + record_file_reader_->ReadDataSection([this](std::unique_ptr record) { + ProcessRecord(std::move(record)); + return true; + }); +} + +void ReportCommand::ProcessRecord(std::unique_ptr record) { + BuildThreadTree(*record, &thread_tree_); + if (record->header.type == PERF_RECORD_SAMPLE) { + ProcessSampleRecord(*static_cast(record.get())); } } @@ -532,10 +556,29 @@ void ReportCommand::ProcessSampleRecord(const SampleRecord& r) { if (sample == nullptr) { return; } - if (accumulate_callchain_ && (r.sample_type & PERF_SAMPLE_CALLCHAIN) != 0) { + if (accumulate_callchain_) { + std::vector ips; + if (r.sample_type & PERF_SAMPLE_CALLCHAIN) { + ips.insert(ips.end(), r.callchain_data.ips.begin(), r.callchain_data.ips.end()); + } + // Use stack_user_data.data.size() instead of stack_user_data.dyn_size, to make up for + // the missing kernel patch in N9. See b/22612370. + if ((r.sample_type & PERF_SAMPLE_REGS_USER) && (r.regs_user_data.reg_mask != 0) && + (r.sample_type & PERF_SAMPLE_STACK_USER) && (!r.stack_user_data.data.empty())) { + RegSet regs = CreateRegSet(r.regs_user_data.reg_mask, r.regs_user_data.regs); + std::vector stack(r.stack_user_data.data.begin(), + r.stack_user_data.data.begin() + r.stack_user_data.data.size()); + std::vector unwind_ips = + UnwindCallChain(ScopedCurrentArch::GetCurrentArch(), *sample->thread, regs, stack); + if (!unwind_ips.empty()) { + ips.push_back(PERF_CONTEXT_USER); + ips.insert(ips.end(), unwind_ips.begin(), unwind_ips.end()); + } + } + std::vector callchain; callchain.push_back(sample); - const std::vector& ips = r.callchain_data.ips; + bool first_ip = true; for (auto& ip : ips) { if (ip >= PERF_CONTEXT_MAX) { @@ -551,11 +594,11 @@ void ReportCommand::ProcessSampleRecord(const SampleRecord& r) { } } else { if (first_ip) { + first_ip = false; // Remove duplication with sampled ip. if (ip == r.ip_data.ip) { continue; } - first_ip = false; } SampleEntry* sample = sample_tree_->AddCallChainSample(r.tid_data.pid, r.tid_data.tid, ip, r.time_data.time, @@ -563,6 +606,7 @@ void ReportCommand::ProcessSampleRecord(const SampleRecord& r) { callchain.push_back(sample); } } + if (print_callgraph_) { std::set added_set; if (!callgraph_show_callee_) { @@ -593,7 +637,8 @@ bool ReportCommand::ReadFeaturesFromRecordFile() { std::string arch = record_file_reader_->ReadFeatureString(PerfFileFormat::FEAT_ARCH); if (!arch.empty()) { - if (!SetCurrentArch(arch)) { + record_file_arch_ = GetArchType(arch); + if (record_file_arch_ == ARCH_UNSUPPORTED) { return false; } } @@ -615,13 +660,29 @@ int ReportCommand::CompareSampleEntry(const SampleEntry& sample1, const SampleEn return 0; } -void ReportCommand::PrintReport() { +bool ReportCommand::PrintReport() { + std::unique_ptr file_handler(nullptr, fclose); + if (report_filename_.empty()) { + report_fp_ = stdout; + } else { + report_fp_ = fopen(report_filename_.c_str(), "w"); + if (report_fp_ == nullptr) { + PLOG(ERROR) << "failed to open file " << report_filename_; + return false; + } + file_handler.reset(report_fp_); + } PrintReportContext(); CollectReportWidth(); PrintReportHeader(); sample_tree_->VisitAllSamples( std::bind(&ReportCommand::PrintReportEntry, this, std::placeholders::_1)); - fflush(stdout); + fflush(report_fp_); + if (ferror(report_fp_) != 0) { + PLOG(ERROR) << "print report failed"; + return false; + } + return true; } void ReportCommand::PrintReportContext() { @@ -634,11 +695,11 @@ void ReportCommand::PrintReportContext() { android::base::StringPrintf("(type %u, config %llu)", event_attr_.type, event_attr_.config); } if (!record_cmdline_.empty()) { - printf("Cmdline: %s\n", record_cmdline_.c_str()); + fprintf(report_fp_, "Cmdline: %s\n", record_cmdline_.c_str()); } - printf("Samples: %" PRIu64 " of event '%s'\n", sample_tree_->TotalSamples(), - event_type_name.c_str()); - printf("Event count: %" PRIu64 "\n\n", sample_tree_->TotalPeriod()); + fprintf(report_fp_, "Samples: %" PRIu64 " of event '%s'\n", sample_tree_->TotalSamples(), + event_type_name.c_str()); + fprintf(report_fp_, "Event count: %" PRIu64 "\n\n", sample_tree_->TotalPeriod()); } void ReportCommand::CollectReportWidth() { @@ -656,9 +717,9 @@ void ReportCommand::PrintReportHeader() { for (size_t i = 0; i < displayable_items_.size(); ++i) { auto& item = displayable_items_[i]; if (i != displayable_items_.size() - 1) { - printf("%-*s ", static_cast(item->Width()), item->Name().c_str()); + fprintf(report_fp_, "%-*s ", static_cast(item->Width()), item->Name().c_str()); } else { - printf("%s\n", item->Name().c_str()); + fprintf(report_fp_, "%s\n", item->Name().c_str()); } } } @@ -667,9 +728,9 @@ void ReportCommand::PrintReportEntry(const SampleEntry& sample) { for (size_t i = 0; i < displayable_items_.size(); ++i) { auto& item = displayable_items_[i]; if (i != displayable_items_.size() - 1) { - printf("%-*s ", static_cast(item->Width()), item->Show(sample).c_str()); + fprintf(report_fp_, "%-*s ", static_cast(item->Width()), item->Show(sample).c_str()); } else { - printf("%s\n", item->Show(sample).c_str()); + fprintf(report_fp_, "%s\n", item->Show(sample).c_str()); } } if (print_callgraph_) { @@ -677,15 +738,26 @@ void ReportCommand::PrintReportEntry(const SampleEntry& sample) { } } -static void PrintCallGraphEntry(size_t depth, std::string prefix, - const std::unique_ptr& node, uint64_t parent_period, - bool last) { +void ReportCommand::PrintCallGraph(const SampleEntry& sample) { + std::string prefix = " "; + fprintf(report_fp_, "%s|\n", prefix.c_str()); + fprintf(report_fp_, "%s-- %s\n", prefix.c_str(), sample.symbol->DemangledName()); + prefix.append(3, ' '); + for (size_t i = 0; i < sample.callchain.children.size(); ++i) { + PrintCallGraphEntry(1, prefix, sample.callchain.children[i], sample.callchain.children_period, + (i + 1 == sample.callchain.children.size())); + } +} + +void ReportCommand::PrintCallGraphEntry(size_t depth, std::string prefix, + const std::unique_ptr& node, + uint64_t parent_period, bool last) { if (depth > 20) { LOG(WARNING) << "truncated callgraph at depth " << depth; return; } prefix += "|"; - printf("%s\n", prefix.c_str()); + fprintf(report_fp_, "%s\n", prefix.c_str()); if (last) { prefix.back() = ' '; } @@ -694,10 +766,10 @@ static void PrintCallGraphEntry(size_t depth, std::string prefix, double percentage = 100.0 * (node->period + node->children_period) / parent_period; percentage_s = android::base::StringPrintf("--%.2lf%%-- ", percentage); } - printf("%s%s%s\n", prefix.c_str(), percentage_s.c_str(), node->chain[0]->symbol->DemangledName()); + fprintf(report_fp_, "%s%s%s\n", prefix.c_str(), percentage_s.c_str(), node->chain[0]->symbol->DemangledName()); prefix.append(percentage_s.size(), ' '); for (size_t i = 1; i < node->chain.size(); ++i) { - printf("%s%s\n", prefix.c_str(), node->chain[i]->symbol->DemangledName()); + fprintf(report_fp_, "%s%s\n", prefix.c_str(), node->chain[i]->symbol->DemangledName()); } for (size_t i = 0; i < node->children.size(); ++i) { @@ -706,17 +778,6 @@ static void PrintCallGraphEntry(size_t depth, std::string prefix, } } -void ReportCommand::PrintCallGraph(const SampleEntry& sample) { - std::string prefix = " "; - printf("%s|\n", prefix.c_str()); - printf("%s-- %s\n", prefix.c_str(), sample.symbol->DemangledName()); - prefix.append(3, ' '); - for (size_t i = 0; i < sample.callchain.children.size(); ++i) { - PrintCallGraphEntry(1, prefix, sample.callchain.children[i], sample.callchain.children_period, - (i + 1 == sample.callchain.children.size())); - } -} - -__attribute__((constructor)) static void RegisterReportCommand() { +void RegisterReportCommand() { RegisterCommand("report", [] { return std::unique_ptr(new ReportCommand()); }); }