blob: 60b328541cd713eb0f15d1664ce5e3f496d30c23 [file] [log] [blame]
Shinichiro Hamaji0d8e79b2015-06-30 03:29:35 +09001// 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 Hamaji9588ae92016-01-20 18:10:30 +090021#include "flags.h"
Shinichiro Hamaji0d8e79b2015-06-30 03:29:35 +090022#include "log.h"
Shinichiro Hamaji702befc2016-01-27 17:21:39 +090023#include "mutex.h"
Shinichiro Hamaji0d8e79b2015-06-30 03:29:35 +090024#include "stringprintf.h"
Shinichiro Hamaji545b6a22016-02-15 18:43:47 +090025#include "thread_local.h"
Shinichiro Hamajicbd34cd2015-07-05 02:53:04 +090026#include "timeutil.h"
Shinichiro Hamaji0d8e79b2015-06-30 03:29:35 +090027
28namespace {
29
Shinichiro Hamajie6438312016-02-16 14:04:05 +090030Mutex g_mu;
Shinichiro Hamaji0d8e79b2015-06-30 03:29:35 +090031vector<Stats*>* g_stats;
Shinichiro Hamaji545b6a22016-02-15 18:43:47 +090032DEFINE_THREAD_LOCAL(double, g_start_time);
Shinichiro Hamaji0d8e79b2015-06-30 03:29:35 +090033
34} // namespace
35
36Stats::Stats(const char* name)
Shinichiro Hamaji23a0c472016-01-20 18:13:10 +090037 : name_(name), elapsed_(0), cnt_(0) {
Shinichiro Hamajie6438312016-02-16 14:04:05 +090038 UniqueLock<Mutex> lock(g_mu);
Shinichiro Hamaji0d8e79b2015-06-30 03:29:35 +090039 if (g_stats == NULL)
40 g_stats = new vector<Stats*>;
41 g_stats->push_back(this);
42}
43
44string Stats::String() const {
Shinichiro Hamajie6438312016-02-16 14:04:05 +090045 UniqueLock<Mutex> lock(mu_);
Shinichiro Hamaji8c8358d2015-06-30 17:08:00 +090046 return StringPrintf("%s: %f / %d", name_, elapsed_, cnt_);
Shinichiro Hamaji0d8e79b2015-06-30 03:29:35 +090047}
48
49void Stats::Start() {
Shinichiro Hamaji545b6a22016-02-15 18:43:47 +090050 CHECK(!TLS_REF(g_start_time));
51 TLS_REF(g_start_time) = GetTime();
Shinichiro Hamajie6438312016-02-16 14:04:05 +090052 UniqueLock<Mutex> lock(mu_);
Shinichiro Hamaji8c8358d2015-06-30 17:08:00 +090053 cnt_++;
Shinichiro Hamaji0d8e79b2015-06-30 03:29:35 +090054}
55
Shinichiro Hamajib123fe52015-06-30 16:32:56 +090056double Stats::End() {
Shinichiro Hamaji545b6a22016-02-15 18:43:47 +090057 CHECK(TLS_REF(g_start_time));
58 double e = GetTime() - TLS_REF(g_start_time);
59 TLS_REF(g_start_time) = 0;
Shinichiro Hamajie6438312016-02-16 14:04:05 +090060 UniqueLock<Mutex> lock(mu_);
Shinichiro Hamajib123fe52015-06-30 16:32:56 +090061 elapsed_ += e;
62 return e;
Shinichiro Hamaji0d8e79b2015-06-30 03:29:35 +090063}
64
Shinichiro Hamajib123fe52015-06-30 16:32:56 +090065ScopedStatsRecorder::ScopedStatsRecorder(Stats* st, const char* msg)
66 : st_(st), msg_(msg) {
Shinichiro Hamaji9588ae92016-01-20 18:10:30 +090067 if (!g_flags.enable_stat_logs)
68 return;
Shinichiro Hamaji0d8e79b2015-06-30 03:29:35 +090069 st_->Start();
70}
71
72ScopedStatsRecorder::~ScopedStatsRecorder() {
Shinichiro Hamaji9588ae92016-01-20 18:10:30 +090073 if (!g_flags.enable_stat_logs)
74 return;
Shinichiro Hamajib123fe52015-06-30 16:32:56 +090075 double e = st_->End();
76 if (msg_ && e > 3.0) {
77 LOG_STAT("slow %s (%f): %s", st_->name_, e, msg_);
78 }
Shinichiro Hamaji0d8e79b2015-06-30 03:29:35 +090079}
80
81void 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}