#include <unordered_set>
#include <vector>
-#include <base/logging.h>
-#include <base/stringprintf.h>
-#include <base/strings.h>
+#include <android-base/logging.h>
+#include <android-base/parseint.h>
+#include <android-base/stringprintf.h>
+#include <android-base/strings.h>
#include "command.h"
+#include "dwarf_unwind.h"
#include "environment.h"
#include "event_attr.h"
#include "event_type.h"
" -i <file> 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"
" --vmlinux <file>\n"
" Parse kernel symbols from <file>.\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_ =
bool ParseOptions(const std::vector<std::string>& args);
bool ReadEventAttrFromRecordFile();
void ReadSampleTreeFromRecordFile();
+ void ProcessRecord(std::unique_ptr<Record> 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<CallChainNode>& node,
+ uint64_t parent_period, bool last);
std::string record_filename_;
+ ArchType record_file_arch_;
std::unique_ptr<RecordFileReader> record_file_reader_;
perf_event_attr event_attr_;
std::vector<std::unique_ptr<Displayable>> displayable_items_;
bool accumulate_callchain_;
bool print_callgraph_;
bool callgraph_show_callee_;
+
+ std::string report_filename_;
+ FILE* report_fp_;
};
bool ReportCommand::Run(const std::vector<std::string>& args) {
if (!ReadFeaturesFromRecordFile()) {
return false;
}
+ ScopedCurrentArch scoped_arch(record_file_arch_);
ReadSampleTreeFromRecordFile();
// 3. Show collected information.
- PrintReport();
+ if (!PrintReport()) {
+ return false;
+ }
return true;
}
} else if (args[i] == "--children") {
accumulate_callchain_ = true;
} else if (args[i] == "--comms" || args[i] == "--dsos") {
+ std::unordered_set<std::string>& filter = (args[i] == "--comms" ? comm_filter : dso_filter);
if (!NextArgumentOrError(args, &i)) {
return false;
}
std::vector<std::string> strs = android::base::Split(args[i], ",");
- std::unordered_set<std::string>& filter = (args[i] == "--comms" ? comm_filter : dso_filter);
filter.insert(strs.begin(), strs.end());
} else if (args[i] == "-g") {
} 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)) {
}
std::vector<std::string> strs = android::base::Split(args[i], ",");
std::vector<int> 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;
}
}
bool ReportCommand::ReadEventAttrFromRecordFile() {
- std::vector<const PerfFileFormat::FileAttr*> attrs = record_file_reader_->AttrSection();
+ const std::vector<PerfFileFormat::FileAttr>& 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;
}
void ReportCommand::ReadSampleTreeFromRecordFile() {
- std::vector<std::unique_ptr<Record>> 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<const SampleRecord*>(record.get()));
- }
+ record_file_reader_->ReadDataSection([this](std::unique_ptr<Record> record) {
+ ProcessRecord(std::move(record));
+ return true;
+ });
+}
+
+void ReportCommand::ProcessRecord(std::unique_ptr<Record> record) {
+ BuildThreadTree(*record, &thread_tree_);
+ if (record->header.type == PERF_RECORD_SAMPLE) {
+ ProcessSampleRecord(*static_cast<const SampleRecord*>(record.get()));
}
}
if (sample == nullptr) {
return;
}
- if (accumulate_callchain_ && (r.sample_type & PERF_SAMPLE_CALLCHAIN) != 0) {
+ if (accumulate_callchain_) {
+ std::vector<uint64_t> 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<char> stack(r.stack_user_data.data.begin(),
+ r.stack_user_data.data.begin() + r.stack_user_data.data.size());
+ std::vector<uint64_t> 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<SampleEntry*> callchain;
callchain.push_back(sample);
- const std::vector<uint64_t>& ips = r.callchain_data.ips;
+
bool first_ip = true;
for (auto& ip : ips) {
if (ip >= PERF_CONTEXT_MAX) {
}
} 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,
callchain.push_back(sample);
}
}
+
if (print_callgraph_) {
std::set<SampleEntry*> added_set;
if (!callgraph_show_callee_) {
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;
}
}
return 0;
}
-void ReportCommand::PrintReport() {
+bool ReportCommand::PrintReport() {
+ std::unique_ptr<FILE, decltype(&fclose)> 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() {
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() {
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<int>(item->Width()), item->Name().c_str());
+ fprintf(report_fp_, "%-*s ", static_cast<int>(item->Width()), item->Name().c_str());
} else {
- printf("%s\n", item->Name().c_str());
+ fprintf(report_fp_, "%s\n", item->Name().c_str());
}
}
}
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<int>(item->Width()), item->Show(sample).c_str());
+ fprintf(report_fp_, "%-*s ", static_cast<int>(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_) {
}
}
-static void PrintCallGraphEntry(size_t depth, std::string prefix,
- const std::unique_ptr<CallChainNode>& 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<CallChainNode>& 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() = ' ';
}
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) {
}
}
-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<Command>(new ReportCommand()); });
}