diff options
author | Dan Willemsen <dwillemsen@google.com> | 2018-01-30 16:55:33 -0800 |
---|---|---|
committer | Dan Willemsen <dwillemsen@google.com> | 2018-01-31 17:00:32 -0800 |
commit | 2d552dbab1acaa6ce19560a5345cbf40c4cd8fad (patch) | |
tree | 59820953be8ddbd108938278db5ad155da6b4abf | |
parent | 2ac11b6c8d7542e946f7dbb65be283c3ced37de8 (diff) | |
download | kati-2d552dbab1acaa6ce19560a5345cbf40c4cd8fad.tar.gz |
Dump the top 10 slowest $(shell) calls
Even if these are <3 seconds (the limit for printing immediately), these
can add up. Keep a map, then dump the slowest 10 commands when we dump
the total stats.
-rw-r--r-- | stats.cc | 24 | ||||
-rw-r--r-- | stats.h | 5 |
2 files changed, 26 insertions, 3 deletions
@@ -16,6 +16,7 @@ #include "stats.h" +#include <algorithm> #include <mutex> #include <vector> @@ -40,6 +41,21 @@ Stats::Stats(const char* name) : name_(name), elapsed_(0), cnt_(0) { g_stats->push_back(this); } +void Stats::DumpTop() const { + unique_lock<mutex> lock(mu_); + if (detailed_.size() > 0) { + vector<pair<string, double>> v(detailed_.begin(), detailed_.end()); + sort( + v.begin(), v.end(), + [](const pair<string, double> a, const pair<string, double> b) -> bool { + return a.second > b.second; + }); + for (unsigned int i = 0; i < 10 && i < v.size(); i++) { + LOG_STAT(" %5.3f %s", v[i].first.c_str(), v[i].second); + } + } +} + string Stats::String() const { unique_lock<mutex> lock(mu_); return StringPrintf("%s: %f / %d", name_, elapsed_, cnt_); @@ -52,12 +68,15 @@ void Stats::Start() { cnt_++; } -double Stats::End() { +double Stats::End(const char* msg) { CHECK(TLS_REF(g_start_time)); double e = GetTime() - TLS_REF(g_start_time); TLS_REF(g_start_time) = 0; unique_lock<mutex> lock(mu_); elapsed_ += e; + if (msg != 0) { + detailed_[string(msg)] += e; + } return e; } @@ -71,7 +90,7 @@ ScopedStatsRecorder::ScopedStatsRecorder(Stats* st, const char* msg) ScopedStatsRecorder::~ScopedStatsRecorder() { if (!g_flags.enable_stat_logs) return; - double e = st_->End(); + double e = st_->End(msg_); if (msg_ && e > 3.0) { LOG_STAT("slow %s (%f): %s", st_->name_, e, msg_); } @@ -82,6 +101,7 @@ void ReportAllStats() { return; for (Stats* st : *g_stats) { LOG_STAT("%s", st->String().c_str()); + st->DumpTop(); } delete g_stats; } @@ -17,6 +17,7 @@ #include <mutex> #include <string> +#include <unordered_map> using namespace std; @@ -24,11 +25,12 @@ class Stats { public: explicit Stats(const char* name); + void DumpTop() const; string String() const; private: void Start(); - double End(); + double End(const char* msg); friend class ScopedStatsRecorder; @@ -36,6 +38,7 @@ class Stats { double elapsed_; int cnt_; mutable mutex mu_; + unordered_map<string, double> detailed_; }; class ScopedStatsRecorder { |