Chromium Code Reviews| 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..e57712b8bcdfff9b5c89b4ef15500406e96dc5f6 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" |
| @@ -156,7 +157,8 @@ JsonPrefStore::JsonPrefStore( |
| pref_filter_(pref_filter.Pass()), |
| initialized_(false), |
| filtering_in_progress_(false), |
| - read_error_(PREF_READ_ERROR_NONE) { |
| + read_error_(PREF_READ_ERROR_NONE), |
| + write_count_histogram_(writer_.commit_interval(), path_) { |
| DCHECK(!path_.empty()); |
| } |
| @@ -174,7 +176,8 @@ JsonPrefStore::JsonPrefStore( |
| pref_filter_(pref_filter.Pass()), |
| initialized_(false), |
| filtering_in_progress_(false), |
| - read_error_(PREF_READ_ERROR_NONE) { |
| + read_error_(PREF_READ_ERROR_NONE), |
| + write_count_histogram_(writer_.commit_interval(), path_) { |
| DCHECK(!path_.empty()); |
| } |
| @@ -394,6 +397,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 +440,74 @@ void JsonPrefStore::FinalizeFileRead(bool initialization_successful, |
| return; |
| } |
| + |
| +const int32_t |
| + JsonPrefStore::WriteCountHistogram::kHistogramWriteReportIntervalMins = 5; |
| + |
| +JsonPrefStore::WriteCountHistogram::WriteCountHistogram( |
| + const base::TimeDelta& commit_interval, |
| + const base::FilePath& path) |
| + : WriteCountHistogram(commit_interval, path, base::Bind(&base::Time::Now)) { |
| +} |
| + |
| +JsonPrefStore::WriteCountHistogram::WriteCountHistogram( |
| + const base::TimeDelta& commit_interval, |
| + const base::FilePath& path, |
| + const base::Callback<base::Time(void)>& get_time_func) |
| + : commit_interval_(commit_interval), |
| + path_(path), |
| + get_time_func_(get_time_func), |
| + report_interval_( |
| + base::TimeDelta::FromMinutes(kHistogramWriteReportIntervalMins)), |
| + last_report_time_(get_time_func.Run()), |
| + writes_since_last_report_(0) { |
| +} |
| + |
| +JsonPrefStore::WriteCountHistogram::~WriteCountHistogram() { |
| + ReportOutstandingWrites(); |
| +} |
| + |
| +void JsonPrefStore::WriteCountHistogram::RecordWriteOccured() { |
| + ReportOutstandingWrites(); |
| + |
| + writes_since_last_report_++; |
|
Alexei Svitkine (slow)
2015/04/24 16:34:47
Nit: ++writes
raymes
2015/04/27 03:27:54
Done.
|
| +} |
| + |
| +void JsonPrefStore::WriteCountHistogram::ReportOutstandingWrites() { |
| + base::Time current_time = get_time_func_.Run(); |
| + 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_; |
| + } |
| +} |
| + |
| +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 + 1; |
| + |
| + // 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; |
| +} |