Darin Petkov | f1e85e4 | 2010-06-10 15:59:53 -0700 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef METRICS_COUNTER_H_ |
| 6 | #define METRICS_COUNTER_H_ |
| 7 | |
Ken Mixter | ccd84c0 | 2010-08-16 19:57:13 -0700 | [diff] [blame^] | 8 | #include <time.h> |
| 9 | |
Darin Petkov | cd8c317 | 2010-06-24 10:13:54 -0700 | [diff] [blame] | 10 | #include <base/basictypes.h> |
Ken Mixter | ccd84c0 | 2010-08-16 19:57:13 -0700 | [diff] [blame^] | 11 | #include <base/scoped_ptr.h> |
Darin Petkov | f1e85e4 | 2010-06-10 15:59:53 -0700 | [diff] [blame] | 12 | #include <gtest/gtest_prod.h> // for FRIEND_TEST |
| 13 | |
| 14 | namespace chromeos_metrics { |
| 15 | |
Ken Mixter | ccd84c0 | 2010-08-16 19:57:13 -0700 | [diff] [blame^] | 16 | // Constants useful for frequency statistics. |
| 17 | const int kSecondsPerDay = 60 * 60 * 24; |
| 18 | const int kSecondsPerWeek = kSecondsPerDay * 7; |
| 19 | |
Darin Petkov | f1e85e4 | 2010-06-10 15:59:53 -0700 | [diff] [blame] | 20 | // TaggedCounter maintains a persistent storage (i.e., a file) |
| 21 | // aggregation counter for a given tag (e.g., day, hour) that survives |
| 22 | // system shutdowns, reboots and crashes, as well as daemon process |
| 23 | // restarts. The counter object is initialized by pointing to the |
| 24 | // persistent storage file and providing a callback used for reporting |
| 25 | // aggregated data. The counter can then be updated with additional |
| 26 | // event counts. The aggregated count is reported through the |
| 27 | // callback when the counter is explicitly flushed or when data for a |
| 28 | // new tag arrives. |
Darin Petkov | cd8c317 | 2010-06-24 10:13:54 -0700 | [diff] [blame] | 29 | // |
| 30 | // The primary reason for using an interface is to allow easier unit |
| 31 | // testing in clients through mocking thus avoiding file access and |
| 32 | // callbacks. Of course, it also enables alternative implementations |
| 33 | // of the counter with additional features. |
Darin Petkov | f1e85e4 | 2010-06-10 15:59:53 -0700 | [diff] [blame] | 34 | class TaggedCounterInterface { |
| 35 | public: |
| 36 | // Callback type used for reporting aggregated or flushed data. |
| 37 | // Once this callback is invoked by the counter, the reported |
Darin Petkov | 1bb904e | 2010-06-16 15:58:06 -0700 | [diff] [blame] | 38 | // aggregated data is discarded. |
Darin Petkov | f1e85e4 | 2010-06-10 15:59:53 -0700 | [diff] [blame] | 39 | // |
| 40 | // |handle| is the |reporter_handle| pointer passed through Init. |
| 41 | // |tag| is the tag associated with the aggregated count. |
| 42 | // |count| is aggregated count. |
Darin Petkov | cd8c317 | 2010-06-24 10:13:54 -0700 | [diff] [blame] | 43 | typedef void (*Reporter)(void* handle, int32 tag, int32 count); |
Darin Petkov | f1e85e4 | 2010-06-10 15:59:53 -0700 | [diff] [blame] | 44 | |
| 45 | virtual ~TaggedCounterInterface() {} |
| 46 | |
| 47 | // Initializes the counter by providing the persistent storage |
| 48 | // location |filename| and a |reporter| callback for reporting |
| 49 | // aggregated counts. |reporter_handle| is sent to the |reporter| |
| 50 | // along with the aggregated counts. |
| 51 | // |
| 52 | // NOTE: The assumption is that this object is the sole owner of the |
| 53 | // persistent storage file so no locking is currently implemented. |
| 54 | virtual void Init(const char* filename, |
| 55 | Reporter reporter, void* reporter_handle) = 0; |
| 56 | |
| 57 | // Adds |count| of events for the given |tag|. If there's an |
| 58 | // existing aggregated count for a different tag, it's reported |
| 59 | // through the reporter callback and discarded. |
Darin Petkov | cd8c317 | 2010-06-24 10:13:54 -0700 | [diff] [blame] | 60 | virtual void Update(int32 tag, int32 count) = 0; |
Darin Petkov | f1e85e4 | 2010-06-10 15:59:53 -0700 | [diff] [blame] | 61 | |
| 62 | // Reports the current aggregated count (if any) through the |
| 63 | // reporter callback and discards it. |
| 64 | virtual void Flush() = 0; |
| 65 | }; |
| 66 | |
| 67 | class TaggedCounter : public TaggedCounterInterface { |
| 68 | public: |
| 69 | TaggedCounter(); |
| 70 | ~TaggedCounter(); |
| 71 | |
| 72 | // Implementation of interface methods. |
| 73 | void Init(const char* filename, Reporter reporter, void* reporter_handle); |
Darin Petkov | cd8c317 | 2010-06-24 10:13:54 -0700 | [diff] [blame] | 74 | void Update(int32 tag, int32 count); |
Darin Petkov | f1e85e4 | 2010-06-10 15:59:53 -0700 | [diff] [blame] | 75 | void Flush(); |
| 76 | |
| 77 | private: |
| 78 | friend class RecordTest; |
| 79 | friend class TaggedCounterTest; |
| 80 | FRIEND_TEST(TaggedCounterTest, BadFileLocation); |
| 81 | FRIEND_TEST(TaggedCounterTest, Flush); |
| 82 | FRIEND_TEST(TaggedCounterTest, InitFromFile); |
| 83 | FRIEND_TEST(TaggedCounterTest, Update); |
| 84 | |
| 85 | // The current tag/count record is cached by the counter object to |
| 86 | // avoid potentially unnecessary I/O. The cached record can be in |
| 87 | // one of the following states: |
| 88 | enum RecordState { |
| 89 | kRecordInvalid, // Invalid record, sync from persistent storage needed. |
| 90 | kRecordNull, // No current record, persistent storage synced. |
| 91 | kRecordNullDirty, // No current record, persistent storage is invalid. |
| 92 | kRecordValid, // Current record valid, persistent storage synced. |
| 93 | kRecordValidDirty // Current record valid, persistent storage is invalid. |
| 94 | }; |
| 95 | |
| 96 | // Defines the tag/count record. Objects of this class are synced |
| 97 | // with the persistent storage through binary reads/writes. |
| 98 | class Record { |
| 99 | public: |
| 100 | // Creates a new Record with |tag_| and |count_| reset to 0. |
| 101 | Record() : tag_(0), count_(0) {} |
| 102 | |
| 103 | // Initializes with |tag| and |count|. If |count| is negative, |
| 104 | // |count_| is set to 0. |
Darin Petkov | cd8c317 | 2010-06-24 10:13:54 -0700 | [diff] [blame] | 105 | void Init(int32 tag, int32 count); |
Darin Petkov | f1e85e4 | 2010-06-10 15:59:53 -0700 | [diff] [blame] | 106 | |
| 107 | // Adds |count| to the current |count_|. Negative |count| is |
| 108 | // ignored. In case of positive overflow, |count_| is saturated to |
Darin Petkov | cd8c317 | 2010-06-24 10:13:54 -0700 | [diff] [blame] | 109 | // kint32max. |
| 110 | void Add(int32 count); |
Darin Petkov | f1e85e4 | 2010-06-10 15:59:53 -0700 | [diff] [blame] | 111 | |
Darin Petkov | cd8c317 | 2010-06-24 10:13:54 -0700 | [diff] [blame] | 112 | int32 tag() const { return tag_; } |
| 113 | int32 count() const { return count_; } |
Darin Petkov | f1e85e4 | 2010-06-10 15:59:53 -0700 | [diff] [blame] | 114 | |
| 115 | private: |
Darin Petkov | cd8c317 | 2010-06-24 10:13:54 -0700 | [diff] [blame] | 116 | int32 tag_; |
| 117 | int32 count_; |
Darin Petkov | f1e85e4 | 2010-06-10 15:59:53 -0700 | [diff] [blame] | 118 | }; |
| 119 | |
| 120 | // Implementation of the Update and Flush methods. Goes through the |
| 121 | // necessary steps to read, report, update, and sync the aggregated |
| 122 | // record. |
Darin Petkov | cd8c317 | 2010-06-24 10:13:54 -0700 | [diff] [blame] | 123 | void UpdateInternal(int32 tag, int32 count, bool flush); |
Darin Petkov | f1e85e4 | 2010-06-10 15:59:53 -0700 | [diff] [blame] | 124 | |
| 125 | // If the current cached record is invalid, reads it from persistent |
| 126 | // storage specified through file descriptor |fd| and updates the |
| 127 | // cached record state to either null, or valid depending on the |
| 128 | // persistent storage contents. |
| 129 | void ReadRecord(int fd); |
| 130 | |
| 131 | // If there's an existing valid record and either |flush| is true, |
| 132 | // or the new |tag| is different than the old one, reports the |
| 133 | // aggregated data through the reporter callback and resets the |
| 134 | // cached record. |
Darin Petkov | cd8c317 | 2010-06-24 10:13:54 -0700 | [diff] [blame] | 135 | void ReportRecord(int32 tag, bool flush); |
Darin Petkov | f1e85e4 | 2010-06-10 15:59:53 -0700 | [diff] [blame] | 136 | |
| 137 | // Updates the cached record given the new |tag| and |count|. This |
| 138 | // method expects either a null cached record, or a valid cached |
Darin Petkov | 1bb904e | 2010-06-16 15:58:06 -0700 | [diff] [blame] | 139 | // record with the same tag as |tag|. If |flush| is true, the method |
| 140 | // asserts that the cached record is null and returns. |
Darin Petkov | cd8c317 | 2010-06-24 10:13:54 -0700 | [diff] [blame] | 141 | void UpdateRecord(int32 tag, int32 count, bool flush); |
Darin Petkov | f1e85e4 | 2010-06-10 15:59:53 -0700 | [diff] [blame] | 142 | |
| 143 | // If the cached record state is dirty, updates the persistent |
| 144 | // storage specified through file descriptor |fd| and switches the |
| 145 | // record state to non-dirty. |
| 146 | void WriteRecord(int fd); |
| 147 | |
| 148 | // Persistent storage file path. |
| 149 | const char* filename_; |
| 150 | |
| 151 | // Aggregated data reporter callback and handle to pass-through. |
| 152 | Reporter reporter_; |
| 153 | void* reporter_handle_; |
| 154 | |
| 155 | // Current cached aggregation record. |
| 156 | Record record_; |
| 157 | |
| 158 | // Current cached aggregation record state. |
| 159 | RecordState record_state_; |
| 160 | }; |
| 161 | |
Ken Mixter | ccd84c0 | 2010-08-16 19:57:13 -0700 | [diff] [blame^] | 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 | |
Darin Petkov | f1e85e4 | 2010-06-10 15:59:53 -0700 | [diff] [blame] | 201 | } // namespace chromeos_metrics |
| 202 | |
| 203 | #endif // METRICS_COUNTER_H_ |