blob: 27d6c502ad3298a10c3b656c245d5ce61460ea2a [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
Dan Willemsen2d552db2018-01-30 16:55:33 -080019#include <algorithm>
Shinichiro Hamaji8380fb82016-02-26 16:51:50 +090020#include <mutex>
Shinichiro Hamaji0d8e79b2015-06-30 03:29:35 +090021#include <vector>
22
Shinichiro Hamaji9588ae92016-01-20 18:10:30 +090023#include "flags.h"
Shinichiro Hamaji0d8e79b2015-06-30 03:29:35 +090024#include "log.h"
25#include "stringprintf.h"
Shinichiro Hamaji545b6a22016-02-15 18:43:47 +090026#include "thread_local.h"
Shinichiro Hamajicbd34cd2015-07-05 02:53:04 +090027#include "timeutil.h"
Shinichiro Hamaji0d8e79b2015-06-30 03:29:35 +090028
29namespace {
30
Shinichiro Hamaji8380fb82016-02-26 16:51:50 +090031mutex g_mu;
Shinichiro Hamaji0d8e79b2015-06-30 03:29:35 +090032vector<Stats*>* g_stats;
Shinichiro Hamaji545b6a22016-02-15 18:43:47 +090033DEFINE_THREAD_LOCAL(double, g_start_time);
Shinichiro Hamaji0d8e79b2015-06-30 03:29:35 +090034
35} // namespace
36
Dan Willemsen3ce083f2017-10-11 22:17:48 -070037Stats::Stats(const char* name) : name_(name), elapsed_(0), cnt_(0) {
Shinichiro Hamaji8380fb82016-02-26 16:51:50 +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
Dan Willemsen2d552db2018-01-30 16:55:33 -080044void Stats::DumpTop() const {
45 unique_lock<mutex> lock(mu_);
46 if (detailed_.size() > 0) {
47 vector<pair<string, double>> v(detailed_.begin(), detailed_.end());
48 sort(
49 v.begin(), v.end(),
50 [](const pair<string, double> a, const pair<string, double> b) -> bool {
51 return a.second > b.second;
52 });
53 for (unsigned int i = 0; i < 10 && i < v.size(); i++) {
54 LOG_STAT(" %5.3f %s", v[i].first.c_str(), v[i].second);
55 }
56 }
57}
58
Shinichiro Hamaji0d8e79b2015-06-30 03:29:35 +090059string Stats::String() const {
Shinichiro Hamaji8380fb82016-02-26 16:51:50 +090060 unique_lock<mutex> lock(mu_);
Shinichiro Hamaji8c8358d2015-06-30 17:08:00 +090061 return StringPrintf("%s: %f / %d", name_, elapsed_, cnt_);
Shinichiro Hamaji0d8e79b2015-06-30 03:29:35 +090062}
63
64void Stats::Start() {
Shinichiro Hamaji545b6a22016-02-15 18:43:47 +090065 CHECK(!TLS_REF(g_start_time));
66 TLS_REF(g_start_time) = GetTime();
Shinichiro Hamaji8380fb82016-02-26 16:51:50 +090067 unique_lock<mutex> lock(mu_);
Shinichiro Hamaji8c8358d2015-06-30 17:08:00 +090068 cnt_++;
Shinichiro Hamaji0d8e79b2015-06-30 03:29:35 +090069}
70
Dan Willemsen2d552db2018-01-30 16:55:33 -080071double Stats::End(const char* msg) {
Shinichiro Hamaji545b6a22016-02-15 18:43:47 +090072 CHECK(TLS_REF(g_start_time));
73 double e = GetTime() - TLS_REF(g_start_time);
74 TLS_REF(g_start_time) = 0;
Shinichiro Hamaji8380fb82016-02-26 16:51:50 +090075 unique_lock<mutex> lock(mu_);
Shinichiro Hamajib123fe52015-06-30 16:32:56 +090076 elapsed_ += e;
Dan Willemsen2d552db2018-01-30 16:55:33 -080077 if (msg != 0) {
78 detailed_[string(msg)] += e;
79 }
Shinichiro Hamajib123fe52015-06-30 16:32:56 +090080 return e;
Shinichiro Hamaji0d8e79b2015-06-30 03:29:35 +090081}
82
Shinichiro Hamajib123fe52015-06-30 16:32:56 +090083ScopedStatsRecorder::ScopedStatsRecorder(Stats* st, const char* msg)
84 : st_(st), msg_(msg) {
Shinichiro Hamaji9588ae92016-01-20 18:10:30 +090085 if (!g_flags.enable_stat_logs)
86 return;
Shinichiro Hamaji0d8e79b2015-06-30 03:29:35 +090087 st_->Start();
88}
89
90ScopedStatsRecorder::~ScopedStatsRecorder() {
Shinichiro Hamaji9588ae92016-01-20 18:10:30 +090091 if (!g_flags.enable_stat_logs)
92 return;
Dan Willemsen2d552db2018-01-30 16:55:33 -080093 double e = st_->End(msg_);
Shinichiro Hamajib123fe52015-06-30 16:32:56 +090094 if (msg_ && e > 3.0) {
95 LOG_STAT("slow %s (%f): %s", st_->name_, e, msg_);
96 }
Shinichiro Hamaji0d8e79b2015-06-30 03:29:35 +090097}
98
99void ReportAllStats() {
100 if (!g_stats)
101 return;
102 for (Stats* st : *g_stats) {
103 LOG_STAT("%s", st->String().c_str());
Dan Willemsen2d552db2018-01-30 16:55:33 -0800104 st->DumpTop();
Shinichiro Hamaji0d8e79b2015-06-30 03:29:35 +0900105 }
106 delete g_stats;
107}