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 | |
| 19 | #include <vector> |
| 20 | |
Shinichiro Hamaji | 9588ae9 | 2016-01-20 18:10:30 +0900 | [diff] [blame] | 21 | #include "flags.h" |
Shinichiro Hamaji | 0d8e79b | 2015-06-30 03:29:35 +0900 | [diff] [blame] | 22 | #include "log.h" |
Shinichiro Hamaji | 702befc | 2016-01-27 17:21:39 +0900 | [diff] [blame] | 23 | #include "mutex.h" |
Shinichiro Hamaji | 0d8e79b | 2015-06-30 03:29:35 +0900 | [diff] [blame] | 24 | #include "stringprintf.h" |
Shinichiro Hamaji | cbd34cd | 2015-07-05 02:53:04 +0900 | [diff] [blame] | 25 | #include "timeutil.h" |
Shinichiro Hamaji | 0d8e79b | 2015-06-30 03:29:35 +0900 | [diff] [blame] | 26 | |
| 27 | namespace { |
| 28 | |
Shinichiro Hamaji | c8154bd | 2016-01-26 17:43:19 +0900 | [diff] [blame] | 29 | mutex g_mu; |
Shinichiro Hamaji | 0d8e79b | 2015-06-30 03:29:35 +0900 | [diff] [blame] | 30 | vector<Stats*>* g_stats; |
| 31 | |
| 32 | } // namespace |
| 33 | |
| 34 | Stats::Stats(const char* name) |
Shinichiro Hamaji | 23a0c47 | 2016-01-20 18:13:10 +0900 | [diff] [blame] | 35 | : name_(name), elapsed_(0), cnt_(0) { |
Shinichiro Hamaji | c8154bd | 2016-01-26 17:43:19 +0900 | [diff] [blame] | 36 | unique_lock<mutex> lock(g_mu); |
Shinichiro Hamaji | 0d8e79b | 2015-06-30 03:29:35 +0900 | [diff] [blame] | 37 | if (g_stats == NULL) |
| 38 | g_stats = new vector<Stats*>; |
| 39 | g_stats->push_back(this); |
| 40 | } |
| 41 | |
| 42 | string Stats::String() const { |
Shinichiro Hamaji | 23a0c47 | 2016-01-20 18:13:10 +0900 | [diff] [blame] | 43 | unique_lock<mutex> lock(mu_); |
Shinichiro Hamaji | 8c8358d | 2015-06-30 17:08:00 +0900 | [diff] [blame] | 44 | return StringPrintf("%s: %f / %d", name_, elapsed_, cnt_); |
Shinichiro Hamaji | 0d8e79b | 2015-06-30 03:29:35 +0900 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | void Stats::Start() { |
Shinichiro Hamaji | 499e1db | 2016-02-10 13:49:42 +0900 | [diff] [blame] | 48 | CHECK(!start_time_); |
Shinichiro Hamaji | 23a0c47 | 2016-01-20 18:13:10 +0900 | [diff] [blame] | 49 | unique_lock<mutex> lock(mu_); |
Shinichiro Hamaji | 499e1db | 2016-02-10 13:49:42 +0900 | [diff] [blame] | 50 | start_time_ = GetTime(); |
Shinichiro Hamaji | 8c8358d | 2015-06-30 17:08:00 +0900 | [diff] [blame] | 51 | cnt_++; |
Shinichiro Hamaji | 0d8e79b | 2015-06-30 03:29:35 +0900 | [diff] [blame] | 52 | } |
| 53 | |
Shinichiro Hamaji | b123fe5 | 2015-06-30 16:32:56 +0900 | [diff] [blame] | 54 | double Stats::End() { |
Shinichiro Hamaji | 23a0c47 | 2016-01-20 18:13:10 +0900 | [diff] [blame] | 55 | unique_lock<mutex> lock(mu_); |
Shinichiro Hamaji | 499e1db | 2016-02-10 13:49:42 +0900 | [diff] [blame] | 56 | CHECK(start_time_); |
| 57 | double e = GetTime() - start_time_; |
| 58 | start_time_ = 0; |
Shinichiro Hamaji | b123fe5 | 2015-06-30 16:32:56 +0900 | [diff] [blame] | 59 | elapsed_ += e; |
| 60 | return e; |
Shinichiro Hamaji | 0d8e79b | 2015-06-30 03:29:35 +0900 | [diff] [blame] | 61 | } |
| 62 | |
Shinichiro Hamaji | b123fe5 | 2015-06-30 16:32:56 +0900 | [diff] [blame] | 63 | ScopedStatsRecorder::ScopedStatsRecorder(Stats* st, const char* msg) |
| 64 | : st_(st), msg_(msg) { |
Shinichiro Hamaji | 9588ae9 | 2016-01-20 18:10:30 +0900 | [diff] [blame] | 65 | if (!g_flags.enable_stat_logs) |
| 66 | return; |
Shinichiro Hamaji | 0d8e79b | 2015-06-30 03:29:35 +0900 | [diff] [blame] | 67 | st_->Start(); |
| 68 | } |
| 69 | |
| 70 | ScopedStatsRecorder::~ScopedStatsRecorder() { |
Shinichiro Hamaji | 9588ae9 | 2016-01-20 18:10:30 +0900 | [diff] [blame] | 71 | if (!g_flags.enable_stat_logs) |
| 72 | return; |
Shinichiro Hamaji | b123fe5 | 2015-06-30 16:32:56 +0900 | [diff] [blame] | 73 | double e = st_->End(); |
| 74 | if (msg_ && e > 3.0) { |
| 75 | LOG_STAT("slow %s (%f): %s", st_->name_, e, msg_); |
| 76 | } |
Shinichiro Hamaji | 0d8e79b | 2015-06-30 03:29:35 +0900 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | void ReportAllStats() { |
| 80 | if (!g_stats) |
| 81 | return; |
| 82 | for (Stats* st : *g_stats) { |
| 83 | LOG_STAT("%s", st->String().c_str()); |
| 84 | } |
| 85 | delete g_stats; |
| 86 | } |