This source file includes following definitions.
- Set
- Add
- GetPtr
- Start
- Stop
- Running
- AddTime
- Record
- largest_add_
- Add
#include "base/metrics/stats_counters.h"
namespace base {
StatsCounter::StatsCounter(const std::string& name)
: counter_id_(-1) {
if (StatsTable::current()) {
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_ == -1) {
counter_id_ = table->FindCounter(name_);
if (table->GetSlot() == 0) {
if (!table->RegisterThread(std::string())) {
counter_id_ = 0;
return NULL;
}
}
}
if (counter_id_ > 0)
return table->GetLocation(counter_id_, table->GetSlot());
return NULL;
}
StatsCounterTimer::StatsCounterTimer(const std::string& name) {
if (StatsTable::current()) {
name_ = "t:";
name_.append(name);
}
}
StatsCounterTimer::~StatsCounterTimer() {
}
void StatsCounterTimer::Start() {
if (!Enabled())
return;
start_time_ = TimeTicks::Now();
stop_time_ = TimeTicks();
}
void StatsCounterTimer::Stop() {
if (!Enabled() || !Running())
return;
stop_time_ = TimeTicks::Now();
Record();
}
bool StatsCounterTimer::Running() {
return Enabled() && !start_time_.is_null() && stop_time_.is_null();
}
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);
}
}