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/parseint.h>
29 #include <android-base/stringprintf.h>
30 #include <android-base/strings.h>
33 #include "dwarf_unwind.h"
34 #include "environment.h"
35 #include "event_attr.h"
36 #include "event_type.h"
37 #include "perf_regs.h"
39 #include "record_file.h"
40 #include "sample_tree.h"
41 #include "thread_tree.h"
47 static std::set<std::string> branch_sort_keys = {
48 "dso_from", "dso_to", "symbol_from", "symbol_to",
50 struct BranchFromEntry {
53 uint64_t vaddr_in_file;
57 : map(nullptr), symbol(nullptr), vaddr_in_file(0), flags(0) {}
63 // accumuated when appearing in other sample's callchain
64 uint64_t accumulated_period;
65 uint64_t sample_count;
66 const ThreadEntry* thread;
67 const char* thread_comm;
70 uint64_t vaddr_in_file;
71 BranchFromEntry branch_from;
72 // a callchain tree representing all callchains in the sample
73 CallChainRoot<SampleEntry> callchain;
75 SampleEntry(uint64_t time, uint64_t period, uint64_t accumulated_period,
76 uint64_t sample_count, const ThreadEntry* thread,
77 const MapEntry* map, const Symbol* symbol, uint64_t vaddr_in_file)
80 accumulated_period(accumulated_period),
81 sample_count(sample_count),
83 thread_comm(thread->comm),
86 vaddr_in_file(vaddr_in_file) {}
88 // The data member 'callchain' can only move, not copy.
89 SampleEntry(SampleEntry&&) = default;
90 SampleEntry(SampleEntry&) = delete;
94 std::vector<SampleEntry*> samples;
95 uint64_t total_samples;
96 uint64_t total_period;
99 BUILD_COMPARE_VALUE_FUNCTION(CompareVaddrInFile, vaddr_in_file);
100 BUILD_DISPLAY_HEX64_FUNCTION(DisplayVaddrInFile, vaddr_in_file);
102 class ReportCmdSampleTreeBuilder
103 : public SampleTreeBuilder<SampleEntry, uint64_t> {
105 ReportCmdSampleTreeBuilder(SampleComparator<SampleEntry> sample_comparator,
106 ThreadTree* thread_tree)
107 : SampleTreeBuilder(sample_comparator),
108 thread_tree_(thread_tree),
112 void SetFilters(const std::unordered_set<int>& pid_filter,
113 const std::unordered_set<int>& tid_filter,
114 const std::unordered_set<std::string>& comm_filter,
115 const std::unordered_set<std::string>& dso_filter,
116 const std::unordered_set<std::string>& symbol_filter) {
117 pid_filter_ = pid_filter;
118 tid_filter_ = tid_filter;
119 comm_filter_ = comm_filter;
120 dso_filter_ = dso_filter;
121 symbol_filter_ = symbol_filter;
124 SampleTree GetSampleTree() const {
125 SampleTree sample_tree;
126 sample_tree.samples = GetSamples();
127 sample_tree.total_samples = total_samples_;
128 sample_tree.total_period = total_period_;
133 SampleEntry* CreateSample(const SampleRecord& r, bool in_kernel,
134 uint64_t* acc_info) override {
135 const ThreadEntry* thread =
136 thread_tree_->FindThreadOrNew(r.tid_data.pid, r.tid_data.tid);
137 const MapEntry* map =
138 thread_tree_->FindMap(thread, r.ip_data.ip, in_kernel);
139 uint64_t vaddr_in_file;
140 const Symbol* symbol =
141 thread_tree_->FindSymbol(map, r.ip_data.ip, &vaddr_in_file);
142 *acc_info = r.period_data.period;
143 return InsertSample(std::unique_ptr<SampleEntry>(
144 new SampleEntry(r.time_data.time, r.period_data.period, 0, 1, thread,
145 map, symbol, vaddr_in_file)));
148 SampleEntry* CreateBranchSample(const SampleRecord& r,
149 const BranchStackItemType& item) override {
150 const ThreadEntry* thread =
151 thread_tree_->FindThreadOrNew(r.tid_data.pid, r.tid_data.tid);
152 const MapEntry* from_map = thread_tree_->FindMap(thread, item.from);
153 uint64_t from_vaddr_in_file;
154 const Symbol* from_symbol =
155 thread_tree_->FindSymbol(from_map, item.from, &from_vaddr_in_file);
156 const MapEntry* to_map = thread_tree_->FindMap(thread, item.to);
157 uint64_t to_vaddr_in_file;
158 const Symbol* to_symbol =
159 thread_tree_->FindSymbol(to_map, item.to, &to_vaddr_in_file);
160 std::unique_ptr<SampleEntry> sample(
161 new SampleEntry(r.time_data.time, r.period_data.period, 0, 1, thread,
162 to_map, to_symbol, to_vaddr_in_file));
163 sample->branch_from.map = from_map;
164 sample->branch_from.symbol = from_symbol;
165 sample->branch_from.vaddr_in_file = from_vaddr_in_file;
166 sample->branch_from.flags = item.flags;
167 return InsertSample(std::move(sample));
170 SampleEntry* CreateCallChainSample(const SampleEntry* sample, uint64_t ip,
172 const std::vector<SampleEntry*>& callchain,
173 const uint64_t& acc_info) override {
174 const ThreadEntry* thread = sample->thread;
175 const MapEntry* map = thread_tree_->FindMap(thread, ip, in_kernel);
176 uint64_t vaddr_in_file;
177 const Symbol* symbol = thread_tree_->FindSymbol(map, ip, &vaddr_in_file);
178 std::unique_ptr<SampleEntry> callchain_sample(new SampleEntry(
179 sample->time, 0, acc_info, 0, thread, map, symbol, vaddr_in_file));
180 return InsertCallChainSample(std::move(callchain_sample), callchain);
183 const ThreadEntry* GetThreadOfSample(SampleEntry* sample) override {
184 return sample->thread;
187 uint64_t GetPeriodForCallChain(const uint64_t& acc_info) override {
191 bool FilterSample(const SampleEntry* sample) override {
192 if (!pid_filter_.empty() &&
193 pid_filter_.find(sample->thread->pid) == pid_filter_.end()) {
196 if (!tid_filter_.empty() &&
197 tid_filter_.find(sample->thread->tid) == tid_filter_.end()) {
200 if (!comm_filter_.empty() &&
201 comm_filter_.find(sample->thread_comm) == comm_filter_.end()) {
204 if (!dso_filter_.empty() &&
205 dso_filter_.find(sample->map->dso->Path()) == dso_filter_.end()) {
208 if (!symbol_filter_.empty() &&
209 symbol_filter_.find(sample->symbol->DemangledName()) ==
210 symbol_filter_.end()) {
216 void UpdateSummary(const SampleEntry* sample) override {
217 total_samples_ += sample->sample_count;
218 total_period_ += sample->period;
221 void MergeSample(SampleEntry* sample1, SampleEntry* sample2) override {
222 sample1->period += sample2->period;
223 sample1->accumulated_period += sample2->accumulated_period;
224 sample1->sample_count += sample2->sample_count;
228 ThreadTree* thread_tree_;
230 std::unordered_set<int> pid_filter_;
231 std::unordered_set<int> tid_filter_;
232 std::unordered_set<std::string> comm_filter_;
233 std::unordered_set<std::string> dso_filter_;
234 std::unordered_set<std::string> symbol_filter_;
236 uint64_t total_samples_;
237 uint64_t total_period_;
240 using ReportCmdSampleTreeSorter = SampleTreeSorter<SampleEntry>;
241 using ReportCmdSampleTreeDisplayer =
242 SampleTreeDisplayer<SampleEntry, SampleTree>;
244 using ReportCmdCallgraphDisplayer =
245 CallgraphDisplayer<SampleEntry, CallChainNode<SampleEntry>>;
247 class ReportCmdCallgraphDisplayerWithVaddrInFile
248 : public ReportCmdCallgraphDisplayer {
250 std::string PrintSampleName(const SampleEntry* sample) override {
251 return android::base::StringPrintf("%s [+0x%" PRIx64 "]",
252 sample->symbol->DemangledName(),
253 sample->vaddr_in_file);
257 struct EventAttrWithName {
258 perf_event_attr attr;
260 std::vector<uint64_t> event_ids;
263 class ReportCommand : public Command {
267 "report", "report sampling information in perf.data",
269 "Usage: simpleperf report [options]\n"
270 "-b Use the branch-to addresses in sampled take branches instead of the\n"
271 " instruction addresses. Only valid for perf.data recorded with -b/-j\n"
273 "--children Print the overhead accumulated by appearing in the callchain.\n"
274 "--comms comm1,comm2,... Report only for selected comms.\n"
275 "--dsos dso1,dso2,... Report only for selected dsos.\n"
276 "-g [callee|caller] Print call graph. If callee mode is used, the graph\n"
277 " shows how functions are called from others. Otherwise,\n"
278 " the graph shows how functions call others.\n"
279 " Default is caller mode.\n"
280 "-i <file> Specify path of record file, default is perf.data.\n"
281 "-n Print the sample count for each item.\n"
282 "--no-demangle Don't demangle symbol names.\n"
283 "--no-show-ip Don't show vaddr in file for unknown symbols.\n"
284 "-o report_file_name Set report file name, default is stdout.\n"
285 "--pids pid1,pid2,... Report only for selected pids.\n"
286 "--sort key1,key2,... Select keys used to sort and print the report. The\n"
287 " appearance order of keys decides the order of keys used\n"
288 " to sort and print the report.\n"
289 " Possible keys include:\n"
290 " pid -- process id\n"
291 " tid -- thread id\n"
292 " comm -- thread name (can be changed during\n"
293 " the lifetime of a thread)\n"
294 " dso -- shared library\n"
295 " symbol -- function name in the shared library\n"
296 " vaddr_in_file -- virtual address in the shared\n"
298 " Keys can only be used with -b option:\n"
299 " dso_from -- shared library branched from\n"
300 " dso_to -- shared library branched to\n"
301 " symbol_from -- name of function branched from\n"
302 " symbol_to -- name of function branched to\n"
303 " The default sort keys are:\n"
304 " comm,pid,tid,dso,symbol\n"
305 "--symbols symbol1;symbol2;... Report only for selected symbols.\n"
306 "--symfs <dir> Look for files with symbols relative to this directory.\n"
307 "--tids tid1,tid2,... Report only for selected tids.\n"
308 "--vmlinux <file> Parse kernel symbols from <file>.\n"
311 record_filename_("perf.data"),
312 record_file_arch_(GetBuildArch()),
313 use_branch_address_(false),
314 system_wide_collection_(false),
315 accumulate_callchain_(false),
316 print_callgraph_(false),
317 callgraph_show_callee_(false) {}
319 bool Run(const std::vector<std::string>& args);
322 bool ParseOptions(const std::vector<std::string>& args);
323 bool ReadEventAttrFromRecordFile();
324 bool ReadFeaturesFromRecordFile();
325 bool ReadSampleTreeFromRecordFile();
326 bool ProcessRecord(std::unique_ptr<Record> record);
327 bool ProcessTracingData(const std::vector<char>& data);
329 void PrintReportContext(FILE* fp);
331 std::string record_filename_;
332 ArchType record_file_arch_;
333 std::unique_ptr<RecordFileReader> record_file_reader_;
334 std::vector<EventAttrWithName> event_attrs_;
335 ThreadTree thread_tree_;
336 SampleTree sample_tree_;
337 std::unique_ptr<ReportCmdSampleTreeBuilder> sample_tree_builder_;
338 std::unique_ptr<ReportCmdSampleTreeSorter> sample_tree_sorter_;
339 std::unique_ptr<ReportCmdSampleTreeDisplayer> sample_tree_displayer_;
340 bool use_branch_address_;
341 std::string record_cmdline_;
342 bool system_wide_collection_;
343 bool accumulate_callchain_;
344 bool print_callgraph_;
345 bool callgraph_show_callee_;
347 std::string report_filename_;
350 bool ReportCommand::Run(const std::vector<std::string>& args) {
352 if (!ParseOptions(args)) {
356 // 2. Read record file and build SampleTree.
357 record_file_reader_ = RecordFileReader::CreateInstance(record_filename_);
358 if (record_file_reader_ == nullptr) {
361 if (!ReadEventAttrFromRecordFile()) {
364 // Read features first to prepare build ids used when building SampleTree.
365 if (!ReadFeaturesFromRecordFile()) {
368 ScopedCurrentArch scoped_arch(record_file_arch_);
369 if (!ReadSampleTreeFromRecordFile()) {
373 // 3. Show collected information.
374 if (!PrintReport()) {
381 bool ReportCommand::ParseOptions(const std::vector<std::string>& args) {
382 bool demangle = true;
383 bool show_ip_for_unknown_symbol = true;
384 std::string symfs_dir;
386 bool print_sample_count = false;
387 std::vector<std::string> sort_keys = {"comm", "pid", "tid", "dso", "symbol"};
388 std::unordered_set<std::string> comm_filter;
389 std::unordered_set<std::string> dso_filter;
390 std::unordered_set<std::string> symbol_filter;
391 std::unordered_set<int> pid_filter;
392 std::unordered_set<int> tid_filter;
394 for (size_t i = 0; i < args.size(); ++i) {
395 if (args[i] == "-b") {
396 use_branch_address_ = true;
397 } else if (args[i] == "--children") {
398 accumulate_callchain_ = true;
399 } else if (args[i] == "--comms" || args[i] == "--dsos") {
400 std::unordered_set<std::string>& filter =
401 (args[i] == "--comms" ? comm_filter : dso_filter);
402 if (!NextArgumentOrError(args, &i)) {
405 std::vector<std::string> strs = android::base::Split(args[i], ",");
406 filter.insert(strs.begin(), strs.end());
408 } else if (args[i] == "-g") {
409 print_callgraph_ = true;
410 accumulate_callchain_ = true;
411 if (i + 1 < args.size() && args[i + 1][0] != '-') {
413 if (args[i] == "callee") {
414 callgraph_show_callee_ = true;
415 } else if (args[i] == "caller") {
416 callgraph_show_callee_ = false;
418 LOG(ERROR) << "Unknown argument with -g option: " << args[i];
422 } else if (args[i] == "-i") {
423 if (!NextArgumentOrError(args, &i)) {
426 record_filename_ = args[i];
428 } else if (args[i] == "-n") {
429 print_sample_count = true;
431 } else if (args[i] == "--no-demangle") {
433 } else if (args[i] == "--no-show-ip") {
434 show_ip_for_unknown_symbol = false;
435 } else if (args[i] == "-o") {
436 if (!NextArgumentOrError(args, &i)) {
439 report_filename_ = args[i];
441 } else if (args[i] == "--pids" || args[i] == "--tids") {
442 if (!NextArgumentOrError(args, &i)) {
445 std::vector<std::string> strs = android::base::Split(args[i], ",");
446 std::vector<int> ids;
447 for (const auto& s : strs) {
449 if (!android::base::ParseInt(s.c_str(), &id, 0)) {
450 LOG(ERROR) << "invalid id in " << args[i] << " option: " << s;
455 std::unordered_set<int>& filter =
456 (args[i] == "--pids" ? pid_filter : tid_filter);
457 filter.insert(ids.begin(), ids.end());
459 } else if (args[i] == "--sort") {
460 if (!NextArgumentOrError(args, &i)) {
463 sort_keys = android::base::Split(args[i], ",");
464 } else if (args[i] == "--symbols") {
465 if (!NextArgumentOrError(args, &i)) {
468 std::vector<std::string> strs = android::base::Split(args[i], ";");
469 symbol_filter.insert(strs.begin(), strs.end());
470 } else if (args[i] == "--symfs") {
471 if (!NextArgumentOrError(args, &i)) {
476 } else if (args[i] == "--vmlinux") {
477 if (!NextArgumentOrError(args, &i)) {
482 ReportUnknownOption(args, i);
487 Dso::SetDemangle(demangle);
488 if (!Dso::SetSymFsDir(symfs_dir)) {
491 if (!vmlinux.empty()) {
492 Dso::SetVmlinux(vmlinux);
495 if (show_ip_for_unknown_symbol) {
496 thread_tree_.ShowIpForUnknownSymbol();
499 SampleDisplayer<SampleEntry, SampleTree> displayer;
500 SampleComparator<SampleEntry> comparator;
502 if (accumulate_callchain_) {
503 displayer.AddDisplayFunction("Children", DisplayAccumulatedOverhead);
504 displayer.AddDisplayFunction("Self", DisplaySelfOverhead);
506 displayer.AddDisplayFunction("Overhead", DisplaySelfOverhead);
508 if (print_sample_count) {
509 displayer.AddDisplayFunction("Sample", DisplaySampleCount);
512 for (auto& key : sort_keys) {
513 if (!use_branch_address_ &&
514 branch_sort_keys.find(key) != branch_sort_keys.end()) {
515 LOG(ERROR) << "sort key '" << key << "' can only be used with -b option.";
519 comparator.AddCompareFunction(ComparePid);
520 displayer.AddDisplayFunction("Pid", DisplayPid);
521 } else if (key == "tid") {
522 comparator.AddCompareFunction(CompareTid);
523 displayer.AddDisplayFunction("Tid", DisplayTid);
524 } else if (key == "comm") {
525 comparator.AddCompareFunction(CompareComm);
526 displayer.AddDisplayFunction("Command", DisplayComm);
527 } else if (key == "dso") {
528 comparator.AddCompareFunction(CompareDso);
529 displayer.AddDisplayFunction("Shared Object", DisplayDso);
530 } else if (key == "symbol") {
531 comparator.AddCompareFunction(CompareSymbol);
532 displayer.AddDisplayFunction("Symbol", DisplaySymbol);
533 } else if (key == "vaddr_in_file") {
534 comparator.AddCompareFunction(CompareVaddrInFile);
535 displayer.AddDisplayFunction("VaddrInFile", DisplayVaddrInFile);
536 } else if (key == "dso_from") {
537 comparator.AddCompareFunction(CompareDsoFrom);
538 displayer.AddDisplayFunction("Source Shared Object", DisplayDsoFrom);
539 } else if (key == "dso_to") {
540 comparator.AddCompareFunction(CompareDso);
541 displayer.AddDisplayFunction("Target Shared Object", DisplayDso);
542 } else if (key == "symbol_from") {
543 comparator.AddCompareFunction(CompareSymbolFrom);
544 displayer.AddDisplayFunction("Source Symbol", DisplaySymbolFrom);
545 } else if (key == "symbol_to") {
546 comparator.AddCompareFunction(CompareSymbol);
547 displayer.AddDisplayFunction("Target Symbol", DisplaySymbol);
549 LOG(ERROR) << "Unknown sort key: " << key;
553 if (print_callgraph_) {
554 bool has_symbol_key = false;
555 bool has_vaddr_in_file_key = false;
556 for (const auto& key : sort_keys) {
557 if (key == "symbol") {
558 has_symbol_key = true;
559 } else if (key == "vaddr_in_file") {
560 has_vaddr_in_file_key = true;
563 if (has_symbol_key) {
564 if (has_vaddr_in_file_key) {
565 displayer.AddExclusiveDisplayFunction(
566 ReportCmdCallgraphDisplayerWithVaddrInFile());
568 displayer.AddExclusiveDisplayFunction(ReportCmdCallgraphDisplayer());
573 sample_tree_builder_.reset(
574 new ReportCmdSampleTreeBuilder(comparator, &thread_tree_));
575 sample_tree_builder_->SetFilters(pid_filter, tid_filter, comm_filter,
576 dso_filter, symbol_filter);
578 SampleComparator<SampleEntry> sort_comparator;
579 sort_comparator.AddCompareFunction(CompareTotalPeriod);
580 sort_comparator.AddComparator(comparator);
581 sample_tree_sorter_.reset(new ReportCmdSampleTreeSorter(sort_comparator));
582 sample_tree_displayer_.reset(new ReportCmdSampleTreeDisplayer(displayer));
586 bool ReportCommand::ReadEventAttrFromRecordFile() {
587 std::vector<AttrWithId> attrs = record_file_reader_->AttrSection();
588 for (const auto& attr_with_id : attrs) {
589 EventAttrWithName attr;
590 attr.attr = *attr_with_id.attr;
591 attr.event_ids = attr_with_id.ids;
592 attr.name = GetEventNameByAttr(attr.attr);
593 event_attrs_.push_back(attr);
595 if (use_branch_address_) {
596 bool has_branch_stack = true;
597 for (const auto& attr : event_attrs_) {
598 if ((attr.attr.sample_type & PERF_SAMPLE_BRANCH_STACK) == 0) {
599 has_branch_stack = false;
603 if (!has_branch_stack) {
604 LOG(ERROR) << record_filename_
605 << " is not recorded with branch stack sampling option.";
612 bool ReportCommand::ReadFeaturesFromRecordFile() {
613 std::vector<BuildIdRecord> records =
614 record_file_reader_->ReadBuildIdFeature();
615 std::vector<std::pair<std::string, BuildId>> build_ids;
616 for (auto& r : records) {
617 build_ids.push_back(std::make_pair(r.filename, r.build_id));
619 Dso::SetBuildIds(build_ids);
622 record_file_reader_->ReadFeatureString(PerfFileFormat::FEAT_ARCH);
624 record_file_arch_ = GetArchType(arch);
625 if (record_file_arch_ == ARCH_UNSUPPORTED) {
630 std::vector<std::string> cmdline = record_file_reader_->ReadCmdlineFeature();
631 if (!cmdline.empty()) {
632 record_cmdline_ = android::base::Join(cmdline, ' ');
633 // TODO: the code to detect system wide collection option is fragile, remove
634 // it once we can do cross unwinding.
635 for (size_t i = 0; i < cmdline.size(); i++) {
636 std::string& s = cmdline[i];
638 system_wide_collection_ = true;
640 } else if (s == "--call-graph" || s == "--cpu" || s == "-e" ||
641 s == "-f" || s == "-F" || s == "-j" || s == "-m" ||
642 s == "-o" || s == "-p" || s == "-t") {
644 } else if (!s.empty() && s[0] != '-') {
649 if (record_file_reader_->HasFeature(PerfFileFormat::FEAT_TRACING_DATA)) {
650 std::vector<char> tracing_data;
651 if (!record_file_reader_->ReadFeatureSection(
652 PerfFileFormat::FEAT_TRACING_DATA, &tracing_data)) {
655 if (!ProcessTracingData(tracing_data)) {
662 bool ReportCommand::ReadSampleTreeFromRecordFile() {
663 thread_tree_.AddThread(0, 0, "swapper");
664 sample_tree_builder_->SetBranchSampleOption(use_branch_address_);
665 // Normally do strict arch check when unwinding stack. But allow unwinding
666 // 32-bit processes on 64-bit devices for system wide profiling.
667 bool strict_unwind_arch_check = !system_wide_collection_;
668 sample_tree_builder_->SetCallChainSampleOptions(
669 accumulate_callchain_, print_callgraph_, !callgraph_show_callee_,
670 strict_unwind_arch_check);
671 if (!record_file_reader_->ReadDataSection(
672 [this](std::unique_ptr<Record> record) {
673 return ProcessRecord(std::move(record));
677 sample_tree_ = sample_tree_builder_->GetSampleTree();
678 sample_tree_sorter_->Sort(sample_tree_.samples, print_callgraph_);
682 bool ReportCommand::ProcessRecord(std::unique_ptr<Record> record) {
683 thread_tree_.Update(*record);
684 if (record->type() == PERF_RECORD_SAMPLE) {
685 sample_tree_builder_->ProcessSampleRecord(
686 *static_cast<const SampleRecord*>(record.get()));
687 } else if (record->type() == PERF_RECORD_TRACING_DATA) {
688 const auto& r = *static_cast<TracingDataRecord*>(record.get());
689 if (!ProcessTracingData(std::vector<char>(r.data, r.data + r.data_size))) {
696 bool ReportCommand::ProcessTracingData(const std::vector<char>& data) {
697 Tracing tracing(data);
698 for (auto& attr : event_attrs_) {
699 if (attr.attr.type == PERF_TYPE_TRACEPOINT) {
700 uint64_t trace_event_id = attr.attr.config;
701 attr.name = tracing.GetTracingEventNameHavingId(trace_event_id);
707 bool ReportCommand::PrintReport() {
708 std::unique_ptr<FILE, decltype(&fclose)> file_handler(nullptr, fclose);
709 FILE* report_fp = stdout;
710 if (!report_filename_.empty()) {
711 report_fp = fopen(report_filename_.c_str(), "w");
712 if (report_fp == nullptr) {
713 PLOG(ERROR) << "failed to open file " << report_filename_;
716 file_handler.reset(report_fp);
718 PrintReportContext(report_fp);
719 sample_tree_displayer_->DisplaySamples(report_fp, sample_tree_.samples,
722 if (ferror(report_fp) != 0) {
723 PLOG(ERROR) << "print report failed";
729 void ReportCommand::PrintReportContext(FILE* report_fp) {
730 if (!record_cmdline_.empty()) {
731 fprintf(report_fp, "Cmdline: %s\n", record_cmdline_.c_str());
733 fprintf(report_fp, "Arch: %s\n", GetArchString(record_file_arch_).c_str());
734 for (const auto& attr : event_attrs_) {
735 fprintf(report_fp, "Event: %s (type %u, config %llu)\n", attr.name.c_str(),
736 attr.attr.type, attr.attr.config);
738 fprintf(report_fp, "Samples: %" PRIu64 "\n", sample_tree_.total_samples);
739 fprintf(report_fp, "Event count: %" PRIu64 "\n\n", sample_tree_.total_period);
744 void RegisterReportCommand() {
745 RegisterCommand("report",
746 [] { return std::unique_ptr<Command>(new ReportCommand()); });