OLD | NEW |
1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef METRICS_COUNTER_H_ | 5 #ifndef METRICS_COUNTER_H_ |
6 #define METRICS_COUNTER_H_ | 6 #define METRICS_COUNTER_H_ |
7 | 7 |
| 8 #include <time.h> |
| 9 |
8 #include <base/basictypes.h> | 10 #include <base/basictypes.h> |
| 11 #include <base/scoped_ptr.h> |
9 #include <gtest/gtest_prod.h> // for FRIEND_TEST | 12 #include <gtest/gtest_prod.h> // for FRIEND_TEST |
10 | 13 |
11 namespace chromeos_metrics { | 14 namespace chromeos_metrics { |
12 | 15 |
| 16 // Constants useful for frequency statistics. |
| 17 const int kSecondsPerDay = 60 * 60 * 24; |
| 18 const int kSecondsPerWeek = kSecondsPerDay * 7; |
| 19 |
13 // TaggedCounter maintains a persistent storage (i.e., a file) | 20 // TaggedCounter maintains a persistent storage (i.e., a file) |
14 // aggregation counter for a given tag (e.g., day, hour) that survives | 21 // aggregation counter for a given tag (e.g., day, hour) that survives |
15 // system shutdowns, reboots and crashes, as well as daemon process | 22 // system shutdowns, reboots and crashes, as well as daemon process |
16 // restarts. The counter object is initialized by pointing to the | 23 // restarts. The counter object is initialized by pointing to the |
17 // persistent storage file and providing a callback used for reporting | 24 // persistent storage file and providing a callback used for reporting |
18 // aggregated data. The counter can then be updated with additional | 25 // aggregated data. The counter can then be updated with additional |
19 // event counts. The aggregated count is reported through the | 26 // event counts. The aggregated count is reported through the |
20 // callback when the counter is explicitly flushed or when data for a | 27 // callback when the counter is explicitly flushed or when data for a |
21 // new tag arrives. | 28 // new tag arrives. |
22 // | 29 // |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
145 Reporter reporter_; | 152 Reporter reporter_; |
146 void* reporter_handle_; | 153 void* reporter_handle_; |
147 | 154 |
148 // Current cached aggregation record. | 155 // Current cached aggregation record. |
149 Record record_; | 156 Record record_; |
150 | 157 |
151 // Current cached aggregation record state. | 158 // Current cached aggregation record state. |
152 RecordState record_state_; | 159 RecordState record_state_; |
153 }; | 160 }; |
154 | 161 |
| 162 // FrequencyCounter uses TaggedCounter to maintain a persistent |
| 163 // storage of the number of events that occur in a given cycle |
| 164 // duration (in other words, a frequency count). For example, to |
| 165 // count the number of blips per day, initialize |cycle_duration| to |
| 166 // chromeos_metrics::kSecondsPerDay, and call Update with the number |
| 167 // of blips that happen concurrently (usually 1). Reporting of the |
| 168 // value is done through TaggedCounter's reporter function. |
| 169 class FrequencyCounter { |
| 170 public: |
| 171 // Create a new frequency counter. |
| 172 FrequencyCounter(); |
| 173 virtual ~FrequencyCounter(); |
| 174 |
| 175 // Initialize a frequency counter, which is necessary before first use. |
| 176 // |filename|, |reporter|, and |reporter_handle| are used as in |
| 177 // TaggedCounter::Init. |cycle_duration| is the number of seconds |
| 178 // in a cycle. |
| 179 virtual void Init(const char* filename, |
| 180 TaggedCounterInterface::Reporter reporter, |
| 181 void* reporter_handle, |
| 182 time_t cycle_duration); |
| 183 // Record that an event occurred. |count| is the number of concurrent |
| 184 // events that have occurred. The time is implicitly assumed to be the |
| 185 // time of the call. |
| 186 virtual void Update(int32 count) { |
| 187 UpdateInternal(count, time(NULL)); |
| 188 } |
| 189 |
| 190 private: |
| 191 friend class FrequencyCounterTest; |
| 192 FRIEND_TEST(FrequencyCounterTest, UpdateInternal); |
| 193 |
| 194 void UpdateInternal(int32 count, time_t now); |
| 195 int32 GetCycleNumber(time_t now); |
| 196 |
| 197 time_t cycle_duration_; |
| 198 scoped_ptr<TaggedCounterInterface> tagged_counter_; |
| 199 }; |
| 200 |
155 } // namespace chromeos_metrics | 201 } // namespace chromeos_metrics |
156 | 202 |
157 #endif // METRICS_COUNTER_H_ | 203 #endif // METRICS_COUNTER_H_ |
OLD | NEW |