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 <gtest/gtest_prod.h> // for FRIEND_TEST | 8 #include <gtest/gtest_prod.h> // for FRIEND_TEST |
9 | 9 |
10 namespace chromeos_metrics { | 10 namespace chromeos_metrics { |
11 | 11 |
12 // TaggedCounter maintains a persistent storage (i.e., a file) | 12 // TaggedCounter maintains a persistent storage (i.e., a file) |
13 // aggregation counter for a given tag (e.g., day, hour) that survives | 13 // aggregation counter for a given tag (e.g., day, hour) that survives |
14 // system shutdowns, reboots and crashes, as well as daemon process | 14 // system shutdowns, reboots and crashes, as well as daemon process |
15 // restarts. The counter object is initialized by pointing to the | 15 // restarts. The counter object is initialized by pointing to the |
16 // persistent storage file and providing a callback used for reporting | 16 // persistent storage file and providing a callback used for reporting |
17 // aggregated data. The counter can then be updated with additional | 17 // aggregated data. The counter can then be updated with additional |
18 // event counts. The aggregated count is reported through the | 18 // event counts. The aggregated count is reported through the |
19 // callback when the counter is explicitly flushed or when data for a | 19 // callback when the counter is explicitly flushed or when data for a |
20 // new tag arrives. | 20 // new tag arrives. |
21 class TaggedCounterInterface { | 21 class TaggedCounterInterface { |
22 public: | 22 public: |
23 // Callback type used for reporting aggregated or flushed data. | 23 // Callback type used for reporting aggregated or flushed data. |
24 // Once this callback is invoked by the counter, the reported | 24 // Once this callback is invoked by the counter, the reported |
25 // aggregated data is discarded. Only aggregated data with positive | 25 // aggregated data is discarded. |
26 // counts is reported. | |
27 // | 26 // |
28 // |handle| is the |reporter_handle| pointer passed through Init. | 27 // |handle| is the |reporter_handle| pointer passed through Init. |
29 // |tag| is the tag associated with the aggregated count. | 28 // |tag| is the tag associated with the aggregated count. |
30 // |count| is aggregated count. | 29 // |count| is aggregated count. |
31 typedef void (*Reporter)(void* handle, int tag, int count); | 30 typedef void (*Reporter)(void* handle, int tag, int count); |
32 | 31 |
33 virtual ~TaggedCounterInterface() {} | 32 virtual ~TaggedCounterInterface() {} |
34 | 33 |
35 // Initializes the counter by providing the persistent storage | 34 // Initializes the counter by providing the persistent storage |
36 // location |filename| and a |reporter| callback for reporting | 35 // location |filename| and a |reporter| callback for reporting |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
117 void ReadRecord(int fd); | 116 void ReadRecord(int fd); |
118 | 117 |
119 // If there's an existing valid record and either |flush| is true, | 118 // If there's an existing valid record and either |flush| is true, |
120 // or the new |tag| is different than the old one, reports the | 119 // or the new |tag| is different than the old one, reports the |
121 // aggregated data through the reporter callback and resets the | 120 // aggregated data through the reporter callback and resets the |
122 // cached record. | 121 // cached record. |
123 void ReportRecord(int tag, bool flush); | 122 void ReportRecord(int tag, bool flush); |
124 | 123 |
125 // Updates the cached record given the new |tag| and |count|. This | 124 // Updates the cached record given the new |tag| and |count|. This |
126 // method expects either a null cached record, or a valid cached | 125 // method expects either a null cached record, or a valid cached |
127 // record with the same tag as |tag|. | 126 // record with the same tag as |tag|. If |flush| is true, the method |
128 void UpdateRecord(int tag, int count); | 127 // asserts that the cached record is null and returns. |
| 128 void UpdateRecord(int tag, int count, bool flush); |
129 | 129 |
130 // If the cached record state is dirty, updates the persistent | 130 // If the cached record state is dirty, updates the persistent |
131 // storage specified through file descriptor |fd| and switches the | 131 // storage specified through file descriptor |fd| and switches the |
132 // record state to non-dirty. | 132 // record state to non-dirty. |
133 void WriteRecord(int fd); | 133 void WriteRecord(int fd); |
134 | 134 |
135 // Persistent storage file path. | 135 // Persistent storage file path. |
136 const char* filename_; | 136 const char* filename_; |
137 | 137 |
138 // Aggregated data reporter callback and handle to pass-through. | 138 // Aggregated data reporter callback and handle to pass-through. |
139 Reporter reporter_; | 139 Reporter reporter_; |
140 void* reporter_handle_; | 140 void* reporter_handle_; |
141 | 141 |
142 // Current cached aggregation record. | 142 // Current cached aggregation record. |
143 Record record_; | 143 Record record_; |
144 | 144 |
145 // Current cached aggregation record state. | 145 // Current cached aggregation record state. |
146 RecordState record_state_; | 146 RecordState record_state_; |
147 }; | 147 }; |
148 | 148 |
149 } // namespace chromeos_metrics | 149 } // namespace chromeos_metrics |
150 | 150 |
151 #endif // METRICS_COUNTER_H_ | 151 #endif // METRICS_COUNTER_H_ |
OLD | NEW |