Shinichiro Hamaji | 0d8e79b | 2015-06-30 03:29:35 +0900 | [diff] [blame] | 1 | // Copyright 2015 Google Inc. All rights reserved |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | // +build ignore |
| 16 | |
| 17 | #include "stats.h" |
| 18 | |
Dan Willemsen | 2d552db | 2018-01-30 16:55:33 -0800 | [diff] [blame] | 19 | #include <algorithm> |
Shinichiro Hamaji | 8380fb8 | 2016-02-26 16:51:50 +0900 | [diff] [blame] | 20 | #include <mutex> |
Shinichiro Hamaji | 0d8e79b | 2015-06-30 03:29:35 +0900 | [diff] [blame] | 21 | #include <vector> |
| 22 | |
Shinichiro Hamaji | 9588ae9 | 2016-01-20 18:10:30 +0900 | [diff] [blame] | 23 | #include "flags.h" |
Shinichiro Hamaji | 0d8e79b | 2015-06-30 03:29:35 +0900 | [diff] [blame] | 24 | #include "log.h" |
| 25 | #include "stringprintf.h" |
Shinichiro Hamaji | 545b6a2 | 2016-02-15 18:43:47 +0900 | [diff] [blame] | 26 | #include "thread_local.h" |
Shinichiro Hamaji | cbd34cd | 2015-07-05 02:53:04 +0900 | [diff] [blame] | 27 | #include "timeutil.h" |
Shinichiro Hamaji | 0d8e79b | 2015-06-30 03:29:35 +0900 | [diff] [blame] | 28 | |
| 29 | namespace { |
| 30 | |
Shinichiro Hamaji | 8380fb8 | 2016-02-26 16:51:50 +0900 | [diff] [blame] | 31 | mutex g_mu; |
Shinichiro Hamaji | 0d8e79b | 2015-06-30 03:29:35 +0900 | [diff] [blame] | 32 | vector<Stats*>* g_stats; |
Shinichiro Hamaji | 545b6a2 | 2016-02-15 18:43:47 +0900 | [diff] [blame] | 33 | DEFINE_THREAD_LOCAL(double, g_start_time); |
Shinichiro Hamaji | 0d8e79b | 2015-06-30 03:29:35 +0900 | [diff] [blame] | 34 | |
| 35 | } // namespace |
| 36 | |
Dan Willemsen | 3ce083f | 2017-10-11 22:17:48 -0700 | [diff] [blame] | 37 | Stats::Stats(const char* name) : name_(name), elapsed_(0), cnt_(0) { |
Shinichiro Hamaji | 8380fb8 | 2016-02-26 16:51:50 +0900 | [diff] [blame] | 38 | unique_lock<mutex> lock(g_mu); |
Shinichiro Hamaji | 0d8e79b | 2015-06-30 03:29:35 +0900 | [diff] [blame] | 39 | if (g_stats == NULL) |
| 40 | g_stats = new vector<Stats*>; |
| 41 | g_stats->push_back(this); |
| 42 | } |
| 43 | |
Dan Willemsen | 2d552db | 2018-01-30 16:55:33 -0800 | [diff] [blame] | 44 | void Stats::DumpTop() const { |
| 45 | unique_lock<mutex> lock(mu_); |
| 46 | if (detailed_.size() > 0) { |
| 47 | vector<pair<string, double>> v(detailed_.begin(), detailed_.end()); |
| 48 | sort( |
| 49 | v.begin(), v.end(), |
| 50 | [](const pair<string, double> a, const pair<string, double> b) -> bool { |
| 51 | return a.second > b.second; |
| 52 | }); |
| 53 | for (unsigned int i = 0; i < 10 && i < v.size(); i++) { |
| 54 | LOG_STAT(" %5.3f %s", v[i].first.c_str(), v[i].second); |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | |
Shinichiro Hamaji | 0d8e79b | 2015-06-30 03:29:35 +0900 | [diff] [blame] | 59 | string Stats::String() const { |
Shinichiro Hamaji | 8380fb8 | 2016-02-26 16:51:50 +0900 | [diff] [blame] | 60 | unique_lock<mutex> lock(mu_); |
Shinichiro Hamaji | 8c8358d | 2015-06-30 17:08:00 +0900 | [diff] [blame] | 61 | return StringPrintf("%s: %f / %d", name_, elapsed_, cnt_); |
Shinichiro Hamaji | 0d8e79b | 2015-06-30 03:29:35 +0900 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | void Stats::Start() { |
Shinichiro Hamaji | 545b6a2 | 2016-02-15 18:43:47 +0900 | [diff] [blame] | 65 | CHECK(!TLS_REF(g_start_time)); |
| 66 | TLS_REF(g_start_time) = GetTime(); |
Shinichiro Hamaji | 8380fb8 | 2016-02-26 16:51:50 +0900 | [diff] [blame] | 67 | unique_lock<mutex> lock(mu_); |
Shinichiro Hamaji | 8c8358d | 2015-06-30 17:08:00 +0900 | [diff] [blame] | 68 | cnt_++; |
Shinichiro Hamaji | 0d8e79b | 2015-06-30 03:29:35 +0900 | [diff] [blame] | 69 | } |
| 70 | |
Dan Willemsen | 2d552db | 2018-01-30 16:55:33 -0800 | [diff] [blame] | 71 | double Stats::End(const char* msg) { |
Shinichiro Hamaji | 545b6a2 | 2016-02-15 18:43:47 +0900 | [diff] [blame] | 72 | CHECK(TLS_REF(g_start_time)); |
| 73 | double e = GetTime() - TLS_REF(g_start_time); |
| 74 | TLS_REF(g_start_time) = 0; |
Shinichiro Hamaji | 8380fb8 | 2016-02-26 16:51:50 +0900 | [diff] [blame] | 75 | unique_lock<mutex> lock(mu_); |
Shinichiro Hamaji | b123fe5 | 2015-06-30 16:32:56 +0900 | [diff] [blame] | 76 | elapsed_ += e; |
Dan Willemsen | 2d552db | 2018-01-30 16:55:33 -0800 | [diff] [blame] | 77 | if (msg != 0) { |
| 78 | detailed_[string(msg)] += e; |
| 79 | } |
Shinichiro Hamaji | b123fe5 | 2015-06-30 16:32:56 +0900 | [diff] [blame] | 80 | return e; |
Shinichiro Hamaji | 0d8e79b | 2015-06-30 03:29:35 +0900 | [diff] [blame] | 81 | } |
| 82 | |
Shinichiro Hamaji | b123fe5 | 2015-06-30 16:32:56 +0900 | [diff] [blame] | 83 | ScopedStatsRecorder::ScopedStatsRecorder(Stats* st, const char* msg) |
| 84 | : st_(st), msg_(msg) { |
Shinichiro Hamaji | 9588ae9 | 2016-01-20 18:10:30 +0900 | [diff] [blame] | 85 | if (!g_flags.enable_stat_logs) |
| 86 | return; |
Shinichiro Hamaji | 0d8e79b | 2015-06-30 03:29:35 +0900 | [diff] [blame] | 87 | st_->Start(); |
| 88 | } |
| 89 | |
| 90 | ScopedStatsRecorder::~ScopedStatsRecorder() { |
Shinichiro Hamaji | 9588ae9 | 2016-01-20 18:10:30 +0900 | [diff] [blame] | 91 | if (!g_flags.enable_stat_logs) |
| 92 | return; |
Dan Willemsen | 2d552db | 2018-01-30 16:55:33 -0800 | [diff] [blame] | 93 | double e = st_->End(msg_); |
Shinichiro Hamaji | b123fe5 | 2015-06-30 16:32:56 +0900 | [diff] [blame] | 94 | if (msg_ && e > 3.0) { |
| 95 | LOG_STAT("slow %s (%f): %s", st_->name_, e, msg_); |
| 96 | } |
Shinichiro Hamaji | 0d8e79b | 2015-06-30 03:29:35 +0900 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | void ReportAllStats() { |
| 100 | if (!g_stats) |
| 101 | return; |
| 102 | for (Stats* st : *g_stats) { |
| 103 | LOG_STAT("%s", st->String().c_str()); |
Dan Willemsen | 2d552db | 2018-01-30 16:55:33 -0800 | [diff] [blame] | 104 | st->DumpTop(); |
Shinichiro Hamaji | 0d8e79b | 2015-06-30 03:29:35 +0900 | [diff] [blame] | 105 | } |
| 106 | delete g_stats; |
| 107 | } |