From 36c662b538bd89e591b1bfcbce59fc0de3602bf6 Mon Sep 17 00:00:00 2001 From: Yabin Cui Date: Mon, 29 Jun 2015 18:19:46 -0700 Subject: [PATCH] Simpleperf: support --children option in report command. 1. Refactor code about managing report items. 2. support --children option. It is a preparation to support -g option. Bug: 19483574 Change-Id: Iba4dc0895c48a9fa1937aed165d7b3a2dc8e5c71 --- simpleperf/cmd_report.cpp | 471 ++++++++++++++++++++++++----------------- simpleperf/cmd_report_test.cpp | 5 + simpleperf/sample_tree.cpp | 103 ++++++--- simpleperf/sample_tree.h | 28 ++- 4 files changed, 368 insertions(+), 239 deletions(-) diff --git a/simpleperf/cmd_report.cpp b/simpleperf/cmd_report.cpp index 59acc2c2..87479bdc 100644 --- a/simpleperf/cmd_report.cpp +++ b/simpleperf/cmd_report.cpp @@ -34,192 +34,195 @@ #include "record_file.h" #include "sample_tree.h" -typedef int (*compare_sample_entry_t)(const SampleEntry& sample1, const SampleEntry& sample2); -typedef std::string (*print_sample_entry_header_t)(); -typedef std::string (*print_sample_entry_t)(const SampleEntry& sample); - -struct ReportItem { - size_t width; - compare_sample_entry_t compare_function; - print_sample_entry_header_t print_header_function; - print_sample_entry_t print_function; -}; +class Displayable { + public: + Displayable(const std::string& name) : name_(name), width_(name.size()) { + } -static int ComparePid(const SampleEntry& sample1, const SampleEntry& sample2) { - return sample1.thread->pid - sample2.thread->pid; -} + virtual ~Displayable() { + } -static std::string PrintHeaderPid() { - return "Pid"; -} + const std::string& Name() const { + return name_; + } + size_t Width() const { + return width_; + } -static std::string PrintPid(const SampleEntry& sample) { - return android::base::StringPrintf("%d", sample.thread->pid); -} + virtual std::string Show(const SampleEntry& sample) const = 0; + void AdjustWidth(const SampleEntry& sample) { + size_t size = Show(sample).size(); + width_ = std::max(width_, size); + } -static ReportItem report_pid = { - .compare_function = ComparePid, - .print_header_function = PrintHeaderPid, - .print_function = PrintPid, + private: + const std::string name_; + size_t width_; }; -static int CompareTid(const SampleEntry& sample1, const SampleEntry& sample2) { - return sample1.thread->tid - sample2.thread->tid; -} - -static std::string PrintHeaderTid() { - return "Tid"; -} +class AccumulatedOverheadItem : public Displayable { + public: + AccumulatedOverheadItem(const SampleTree& sample_tree) + : Displayable("Children"), sample_tree_(sample_tree) { + } -static std::string PrintTid(const SampleEntry& sample) { - return android::base::StringPrintf("%d", sample.thread->tid); -} + std::string Show(const SampleEntry& sample) const override { + uint64_t period = sample.period + sample.accumulated_period; + uint64_t total_period = sample_tree_.TotalPeriod(); + double percentage = (total_period != 0) ? 100.0 * period / total_period : 0.0; + return android::base::StringPrintf("%.2lf%%", percentage); + } -static ReportItem report_tid = { - .compare_function = CompareTid, - .print_header_function = PrintHeaderTid, - .print_function = PrintTid, + private: + const SampleTree& sample_tree_; }; -static int CompareComm(const SampleEntry& sample1, const SampleEntry& sample2) { - return strcmp(sample1.thread_comm, sample2.thread_comm); -} - -static std::string PrintHeaderComm() { - return "Command"; -} +class SelfOverheadItem : public Displayable { + public: + SelfOverheadItem(const SampleTree& sample_tree, const std::string& name = "Self") + : Displayable(name), sample_tree_(sample_tree) { + } -static std::string PrintComm(const SampleEntry& sample) { - return sample.thread_comm; -} + std::string Show(const SampleEntry& sample) const override { + uint64_t period = sample.period; + uint64_t total_period = sample_tree_.TotalPeriod(); + double percentage = (total_period != 0) ? 100.0 * period / total_period : 0.0; + return android::base::StringPrintf("%.2lf%%", percentage); + } -static ReportItem report_comm = { - .compare_function = CompareComm, - .print_header_function = PrintHeaderComm, - .print_function = PrintComm, + private: + const SampleTree& sample_tree_; }; -static int CompareDso(const SampleEntry& sample1, const SampleEntry& sample2) { - return strcmp(sample1.map->dso->path.c_str(), sample2.map->dso->path.c_str()); -} +class SampleCountItem : public Displayable { + public: + SampleCountItem() : Displayable("Sample") { + } -static std::string PrintHeaderDso() { - return "Shared Object"; -} + std::string Show(const SampleEntry& sample) const override { + return android::base::StringPrintf("%" PRId64, sample.sample_count); + } +}; -static std::string PrintDso(const SampleEntry& sample) { - std::string filename = sample.map->dso->path; - if (filename == DEFAULT_EXECNAME_FOR_THREAD_MMAP) { - filename = "[unknown]"; +class Comparable { + public: + virtual ~Comparable() { } - return filename; -} -static ReportItem report_dso = { - .compare_function = CompareDso, - .print_header_function = PrintHeaderDso, - .print_function = PrintDso, + virtual int Compare(const SampleEntry& sample1, const SampleEntry& sample2) const = 0; }; -static int CompareSymbol(const SampleEntry& sample1, const SampleEntry& sample2) { - return strcmp(sample1.symbol->name.c_str(), sample2.symbol->name.c_str()); -} - -static std::string PrintHeaderSymbol() { - return "Symbol"; -} +class PidItem : public Displayable, public Comparable { + public: + PidItem() : Displayable("Pid") { + } -static std::string PrintSymbol(const SampleEntry& sample) { - return sample.symbol->name; -} + int Compare(const SampleEntry& sample1, const SampleEntry& sample2) const override { + return sample1.thread->pid - sample2.thread->pid; + } -static ReportItem report_symbol = { - .compare_function = CompareSymbol, - .print_header_function = PrintHeaderSymbol, - .print_function = PrintSymbol, + std::string Show(const SampleEntry& sample) const override { + return android::base::StringPrintf("%d", sample.thread->pid); + } }; -static int CompareDsoFrom(const SampleEntry& sample1, const SampleEntry& sample2) { - return strcmp(sample1.branch_from.map->dso->path.c_str(), - sample2.branch_from.map->dso->path.c_str()); -} - -static std::string PrintHeaderDsoFrom() { - return "Source Shared Object"; -} +class TidItem : public Displayable, public Comparable { + public: + TidItem() : Displayable("Tid") { + } -static std::string PrintDsoFrom(const SampleEntry& sample) { - return sample.branch_from.map->dso->path; -} + int Compare(const SampleEntry& sample1, const SampleEntry& sample2) const override { + return sample1.thread->tid - sample2.thread->tid; + } -static ReportItem report_dso_from = { - .compare_function = CompareDsoFrom, - .print_header_function = PrintHeaderDsoFrom, - .print_function = PrintDsoFrom, + std::string Show(const SampleEntry& sample) const override { + return android::base::StringPrintf("%d", sample.thread->tid); + } }; -static std::string PrintHeaderDsoTo() { - return "Target Shared Object"; -} +class CommItem : public Displayable, public Comparable { + public: + CommItem() : Displayable("Command") { + } -static ReportItem report_dso_to = { - .compare_function = CompareDso, - .print_header_function = PrintHeaderDsoTo, - .print_function = PrintDso, + int Compare(const SampleEntry& sample1, const SampleEntry& sample2) const override { + return strcmp(sample1.thread_comm, sample2.thread_comm); + } + + std::string Show(const SampleEntry& sample) const override { + return sample.thread_comm; + } }; -static int CompareSymbolFrom(const SampleEntry& sample1, const SampleEntry& sample2) { - return strcmp(sample1.branch_from.symbol->name.c_str(), sample2.branch_from.symbol->name.c_str()); -} +class DsoItem : public Displayable, public Comparable { + public: + DsoItem(const std::string& name = "Shared Object") : Displayable(name) { + } -static std::string PrintHeaderSymbolFrom() { - return "Source Symbol"; -} + int Compare(const SampleEntry& sample1, const SampleEntry& sample2) const override { + return strcmp(sample1.map->dso->path.c_str(), sample2.map->dso->path.c_str()); + } -static std::string PrintSymbolFrom(const SampleEntry& sample) { - return sample.branch_from.symbol->name; -} + std::string Show(const SampleEntry& sample) const override { + return sample.map->dso->path; + } +}; -static ReportItem report_symbol_from = { - .compare_function = CompareSymbolFrom, - .print_header_function = PrintHeaderSymbolFrom, - .print_function = PrintSymbolFrom, +class SymbolItem : public Displayable, public Comparable { + public: + SymbolItem(const std::string& name = "Symbol") : Displayable(name) { + } + + int Compare(const SampleEntry& sample1, const SampleEntry& sample2) const override { + return strcmp(sample1.symbol->name.c_str(), sample2.symbol->name.c_str()); + } + + std::string Show(const SampleEntry& sample) const override { + return sample.symbol->name; + } }; -static std::string PrintHeaderSymbolTo() { - return "Target Symbol"; -} +class DsoFromItem : public Displayable, public Comparable { + public: + DsoFromItem() : Displayable("Source Shared Object") { + } + + int Compare(const SampleEntry& sample1, const SampleEntry& sample2) const override { + return strcmp(sample1.branch_from.map->dso->path.c_str(), + sample2.branch_from.map->dso->path.c_str()); + } -static ReportItem report_symbol_to = { - .compare_function = CompareSymbol, - .print_header_function = PrintHeaderSymbolTo, - .print_function = PrintSymbol, + std::string Show(const SampleEntry& sample) const override { + return sample.branch_from.map->dso->path; + } }; -static std::string PrintHeaderSampleCount() { - return "Sample"; -} +class DsoToItem : public DsoItem { + public: + DsoToItem() : DsoItem("Target Shared Object") { + } +}; -static std::string PrintSampleCount(const SampleEntry& sample) { - return android::base::StringPrintf("%" PRId64, sample.sample_count); -} +class SymbolFromItem : public Displayable, public Comparable { + public: + SymbolFromItem() : Displayable("Source Symbol") { + } -static ReportItem report_sample_count = { - .compare_function = nullptr, - .print_header_function = PrintHeaderSampleCount, - .print_function = PrintSampleCount, + int Compare(const SampleEntry& sample1, const SampleEntry& sample2) const override { + return strcmp(sample1.branch_from.symbol->name.c_str(), + sample2.branch_from.symbol->name.c_str()); + } + + std::string Show(const SampleEntry& sample) const override { + return sample.branch_from.symbol->name; + } }; -static std::unordered_map report_item_map = { - {"comm", &report_comm}, - {"pid", &report_pid}, - {"tid", &report_tid}, - {"dso", &report_dso}, - {"symbol", &report_symbol}, - {"dso_from", &report_dso_from}, - {"dso_to", &report_dso_to}, - {"symbol_from", &report_symbol_from}, - {"symbol_to", &report_symbol_to}}; +class SymbolToItem : public SymbolItem { + public: + SymbolToItem() : SymbolItem("Target Symbol") { + } +}; static std::set branch_sort_keys = { "dso_from", "dso_to", "symbol_from", "symbol_to", @@ -233,7 +236,8 @@ class ReportCommand : public Command { "Usage: simpleperf report [options]\n" " -b Use the branch-to addresses in sampled take branches instead of\n" " the instruction addresses. Only valid for perf.data recorded with\n" - " -b/-j option." + " -b/-j option.\n" + " --children Print the overhead accumulated by appearing in the callchain.\n" " -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" @@ -244,7 +248,11 @@ class ReportCommand : public Command { " used with -b option. Default keys are \"comm,pid,tid,dso,symbol\"\n" " --symfs Look for files with symbols relative to this directory.\n"), record_filename_("perf.data"), - use_branch_address_(false) { + use_branch_address_(false), + accumulate_callchain_(false) { + compare_sample_func_t compare_sample_callback = std::bind( + &ReportCommand::CompareSampleEntry, this, std::placeholders::_1, std::placeholders::_2); + sample_tree_ = std::unique_ptr(new SampleTree(compare_sample_callback)); } bool Run(const std::vector& args); @@ -253,6 +261,7 @@ class ReportCommand : public Command { bool ParseOptions(const std::vector& args); bool ReadEventAttrFromRecordFile(); void ReadSampleTreeFromRecordFile(); + void ProcessSampleRecord(const SampleRecord& r); void ReadFeaturesFromRecordFile(); int CompareSampleEntry(const SampleEntry& sample1, const SampleEntry& sample2); void PrintReport(); @@ -265,10 +274,12 @@ class ReportCommand : public Command { std::string record_filename_; std::unique_ptr record_file_reader_; perf_event_attr event_attr_; - std::vector report_items_; + std::vector> displayable_items_; + std::vector comparable_items_; std::unique_ptr sample_tree_; bool use_branch_address_; std::string record_cmdline_; + bool accumulate_callchain_; }; bool ReportCommand::Run(const std::vector& args) { @@ -286,6 +297,7 @@ bool ReportCommand::Run(const std::vector& args) { return false; } ReadSampleTreeFromRecordFile(); + ReadFeaturesFromRecordFile(); // 3. Show collected information. PrintReport(); @@ -299,6 +311,8 @@ bool ReportCommand::ParseOptions(const std::vector& args) { for (size_t i = 0; i < args.size(); ++i) { if (args[i] == "-b") { use_branch_address_ = true; + } else if (args[i] == "--children") { + accumulate_callchain_ = true; } else if (args[i] == "-i") { if (!NextArgumentOrError(args, &i)) { return false; @@ -329,17 +343,58 @@ bool ReportCommand::ParseOptions(const std::vector& args) { } } + if (!accumulate_callchain_) { + displayable_items_.push_back( + std::unique_ptr(new SelfOverheadItem(*sample_tree_, "Overhead"))); + } else { + displayable_items_.push_back( + std::unique_ptr(new AccumulatedOverheadItem(*sample_tree_))); + displayable_items_.push_back(std::unique_ptr(new SelfOverheadItem(*sample_tree_))); + } if (print_sample_count) { - report_items_.push_back(&report_sample_count); + displayable_items_.push_back(std::unique_ptr(new SampleCountItem)); } for (auto& key : sort_keys) { if (!use_branch_address_ && branch_sort_keys.find(key) != branch_sort_keys.end()) { LOG(ERROR) << "sort key '" << key << "' can only be used with -b option."; return false; } - auto it = report_item_map.find(key); - if (it != report_item_map.end()) { - report_items_.push_back(it->second); + if (key == "pid") { + PidItem* item = new PidItem; + displayable_items_.push_back(std::unique_ptr(item)); + comparable_items_.push_back(item); + } else if (key == "tid") { + TidItem* item = new TidItem; + displayable_items_.push_back(std::unique_ptr(item)); + comparable_items_.push_back(item); + } else if (key == "comm") { + CommItem* item = new CommItem; + displayable_items_.push_back(std::unique_ptr(item)); + comparable_items_.push_back(item); + } else if (key == "dso") { + DsoItem* item = new DsoItem; + displayable_items_.push_back(std::unique_ptr(item)); + comparable_items_.push_back(item); + } else if (key == "symbol") { + SymbolItem* item = new SymbolItem; + displayable_items_.push_back(std::unique_ptr(item)); + comparable_items_.push_back(item); + } else if (key == "dso_from") { + DsoFromItem* item = new DsoFromItem; + displayable_items_.push_back(std::unique_ptr(item)); + comparable_items_.push_back(item); + } else if (key == "dso_to") { + DsoToItem* item = new DsoToItem; + displayable_items_.push_back(std::unique_ptr(item)); + comparable_items_.push_back(item); + } else if (key == "symbol_from") { + SymbolFromItem* item = new SymbolFromItem; + displayable_items_.push_back(std::unique_ptr(item)); + comparable_items_.push_back(item); + } else if (key == "symbol_to") { + SymbolToItem* item = new SymbolToItem; + displayable_items_.push_back(std::unique_ptr(item)); + comparable_items_.push_back(item); } else { LOG(ERROR) << "Unknown sort key: " << key; return false; @@ -363,9 +418,6 @@ bool ReportCommand::ReadEventAttrFromRecordFile() { } void ReportCommand::ReadSampleTreeFromRecordFile() { - compare_sample_func_t compare_sample_callback = std::bind( - &ReportCommand::CompareSampleEntry, this, std::placeholders::_1, std::placeholders::_2); - sample_tree_ = std::unique_ptr(new SampleTree(compare_sample_callback)); sample_tree_->AddThread(0, 0, "swapper"); std::vector> records = record_file_reader_->DataSection(); @@ -385,23 +437,13 @@ void ReportCommand::ReadSampleTreeFromRecordFile() { sample_tree_->AddKernelMap(r.data.addr, r.data.len, r.data.pgoff, r.sample_id.time_data.time, r.filename); } else { + std::string filename = + (r.filename == DEFAULT_EXECNAME_FOR_THREAD_MMAP) ? "[unknown]" : r.filename; sample_tree_->AddThreadMap(r.data.pid, r.data.tid, r.data.addr, r.data.len, r.data.pgoff, - r.sample_id.time_data.time, r.filename); + r.sample_id.time_data.time, filename); } } else if (record->header.type == PERF_RECORD_SAMPLE) { - const SampleRecord& r = *static_cast(record.get()); - if (use_branch_address_ == false) { - bool in_kernel = (r.header.misc & PERF_RECORD_MISC_CPUMODE_MASK) == PERF_RECORD_MISC_KERNEL; - sample_tree_->AddSample(r.tid_data.pid, r.tid_data.tid, r.ip_data.ip, r.time_data.time, - r.period_data.period, in_kernel); - } else { - for (auto& item : r.branch_stack_data.stack) { - if (item.from != 0 && item.to != 0) { - sample_tree_->AddBranchSample(r.tid_data.pid, r.tid_data.tid, item.from, item.to, - item.flags, r.time_data.time, r.period_data.period); - } - } - } + ProcessSampleRecord(*static_cast(record.get())); } else if (record->header.type == PERF_RECORD_COMM) { const CommRecord& r = *static_cast(record.get()); sample_tree_->AddThread(r.data.pid, r.data.tid, r.comm); @@ -412,6 +454,46 @@ void ReportCommand::ReadSampleTreeFromRecordFile() { } } +void ReportCommand::ProcessSampleRecord(const SampleRecord& r) { + if (use_branch_address_ && (r.sample_type & PERF_SAMPLE_BRANCH_STACK)) { + for (auto& item : r.branch_stack_data.stack) { + if (item.from != 0 && item.to != 0) { + sample_tree_->AddBranchSample(r.tid_data.pid, r.tid_data.tid, item.from, item.to, + item.flags, r.time_data.time, r.period_data.period); + } + } + } else { + bool in_kernel = (r.header.misc & PERF_RECORD_MISC_CPUMODE_MASK) == PERF_RECORD_MISC_KERNEL; + SampleEntry* sample = sample_tree_->AddSample(r.tid_data.pid, r.tid_data.tid, r.ip_data.ip, + r.time_data.time, r.period_data.period, in_kernel); + CHECK(sample != nullptr); + if (accumulate_callchain_ && (r.sample_type & PERF_SAMPLE_CALLCHAIN) != 0) { + std::vector callchain; + callchain.push_back(sample); + const std::vector& ips = r.callchain_data.ips; + for (auto& ip : ips) { + if (ip >= PERF_CONTEXT_MAX) { + switch (ip) { + case PERF_CONTEXT_KERNEL: + in_kernel = true; + break; + case PERF_CONTEXT_USER: + in_kernel = false; + break; + default: + LOG(ERROR) << "Unexpected perf_context in callchain: " << ip; + } + } else { + sample = + sample_tree_->AddCallChainSample(r.tid_data.pid, r.tid_data.tid, ip, r.time_data.time, + r.period_data.period, in_kernel, callchain); + callchain.push_back(sample); + } + } + } + } +} + void ReportCommand::ReadFeaturesFromRecordFile() { std::vector cmdline = record_file_reader_->ReadCmdlineFeature(); if (!cmdline.empty()) { @@ -420,12 +502,10 @@ void ReportCommand::ReadFeaturesFromRecordFile() { } int ReportCommand::CompareSampleEntry(const SampleEntry& sample1, const SampleEntry& sample2) { - for (auto& item : report_items_) { - if (item->compare_function != nullptr) { - int result = item->compare_function(sample1, sample2); - if (result != 0) { - return result; - } + for (auto& item : comparable_items_) { + int result = item->Compare(sample1, sample2); + if (result != 0) { + return result; } } return 0; @@ -458,45 +538,36 @@ void ReportCommand::PrintReportContext() { } void ReportCommand::CollectReportWidth() { - for (auto& item : report_items_) { - std::string s = item->print_header_function(); - item->width = s.size(); - } sample_tree_->VisitAllSamples( std::bind(&ReportCommand::CollectReportEntryWidth, this, std::placeholders::_1)); } void ReportCommand::CollectReportEntryWidth(const SampleEntry& sample) { - for (auto& item : report_items_) { - std::string s = item->print_function(sample); - item->width = std::max(item->width, s.size()); + for (auto& item : displayable_items_) { + item->AdjustWidth(sample); } } void ReportCommand::PrintReportHeader() { - printf("%8s", "Overhead"); - for (size_t i = 0; i < report_items_.size(); ++i) { - auto& item = report_items_[i]; - printf(" "); - std::string s = item->print_header_function(); - printf("%-*s", (i + 1 == report_items_.size()) ? 0 : static_cast(item->width), s.c_str()); - } - printf("\n"); + 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()); + } else { + printf("%s\n", item->Name().c_str()); + } + } } void ReportCommand::PrintReportEntry(const SampleEntry& sample) { - double percentage = 0.0; - if (sample_tree_->TotalPeriod() != 0) { - percentage = 100.0 * sample.period / sample_tree_->TotalPeriod(); - } - printf("%7.2lf%%", percentage); - for (size_t i = 0; i < report_items_.size(); ++i) { - auto& item = report_items_[i]; - printf(" "); - std::string s = item->print_function(sample); - printf("%-*s", (i + 1 == report_items_.size()) ? 0 : static_cast(item->width), s.c_str()); - } - printf("\n"); + 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()); + } else { + printf("%s\n", item->Show(sample).c_str()); + } + } } __attribute__((constructor)) static void RegisterReportCommand() { diff --git a/simpleperf/cmd_report_test.cpp b/simpleperf/cmd_report_test.cpp index ceaf9c73..236673fb 100644 --- a/simpleperf/cmd_report_test.cpp +++ b/simpleperf/cmd_report_test.cpp @@ -62,3 +62,8 @@ TEST(report_cmd, use_branch_address) { << "This test does nothing as branch stack sampling is not supported on this device."; } } + +TEST(report_cmd, children_option) { + ASSERT_TRUE(RecordCmd()->Run({"-g", "sleep", "1"})); + ASSERT_TRUE(ReportCmd()->Run({"--children"})); +} diff --git a/simpleperf/sample_tree.cpp b/simpleperf/sample_tree.cpp index 89d632cb..d07a8d29 100644 --- a/simpleperf/sample_tree.cpp +++ b/simpleperf/sample_tree.cpp @@ -160,14 +160,15 @@ const MapEntry* SampleTree::FindMap(const ThreadEntry* thread, uint64_t ip, bool return &unknown_map_; } -void SampleTree::AddSample(int pid, int tid, uint64_t ip, uint64_t time, uint64_t period, - bool in_kernel) { +SampleEntry* SampleTree::AddSample(int pid, int tid, uint64_t ip, uint64_t time, uint64_t period, + bool in_kernel) { const ThreadEntry* thread = FindThreadOrNew(pid, tid); const MapEntry* map = FindMap(thread, ip, in_kernel); const SymbolEntry* symbol = FindSymbol(map, ip); - SampleEntry sample = { + SampleEntry value = { ip, time, period, + 0, // accumulated_period 1, // sample_count thread, thread->comm, // thead_comm @@ -179,7 +180,7 @@ void SampleTree::AddSample(int pid, int tid, uint64_t ip, uint64_t time, uint64_ 0, // flags }, }; - InsertSample(sample); + return InsertSample(value); } void SampleTree::AddBranchSample(int pid, int tid, uint64_t from_ip, uint64_t to_ip, @@ -196,34 +197,78 @@ void SampleTree::AddBranchSample(int pid, int tid, uint64_t from_ip, uint64_t to } const SymbolEntry* to_symbol = FindSymbol(to_map, to_ip); - SampleEntry sample = {to_ip, // ip - time, period, - 1, // sample_count - thread, - thread->comm, // thread_comm - to_map, // map - to_symbol, // symbol - BranchFromEntry{ - from_ip, // ip - from_map, // map - from_symbol, // symbol - branch_flags, // flags - }}; - InsertSample(sample); -} - -void SampleTree::InsertSample(const SampleEntry& sample) { - auto it = sample_tree_.find(sample); + SampleEntry value = {to_ip, // ip + time, period, + 0, // accumulated_period + 1, // sample_count + thread, + thread->comm, // thread_comm + to_map, // map + to_symbol, // symbol + BranchFromEntry{ + from_ip, // ip + from_map, // map + from_symbol, // symbol + branch_flags, // flags + }}; + InsertSample(value); +} + +SampleEntry* SampleTree::AddCallChainSample(int pid, int tid, uint64_t ip, uint64_t time, + uint64_t period, bool in_kernel, + const std::vector& callchain) { + const ThreadEntry* thread = FindThreadOrNew(pid, tid); + const MapEntry* map = FindMap(thread, ip, in_kernel); + const SymbolEntry* symbol = FindSymbol(map, ip); + + SampleEntry value = { + ip, time, + 0, // period + period, // accumulated_period + 0, // sample_count + thread, + thread->comm, // thread_comm + map, symbol, + BranchFromEntry{ + 0, // ip + nullptr, // map + nullptr, // symbol + 0, // flags + }, + }; + auto it = sample_tree_.find(&value); + if (it != sample_tree_.end()) { + SampleEntry* sample = *it; + // Process only once for recursive function call. + if (std::find(callchain.begin(), callchain.end(), sample) != callchain.end()) { + return sample; + } + } + return InsertSample(value); +} + +SampleEntry* SampleTree::InsertSample(SampleEntry& value) { + SampleEntry* result; + auto it = sample_tree_.find(&value); if (it == sample_tree_.end()) { - auto pair = sample_tree_.insert(sample); + result = AllocateSample(value); + auto pair = sample_tree_.insert(result); CHECK(pair.second); } else { - SampleEntry* find_sample = const_cast(&*it); - find_sample->period += sample.period; - find_sample->sample_count++; + result = *it; + result->period += value.period; + result->accumulated_period += value.accumulated_period; + result->sample_count += value.sample_count; } - total_samples_++; - total_period_ += sample.period; + total_samples_ += value.sample_count; + total_period_ += value.period; + return result; +} + +SampleEntry* SampleTree::AllocateSample(const SampleEntry& value) { + SampleEntry* sample = new SampleEntry(value); + sample_storage_.push_back(std::unique_ptr(sample)); + return sample; } const SymbolEntry* SampleTree::FindSymbol(const MapEntry* map, uint64_t ip) { @@ -248,6 +293,6 @@ void SampleTree::VisitAllSamples(std::function callbac } } for (auto& sample : sorted_sample_tree_) { - callback(sample); + callback(*sample); } } diff --git a/simpleperf/sample_tree.h b/simpleperf/sample_tree.h index 8ed2e992..0be82866 100644 --- a/simpleperf/sample_tree.h +++ b/simpleperf/sample_tree.h @@ -56,6 +56,7 @@ struct SampleEntry { uint64_t ip; uint64_t time; uint64_t period; + uint64_t accumulated_period; // Accumulated when appearing in other samples' callchain. uint64_t sample_count; const ThreadEntry* thread; const char* thread_comm; // It refers to the thread comm when the sample happens. @@ -96,9 +97,12 @@ class SampleTree { const std::string& filename); void AddThreadMap(int pid, int tid, uint64_t start_addr, uint64_t len, uint64_t pgoff, uint64_t time, const std::string& filename); - void AddSample(int pid, int tid, uint64_t ip, uint64_t time, uint64_t period, bool in_kernel); + SampleEntry* AddSample(int pid, int tid, uint64_t ip, uint64_t time, uint64_t period, + bool in_kernel); void AddBranchSample(int pid, int tid, uint64_t from_ip, uint64_t to_ip, uint64_t branch_flags, uint64_t time, uint64_t period); + SampleEntry* AddCallChainSample(int pid, int tid, uint64_t ip, uint64_t time, uint64_t period, + bool in_kernel, const std::vector& callchain); void VisitAllSamples(std::function callback); uint64_t TotalSamples() const { @@ -115,11 +119,12 @@ class SampleTree { DsoEntry* FindKernelDsoOrNew(const std::string& filename); DsoEntry* FindUserDsoOrNew(const std::string& filename); const SymbolEntry* FindSymbol(const MapEntry* map, uint64_t ip); - void InsertSample(const SampleEntry& sample); + SampleEntry* InsertSample(SampleEntry& value); + SampleEntry* AllocateSample(const SampleEntry& value); struct SampleComparator { - bool operator()(const SampleEntry& sample1, const SampleEntry& sample2) const { - return compare_function(sample1, sample2) < 0; + bool operator()(SampleEntry* sample1, SampleEntry* sample2) const { + return compare_function(*sample1, *sample2) < 0; } SampleComparator(compare_sample_func_t compare_function) : compare_function(compare_function) { } @@ -128,11 +133,13 @@ class SampleTree { }; struct SortedSampleComparator { - bool operator()(const SampleEntry& sample1, const SampleEntry& sample2) const { - if (sample1.period != sample2.period) { - return sample1.period > sample2.period; + bool operator()(SampleEntry* sample1, SampleEntry* sample2) const { + uint64_t period1 = sample1->period + sample1->accumulated_period; + uint64_t period2 = sample2->period + sample2->accumulated_period; + if (period1 != period2) { + return period1 > period2; } - return compare_function(sample1, sample2) < 0; + return compare_function(*sample1, *sample2) < 0; } SortedSampleComparator(compare_sample_func_t compare_function) : compare_function(compare_function) { @@ -155,9 +162,10 @@ class SampleTree { SymbolEntry unknown_symbol_; SampleComparator sample_comparator_; - std::set sample_tree_; + std::set sample_tree_; SortedSampleComparator sorted_sample_comparator_; - std::set sorted_sample_tree_; + std::set sorted_sample_tree_; + std::vector> sample_storage_; uint64_t total_samples_; uint64_t total_period_; -- 2.11.0