blob: 920d13f0f84e0f235ddd4a238e504011034ef22c [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 Hamajicbd34cd2015-07-05 02:53:04 +090025#include "timeutil.h"
Shinichiro Hamaji0d8e79b2015-06-30 03:29:35 +090026
27namespace {
28
Shinichiro Hamajic8154bd2016-01-26 17:43:19 +090029mutex g_mu;
Shinichiro Hamaji0d8e79b2015-06-30 03:29:35 +090030vector<Stats*>* g_stats;
31
32} // namespace
33
34Stats::Stats(const char* name)
Shinichiro Hamaji23a0c472016-01-20 18:13:10 +090035 : name_(name), elapsed_(0), cnt_(0) {
Shinichiro Hamajic8154bd2016-01-26 17:43:19 +090036 unique_lock<mutex> lock(g_mu);
Shinichiro Hamaji0d8e79b2015-06-30 03:29:35 +090037 if (g_stats == NULL)
38 g_stats = new vector<Stats*>;
39 g_stats->push_back(this);
40}
41
42string Stats::String() const {
Shinichiro Hamaji23a0c472016-01-20 18:13:10 +090043 unique_lock<mutex> lock(mu_);
Shinichiro Hamaji8c8358d2015-06-30 17:08:00 +090044 return StringPrintf("%s: %f / %d", name_, elapsed_, cnt_);
Shinichiro Hamaji0d8e79b2015-06-30 03:29:35 +090045}
46
47void Stats::Start() {
Shinichiro Hamaji499e1db2016-02-10 13:49:42 +090048 CHECK(!start_time_);
Shinichiro Hamaji23a0c472016-01-20 18:13:10 +090049 unique_lock<mutex> lock(mu_);
Shinichiro Hamaji499e1db2016-02-10 13:49:42 +090050 start_time_ = GetTime();
Shinichiro Hamaji8c8358d2015-06-30 17:08:00 +090051 cnt_++;
Shinichiro Hamaji0d8e79b2015-06-30 03:29:35 +090052}
53
Shinichiro Hamajib123fe52015-06-30 16:32:56 +090054double Stats::End() {
Shinichiro Hamaji23a0c472016-01-20 18:13:10 +090055 unique_lock<mutex> lock(mu_);
Shinichiro Hamaji499e1db2016-02-10 13:49:42 +090056 CHECK(start_time_);
57 double e = GetTime() - start_time_;
58 start_time_ = 0;
Shinichiro Hamajib123fe52015-06-30 16:32:56 +090059 elapsed_ += e;
60 return e;
Shinichiro Hamaji0d8e79b2015-06-30 03:29:35 +090061}
62
Shinichiro Hamajib123fe52015-06-30 16:32:56 +090063ScopedStatsRecorder::ScopedStatsRecorder(Stats* st, const char* msg)
64 : st_(st), msg_(msg) {
Shinichiro Hamaji9588ae92016-01-20 18:10:30 +090065 if (!g_flags.enable_stat_logs)
66 return;
Shinichiro Hamaji0d8e79b2015-06-30 03:29:35 +090067 st_->Start();
68}
69
70ScopedStatsRecorder::~ScopedStatsRecorder() {
Shinichiro Hamaji9588ae92016-01-20 18:10:30 +090071 if (!g_flags.enable_stat_logs)
72 return;
Shinichiro Hamajib123fe52015-06-30 16:32:56 +090073 double e = st_->End();
74 if (msg_ && e > 3.0) {
75 LOG_STAT("slow %s (%f): %s", st_->name_, e, msg_);
76 }
Shinichiro Hamaji0d8e79b2015-06-30 03:29:35 +090077}
78
79void 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}