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