blob: 958f0483fe00b10a39ac790bafe9fea157e75570 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/metrics/stats_counters.h"
namespace base {
StatsCounter::StatsCounter(const std::string& name)
: counter_id_(-1) {
// We prepend the name with 'c:' to indicate that it is a counter.
name_ = "c:";
name_.append(name);
}
StatsCounter::~StatsCounter() {
}
void StatsCounter::Set(int value) {
int* loc = GetPtr();
if (loc)
*loc = value;
}
void StatsCounter::Add(int value) {
int* loc = GetPtr();
if (loc)
(*loc) += value;
}
StatsCounter::StatsCounter()
: counter_id_(-1) {
}
int* StatsCounter::GetPtr() {
StatsTable* table = StatsTable::current();
if (!table)
return NULL;
// If counter_id_ is -1, then we haven't looked it up yet.
if (counter_id_ == -1) {
counter_id_ = table->FindCounter(name_);
if (table->GetSlot() == 0) {
if (!table->RegisterThread("")) {
// There is no room for this thread. This thread
// cannot use counters.
counter_id_ = 0;
return NULL;
}
}
}
// If counter_id_ is > 0, then we have a valid counter.
if (counter_id_ > 0)
return table->GetLocation(counter_id_, table->GetSlot());
// counter_id_ was zero, which means the table is full.
return NULL;
}
StatsCounterTimer::StatsCounterTimer(const std::string& name) {
// we prepend the name with 't:' to indicate that it is a timer.
name_ = "t:";
name_.append(name);
}
StatsCounterTimer::~StatsCounterTimer() {
}
void StatsCounterTimer::Start() {
if (!Enabled())
return;
start_time_ = TimeTicks::Now();
stop_time_ = TimeTicks();
}
// Stop the timer and record the results.
void StatsCounterTimer::Stop() {
if (!Enabled() || !Running())
return;
stop_time_ = TimeTicks::Now();
Record();
}
// Returns true if the timer is running.
bool StatsCounterTimer::Running() {
return Enabled() && !start_time_.is_null() && stop_time_.is_null();
}
// Accept a TimeDelta to increment.
void StatsCounterTimer::AddTime(TimeDelta time) {
Add(static_cast<int>(time.InMilliseconds()));
}
void StatsCounterTimer::Record() {
AddTime(stop_time_ - start_time_);
}
StatsRate::StatsRate(const std::string& name)
: StatsCounterTimer(name),
counter_(name),
largest_add_(std::string(" ").append(name).append("MAX")) {
}
StatsRate::~StatsRate() {
}
void StatsRate::Add(int value) {
counter_.Increment();
StatsCounterTimer::Add(value);
if (value > largest_add_.value())
largest_add_.Set(value);
}
} // namespace base
|