blob: ec509d8a29b49cace83ffd1e24021c8fe82d642b [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
Shinichiro Hamaji6bbf9e22016-01-19 18:59:13 +090019#include <mutex>
Shinichiro Hamaji0d8e79b2015-06-30 03:29:35 +090020#include <vector>
21
Shinichiro Hamaji9588ae92016-01-20 18:10:30 +090022#include "flags.h"
Shinichiro Hamaji0d8e79b2015-06-30 03:29:35 +090023#include "log.h"
24#include "stringprintf.h"
Shinichiro Hamaji4197da62016-01-25 16:06:29 +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 Hamajic8154bd2016-01-26 17:43:19 +090030mutex g_mu;
Shinichiro Hamaji0d8e79b2015-06-30 03:29:35 +090031vector<Stats*>* g_stats;
Shinichiro Hamaji4197da62016-01-25 16:06:29 +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 Hamajic8154bd2016-01-26 17:43:19 +090038 unique_lock<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 Hamaji23a0c472016-01-20 18:13:10 +090045 unique_lock<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 Hamaji4197da62016-01-25 16:06:29 +090050 CHECK(!g_start_time.Ref());
51 g_start_time.Ref() = GetTime();
Shinichiro Hamaji23a0c472016-01-20 18:13:10 +090052 unique_lock<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 Hamaji4197da62016-01-25 16:06:29 +090057 CHECK(g_start_time.Ref());
58 double e = GetTime() - g_start_time.Ref();
59 g_start_time.Ref() = 0;
Shinichiro Hamaji23a0c472016-01-20 18:13:10 +090060 unique_lock<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}