blob: a5e5302f2ad182e2539435965372dc8c948583ff [file] [log] [blame]
Darin Petkovf1e85e42010-06-10 15:59:53 -07001// 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 Mixterccd84c02010-08-16 19:57:13 -07008#include <time.h>
9
Darin Petkovcd8c3172010-06-24 10:13:54 -070010#include <base/basictypes.h>
Ken Mixterccd84c02010-08-16 19:57:13 -070011#include <base/scoped_ptr.h>
Darin Petkovf1e85e42010-06-10 15:59:53 -070012#include <gtest/gtest_prod.h> // for FRIEND_TEST
13
14namespace chromeos_metrics {
15
Ken Mixterccd84c02010-08-16 19:57:13 -070016// Constants useful for frequency statistics.
17const int kSecondsPerDay = 60 * 60 * 24;
18const int kSecondsPerWeek = kSecondsPerDay * 7;
19
Darin Petkovf1e85e42010-06-10 15:59:53 -070020// 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 Petkovcd8c3172010-06-24 10:13:54 -070029//
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 Petkovf1e85e42010-06-10 15:59:53 -070034class 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 Petkov1bb904e2010-06-16 15:58:06 -070038 // aggregated data is discarded.
Darin Petkovf1e85e42010-06-10 15:59:53 -070039 //
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 Petkovcd8c3172010-06-24 10:13:54 -070043 typedef void (*Reporter)(void* handle, int32 tag, int32 count);
Darin Petkovf1e85e42010-06-10 15:59:53 -070044
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 Petkovcd8c3172010-06-24 10:13:54 -070060 virtual void Update(int32 tag, int32 count) = 0;
Darin Petkovf1e85e42010-06-10 15:59:53 -070061
62 // Reports the current aggregated count (if any) through the
63 // reporter callback and discards it.
64 virtual void Flush() = 0;
65};
66
67class 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 Petkovcd8c3172010-06-24 10:13:54 -070074 void Update(int32 tag, int32 count);
Darin Petkovf1e85e42010-06-10 15:59:53 -070075 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 Petkovcd8c3172010-06-24 10:13:54 -0700105 void Init(int32 tag, int32 count);
Darin Petkovf1e85e42010-06-10 15:59:53 -0700106
107 // Adds |count| to the current |count_|. Negative |count| is
108 // ignored. In case of positive overflow, |count_| is saturated to
Darin Petkovcd8c3172010-06-24 10:13:54 -0700109 // kint32max.
110 void Add(int32 count);
Darin Petkovf1e85e42010-06-10 15:59:53 -0700111
Darin Petkovcd8c3172010-06-24 10:13:54 -0700112 int32 tag() const { return tag_; }
113 int32 count() const { return count_; }
Darin Petkovf1e85e42010-06-10 15:59:53 -0700114
115 private:
Darin Petkovcd8c3172010-06-24 10:13:54 -0700116 int32 tag_;
117 int32 count_;
Darin Petkovf1e85e42010-06-10 15:59:53 -0700118 };
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 Petkovcd8c3172010-06-24 10:13:54 -0700123 void UpdateInternal(int32 tag, int32 count, bool flush);
Darin Petkovf1e85e42010-06-10 15:59:53 -0700124
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 Petkovcd8c3172010-06-24 10:13:54 -0700135 void ReportRecord(int32 tag, bool flush);
Darin Petkovf1e85e42010-06-10 15:59:53 -0700136
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 Petkov1bb904e2010-06-16 15:58:06 -0700139 // record with the same tag as |tag|. If |flush| is true, the method
140 // asserts that the cached record is null and returns.
Darin Petkovcd8c3172010-06-24 10:13:54 -0700141 void UpdateRecord(int32 tag, int32 count, bool flush);
Darin Petkovf1e85e42010-06-10 15:59:53 -0700142
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 Mixterccd84c02010-08-16 19:57:13 -0700162// 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.
169class 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 Petkovf1e85e42010-06-10 15:59:53 -0700201} // namespace chromeos_metrics
202
203#endif // METRICS_COUNTER_H_