blob: 876b107323c7c18863098a46c1ab363cd41917a5 [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
Darin Petkov1bb904e2010-06-16 15:58:06 -070025 // aggregated data is discarded.
Darin Petkovf1e85e42010-06-10 15:59:53 -070026 //
27 // |handle| is the |reporter_handle| pointer passed through Init.
28 // |tag| is the tag associated with the aggregated count.
29 // |count| is aggregated count.
30 typedef void (*Reporter)(void* handle, int tag, int count);
31
32 virtual ~TaggedCounterInterface() {}
33
34 // Initializes the counter by providing the persistent storage
35 // location |filename| and a |reporter| callback for reporting
36 // aggregated counts. |reporter_handle| is sent to the |reporter|
37 // along with the aggregated counts.
38 //
39 // NOTE: The assumption is that this object is the sole owner of the
40 // persistent storage file so no locking is currently implemented.
41 virtual void Init(const char* filename,
42 Reporter reporter, void* reporter_handle) = 0;
43
44 // Adds |count| of events for the given |tag|. If there's an
45 // existing aggregated count for a different tag, it's reported
46 // through the reporter callback and discarded.
47 virtual void Update(int tag, int count) = 0;
48
49 // Reports the current aggregated count (if any) through the
50 // reporter callback and discards it.
51 virtual void Flush() = 0;
52};
53
54class TaggedCounter : public TaggedCounterInterface {
55 public:
56 TaggedCounter();
57 ~TaggedCounter();
58
59 // Implementation of interface methods.
60 void Init(const char* filename, Reporter reporter, void* reporter_handle);
61 void Update(int tag, int count);
62 void Flush();
63
64 private:
65 friend class RecordTest;
66 friend class TaggedCounterTest;
67 FRIEND_TEST(TaggedCounterTest, BadFileLocation);
68 FRIEND_TEST(TaggedCounterTest, Flush);
69 FRIEND_TEST(TaggedCounterTest, InitFromFile);
70 FRIEND_TEST(TaggedCounterTest, Update);
71
72 // The current tag/count record is cached by the counter object to
73 // avoid potentially unnecessary I/O. The cached record can be in
74 // one of the following states:
75 enum RecordState {
76 kRecordInvalid, // Invalid record, sync from persistent storage needed.
77 kRecordNull, // No current record, persistent storage synced.
78 kRecordNullDirty, // No current record, persistent storage is invalid.
79 kRecordValid, // Current record valid, persistent storage synced.
80 kRecordValidDirty // Current record valid, persistent storage is invalid.
81 };
82
83 // Defines the tag/count record. Objects of this class are synced
84 // with the persistent storage through binary reads/writes.
85 class Record {
86 public:
87 // Creates a new Record with |tag_| and |count_| reset to 0.
88 Record() : tag_(0), count_(0) {}
89
90 // Initializes with |tag| and |count|. If |count| is negative,
91 // |count_| is set to 0.
92 void Init(int tag, int count);
93
94 // Adds |count| to the current |count_|. Negative |count| is
95 // ignored. In case of positive overflow, |count_| is saturated to
96 // INT_MAX.
97 void Add(int count);
98
99 int tag() const { return tag_; }
100 int count() const { return count_; }
101
102 private:
103 int tag_;
104 int count_;
105 };
106
107 // Implementation of the Update and Flush methods. Goes through the
108 // necessary steps to read, report, update, and sync the aggregated
109 // record.
110 void UpdateInternal(int tag, int count, bool flush);
111
112 // If the current cached record is invalid, reads it from persistent
113 // storage specified through file descriptor |fd| and updates the
114 // cached record state to either null, or valid depending on the
115 // persistent storage contents.
116 void ReadRecord(int fd);
117
118 // If there's an existing valid record and either |flush| is true,
119 // or the new |tag| is different than the old one, reports the
120 // aggregated data through the reporter callback and resets the
121 // cached record.
122 void ReportRecord(int tag, bool flush);
123
124 // Updates the cached record given the new |tag| and |count|. This
125 // method expects either a null cached record, or a valid cached
Darin Petkov1bb904e2010-06-16 15:58:06 -0700126 // record with the same tag as |tag|. If |flush| is true, the method
127 // asserts that the cached record is null and returns.
128 void UpdateRecord(int tag, int count, bool flush);
Darin Petkovf1e85e42010-06-10 15:59:53 -0700129
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_