Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(548)

Side by Side Diff: base/prefs/json_pref_store.h

Issue 1083603002: Add histograms to record the number of writes to the prefs file (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/metrics/statistics_recorder.cc ('k') | base/prefs/json_pref_store.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium 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 BASE_PREFS_JSON_PREF_STORE_H_ 5 #ifndef BASE_PREFS_JSON_PREF_STORE_H_
6 #define BASE_PREFS_JSON_PREF_STORE_H_ 6 #define BASE_PREFS_JSON_PREF_STORE_H_
7 7
8 #include <set> 8 #include <set>
9 #include <string> 9 #include <string>
10 10
11 #include "base/basictypes.h" 11 #include "base/basictypes.h"
12 #include "base/callback_forward.h" 12 #include "base/callback_forward.h"
13 #include "base/compiler_specific.h" 13 #include "base/compiler_specific.h"
14 #include "base/files/file_path.h" 14 #include "base/files/file_path.h"
15 #include "base/files/important_file_writer.h" 15 #include "base/files/important_file_writer.h"
16 #include "base/gtest_prod_util.h"
16 #include "base/memory/scoped_ptr.h" 17 #include "base/memory/scoped_ptr.h"
18 #include "base/memory/scoped_vector.h"
17 #include "base/memory/weak_ptr.h" 19 #include "base/memory/weak_ptr.h"
18 #include "base/message_loop/message_loop_proxy.h" 20 #include "base/message_loop/message_loop_proxy.h"
19 #include "base/observer_list.h" 21 #include "base/observer_list.h"
20 #include "base/prefs/base_prefs_export.h" 22 #include "base/prefs/base_prefs_export.h"
21 #include "base/prefs/persistent_pref_store.h" 23 #include "base/prefs/persistent_pref_store.h"
22 #include "base/threading/non_thread_safe.h" 24 #include "base/threading/non_thread_safe.h"
23 25
24 class PrefFilter; 26 class PrefFilter;
25 27
26 namespace base { 28 namespace base {
29 class Clock;
27 class DictionaryValue; 30 class DictionaryValue;
28 class FilePath; 31 class FilePath;
32 class HistogramBase;
29 class SequencedTaskRunner; 33 class SequencedTaskRunner;
30 class SequencedWorkerPool; 34 class SequencedWorkerPool;
31 class Value; 35 class Value;
36 FORWARD_DECLARE_TEST(JsonPrefStoreTest, WriteCountHistogramTestBasic);
37 FORWARD_DECLARE_TEST(JsonPrefStoreTest, WriteCountHistogramTestSinglePeriod);
38 FORWARD_DECLARE_TEST(JsonPrefStoreTest, WriteCountHistogramTestMultiplePeriods);
39 FORWARD_DECLARE_TEST(JsonPrefStoreTest, WriteCountHistogramTestPeriodWithGaps);
32 } 40 }
33 41
34 // A writable PrefStore implementation that is used for user preferences. 42 // A writable PrefStore implementation that is used for user preferences.
35 class BASE_PREFS_EXPORT JsonPrefStore 43 class BASE_PREFS_EXPORT JsonPrefStore
36 : public PersistentPrefStore, 44 : public PersistentPrefStore,
37 public base::ImportantFileWriter::DataSerializer, 45 public base::ImportantFileWriter::DataSerializer,
38 public base::SupportsWeakPtr<JsonPrefStore>, 46 public base::SupportsWeakPtr<JsonPrefStore>,
39 public base::NonThreadSafe { 47 public base::NonThreadSafe {
40 public: 48 public:
41 struct ReadResult; 49 struct ReadResult;
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
91 // Just like RemoveValue(), but doesn't notify observers. Used when doing some 99 // Just like RemoveValue(), but doesn't notify observers. Used when doing some
92 // cleanup that shouldn't otherwise alert observers. 100 // cleanup that shouldn't otherwise alert observers.
93 void RemoveValueSilently(const std::string& key); 101 void RemoveValueSilently(const std::string& key);
94 102
95 // Registers |on_next_successful_write| to be called once, on the next 103 // Registers |on_next_successful_write| to be called once, on the next
96 // successful write event of |writer_|. 104 // successful write event of |writer_|.
97 void RegisterOnNextSuccessfulWriteCallback( 105 void RegisterOnNextSuccessfulWriteCallback(
98 const base::Closure& on_next_successful_write); 106 const base::Closure& on_next_successful_write);
99 107
100 private: 108 private:
109 // Represents a histogram for recording the number of writes to the pref file
110 // that occur every kHistogramWriteReportIntervalInMins minutes.
111 class BASE_PREFS_EXPORT WriteCountHistogram {
112 public:
113 static const int32_t kHistogramWriteReportIntervalMins;
114
115 WriteCountHistogram(const base::TimeDelta& commit_interval,
116 const base::FilePath& path);
117 // Constructor for testing. |clock| is a test Clock that is used to retrieve
118 // the time.
119 WriteCountHistogram(const base::TimeDelta& commit_interval,
120 const base::FilePath& path,
121 scoped_ptr<base::Clock> clock);
122 ~WriteCountHistogram();
123
124 // Record that a write has occured.
125 void RecordWriteOccured();
126
127 // Reports writes (that have not yet been reported) in all of the recorded
128 // intervals that have elapsed up until current time.
129 void ReportOutstandingWrites();
130
131 base::HistogramBase* GetHistogram();
132
133 private:
134 // The minimum interval at which writes can occur.
135 const base::TimeDelta commit_interval_;
136
137 // The path to the file.
138 const base::FilePath path_;
139
140 // Clock which is used to retrieve the current time.
141 scoped_ptr<base::Clock> clock_;
142
143 // The interval at which to report write counts.
144 const base::TimeDelta report_interval_;
145
146 // The time at which the last histogram value was reported for the number
147 // of write counts.
148 base::Time last_report_time_;
149
150 // The number of writes that have occured since the last write count was
151 // reported.
152 uint32_t writes_since_last_report_;
153
154 DISALLOW_COPY_AND_ASSIGN(WriteCountHistogram);
155 };
156
157 FRIEND_TEST_ALL_PREFIXES(base::JsonPrefStoreTest,
158 WriteCountHistogramTestBasic);
159 FRIEND_TEST_ALL_PREFIXES(base::JsonPrefStoreTest,
160 WriteCountHistogramTestSinglePeriod);
161 FRIEND_TEST_ALL_PREFIXES(base::JsonPrefStoreTest,
162 WriteCountHistogramTestMultiplePeriods);
163 FRIEND_TEST_ALL_PREFIXES(base::JsonPrefStoreTest,
164 WriteCountHistogramTestPeriodWithGaps);
165
101 ~JsonPrefStore() override; 166 ~JsonPrefStore() override;
102 167
103 // This method is called after the JSON file has been read. It then hands 168 // This method is called after the JSON file has been read. It then hands
104 // |value| (or an empty dictionary in some read error cases) to the 169 // |value| (or an empty dictionary in some read error cases) to the
105 // |pref_filter| if one is set. It also gives a callback pointing at 170 // |pref_filter| if one is set. It also gives a callback pointing at
106 // FinalizeFileRead() to that |pref_filter_| which is then responsible for 171 // FinalizeFileRead() to that |pref_filter_| which is then responsible for
107 // invoking it when done. If there is no |pref_filter_|, FinalizeFileRead() 172 // invoking it when done. If there is no |pref_filter_|, FinalizeFileRead()
108 // is invoked directly. 173 // is invoked directly.
109 void OnFileRead(scoped_ptr<ReadResult> read_result); 174 void OnFileRead(scoped_ptr<ReadResult> read_result);
110 175
(...skipping 26 matching lines...) Expand all
137 ObserverList<PrefStore::Observer, true> observers_; 202 ObserverList<PrefStore::Observer, true> observers_;
138 203
139 scoped_ptr<ReadErrorDelegate> error_delegate_; 204 scoped_ptr<ReadErrorDelegate> error_delegate_;
140 205
141 bool initialized_; 206 bool initialized_;
142 bool filtering_in_progress_; 207 bool filtering_in_progress_;
143 PrefReadError read_error_; 208 PrefReadError read_error_;
144 209
145 std::set<std::string> keys_need_empty_value_; 210 std::set<std::string> keys_need_empty_value_;
146 211
212 WriteCountHistogram write_count_histogram_;
213
147 DISALLOW_COPY_AND_ASSIGN(JsonPrefStore); 214 DISALLOW_COPY_AND_ASSIGN(JsonPrefStore);
148 }; 215 };
149 216
150 #endif // BASE_PREFS_JSON_PREF_STORE_H_ 217 #endif // BASE_PREFS_JSON_PREF_STORE_H_
OLDNEW
« no previous file with comments | « base/metrics/statistics_recorder.cc ('k') | base/prefs/json_pref_store.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698