2 * Copyright (C) 2016 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.
21 #include "system/extras/simpleperf/report_sample.pb.h"
23 #include <google/protobuf/io/coded_stream.h>
24 #include <google/protobuf/io/zero_copy_stream_impl_lite.h>
27 #include "record_file.h"
28 #include "thread_tree.h"
31 namespace proto = simpleperf_report_proto;
35 class ProtobufFileWriter : public google::protobuf::io::CopyingOutputStream {
37 explicit ProtobufFileWriter(FILE* out_fp) : out_fp_(out_fp) {}
39 bool Write(const void* buffer, int size) override {
40 return fwrite(buffer, size, 1, out_fp_) == 1;
47 class ProtobufFileReader : public google::protobuf::io::CopyingInputStream {
49 explicit ProtobufFileReader(FILE* in_fp) : in_fp_(in_fp) {}
51 int Read(void* buffer, int size) override {
52 return fread(buffer, 1, size, in_fp_);
59 class ReportSampleCommand : public Command {
63 "report-sample", "report raw sample information in perf.data",
65 "Usage: simpleperf report-sample [options]\n"
66 "--dump-protobuf-report <file>\n"
67 " Dump report file generated by\n"
68 " `simpleperf report-sample --protobuf -o <file>`.\n"
69 "-i <file> Specify path of record file, default is perf.data.\n"
70 "-o report_file_name Set report file name, default is stdout.\n"
71 "--protobuf Use protobuf format in report_sample.proto to output samples.\n"
72 " Need to set a report_file_name when using this option.\n"
73 "--show-callchain Print callchain samples.\n"
76 record_filename_("perf.data"),
77 show_callchain_(false),
84 bool Run(const std::vector<std::string>& args) override;
87 bool ParseOptions(const std::vector<std::string>& args);
88 bool DumpProtobufReport(const std::string& filename);
89 bool ProcessRecord(std::unique_ptr<Record> record);
90 bool PrintSampleRecordInProtobuf(const SampleRecord& record);
91 void GetCallEntry(const ThreadEntry* thread, bool in_kernel, uint64_t ip,
92 uint64_t* pvaddr_in_file, uint32_t* pfile_id,
94 void GetCallEntry(const ThreadEntry* thread, bool in_kernel, uint64_t ip,
95 uint64_t* pvaddr_in_file, Dso** pdso,
96 const Symbol** psymbol);
97 bool PrintLostSituationInProtobuf();
98 bool PrintFileInfoInProtobuf();
99 bool PrintSampleRecord(const SampleRecord& record);
100 void PrintLostSituation();
102 std::string record_filename_;
103 std::unique_ptr<RecordFileReader> record_file_reader_;
104 std::string dump_protobuf_report_file_;
105 bool show_callchain_;
107 ThreadTree thread_tree_;
108 std::string report_filename_;
110 google::protobuf::io::CodedOutputStream* coded_os_;
111 size_t sample_count_;
115 bool ReportSampleCommand::Run(const std::vector<std::string>& args) {
117 if (!ParseOptions(args)) {
120 // 2. Prepare report fp.
122 std::unique_ptr<FILE, decltype(&fclose)> fp(nullptr, fclose);
123 if (!report_filename_.empty()) {
124 const char* open_mode = "w";
125 if (!dump_protobuf_report_file_.empty() && use_protobuf_) {
128 fp.reset(fopen(report_filename_.c_str(), open_mode));
130 PLOG(ERROR) << "failed to open " << report_filename_;
133 report_fp_ = fp.get();
136 // 3. Dump protobuf report.
137 if (!dump_protobuf_report_file_.empty()) {
138 return DumpProtobufReport(dump_protobuf_report_file_);
141 // 4. Open record file.
142 record_file_reader_ = RecordFileReader::CreateInstance(record_filename_);
143 if (record_file_reader_ == nullptr) {
146 record_file_reader_->LoadBuildIdAndFileFeatures(thread_tree_);
149 GOOGLE_PROTOBUF_VERIFY_VERSION;
151 thread_tree_.ShowMarkForUnknownSymbol();
152 thread_tree_.ShowIpForUnknownSymbol();
155 // 5. Prepare protobuf output stream.
156 std::unique_ptr<ProtobufFileWriter> protobuf_writer;
157 std::unique_ptr<google::protobuf::io::CopyingOutputStreamAdaptor> protobuf_os;
158 std::unique_ptr<google::protobuf::io::CodedOutputStream> protobuf_coded_os;
160 protobuf_writer.reset(new ProtobufFileWriter(report_fp_));
161 protobuf_os.reset(new google::protobuf::io::CopyingOutputStreamAdaptor(
162 protobuf_writer.get()));
163 protobuf_coded_os.reset(
164 new google::protobuf::io::CodedOutputStream(protobuf_os.get()));
165 coded_os_ = protobuf_coded_os.get();
168 // 6. Read record file, and print samples online.
169 if (!record_file_reader_->ReadDataSection(
170 [this](std::unique_ptr<Record> record) {
171 return ProcessRecord(std::move(record));
177 if (!PrintLostSituationInProtobuf()) {
180 if (!PrintFileInfoInProtobuf()) {
183 coded_os_->WriteLittleEndian32(0);
184 if (coded_os_->HadError()) {
185 LOG(ERROR) << "print protobuf report failed";
188 protobuf_coded_os.reset(nullptr);
189 google::protobuf::ShutdownProtobufLibrary();
191 PrintLostSituation();
194 if (ferror(report_fp_) != 0) {
195 PLOG(ERROR) << "print report failed";
201 bool ReportSampleCommand::ParseOptions(const std::vector<std::string>& args) {
202 for (size_t i = 0; i < args.size(); ++i) {
203 if (args[i] == "--dump-protobuf-report") {
204 if (!NextArgumentOrError(args, &i)) {
207 dump_protobuf_report_file_ = args[i];
208 } else if (args[i] == "-i") {
209 if (!NextArgumentOrError(args, &i)) {
212 record_filename_ = args[i];
213 } else if (args[i] == "-o") {
214 if (!NextArgumentOrError(args, &i)) {
217 report_filename_ = args[i];
218 } else if (args[i] == "--protobuf") {
219 use_protobuf_ = true;
220 } else if (args[i] == "--show-callchain") {
221 show_callchain_ = true;
223 ReportUnknownOption(args, i);
228 if (use_protobuf_ && report_filename_.empty()) {
229 LOG(ERROR) << "please specify a report filename to write protobuf data";
235 bool ReportSampleCommand::DumpProtobufReport(const std::string& filename) {
236 GOOGLE_PROTOBUF_VERIFY_VERSION;
237 std::unique_ptr<FILE, decltype(&fclose)> fp(fopen(filename.c_str(), "rb"),
240 PLOG(ERROR) << "failed to open " << filename;
243 ProtobufFileReader protobuf_reader(fp.get());
244 google::protobuf::io::CopyingInputStreamAdaptor adaptor(&protobuf_reader);
245 google::protobuf::io::CodedInputStream coded_is(&adaptor);
246 // map from file_id to max_symbol_id requested on the file.
247 std::unordered_map<uint32_t, int32_t> max_symbol_id_map;
248 // files[file_id] is the number of symbols in the file.
249 std::vector<uint32_t> files;
250 uint32_t max_message_size = 64 * (1 << 20);
251 uint32_t warning_message_size = 512 * (1 << 20);
252 coded_is.SetTotalBytesLimit(max_message_size, warning_message_size);
255 if (!coded_is.ReadLittleEndian32(&size)) {
256 PLOG(ERROR) << "failed to read " << filename;
262 // Handle files having large symbol table.
263 if (size > max_message_size) {
264 max_message_size = size;
265 coded_is.SetTotalBytesLimit(max_message_size, warning_message_size);
267 auto limit = coded_is.PushLimit(size);
268 proto::Record proto_record;
269 if (!proto_record.ParseFromCodedStream(&coded_is)) {
270 PLOG(ERROR) << "failed to read " << filename;
273 coded_is.PopLimit(limit);
274 if (proto_record.has_sample()) {
275 auto& sample = proto_record.sample();
276 static size_t sample_count = 0;
277 FprintIndented(report_fp_, 0, "sample %zu:\n", ++sample_count);
278 FprintIndented(report_fp_, 1, "time: %" PRIu64 "\n", sample.time());
279 FprintIndented(report_fp_, 1, "thread_id: %d\n", sample.thread_id());
280 FprintIndented(report_fp_, 1, "callchain:\n");
281 for (int i = 0; i < sample.callchain_size(); ++i) {
282 const proto::Sample_CallChainEntry& callchain = sample.callchain(i);
283 FprintIndented(report_fp_, 2, "vaddr_in_file: %" PRIx64 "\n",
284 callchain.vaddr_in_file());
285 FprintIndented(report_fp_, 2, "file_id: %u\n", callchain.file_id());
286 int32_t symbol_id = callchain.symbol_id();
287 FprintIndented(report_fp_, 2, "symbol_id: %d\n", symbol_id);
288 if (symbol_id < -1) {
289 LOG(ERROR) << "unexpected symbol_id " << symbol_id;
292 if (symbol_id != -1) {
293 max_symbol_id_map[callchain.file_id()] =
294 std::max(max_symbol_id_map[callchain.file_id()], symbol_id);
297 } else if (proto_record.has_lost()) {
298 auto& lost = proto_record.lost();
299 FprintIndented(report_fp_, 0, "lost_situation:\n");
300 FprintIndented(report_fp_, 1, "sample_count: %" PRIu64 "\n",
301 lost.sample_count());
302 FprintIndented(report_fp_, 1, "lost_count: %" PRIu64 "\n",
304 } else if (proto_record.has_file()) {
305 auto& file = proto_record.file();
306 FprintIndented(report_fp_, 0, "file:\n");
307 FprintIndented(report_fp_, 1, "id: %u\n", file.id());
308 FprintIndented(report_fp_, 1, "path: %s\n", file.path().c_str());
309 for (int i = 0; i < file.symbol_size(); ++i) {
310 FprintIndented(report_fp_, 1, "symbol: %s\n", file.symbol(i).c_str());
312 if (file.id() != files.size()) {
313 LOG(ERROR) << "file id doesn't increase orderly, expected "
314 << files.size() << ", really " << file.id();
317 files.push_back(file.symbol_size());
319 LOG(ERROR) << "unexpected record type ";
323 for (auto pair : max_symbol_id_map) {
324 if (pair.first >= files.size()) {
325 LOG(ERROR) << "file_id(" << pair.first << ") >= file count ("
326 << files.size() << ")";
329 if (static_cast<uint32_t>(pair.second) >= files[pair.first]) {
330 LOG(ERROR) << "symbol_id(" << pair.second << ") >= symbol count ("
331 << files[pair.first] << ") in file_id( " << pair.first << ")";
335 google::protobuf::ShutdownProtobufLibrary();
339 bool ReportSampleCommand::ProcessRecord(std::unique_ptr<Record> record) {
340 thread_tree_.Update(*record);
341 if (record->type() == PERF_RECORD_SAMPLE) {
343 auto& r = *static_cast<const SampleRecord*>(record.get());
345 return PrintSampleRecordInProtobuf(r);
347 return PrintSampleRecord(r);
349 } else if (record->type() == PERF_RECORD_LOST) {
350 lost_count_ += static_cast<const LostRecord*>(record.get())->lost;
355 bool ReportSampleCommand::PrintSampleRecordInProtobuf(const SampleRecord& r) {
356 uint64_t vaddr_in_file;
359 proto::Record proto_record;
360 proto::Sample* sample = proto_record.mutable_sample();
361 sample->set_time(r.time_data.time);
362 sample->set_thread_id(r.tid_data.tid);
364 bool in_kernel = r.InKernel();
365 const ThreadEntry* thread =
366 thread_tree_.FindThreadOrNew(r.tid_data.pid, r.tid_data.tid);
367 GetCallEntry(thread, in_kernel, r.ip_data.ip, &vaddr_in_file, &file_id,
369 proto::Sample_CallChainEntry* callchain = sample->add_callchain();
370 callchain->set_vaddr_in_file(vaddr_in_file);
371 callchain->set_file_id(file_id);
372 callchain->set_symbol_id(symbol_id);
374 if (show_callchain_ && (r.sample_type & PERF_SAMPLE_CALLCHAIN)) {
375 bool first_ip = true;
376 for (uint64_t i = 0; i < r.callchain_data.ip_nr; ++i) {
377 uint64_t ip = r.callchain_data.ips[i];
378 if (ip >= PERF_CONTEXT_MAX) {
380 case PERF_CONTEXT_KERNEL:
383 case PERF_CONTEXT_USER:
387 LOG(DEBUG) << "Unexpected perf_context in callchain: " << std::hex
393 // Remove duplication with sample ip.
394 if (ip == r.ip_data.ip) {
398 GetCallEntry(thread, in_kernel, ip, &vaddr_in_file, &file_id,
400 callchain = sample->add_callchain();
401 callchain->set_vaddr_in_file(vaddr_in_file);
402 callchain->set_file_id(file_id);
403 callchain->set_symbol_id(symbol_id);
407 coded_os_->WriteLittleEndian32(proto_record.ByteSize());
408 if (!proto_record.SerializeToCodedStream(coded_os_)) {
409 LOG(ERROR) << "failed to write sample to protobuf";
415 void ReportSampleCommand::GetCallEntry(const ThreadEntry* thread,
416 bool in_kernel, uint64_t ip,
417 uint64_t* pvaddr_in_file,
419 int32_t* psymbol_id) {
421 const Symbol* symbol;
422 GetCallEntry(thread, in_kernel, ip, pvaddr_in_file, &dso, &symbol);
423 if (!dso->GetDumpId(pfile_id)) {
424 *pfile_id = dso->CreateDumpId();
426 if (symbol != thread_tree_.UnknownSymbol()) {
427 if (!symbol->GetDumpId(reinterpret_cast<uint32_t*>(psymbol_id))) {
428 *psymbol_id = dso->CreateSymbolDumpId(symbol);
435 void ReportSampleCommand::GetCallEntry(const ThreadEntry* thread,
436 bool in_kernel, uint64_t ip,
437 uint64_t* pvaddr_in_file, Dso** pdso,
438 const Symbol** psymbol) {
439 const MapEntry* map = thread_tree_.FindMap(thread, ip, in_kernel);
440 *psymbol = thread_tree_.FindSymbol(map, ip, pvaddr_in_file, pdso);
441 // If we can't find symbol, use the dso shown in the map.
442 if (*psymbol == thread_tree_.UnknownSymbol()) {
447 bool ReportSampleCommand::PrintLostSituationInProtobuf() {
448 proto::Record proto_record;
449 proto::LostSituation* lost = proto_record.mutable_lost();
450 lost->set_sample_count(sample_count_);
451 lost->set_lost_count(lost_count_);
452 coded_os_->WriteLittleEndian32(proto_record.ByteSize());
453 if (!proto_record.SerializeToCodedStream(coded_os_)) {
454 LOG(ERROR) << "failed to write lost situation to protobuf";
460 static bool CompareDsoByDumpId(Dso* d1, Dso* d2) {
461 uint32_t id1 = UINT_MAX;
463 uint32_t id2 = UINT_MAX;
468 bool ReportSampleCommand::PrintFileInfoInProtobuf() {
469 std::vector<Dso*> dsos = thread_tree_.GetAllDsos();
470 std::sort(dsos.begin(), dsos.end(), CompareDsoByDumpId);
471 for (Dso* dso : dsos) {
473 if (!dso->GetDumpId(&file_id)) {
476 proto::Record proto_record;
477 proto::File* file = proto_record.mutable_file();
478 file->set_id(file_id);
479 file->set_path(dso->Path());
480 const std::vector<Symbol>& symbols = dso->GetSymbols();
481 std::vector<const Symbol*> dump_symbols;
482 for (const auto& sym : symbols) {
483 if (sym.HasDumpId()) {
484 dump_symbols.push_back(&sym);
487 std::sort(dump_symbols.begin(), dump_symbols.end(),
488 Symbol::CompareByDumpId);
490 for (const auto& sym : dump_symbols) {
491 std::string* symbol = file->add_symbol();
492 *symbol = sym->DemangledName();
494 coded_os_->WriteLittleEndian32(proto_record.ByteSize());
495 if (!proto_record.SerializeToCodedStream(coded_os_)) {
496 LOG(ERROR) << "failed to write file info to protobuf";
503 bool ReportSampleCommand::PrintSampleRecord(const SampleRecord& r) {
504 uint64_t vaddr_in_file;
506 const Symbol* symbol;
508 FprintIndented(report_fp_, 0, "sample:\n");
509 FprintIndented(report_fp_, 1, "time: %" PRIu64 "\n", r.time_data.time);
510 FprintIndented(report_fp_, 1, "thread_id: %d\n", r.tid_data.tid);
511 bool in_kernel = r.InKernel();
512 const ThreadEntry* thread =
513 thread_tree_.FindThreadOrNew(r.tid_data.pid, r.tid_data.tid);
514 GetCallEntry(thread, in_kernel, r.ip_data.ip, &vaddr_in_file, &dso, &symbol);
515 FprintIndented(report_fp_, 1, "vaddr_in_file: %" PRIx64 "\n", vaddr_in_file);
516 FprintIndented(report_fp_, 1, "file: %s\n", dso->Path().c_str());
517 FprintIndented(report_fp_, 1, "symbol: %s\n", symbol->DemangledName());
519 if (show_callchain_ && (r.sample_type & PERF_SAMPLE_CALLCHAIN)) {
520 FprintIndented(report_fp_, 1, "callchain:\n");
521 bool first_ip = true;
522 for (uint64_t i = 0; i < r.callchain_data.ip_nr; ++i) {
523 uint64_t ip = r.callchain_data.ips[i];
524 if (ip >= PERF_CONTEXT_MAX) {
526 case PERF_CONTEXT_KERNEL:
529 case PERF_CONTEXT_USER:
533 LOG(DEBUG) << "Unexpected perf_context in callchain: " << std::hex
539 // Remove duplication with sample ip.
540 if (ip == r.ip_data.ip) {
544 GetCallEntry(thread, in_kernel, ip, &vaddr_in_file, &dso, &symbol);
545 FprintIndented(report_fp_, 2, "vaddr_in_file: %" PRIx64 "\n",
547 FprintIndented(report_fp_, 2, "file: %s\n", dso->Path().c_str());
548 FprintIndented(report_fp_, 2, "symbol: %s\n", symbol->DemangledName());
555 void ReportSampleCommand::PrintLostSituation() {
556 FprintIndented(report_fp_, 0, "lost_situation:\n");
557 FprintIndented(report_fp_, 1, "sample_count: %" PRIu64 "\n", sample_count_);
558 FprintIndented(report_fp_, 1, "lost_count: %" PRIu64 "\n", sample_count_);
563 void RegisterReportSampleCommand() {
564 RegisterCommand("report-sample", [] {
565 return std::unique_ptr<Command>(new ReportSampleCommand());