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