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