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

Unified Diff: base/prefs/json_pref_store.cc

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, 8 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/prefs/json_pref_store.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/prefs/json_pref_store.cc
diff --git a/base/prefs/json_pref_store.cc b/base/prefs/json_pref_store.cc
index 47cd424d9cc9dfa7e8051d13ce72316a6bbd78b9..890ac138062b61590fbf11dd3a299557b4c9d3d5 100644
--- a/base/prefs/json_pref_store.cc
+++ b/base/prefs/json_pref_store.cc
@@ -16,6 +16,7 @@
#include "base/metrics/histogram.h"
#include "base/prefs/pref_filter.h"
#include "base/sequenced_task_runner.h"
+#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/task_runner_util.h"
#include "base/threading/sequenced_worker_pool.h"
@@ -42,8 +43,42 @@ JsonPrefStore::ReadResult::ReadResult()
JsonPrefStore::ReadResult::~ReadResult() {
}
+class JsonPrefStore::WriteCountHistogram {
+ public:
+ WriteCountHistogram(const base::TimeDelta& commit_interval,
+ const base::TimeDelta& report_interval,
+ const base::FilePath& path);
+ ~WriteCountHistogram();
+
+ void RecordWriteOccured();
+
+ private:
+ base::HistogramBase* GetHistogram();
+
+ // The minimum interval at which writes can occur.
+ const base::TimeDelta commit_interval_;
+
+ // The interval at which to report write counts.
+ const base::TimeDelta report_interval_;
+
+ // The path to the file.
+ const base::FilePath path_;
+
+ // The time at which the last histogram value was reported for the number
+ // of write counts.
+ base::Time last_report_time_;
+
+ // The number of writes that have occured since the last write count was
+ // reported.
+ uint32_t writes_since_last_report_;
+
+ DISALLOW_COPY_AND_ASSIGN(WriteCountHistogram);
+};
+
namespace {
+const int32 kHistogramWriteReportIntervalInMins = 5;
+
// Some extensions we'll tack on to copies of the Preferences files.
const base::FilePath::CharType kBadExtension[] = FILE_PATH_LITERAL("bad");
@@ -158,6 +193,11 @@ JsonPrefStore::JsonPrefStore(
filtering_in_progress_(false),
read_error_(PREF_READ_ERROR_NONE) {
DCHECK(!path_.empty());
+
+ write_count_histogram_.reset(new WriteCountHistogram(
Mattias Nissler (ping if slow) 2015/04/20 09:43:47 looks like there's no good reason to keep this in
+ writer_.commit_interval(),
+ base::TimeDelta::FromMinutes(kHistogramWriteReportIntervalInMins),
+ path_));
}
JsonPrefStore::JsonPrefStore(
@@ -394,6 +434,8 @@ JsonPrefStore::~JsonPrefStore() {
bool JsonPrefStore::SerializeData(std::string* output) {
DCHECK(CalledOnValidThread());
+ write_count_histogram_->RecordWriteOccured();
+
if (pref_filter_)
pref_filter_->FilterSerializeData(prefs_.get());
@@ -435,3 +477,60 @@ void JsonPrefStore::FinalizeFileRead(bool initialization_successful,
return;
}
+
+JsonPrefStore::WriteCountHistogram::WriteCountHistogram(
+ const base::TimeDelta& commit_interval,
+ const base::TimeDelta& report_interval,
+ const base::FilePath& path)
+ : commit_interval_(commit_interval),
+ report_interval_(report_interval),
+ path_(path),
+ last_report_time_(base::Time::Now()),
+ writes_since_last_report_(0) {
+}
+
+JsonPrefStore::WriteCountHistogram::~WriteCountHistogram() {
+ // Record any outstanding writes before shutdown.
+ GetHistogram()->Add(writes_since_last_report_);
+}
+
+void JsonPrefStore::WriteCountHistogram::RecordWriteOccured() {
+ base::Time current_time = base::Time::Now();
+ base::TimeDelta time_since_last_report = current_time - last_report_time_;
+ base::HistogramBase* histogram = GetHistogram();
+
+ // If the time since the last report exceeds the report interval, we report
+ // all of the fully elapsed intervals up to the current time.
+ while (time_since_last_report > report_interval_) {
+ histogram->Add(writes_since_last_report_);
+
+ writes_since_last_report_ = 0;
+ last_report_time_ += report_interval_;
+ time_since_last_report = current_time - last_report_time_;
+ }
+
+ writes_since_last_report_++;
+}
+
+base::HistogramBase* JsonPrefStore::WriteCountHistogram::GetHistogram() {
+ std::string spaceless_basename;
+ base::ReplaceChars(path_.BaseName().MaybeAsASCII(), " ", "_",
+ &spaceless_basename);
+ std::string histogram_name =
+ "Settings.JsonDataWriteCount." + spaceless_basename;
+
+ // The min value for a histogram is 1. The max value is the maximum number of
+ // writes that can occur in the window being recorded. The number of buckets
+ // used is the max value (plus the underflow/overflow buckets).
+ int32_t min_value = 1;
+ int32_t max_value = report_interval_ / commit_interval_;
+ int32_t num_buckets = max_value + 2;
+
+ // The histogram below is an expansion of the UMA_HISTOGRAM_CUSTOM_COUNTS
+ // macro adapted to allow for a dynamically suffixed histogram name.
+ // Note: The factory creates and owns the histogram.
+ base::HistogramBase* histogram = base::Histogram::FactoryGet(
+ histogram_name, min_value, max_value, num_buckets,
+ base::HistogramBase::kUmaTargetedHistogramFlag);
+ return histogram;
+}
« no previous file with comments | « base/prefs/json_pref_store.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698