1 /*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17 #include <inttypes.h>
18 #include <algorithm>
19 #include <functional>
20 #include <map>
21 #include <set>
22 #include <string>
23 #include <unordered_map>
24 #include <unordered_set>
25 #include <vector>
26
27 #include <android-base/file.h>
28 #include <android-base/logging.h>
29 #include <android-base/parseint.h>
30 #include <android-base/stringprintf.h>
31 #include <android-base/strings.h>
32
33 #include "command.h"
34 #include "event_attr.h"
35 #include "event_type.h"
36 #include "perf_regs.h"
37 #include "record.h"
38 #include "record_file.h"
39 #include "sample_tree.h"
40 #include "thread_tree.h"
41 #include "tracing.h"
42 #include "utils.h"
43
44 namespace {
45
46 static std::set<std::string> branch_sort_keys = {
47 "dso_from", "dso_to", "symbol_from", "symbol_to",
48 };
49 struct BranchFromEntry {
50 const MapEntry* map;
51 const Symbol* symbol;
52 uint64_t vaddr_in_file;
53 uint64_t flags;
54
BranchFromEntry__anona4eb4dc80111::BranchFromEntry55 BranchFromEntry()
56 : map(nullptr), symbol(nullptr), vaddr_in_file(0), flags(0) {}
57 };
58
59 struct SampleEntry {
60 uint64_t time;
61 uint64_t period;
62 // accumuated when appearing in other sample's callchain
63 uint64_t accumulated_period;
64 uint64_t sample_count;
65 const ThreadEntry* thread;
66 const char* thread_comm;
67 const MapEntry* map;
68 const Symbol* symbol;
69 uint64_t vaddr_in_file;
70 BranchFromEntry branch_from;
71 // a callchain tree representing all callchains in the sample
72 CallChainRoot<SampleEntry> callchain;
73
SampleEntry__anona4eb4dc80111::SampleEntry74 SampleEntry(uint64_t time, uint64_t period, uint64_t accumulated_period,
75 uint64_t sample_count, const ThreadEntry* thread,
76 const MapEntry* map, const Symbol* symbol, uint64_t vaddr_in_file)
77 : time(time),
78 period(period),
79 accumulated_period(accumulated_period),
80 sample_count(sample_count),
81 thread(thread),
82 thread_comm(thread->comm),
83 map(map),
84 symbol(symbol),
85 vaddr_in_file(vaddr_in_file) {}
86
87 // The data member 'callchain' can only move, not copy.
88 SampleEntry(SampleEntry&&) = default;
89 SampleEntry(SampleEntry&) = delete;
90
GetPeriod__anona4eb4dc80111::SampleEntry91 uint64_t GetPeriod() const {
92 return period;
93 }
94 };
95
96 struct SampleTree {
97 std::vector<SampleEntry*> samples;
98 uint64_t total_samples;
99 uint64_t total_period;
100 uint64_t total_error_callchains;
101 };
102
103 BUILD_COMPARE_VALUE_FUNCTION(CompareVaddrInFile, vaddr_in_file);
104 BUILD_DISPLAY_HEX64_FUNCTION(DisplayVaddrInFile, vaddr_in_file);
105
106 class ReportCmdSampleTreeBuilder : public SampleTreeBuilder<SampleEntry, uint64_t> {
107 public:
ReportCmdSampleTreeBuilder(const SampleComparator<SampleEntry> & sample_comparator,ThreadTree * thread_tree)108 ReportCmdSampleTreeBuilder(const SampleComparator<SampleEntry>& sample_comparator,
109 ThreadTree* thread_tree)
110 : SampleTreeBuilder(sample_comparator),
111 thread_tree_(thread_tree),
112 total_samples_(0),
113 total_period_(0),
114 total_error_callchains_(0) {}
115
SetFilters(const std::unordered_set<int> & pid_filter,const std::unordered_set<int> & tid_filter,const std::unordered_set<std::string> & comm_filter,const std::unordered_set<std::string> & dso_filter,const std::unordered_set<std::string> & symbol_filter)116 void SetFilters(const std::unordered_set<int>& pid_filter,
117 const std::unordered_set<int>& tid_filter,
118 const std::unordered_set<std::string>& comm_filter,
119 const std::unordered_set<std::string>& dso_filter,
120 const std::unordered_set<std::string>& symbol_filter) {
121 pid_filter_ = pid_filter;
122 tid_filter_ = tid_filter;
123 comm_filter_ = comm_filter;
124 dso_filter_ = dso_filter;
125 symbol_filter_ = symbol_filter;
126 }
127
GetSampleTree()128 SampleTree GetSampleTree() {
129 AddCallChainDuplicateInfo();
130 SampleTree sample_tree;
131 sample_tree.samples = GetSamples();
132 sample_tree.total_samples = total_samples_;
133 sample_tree.total_period = total_period_;
134 sample_tree.total_error_callchains = total_error_callchains_;
135 return sample_tree;
136 }
137
ReportCmdProcessSampleRecord(std::shared_ptr<SampleRecord> & r)138 virtual void ReportCmdProcessSampleRecord(std::shared_ptr<SampleRecord>& r) {
139 return ProcessSampleRecord(*r);
140 }
141
ReportCmdProcessSampleRecord(const SampleRecord & r)142 virtual void ReportCmdProcessSampleRecord(const SampleRecord& r) {
143 return ProcessSampleRecord(r);
144 }
145
146 protected:
147 virtual uint64_t GetPeriod(const SampleRecord& r) = 0;
148
CreateSample(const SampleRecord & r,bool in_kernel,uint64_t * acc_info)149 SampleEntry* CreateSample(const SampleRecord& r, bool in_kernel,
150 uint64_t* acc_info) override {
151 const ThreadEntry* thread =
152 thread_tree_->FindThreadOrNew(r.tid_data.pid, r.tid_data.tid);
153 const MapEntry* map =
154 thread_tree_->FindMap(thread, r.ip_data.ip, in_kernel);
155 uint64_t vaddr_in_file;
156 const Symbol* symbol =
157 thread_tree_->FindSymbol(map, r.ip_data.ip, &vaddr_in_file);
158 uint64_t period = GetPeriod(r);
159 *acc_info = period;
160 return InsertSample(std::unique_ptr<SampleEntry>(
161 new SampleEntry(r.time_data.time, period, 0, 1, thread, map, symbol, vaddr_in_file)));
162 }
163
CreateBranchSample(const SampleRecord & r,const BranchStackItemType & item)164 SampleEntry* CreateBranchSample(const SampleRecord& r,
165 const BranchStackItemType& item) override {
166 const ThreadEntry* thread =
167 thread_tree_->FindThreadOrNew(r.tid_data.pid, r.tid_data.tid);
168 const MapEntry* from_map = thread_tree_->FindMap(thread, item.from);
169 uint64_t from_vaddr_in_file;
170 const Symbol* from_symbol =
171 thread_tree_->FindSymbol(from_map, item.from, &from_vaddr_in_file);
172 const MapEntry* to_map = thread_tree_->FindMap(thread, item.to);
173 uint64_t to_vaddr_in_file;
174 const Symbol* to_symbol =
175 thread_tree_->FindSymbol(to_map, item.to, &to_vaddr_in_file);
176 std::unique_ptr<SampleEntry> sample(
177 new SampleEntry(r.time_data.time, r.period_data.period, 0, 1, thread,
178 to_map, to_symbol, to_vaddr_in_file));
179 sample->branch_from.map = from_map;
180 sample->branch_from.symbol = from_symbol;
181 sample->branch_from.vaddr_in_file = from_vaddr_in_file;
182 sample->branch_from.flags = item.flags;
183 return InsertSample(std::move(sample));
184 }
185
CreateCallChainSample(const SampleEntry * sample,uint64_t ip,bool in_kernel,const std::vector<SampleEntry * > & callchain,const uint64_t & acc_info)186 SampleEntry* CreateCallChainSample(const SampleEntry* sample, uint64_t ip,
187 bool in_kernel,
188 const std::vector<SampleEntry*>& callchain,
189 const uint64_t& acc_info) override {
190 const ThreadEntry* thread = sample->thread;
191 const MapEntry* map = thread_tree_->FindMap(thread, ip, in_kernel);
192 if (thread_tree_->IsUnknownDso(map->dso)) {
193 // The unwinders can give wrong ip addresses, which can't map to a valid dso. Skip them.
194 total_error_callchains_++;
195 return nullptr;
196 }
197 uint64_t vaddr_in_file;
198 const Symbol* symbol = thread_tree_->FindSymbol(map, ip, &vaddr_in_file);
199 std::unique_ptr<SampleEntry> callchain_sample(new SampleEntry(
200 sample->time, 0, acc_info, 0, thread, map, symbol, vaddr_in_file));
201 callchain_sample->thread_comm = sample->thread_comm;
202 return InsertCallChainSample(std::move(callchain_sample), callchain);
203 }
204
GetThreadOfSample(SampleEntry * sample)205 const ThreadEntry* GetThreadOfSample(SampleEntry* sample) override {
206 return sample->thread;
207 }
208
GetPeriodForCallChain(const uint64_t & acc_info)209 uint64_t GetPeriodForCallChain(const uint64_t& acc_info) override {
210 return acc_info;
211 }
212
FilterSample(const SampleEntry * sample)213 bool FilterSample(const SampleEntry* sample) override {
214 if (!pid_filter_.empty() &&
215 pid_filter_.find(sample->thread->pid) == pid_filter_.end()) {
216 return false;
217 }
218 if (!tid_filter_.empty() &&
219 tid_filter_.find(sample->thread->tid) == tid_filter_.end()) {
220 return false;
221 }
222 if (!comm_filter_.empty() &&
223 comm_filter_.find(sample->thread_comm) == comm_filter_.end()) {
224 return false;
225 }
226 if (!dso_filter_.empty() &&
227 dso_filter_.find(sample->map->dso->Path()) == dso_filter_.end()) {
228 return false;
229 }
230 if (!symbol_filter_.empty() &&
231 symbol_filter_.find(sample->symbol->DemangledName()) ==
232 symbol_filter_.end()) {
233 return false;
234 }
235 return true;
236 }
237
UpdateSummary(const SampleEntry * sample)238 void UpdateSummary(const SampleEntry* sample) override {
239 total_samples_ += sample->sample_count;
240 total_period_ += sample->period;
241 }
242
MergeSample(SampleEntry * sample1,SampleEntry * sample2)243 void MergeSample(SampleEntry* sample1, SampleEntry* sample2) override {
244 sample1->period += sample2->period;
245 sample1->accumulated_period += sample2->accumulated_period;
246 sample1->sample_count += sample2->sample_count;
247 }
248
249 private:
250 ThreadTree* thread_tree_;
251
252 std::unordered_set<int> pid_filter_;
253 std::unordered_set<int> tid_filter_;
254 std::unordered_set<std::string> comm_filter_;
255 std::unordered_set<std::string> dso_filter_;
256 std::unordered_set<std::string> symbol_filter_;
257
258 uint64_t total_samples_;
259 uint64_t total_period_;
260 uint64_t total_error_callchains_;
261 };
262
263 // Build sample tree based on event count in each sample.
264 class EventCountSampleTreeBuilder : public ReportCmdSampleTreeBuilder {
265 public:
EventCountSampleTreeBuilder(const SampleComparator<SampleEntry> & sample_comparator,ThreadTree * thread_tree)266 EventCountSampleTreeBuilder(const SampleComparator<SampleEntry>& sample_comparator,
267 ThreadTree* thread_tree)
268 : ReportCmdSampleTreeBuilder(sample_comparator, thread_tree) { }
269
270 protected:
GetPeriod(const SampleRecord & r)271 uint64_t GetPeriod(const SampleRecord& r) override {
272 return r.period_data.period;
273 }
274 };
275
276 // Build sample tree based on the time difference between current sample and next sample.
277 class TimestampSampleTreeBuilder : public ReportCmdSampleTreeBuilder {
278 public:
TimestampSampleTreeBuilder(const SampleComparator<SampleEntry> & sample_comparator,ThreadTree * thread_tree)279 TimestampSampleTreeBuilder(const SampleComparator<SampleEntry>& sample_comparator,
280 ThreadTree* thread_tree)
281 : ReportCmdSampleTreeBuilder(sample_comparator, thread_tree) { }
282
ReportCmdProcessSampleRecord(std::shared_ptr<SampleRecord> & r)283 void ReportCmdProcessSampleRecord(std::shared_ptr<SampleRecord>& r) override {
284 pid_t tid = static_cast<pid_t>(r->tid_data.tid);
285 auto it = next_sample_cache_.find(tid);
286 if (it == next_sample_cache_.end()) {
287 next_sample_cache_[tid] = r;
288 } else {
289 std::shared_ptr<SampleRecord> cur = it->second;
290 it->second = r;
291 ProcessSampleRecord(*cur);
292 }
293 }
294
295 protected:
GetPeriod(const SampleRecord & r)296 uint64_t GetPeriod(const SampleRecord& r) override {
297 auto it = next_sample_cache_.find(r.tid_data.tid);
298 CHECK(it != next_sample_cache_.end());
299 // Normally the samples are sorted by time, but check here for safety.
300 if (it->second->time_data.time > r.time_data.time) {
301 return it->second->time_data.time - r.time_data.time;
302 }
303 return 1u;
304 }
305
306 private:
307 std::unordered_map<pid_t, std::shared_ptr<SampleRecord>> next_sample_cache_;
308 };
309
310 struct SampleTreeBuilderOptions {
311 SampleComparator<SampleEntry> comparator;
312 ThreadTree* thread_tree;
313 std::unordered_set<std::string> comm_filter;
314 std::unordered_set<std::string> dso_filter;
315 std::unordered_set<std::string> symbol_filter;
316 std::unordered_set<int> pid_filter;
317 std::unordered_set<int> tid_filter;
318 bool use_branch_address;
319 bool accumulate_callchain;
320 bool build_callchain;
321 bool use_caller_as_callchain_root;
322 bool trace_offcpu;
323
CreateSampleTreeBuilder__anona4eb4dc80111::SampleTreeBuilderOptions324 std::unique_ptr<ReportCmdSampleTreeBuilder> CreateSampleTreeBuilder() {
325 std::unique_ptr<ReportCmdSampleTreeBuilder> builder;
326 if (trace_offcpu) {
327 builder.reset(new TimestampSampleTreeBuilder(comparator, thread_tree));
328 } else {
329 builder.reset(new EventCountSampleTreeBuilder(comparator, thread_tree));
330 }
331 builder->SetFilters(pid_filter, tid_filter, comm_filter, dso_filter, symbol_filter);
332 builder->SetBranchSampleOption(use_branch_address);
333 builder->SetCallChainSampleOptions(accumulate_callchain, build_callchain,
334 use_caller_as_callchain_root);
335 return builder;
336 }
337 };
338
339 using ReportCmdSampleTreeSorter = SampleTreeSorter<SampleEntry>;
340 using ReportCmdSampleTreeDisplayer =
341 SampleTreeDisplayer<SampleEntry, SampleTree>;
342
343 using ReportCmdCallgraphDisplayer =
344 CallgraphDisplayer<SampleEntry, CallChainNode<SampleEntry>>;
345
346 class ReportCmdCallgraphDisplayerWithVaddrInFile
347 : public ReportCmdCallgraphDisplayer {
348 protected:
PrintSampleName(const SampleEntry * sample)349 std::string PrintSampleName(const SampleEntry* sample) override {
350 return android::base::StringPrintf("%s [+0x%" PRIx64 "]",
351 sample->symbol->DemangledName(),
352 sample->vaddr_in_file);
353 }
354 };
355
356 struct EventAttrWithName {
357 perf_event_attr attr;
358 std::string name;
359 };
360
361 class ReportCommand : public Command {
362 public:
ReportCommand()363 ReportCommand()
364 : Command(
365 "report", "report sampling information in perf.data",
366 // clang-format off
367 "Usage: simpleperf report [options]\n"
368 "The default options are: -i perf.data --sort comm,pid,tid,dso,symbol.\n"
369 "-b Use the branch-to addresses in sampled take branches instead of the\n"
370 " instruction addresses. Only valid for perf.data recorded with -b/-j\n"
371 " option.\n"
372 "--children Print the overhead accumulated by appearing in the callchain.\n"
373 "--comms comm1,comm2,... Report only for selected comms.\n"
374 "--dsos dso1,dso2,... Report only for selected dsos.\n"
375 "--full-callgraph Print full call graph. Used with -g option. By default,\n"
376 " brief call graph is printed.\n"
377 "-g [callee|caller] Print call graph. If callee mode is used, the graph\n"
378 " shows how functions are called from others. Otherwise,\n"
379 " the graph shows how functions call others.\n"
380 " Default is caller mode.\n"
381 "-i <file> Specify path of record file, default is perf.data.\n"
382 "--kallsyms <file> Set the file to read kernel symbols.\n"
383 "--max-stack <frames> Set max stack frames shown when printing call graph.\n"
384 "-n Print the sample count for each item.\n"
385 "--no-demangle Don't demangle symbol names.\n"
386 "--no-show-ip Don't show vaddr in file for unknown symbols.\n"
387 "-o report_file_name Set report file name, default is stdout.\n"
388 "--percent-limit <percent> Set min percentage shown when printing call graph.\n"
389 "--pids pid1,pid2,... Report only for selected pids.\n"
390 "--raw-period Report period count instead of period percentage.\n"
391 "--sort key1,key2,... Select keys used to sort and print the report. The\n"
392 " appearance order of keys decides the order of keys used\n"
393 " to sort and print the report.\n"
394 " Possible keys include:\n"
395 " pid -- process id\n"
396 " tid -- thread id\n"
397 " comm -- thread name (can be changed during\n"
398 " the lifetime of a thread)\n"
399 " dso -- shared library\n"
400 " symbol -- function name in the shared library\n"
401 " vaddr_in_file -- virtual address in the shared\n"
402 " library\n"
403 " Keys can only be used with -b option:\n"
404 " dso_from -- shared library branched from\n"
405 " dso_to -- shared library branched to\n"
406 " symbol_from -- name of function branched from\n"
407 " symbol_to -- name of function branched to\n"
408 " The default sort keys are:\n"
409 " comm,pid,tid,dso,symbol\n"
410 "--symbols symbol1;symbol2;... Report only for selected symbols.\n"
411 "--symfs <dir> Look for files with symbols relative to this directory.\n"
412 "--tids tid1,tid2,... Report only for selected tids.\n"
413 "--vmlinux <file> Parse kernel symbols from <file>.\n"
414 // clang-format on
415 ),
416 record_filename_("perf.data"),
417 record_file_arch_(GetBuildArch()),
418 use_branch_address_(false),
419 system_wide_collection_(false),
420 accumulate_callchain_(false),
421 print_callgraph_(false),
422 callgraph_show_callee_(false),
423 callgraph_max_stack_(UINT32_MAX),
424 callgraph_percent_limit_(0),
425 raw_period_(false),
426 brief_callgraph_(true),
427 trace_offcpu_(false),
428 sched_switch_attr_id_(0u) {}
429
430 bool Run(const std::vector<std::string>& args);
431
432 private:
433 bool ParseOptions(const std::vector<std::string>& args);
434 bool ReadMetaInfoFromRecordFile();
435 bool ReadEventAttrFromRecordFile();
436 bool ReadFeaturesFromRecordFile();
437 bool ReadSampleTreeFromRecordFile();
438 bool ProcessRecord(std::unique_ptr<Record> record);
439 void ProcessSampleRecordInTraceOffCpuMode(std::unique_ptr<Record> record, size_t attr_id);
440 bool ProcessTracingData(const std::vector<char>& data);
441 bool PrintReport();
442 void PrintReportContext(FILE* fp);
443
444 std::string record_filename_;
445 ArchType record_file_arch_;
446 std::unique_ptr<RecordFileReader> record_file_reader_;
447 std::vector<EventAttrWithName> event_attrs_;
448 ThreadTree thread_tree_;
449 // Create a SampleTreeBuilder and SampleTree for each event_attr.
450 std::vector<SampleTree> sample_tree_;
451 SampleTreeBuilderOptions sample_tree_builder_options_;
452 std::vector<std::unique_ptr<ReportCmdSampleTreeBuilder>> sample_tree_builder_;
453
454 std::unique_ptr<ReportCmdSampleTreeSorter> sample_tree_sorter_;
455 std::unique_ptr<ReportCmdSampleTreeDisplayer> sample_tree_displayer_;
456 bool use_branch_address_;
457 std::string record_cmdline_;
458 bool system_wide_collection_;
459 bool accumulate_callchain_;
460 bool print_callgraph_;
461 bool callgraph_show_callee_;
462 uint32_t callgraph_max_stack_;
463 double callgraph_percent_limit_;
464 bool raw_period_;
465 bool brief_callgraph_;
466 bool trace_offcpu_;
467 size_t sched_switch_attr_id_;
468
469 std::string report_filename_;
470 std::unordered_map<std::string, std::string> meta_info_;
471 std::unique_ptr<ScopedEventTypes> scoped_event_types_;
472 };
473
Run(const std::vector<std::string> & args)474 bool ReportCommand::Run(const std::vector<std::string>& args) {
475 // 1. Parse options.
476 if (!ParseOptions(args)) {
477 return false;
478 }
479
480 // 2. Read record file and build SampleTree.
481 record_file_reader_ = RecordFileReader::CreateInstance(record_filename_);
482 if (record_file_reader_ == nullptr) {
483 return false;
484 }
485 if (!ReadMetaInfoFromRecordFile()) {
486 return false;
487 }
488 if (!ReadEventAttrFromRecordFile()) {
489 return false;
490 }
491 // Read features first to prepare build ids used when building SampleTree.
492 if (!ReadFeaturesFromRecordFile()) {
493 return false;
494 }
495 ScopedCurrentArch scoped_arch(record_file_arch_);
496 if (!ReadSampleTreeFromRecordFile()) {
497 return false;
498 }
499
500 // 3. Show collected information.
501 if (!PrintReport()) {
502 return false;
503 }
504
505 return true;
506 }
507
ParseOptions(const std::vector<std::string> & args)508 bool ReportCommand::ParseOptions(const std::vector<std::string>& args) {
509 bool demangle = true;
510 bool show_ip_for_unknown_symbol = true;
511 std::string vmlinux;
512 bool print_sample_count = false;
513 std::vector<std::string> sort_keys = {"comm", "pid", "tid", "dso", "symbol"};
514
515 for (size_t i = 0; i < args.size(); ++i) {
516 if (args[i] == "-b") {
517 use_branch_address_ = true;
518 } else if (args[i] == "--children") {
519 accumulate_callchain_ = true;
520 } else if (args[i] == "--comms" || args[i] == "--dsos") {
521 std::unordered_set<std::string>& filter =
522 (args[i] == "--comms" ? sample_tree_builder_options_.comm_filter
523 : sample_tree_builder_options_.dso_filter);
524 if (!NextArgumentOrError(args, &i)) {
525 return false;
526 }
527 std::vector<std::string> strs = android::base::Split(args[i], ",");
528 filter.insert(strs.begin(), strs.end());
529 } else if (args[i] == "--full-callgraph") {
530 brief_callgraph_ = false;
531 } else if (args[i] == "-g") {
532 print_callgraph_ = true;
533 accumulate_callchain_ = true;
534 if (i + 1 < args.size() && args[i + 1][0] != '-') {
535 ++i;
536 if (args[i] == "callee") {
537 callgraph_show_callee_ = true;
538 } else if (args[i] == "caller") {
539 callgraph_show_callee_ = false;
540 } else {
541 LOG(ERROR) << "Unknown argument with -g option: " << args[i];
542 return false;
543 }
544 }
545 } else if (args[i] == "-i") {
546 if (!NextArgumentOrError(args, &i)) {
547 return false;
548 }
549 record_filename_ = args[i];
550
551 } else if (args[i] == "--kallsyms") {
552 if (!NextArgumentOrError(args, &i)) {
553 return false;
554 }
555 std::string kallsyms;
556 if (!android::base::ReadFileToString(args[i], &kallsyms)) {
557 LOG(ERROR) << "Can't read kernel symbols from " << args[i];
558 return false;
559 }
560 Dso::SetKallsyms(kallsyms);
561 } else if (args[i] == "--max-stack") {
562 if (!GetUintOption(args, &i, &callgraph_max_stack_)) {
563 return false;
564 }
565 } else if (args[i] == "-n") {
566 print_sample_count = true;
567
568 } else if (args[i] == "--no-demangle") {
569 demangle = false;
570 } else if (args[i] == "--no-show-ip") {
571 show_ip_for_unknown_symbol = false;
572 } else if (args[i] == "-o") {
573 if (!NextArgumentOrError(args, &i)) {
574 return false;
575 }
576 report_filename_ = args[i];
577 } else if (args[i] == "--percent-limit") {
578 if (!GetDoubleOption(args, &i, &callgraph_percent_limit_)) {
579 return false;
580 }
581 } else if (args[i] == "--pids" || args[i] == "--tids") {
582 const std::string& option = args[i];
583 std::unordered_set<int>& filter =
584 (option == "--pids" ? sample_tree_builder_options_.pid_filter
585 : sample_tree_builder_options_.tid_filter);
586 if (!NextArgumentOrError(args, &i)) {
587 return false;
588 }
589 std::vector<std::string> strs = android::base::Split(args[i], ",");
590 for (const auto& s : strs) {
591 int id;
592 if (!android::base::ParseInt(s.c_str(), &id, 0)) {
593 LOG(ERROR) << "invalid id in " << option << " option: " << s;
594 return false;
595 }
596 filter.insert(id);
597 }
598 } else if (args[i] == "--raw-period") {
599 raw_period_ = true;
600 } else if (args[i] == "--sort") {
601 if (!NextArgumentOrError(args, &i)) {
602 return false;
603 }
604 sort_keys = android::base::Split(args[i], ",");
605 } else if (args[i] == "--symbols") {
606 if (!NextArgumentOrError(args, &i)) {
607 return false;
608 }
609 std::vector<std::string> strs = android::base::Split(args[i], ";");
610 sample_tree_builder_options_.symbol_filter.insert(strs.begin(), strs.end());
611 } else if (args[i] == "--symfs") {
612 if (!NextArgumentOrError(args, &i)) {
613 return false;
614 }
615 if (!Dso::SetSymFsDir(args[i])) {
616 return false;
617 }
618 } else if (args[i] == "--vmlinux") {
619 if (!NextArgumentOrError(args, &i)) {
620 return false;
621 }
622 vmlinux = args[i];
623 } else {
624 ReportUnknownOption(args, i);
625 return false;
626 }
627 }
628
629 Dso::SetDemangle(demangle);
630 if (!vmlinux.empty()) {
631 Dso::SetVmlinux(vmlinux);
632 }
633
634 if (show_ip_for_unknown_symbol) {
635 thread_tree_.ShowIpForUnknownSymbol();
636 }
637
638 SampleDisplayer<SampleEntry, SampleTree> displayer;
639 SampleComparator<SampleEntry> comparator;
640
641 if (accumulate_callchain_) {
642 if (raw_period_) {
643 displayer.AddDisplayFunction("Children", DisplayAccumulatedPeriod);
644 displayer.AddDisplayFunction("Self", DisplaySelfPeriod);
645 } else {
646 displayer.AddDisplayFunction("Children", DisplayAccumulatedOverhead);
647 displayer.AddDisplayFunction("Self", DisplaySelfOverhead);
648 }
649 } else {
650 if (raw_period_) {
651 displayer.AddDisplayFunction("Overhead", DisplaySelfPeriod);
652 } else {
653 displayer.AddDisplayFunction("Overhead", DisplaySelfOverhead);
654 }
655 }
656 if (print_sample_count) {
657 displayer.AddDisplayFunction("Sample", DisplaySampleCount);
658 }
659
660 for (auto& key : sort_keys) {
661 if (!use_branch_address_ &&
662 branch_sort_keys.find(key) != branch_sort_keys.end()) {
663 LOG(ERROR) << "sort key '" << key << "' can only be used with -b option.";
664 return false;
665 }
666 if (key == "pid") {
667 comparator.AddCompareFunction(ComparePid);
668 displayer.AddDisplayFunction("Pid", DisplayPid);
669 } else if (key == "tid") {
670 comparator.AddCompareFunction(CompareTid);
671 displayer.AddDisplayFunction("Tid", DisplayTid);
672 } else if (key == "comm") {
673 comparator.AddCompareFunction(CompareComm);
674 displayer.AddDisplayFunction("Command", DisplayComm);
675 } else if (key == "dso") {
676 comparator.AddCompareFunction(CompareDso);
677 displayer.AddDisplayFunction("Shared Object", DisplayDso);
678 } else if (key == "symbol") {
679 comparator.AddCompareFunction(CompareSymbol);
680 displayer.AddDisplayFunction("Symbol", DisplaySymbol);
681 } else if (key == "vaddr_in_file") {
682 comparator.AddCompareFunction(CompareVaddrInFile);
683 displayer.AddDisplayFunction("VaddrInFile", DisplayVaddrInFile);
684 } else if (key == "dso_from") {
685 comparator.AddCompareFunction(CompareDsoFrom);
686 displayer.AddDisplayFunction("Source Shared Object", DisplayDsoFrom);
687 } else if (key == "dso_to") {
688 comparator.AddCompareFunction(CompareDso);
689 displayer.AddDisplayFunction("Target Shared Object", DisplayDso);
690 } else if (key == "symbol_from") {
691 comparator.AddCompareFunction(CompareSymbolFrom);
692 displayer.AddDisplayFunction("Source Symbol", DisplaySymbolFrom);
693 } else if (key == "symbol_to") {
694 comparator.AddCompareFunction(CompareSymbol);
695 displayer.AddDisplayFunction("Target Symbol", DisplaySymbol);
696 } else {
697 LOG(ERROR) << "Unknown sort key: " << key;
698 return false;
699 }
700 }
701 if (print_callgraph_) {
702 bool has_symbol_key = false;
703 bool has_vaddr_in_file_key = false;
704 for (const auto& key : sort_keys) {
705 if (key == "symbol") {
706 has_symbol_key = true;
707 } else if (key == "vaddr_in_file") {
708 has_vaddr_in_file_key = true;
709 }
710 }
711 if (has_symbol_key) {
712 if (has_vaddr_in_file_key) {
713 displayer.AddExclusiveDisplayFunction(
714 ReportCmdCallgraphDisplayerWithVaddrInFile());
715 } else {
716 displayer.AddExclusiveDisplayFunction(ReportCmdCallgraphDisplayer(
717 callgraph_max_stack_, callgraph_percent_limit_, brief_callgraph_));
718 }
719 }
720 }
721
722 sample_tree_builder_options_.comparator = comparator;
723 sample_tree_builder_options_.thread_tree = &thread_tree_;
724
725 SampleComparator<SampleEntry> sort_comparator;
726 sort_comparator.AddCompareFunction(CompareTotalPeriod);
727 if (print_callgraph_) {
728 sort_comparator.AddCompareFunction(CompareCallGraphDuplicated);
729 }
730 sort_comparator.AddCompareFunction(ComparePeriod);
731 sort_comparator.AddComparator(comparator);
732 sample_tree_sorter_.reset(new ReportCmdSampleTreeSorter(sort_comparator));
733 sample_tree_displayer_.reset(new ReportCmdSampleTreeDisplayer(displayer));
734 return true;
735 }
736
ReadMetaInfoFromRecordFile()737 bool ReportCommand::ReadMetaInfoFromRecordFile() {
738 if (record_file_reader_->HasFeature(PerfFileFormat::FEAT_META_INFO)) {
739 if (!record_file_reader_->ReadMetaInfoFeature(&meta_info_)) {
740 return false;
741 }
742 auto it = meta_info_.find("system_wide_collection");
743 if (it != meta_info_.end()) {
744 system_wide_collection_ = it->second == "true";
745 }
746 it = meta_info_.find("trace_offcpu");
747 if (it != meta_info_.end()) {
748 trace_offcpu_ = it->second == "true";
749 }
750 it = meta_info_.find("event_type_info");
751 if (it != meta_info_.end()) {
752 scoped_event_types_.reset(new ScopedEventTypes(it->second));
753 }
754 }
755 return true;
756 }
757
ReadEventAttrFromRecordFile()758 bool ReportCommand::ReadEventAttrFromRecordFile() {
759 std::vector<EventAttrWithId> attrs = record_file_reader_->AttrSection();
760 for (const auto& attr_with_id : attrs) {
761 EventAttrWithName attr;
762 attr.attr = *attr_with_id.attr;
763 attr.name = GetEventNameByAttr(attr.attr);
764 event_attrs_.push_back(attr);
765 }
766 if (use_branch_address_) {
767 bool has_branch_stack = true;
768 for (const auto& attr : event_attrs_) {
769 if ((attr.attr.sample_type & PERF_SAMPLE_BRANCH_STACK) == 0) {
770 has_branch_stack = false;
771 break;
772 }
773 }
774 if (!has_branch_stack) {
775 LOG(ERROR) << record_filename_
776 << " is not recorded with branch stack sampling option.";
777 return false;
778 }
779 }
780 if (trace_offcpu_) {
781 size_t i;
782 for (i = 0; i < event_attrs_.size(); ++i) {
783 if (event_attrs_[i].name == "sched:sched_switch") {
784 break;
785 }
786 }
787 CHECK_NE(i, event_attrs_.size());
788 sched_switch_attr_id_ = i;
789 }
790 return true;
791 }
792
ReadFeaturesFromRecordFile()793 bool ReportCommand::ReadFeaturesFromRecordFile() {
794 record_file_reader_->LoadBuildIdAndFileFeatures(thread_tree_);
795
796 std::string arch =
797 record_file_reader_->ReadFeatureString(PerfFileFormat::FEAT_ARCH);
798 if (!arch.empty()) {
799 record_file_arch_ = GetArchType(arch);
800 if (record_file_arch_ == ARCH_UNSUPPORTED) {
801 return false;
802 }
803 }
804
805 std::vector<std::string> cmdline = record_file_reader_->ReadCmdlineFeature();
806 if (!cmdline.empty()) {
807 record_cmdline_ = android::base::Join(cmdline, ' ');
808 if (meta_info_.find("system_wide_collection") == meta_info_.end()) {
809 // TODO: the code to detect system wide collection option is fragile, remove
810 // it once we can do cross unwinding.
811 for (size_t i = 0; i < cmdline.size(); i++) {
812 std::string& s = cmdline[i];
813 if (s == "-a") {
814 system_wide_collection_ = true;
815 break;
816 } else if (s == "--call-graph" || s == "--cpu" || s == "-e" ||
817 s == "-f" || s == "-F" || s == "-j" || s == "-m" ||
818 s == "-o" || s == "-p" || s == "-t") {
819 i++;
820 } else if (!s.empty() && s[0] != '-') {
821 break;
822 }
823 }
824 }
825 }
826 if (record_file_reader_->HasFeature(PerfFileFormat::FEAT_TRACING_DATA)) {
827 std::vector<char> tracing_data;
828 if (!record_file_reader_->ReadFeatureSection(
829 PerfFileFormat::FEAT_TRACING_DATA, &tracing_data)) {
830 return false;
831 }
832 if (!ProcessTracingData(tracing_data)) {
833 return false;
834 }
835 }
836 return true;
837 }
838
ReadSampleTreeFromRecordFile()839 bool ReportCommand::ReadSampleTreeFromRecordFile() {
840 sample_tree_builder_options_.use_branch_address = use_branch_address_;
841 sample_tree_builder_options_.accumulate_callchain = accumulate_callchain_;
842 sample_tree_builder_options_.build_callchain = print_callgraph_;
843 sample_tree_builder_options_.use_caller_as_callchain_root = !callgraph_show_callee_;
844 sample_tree_builder_options_.trace_offcpu = trace_offcpu_;
845
846 for (size_t i = 0; i < event_attrs_.size(); ++i) {
847 sample_tree_builder_.push_back(sample_tree_builder_options_.CreateSampleTreeBuilder());
848 }
849
850 if (!record_file_reader_->ReadDataSection(
851 [this](std::unique_ptr<Record> record) {
852 return ProcessRecord(std::move(record));
853 })) {
854 return false;
855 }
856 for (size_t i = 0; i < sample_tree_builder_.size(); ++i) {
857 sample_tree_.push_back(sample_tree_builder_[i]->GetSampleTree());
858 sample_tree_sorter_->Sort(sample_tree_.back().samples, print_callgraph_);
859 }
860 return true;
861 }
862
ProcessRecord(std::unique_ptr<Record> record)863 bool ReportCommand::ProcessRecord(std::unique_ptr<Record> record) {
864 thread_tree_.Update(*record);
865 if (record->type() == PERF_RECORD_SAMPLE) {
866 size_t attr_id = record_file_reader_->GetAttrIndexOfRecord(record.get());
867 if (!trace_offcpu_) {
868 sample_tree_builder_[attr_id]->ReportCmdProcessSampleRecord(
869 *static_cast<SampleRecord*>(record.get()));
870 } else {
871 ProcessSampleRecordInTraceOffCpuMode(std::move(record), attr_id);
872 }
873 } else if (record->type() == PERF_RECORD_TRACING_DATA ||
874 record->type() == SIMPLE_PERF_RECORD_TRACING_DATA) {
875 const auto& r = *static_cast<TracingDataRecord*>(record.get());
876 if (!ProcessTracingData(std::vector<char>(r.data, r.data + r.data_size))) {
877 return false;
878 }
879 }
880 return true;
881 }
882
883
ProcessSampleRecordInTraceOffCpuMode(std::unique_ptr<Record> record,size_t attr_id)884 void ReportCommand::ProcessSampleRecordInTraceOffCpuMode(std::unique_ptr<Record> record,
885 size_t attr_id) {
886 std::shared_ptr<SampleRecord> r(static_cast<SampleRecord*>(record.release()));
887 if (attr_id == sched_switch_attr_id_) {
888 // If this sample belongs to sched_switch event, we should broadcast the offcpu info
889 // to other event types.
890 for (size_t i = 0; i < event_attrs_.size(); ++i) {
891 if (i == sched_switch_attr_id_) {
892 continue;
893 }
894 sample_tree_builder_[i]->ReportCmdProcessSampleRecord(r);
895 }
896 } else {
897 sample_tree_builder_[attr_id]->ReportCmdProcessSampleRecord(r);
898 }
899 }
900
ProcessTracingData(const std::vector<char> & data)901 bool ReportCommand::ProcessTracingData(const std::vector<char>& data) {
902 Tracing tracing(data);
903 for (auto& attr : event_attrs_) {
904 if (attr.attr.type == PERF_TYPE_TRACEPOINT) {
905 uint64_t trace_event_id = attr.attr.config;
906 attr.name = tracing.GetTracingEventNameHavingId(trace_event_id);
907 }
908 }
909 return true;
910 }
911
PrintReport()912 bool ReportCommand::PrintReport() {
913 std::unique_ptr<FILE, decltype(&fclose)> file_handler(nullptr, fclose);
914 FILE* report_fp = stdout;
915 if (!report_filename_.empty()) {
916 report_fp = fopen(report_filename_.c_str(), "w");
917 if (report_fp == nullptr) {
918 PLOG(ERROR) << "failed to open file " << report_filename_;
919 return false;
920 }
921 file_handler.reset(report_fp);
922 }
923 PrintReportContext(report_fp);
924 for (size_t i = 0; i < event_attrs_.size(); ++i) {
925 if (trace_offcpu_ && i == sched_switch_attr_id_) {
926 continue;
927 }
928 if (i != 0) {
929 fprintf(report_fp, "\n");
930 }
931 EventAttrWithName& attr = event_attrs_[i];
932 SampleTree& sample_tree = sample_tree_[i];
933 fprintf(report_fp, "Event: %s (type %u, config %llu)\n", attr.name.c_str(),
934 attr.attr.type, attr.attr.config);
935 fprintf(report_fp, "Samples: %" PRIu64 "\n", sample_tree.total_samples);
936 if (sample_tree.total_error_callchains != 0) {
937 fprintf(report_fp, "Error Callchains: %" PRIu64 ", %f%%\n",
938 sample_tree.total_error_callchains,
939 sample_tree.total_error_callchains * 100.0 / sample_tree.total_samples);
940 }
941 const char* period_prefix = trace_offcpu_ ? "Time in ns" : "Event count";
942 fprintf(report_fp, "%s: %" PRIu64 "\n\n", period_prefix, sample_tree.total_period);
943 sample_tree_displayer_->DisplaySamples(report_fp, sample_tree.samples, &sample_tree);
944 }
945 fflush(report_fp);
946 if (ferror(report_fp) != 0) {
947 PLOG(ERROR) << "print report failed";
948 return false;
949 }
950 return true;
951 }
952
PrintReportContext(FILE * report_fp)953 void ReportCommand::PrintReportContext(FILE* report_fp) {
954 if (!record_cmdline_.empty()) {
955 fprintf(report_fp, "Cmdline: %s\n", record_cmdline_.c_str());
956 }
957 fprintf(report_fp, "Arch: %s\n", GetArchString(record_file_arch_).c_str());
958 }
959
960 } // namespace
961
RegisterReportCommand()962 void RegisterReportCommand() {
963 RegisterCommand("report",
964 [] { return std::unique_ptr<Command>(new ReportCommand()); });
965 }
966