diff options
author | android-build-team Robot <android-build-team-robot@google.com> | 2020-04-02 02:04:49 +0000 |
---|---|---|
committer | android-build-team Robot <android-build-team-robot@google.com> | 2020-04-02 02:04:49 +0000 |
commit | 3f31fd115b12dcbb7515f89f58793b135b0b45bd (patch) | |
tree | 52289547eaa00b333154f3cf7c73f2dff38e4b84 | |
parent | 252a6c753950ab417ed22e4374b187dd690850b9 (diff) | |
parent | c118258b5f24ae4ce3fbff947ef26b1c80d3a3df (diff) | |
download | extras-3f31fd115b12dcbb7515f89f58793b135b0b45bd.tar.gz |
Snap for 6357023 from c118258b5f24ae4ce3fbff947ef26b1c80d3a3df to rvc-d1-release
Change-Id: I5c9480298504af21f86d580d1ce2806a54e3037d
-rw-r--r-- | simpleperf/cmd_stat.cpp | 355 | ||||
-rw-r--r-- | simpleperf/cmd_stat_impl.h | 38 | ||||
-rw-r--r-- | simpleperf/cmd_stat_test.cpp | 61 |
3 files changed, 259 insertions, 195 deletions
diff --git a/simpleperf/cmd_stat.cpp b/simpleperf/cmd_stat.cpp index c8712df7..1ee6ef81 100644 --- a/simpleperf/cmd_stat.cpp +++ b/simpleperf/cmd_stat.cpp @@ -45,7 +45,7 @@ using namespace simpleperf; -namespace { +namespace simpleperf { static std::vector<std::string> default_measured_event_types{ "cpu-cycles", "stalled-cycles-frontend", "stalled-cycles-backend", @@ -90,236 +90,207 @@ static const std::unordered_map<std::string_view, std::pair<std::string_view, st {"raw-l2d-tlb-refill-rd", {"raw-l2d-tlb-rd", "level 2 data TLB refill rate, read"}}, }; -class CounterSummaries { - public: - explicit CounterSummaries(std::vector<CounterSummary>&& summaries, bool csv) - : summaries_(std::move(summaries)), csv_(csv) {} - const std::vector<CounterSummary>& Summaries() { return summaries_; } - - const CounterSummary* FindSummary(const std::string& type_name, const std::string& modifier, - const ThreadInfo* thread, int cpu) { - for (const auto& s : summaries_) { - if (s.type_name == type_name && s.modifier == modifier && s.thread == thread && - s.cpu == cpu) { - return &s; - } +const CounterSummary* CounterSummaries::FindSummary(const std::string& type_name, + const std::string& modifier, + const ThreadInfo* thread, int cpu) { + for (const auto& s : summaries_) { + if (s.type_name == type_name && s.modifier == modifier && s.thread == thread && s.cpu == cpu) { + return &s; } - return nullptr; - } - - // If we have two summaries monitoring the same event type at the same time, - // that one is for user space only, and the other is for kernel space only; - // then we can automatically generate a summary combining the two results. - // For example, a summary of branch-misses:u and a summary for branch-misses:k - // can generate a summary of branch-misses. - void AutoGenerateSummaries() { - for (size_t i = 0; i < summaries_.size(); ++i) { - const CounterSummary& s = summaries_[i]; - if (s.modifier == "u") { - const CounterSummary* other = FindSummary(s.type_name, "k", s.thread, s.cpu); - if (other != nullptr && other->IsMonitoredAtTheSameTime(s)) { - if (FindSummary(s.type_name, "", s.thread, s.cpu) == nullptr) { - summaries_.emplace_back(s.type_name, "", s.group_id, s.thread, s.cpu, - s.count + other->count, s.scale, true, csv_); - } + } + return nullptr; +} + +void CounterSummaries::AutoGenerateSummaries() { + for (size_t i = 0; i < summaries_.size(); ++i) { + const CounterSummary& s = summaries_[i]; + if (s.modifier == "u") { + const CounterSummary* other = FindSummary(s.type_name, "k", s.thread, s.cpu); + if (other != nullptr && other->IsMonitoredAtTheSameTime(s)) { + if (FindSummary(s.type_name, "", s.thread, s.cpu) == nullptr) { + summaries_.emplace_back(s.type_name, "", s.group_id, s.thread, s.cpu, + s.count + other->count, s.runtime_in_ns, s.scale, true, csv_); } } } } +} - void GenerateComments(double duration_in_sec) { - for (auto& s : summaries_) { - s.comment = GetCommentForSummary(s, duration_in_sec); - } +void CounterSummaries::GenerateComments(double duration_in_sec) { + for (auto& s : summaries_) { + s.comment = GetCommentForSummary(s, duration_in_sec); } +} - void Show(FILE* fp) { - if (csv_) { - ShowCSV(fp); - } else { - ShowText(fp); - } +void CounterSummaries::Show(FILE* fp) { + if (csv_) { + ShowCSV(fp); + } else { + ShowText(fp); } +} - void ShowCSV(FILE* fp) { - for (auto& s : summaries_) { - if (s.thread != nullptr) { - fprintf(fp, "%s,%d,%d,", s.thread->name.c_str(), s.thread->pid, s.thread->tid); - } - fprintf(fp, "%s,%s,%s,(%.0lf%%)%s\n", s.readable_count.c_str(), s.Name().c_str(), - s.comment.c_str(), 1.0 / s.scale * 100, (s.auto_generated ? " (generated)," : ",")); +void CounterSummaries::ShowCSV(FILE* fp) { + for (auto& s : summaries_) { + if (s.thread != nullptr) { + fprintf(fp, "%s,%d,%d,", s.thread->name.c_str(), s.thread->pid, s.thread->tid); } + fprintf(fp, "%s,%s,%s,(%.0f%%)%s\n", s.readable_count.c_str(), s.Name().c_str(), + s.comment.c_str(), 1.0 / s.scale * 100, (s.auto_generated ? " (generated)," : ",")); } +} - void ShowText(FILE* fp) { - bool show_thread = !summaries_.empty() && summaries_[0].thread != nullptr; - bool show_cpu = !summaries_.empty() && summaries_[0].cpu != -1; - std::vector<std::string> titles; - - if (show_thread) { - titles = {"thread_name", "pid", "tid"}; - } - if (show_cpu) { - titles.emplace_back("cpu"); - } - titles.emplace_back("count"); - titles.emplace_back("event_name"); - titles.emplace_back(" # percentage = event_run_time / enabled_time"); +void CounterSummaries::ShowText(FILE* fp) { + bool show_thread = !summaries_.empty() && summaries_[0].thread != nullptr; + bool show_cpu = !summaries_.empty() && summaries_[0].cpu != -1; + std::vector<std::string> titles; - std::vector<size_t> width(titles.size(), 0); + if (show_thread) { + titles = {"thread_name", "pid", "tid"}; + } + if (show_cpu) { + titles.emplace_back("cpu"); + } + titles.emplace_back("count"); + titles.emplace_back("event_name"); + titles.emplace_back(" # count / runtime, runtime / enabled_time"); - auto adjust_width = [](size_t& w, size_t size) { - w = std::max(w, size); - }; + std::vector<size_t> width(titles.size(), 0); - // The last title is too long. Don't include it for width adjustment. - for (size_t i = 0; i + 1 < titles.size(); i++) { - adjust_width(width[i], titles[i].size()); - } + auto adjust_width = [](size_t& w, size_t size) { w = std::max(w, size); }; - for (auto& s : summaries_) { - size_t i = 0; - if (show_thread) { - adjust_width(width[i++], s.thread->name.size()); - adjust_width(width[i++], std::to_string(s.thread->pid).size()); - adjust_width(width[i++], std::to_string(s.thread->tid).size()); - } - if (show_cpu) { - adjust_width(width[i++], std::to_string(s.cpu).size()); - } - adjust_width(width[i++], s.readable_count.size()); - adjust_width(width[i++], s.Name().size()); - adjust_width(width[i++], s.comment.size()); - } + // The last title is too long. Don't include it for width adjustment. + for (size_t i = 0; i + 1 < titles.size(); i++) { + adjust_width(width[i], titles[i].size()); + } - fprintf(fp, "# "); - for (size_t i = 0; i < titles.size(); i++) { - if (titles[i] == "count") { - fprintf(fp, "%*s", static_cast<int>(width[i]), titles[i].c_str()); - } else { - fprintf(fp, "%-*s", static_cast<int>(width[i]), titles[i].c_str()); - } - if (i + 1 < titles.size()) { - fprintf(fp, " "); - } + for (auto& s : summaries_) { + size_t i = 0; + if (show_thread) { + adjust_width(width[i++], s.thread->name.size()); + adjust_width(width[i++], std::to_string(s.thread->pid).size()); + adjust_width(width[i++], std::to_string(s.thread->tid).size()); } - fprintf(fp, "\n"); - - for (auto& s : summaries_) { - size_t i = 0; - if (show_thread) { - fprintf(fp, " %-*s", static_cast<int>(width[i++]), s.thread->name.c_str()); - fprintf(fp, " %-*d", static_cast<int>(width[i++]), s.thread->pid); - fprintf(fp, " %-*d", static_cast<int>(width[i++]), s.thread->tid); - } - if (show_cpu) { - fprintf(fp, " %-*d", static_cast<int>(width[i++]), s.cpu); - } - fprintf(fp, " %*s %-*s # %-*s (%.0lf%%)%s\n", - static_cast<int>(width[i]), s.readable_count.c_str(), - static_cast<int>(width[i+1]), s.Name().c_str(), - static_cast<int>(width[i+2]), s.comment.c_str(), - 1.0 / s.scale * 100, (s.auto_generated ? " (generated)" : "")); + if (show_cpu) { + adjust_width(width[i++], std::to_string(s.cpu).size()); } + adjust_width(width[i++], s.readable_count.size()); + adjust_width(width[i++], s.Name().size()); + adjust_width(width[i++], s.comment.size()); } - private: - std::string GetCommentForSummary(const CounterSummary& s, - double duration_in_sec) { - char sap_mid; - if (csv_) { - sap_mid = ','; + fprintf(fp, "# "); + for (size_t i = 0; i < titles.size(); i++) { + if (titles[i] == "count") { + fprintf(fp, "%*s", static_cast<int>(width[i]), titles[i].c_str()); } else { - sap_mid = ' '; - } - if (s.type_name == "task-clock") { - double run_sec = s.count / 1e9; - double used_cpus = run_sec / (duration_in_sec / s.scale); - return android::base::StringPrintf("%lf%ccpus used", used_cpus, sap_mid); + fprintf(fp, "%-*s", static_cast<int>(width[i]), titles[i].c_str()); } - if (s.type_name == "cpu-clock") { - return ""; - } - if (s.type_name == "cpu-cycles") { - double running_time_in_sec; - if (!FindRunningTimeForSummary(s, &running_time_in_sec)) { - return ""; - } - double hz = s.count / (running_time_in_sec / s.scale); - return android::base::StringPrintf("%lf%cGHz", hz / 1e9, sap_mid); + if (i + 1 < titles.size()) { + fprintf(fp, " "); } - if (s.type_name == "instructions" && s.count != 0) { - const CounterSummary* other = FindSummary("cpu-cycles", s.modifier, s.thread, s.cpu); - if (other != nullptr && other->IsMonitoredAtTheSameTime(s)) { - double cpi = static_cast<double>(other->count) / s.count; - return android::base::StringPrintf("%lf%ccycles per instruction", cpi, - sap_mid); - } + } + fprintf(fp, "\n"); + + for (auto& s : summaries_) { + size_t i = 0; + if (show_thread) { + fprintf(fp, " %-*s", static_cast<int>(width[i++]), s.thread->name.c_str()); + fprintf(fp, " %-*d", static_cast<int>(width[i++]), s.thread->pid); + fprintf(fp, " %-*d", static_cast<int>(width[i++]), s.thread->tid); } - std::string rate_comment = GetRateComment(s, sap_mid); - if (!rate_comment.empty()) { - return rate_comment; + if (show_cpu) { + fprintf(fp, " %-*d", static_cast<int>(width[i++]), s.cpu); } - double running_time_in_sec; - if (!FindRunningTimeForSummary(s, &running_time_in_sec)) { + fprintf(fp, " %*s %-*s # %-*s (%.0f%%)%s\n", static_cast<int>(width[i]), + s.readable_count.c_str(), static_cast<int>(width[i + 1]), s.Name().c_str(), + static_cast<int>(width[i + 2]), s.comment.c_str(), 1.0 / s.scale * 100, + (s.auto_generated ? " (generated)" : "")); + } +} + +std::string CounterSummaries::GetCommentForSummary(const CounterSummary& s, + double duration_in_sec) { + char sap_mid; + if (csv_) { + sap_mid = ','; + } else { + sap_mid = ' '; + } + if (s.type_name == "task-clock") { + double run_sec = s.count / 1e9; + double used_cpus = run_sec / duration_in_sec; + return android::base::StringPrintf("%f%ccpus used", used_cpus, sap_mid); + } + if (s.type_name == "cpu-clock") { + return ""; + } + if (s.type_name == "cpu-cycles") { + if (s.runtime_in_ns == 0) { return ""; } - double rate = s.count / (running_time_in_sec / s.scale); - if (rate > 1e9) { - return android::base::StringPrintf("%.3lf%cG/sec", rate / 1e9, sap_mid); - } - if (rate > 1e6) { - return android::base::StringPrintf("%.3lf%cM/sec", rate / 1e6, sap_mid); - } - if (rate > 1e3) { - return android::base::StringPrintf("%.3lf%cK/sec", rate / 1e3, sap_mid); + double ghz = static_cast<double>(s.count) / s.runtime_in_ns; + return android::base::StringPrintf("%f%cGHz", ghz, sap_mid); + } + if (s.type_name == "instructions" && s.count != 0) { + const CounterSummary* other = FindSummary("cpu-cycles", s.modifier, s.thread, s.cpu); + if (other != nullptr && other->IsMonitoredAtTheSameTime(s)) { + double cpi = static_cast<double>(other->count) / s.count; + return android::base::StringPrintf("%f%ccycles per instruction", cpi, sap_mid); } - return android::base::StringPrintf("%.3lf%c/sec", rate, sap_mid); } + std::string rate_comment = GetRateComment(s, sap_mid); + if (!rate_comment.empty()) { + return rate_comment; + } + if (s.runtime_in_ns == 0) { + return ""; + } + double runtime_in_sec = static_cast<double>(s.runtime_in_ns) / 1e9; + double rate = s.count / runtime_in_sec; + if (rate >= 1e9 - 1e5) { + return android::base::StringPrintf("%.3f%cG/sec", rate / 1e9, sap_mid); + } + if (rate >= 1e6 - 1e2) { + return android::base::StringPrintf("%.3f%cM/sec", rate / 1e6, sap_mid); + } + if (rate >= 1e3) { + return android::base::StringPrintf("%.3f%cK/sec", rate / 1e3, sap_mid); + } + return android::base::StringPrintf("%.3f%c/sec", rate, sap_mid); +} - std::string GetRateComment(const CounterSummary& s, char sep) { - std::string_view miss_event_name = s.type_name; - std::string event_name; - std::string rate_desc; - if (auto it = COMMON_EVENT_RATE_MAP.find(miss_event_name); it != COMMON_EVENT_RATE_MAP.end()) { +std::string CounterSummaries::GetRateComment(const CounterSummary& s, char sep) { + std::string_view miss_event_name = s.type_name; + std::string event_name; + std::string rate_desc; + if (auto it = COMMON_EVENT_RATE_MAP.find(miss_event_name); it != COMMON_EVENT_RATE_MAP.end()) { + event_name = it->second.first; + rate_desc = it->second.second; + } + if (event_name.empty() && (GetBuildArch() == ARCH_ARM || GetBuildArch() == ARCH_ARM64)) { + if (auto it = ARM_EVENT_RATE_MAP.find(miss_event_name); it != ARM_EVENT_RATE_MAP.end()) { event_name = it->second.first; rate_desc = it->second.second; } - if (event_name.empty() && (GetBuildArch() == ARCH_ARM || GetBuildArch() == ARCH_ARM64)) { - if (auto it = ARM_EVENT_RATE_MAP.find(miss_event_name); it != ARM_EVENT_RATE_MAP.end()) { - event_name = it->second.first; - rate_desc = it->second.second; - } - } - if (event_name.empty() && android::base::ConsumeSuffix(&miss_event_name, "-misses")) { - event_name = std::string(miss_event_name) + "s"; - rate_desc = "miss rate"; - } - if (!event_name.empty()) { - const CounterSummary* other = FindSummary(event_name, s.modifier, s.thread, s.cpu); - if (other != nullptr && other->IsMonitoredAtTheSameTime(s) && other->count != 0) { - double miss_rate = static_cast<double>(s.count) / other->count; - return android::base::StringPrintf("%f%%%c%s", miss_rate * 100, sep, rate_desc.c_str()); - } - } - return ""; } - - bool FindRunningTimeForSummary(const CounterSummary& summary, double* running_time_in_sec) { - for (auto& s : summaries_) { - if ((s.type_name == "task-clock" || s.type_name == "cpu-clock") && - s.IsMonitoredAtTheSameTime(summary) && s.count != 0u) { - *running_time_in_sec = s.count / 1e9; - return true; - } + if (event_name.empty() && android::base::ConsumeSuffix(&miss_event_name, "-misses")) { + event_name = std::string(miss_event_name) + "s"; + rate_desc = "miss rate"; + } + if (!event_name.empty()) { + const CounterSummary* other = FindSummary(event_name, s.modifier, s.thread, s.cpu); + if (other != nullptr && other->IsMonitoredAtTheSameTime(s) && other->count != 0) { + double miss_rate = static_cast<double>(s.count) / other->count; + return android::base::StringPrintf("%f%%%c%s", miss_rate * 100, sep, rate_desc.c_str()); } - return false; } + return ""; +} - private: - std::vector<CounterSummary> summaries_; - bool csv_; -}; +} // namespace simpleperf + +namespace { // devfreq may use performance counters to calculate memory latency (as in // drivers/devfreq/arm-memlat-mon.c). Hopefully we can get more available counters by asking devfreq diff --git a/simpleperf/cmd_stat_impl.h b/simpleperf/cmd_stat_impl.h index 16dd091d..a7f60df7 100644 --- a/simpleperf/cmd_stat_impl.h +++ b/simpleperf/cmd_stat_impl.h @@ -77,20 +77,22 @@ struct CounterSummary { const ThreadInfo* thread; int cpu; // -1 represents all cpus uint64_t count; + uint64_t runtime_in_ns; double scale; std::string readable_count; std::string comment; bool auto_generated; CounterSummary(const std::string& type_name, const std::string& modifier, uint32_t group_id, - const ThreadInfo* thread, int cpu, uint64_t count, double scale, - bool auto_generated, bool csv) + const ThreadInfo* thread, int cpu, uint64_t count, uint64_t runtime_in_ns, + double scale, bool auto_generated, bool csv) : type_name(type_name), modifier(modifier), group_id(group_id), thread(thread), cpu(cpu), count(count), + runtime_in_ns(runtime_in_ns), scale(scale), auto_generated(auto_generated) { readable_count = ReadableCountValue(csv); @@ -207,7 +209,7 @@ class CounterSummaryBuilder { thread = &it->second; } summaries_.emplace_back(info.event_name, info.event_modifier, info.group_id, thread, cpu, - sum.value, scale, false, csv_); + sum.value, sum.time_running, scale, false, csv_); } void SortSummaries(std::vector<CounterSummary>::iterator begin, @@ -243,4 +245,34 @@ class CounterSummaryBuilder { std::vector<CounterSummary> summaries_; }; +class CounterSummaries { + public: + explicit CounterSummaries(std::vector<CounterSummary>&& summaries, bool csv) + : summaries_(std::move(summaries)), csv_(csv) {} + const std::vector<CounterSummary>& Summaries() { return summaries_; } + + const CounterSummary* FindSummary(const std::string& type_name, const std::string& modifier, + const ThreadInfo* thread, int cpu); + + // If we have two summaries monitoring the same event type at the same time, + // that one is for user space only, and the other is for kernel space only; + // then we can automatically generate a summary combining the two results. + // For example, a summary of branch-misses:u and a summary for branch-misses:k + // can generate a summary of branch-misses. + void AutoGenerateSummaries(); + void GenerateComments(double duration_in_sec); + void Show(FILE* fp); + void ShowCSV(FILE* fp); + void ShowText(FILE* fp); + + private: + std::string GetCommentForSummary(const CounterSummary& s, double duration_in_sec); + std::string GetRateComment(const CounterSummary& s, char sep); + bool FindRunningTimeForSummary(const CounterSummary& summary, double* running_time_in_sec); + + private: + std::vector<CounterSummary> summaries_; + bool csv_; +}; + } // namespace simpleperf
\ No newline at end of file diff --git a/simpleperf/cmd_stat_test.cpp b/simpleperf/cmd_stat_test.cpp index 8bc4e4ff..6b6196a9 100644 --- a/simpleperf/cmd_stat_test.cpp +++ b/simpleperf/cmd_stat_test.cpp @@ -487,3 +487,64 @@ TEST_F(StatCmdSummaryBuilderTest, per_thread_core_aggregate) { ASSERT_EQ(summaries[3].count, 1); ASSERT_NEAR(summaries[3].scale, 1.0, 1e-5); } + +class StatCmdSummariesTest : public ::testing::Test { + protected: + void AddSummary(const std::string event_name, pid_t tid, int cpu, uint64_t count, + uint64_t runtime_in_ns) { + ThreadInfo* thread = nullptr; + if (tid != -1) { + thread = &thread_map_[tid]; + } + summary_v_.emplace_back(event_name, "", 0, thread, cpu, count, runtime_in_ns, 1.0, false, + false); + } + + const std::string* GetComment(size_t index) { + if (!summaries_) { + summaries_.reset(new CounterSummaries(std::move(summary_v_), false)); + summaries_->GenerateComments(1.0); + } + if (index < summaries_->Summaries().size()) { + return &(summaries_->Summaries()[index].comment); + } + return nullptr; + } + + std::unordered_map<pid_t, ThreadInfo> thread_map_; + std::vector<CounterSummary> summary_v_; + std::unique_ptr<CounterSummaries> summaries_; +}; + +TEST_F(StatCmdSummariesTest, task_clock_comment) { + AddSummary("task-clock", -1, -1, 1e9, 0); + AddSummary("task-clock", 0, -1, 2e9, 0); + AddSummary("task-clock", -1, 0, 0.5e9, 0); + AddSummary("task-clock", 1, 1, 3e9, 0); + ASSERT_EQ(*GetComment(0), "1.000000 cpus used"); + ASSERT_EQ(*GetComment(1), "2.000000 cpus used"); + ASSERT_EQ(*GetComment(2), "0.500000 cpus used"); + ASSERT_EQ(*GetComment(3), "3.000000 cpus used"); +} + +TEST_F(StatCmdSummariesTest, cpu_cycles_comment) { + AddSummary("cpu-cycles", -1, -1, 100, 100); + AddSummary("cpu-cycles", 0, -1, 200, 100); + AddSummary("cpu-cycles", -1, 0, 50, 100); + AddSummary("cpu-cycles", 1, 1, 300, 100); + ASSERT_EQ(*GetComment(0), "1.000000 GHz"); + ASSERT_EQ(*GetComment(1), "2.000000 GHz"); + ASSERT_EQ(*GetComment(2), "0.500000 GHz"); + ASSERT_EQ(*GetComment(3), "3.000000 GHz"); +} + +TEST_F(StatCmdSummariesTest, rate_comment) { + AddSummary("branch-misses", -1, -1, 1e9, 1e9); + AddSummary("branch-misses", 0, -1, 1e6, 1e9); + AddSummary("branch-misses", -1, 0, 1e3, 1e9); + AddSummary("branch-misses", 1, 1, 1, 1e9); + ASSERT_EQ(*GetComment(0), "1.000 G/sec"); + ASSERT_EQ(*GetComment(1), "1.000 M/sec"); + ASSERT_EQ(*GetComment(2), "1.000 K/sec"); + ASSERT_EQ(*GetComment(3), "1.000 /sec"); +}
\ No newline at end of file |