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

Side by Side Diff: components/metrics/metrics_service.cc

Issue 1425533011: Support "shared" histograms between processes. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@shmem-alloc
Patch Set: addressed review comments by Alexei Created 5 years 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
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 //------------------------------------------------------------------------------ 5 //------------------------------------------------------------------------------
6 // Description of the life cycle of a instance of MetricsService. 6 // Description of the life cycle of a instance of MetricsService.
7 // 7 //
8 // OVERVIEW 8 // OVERVIEW
9 // 9 //
10 // A MetricsService instance is typically created at application startup. It is 10 // A MetricsService instance is typically created at application startup. It is
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
124 124
125 #include "components/metrics/metrics_service.h" 125 #include "components/metrics/metrics_service.h"
126 126
127 #include <algorithm> 127 #include <algorithm>
128 128
129 #include "base/bind.h" 129 #include "base/bind.h"
130 #include "base/callback.h" 130 #include "base/callback.h"
131 #include "base/location.h" 131 #include "base/location.h"
132 #include "base/metrics/histogram_base.h" 132 #include "base/metrics/histogram_base.h"
133 #include "base/metrics/histogram_macros.h" 133 #include "base/metrics/histogram_macros.h"
134 #include "base/metrics/histogram_persistence.h"
134 #include "base/metrics/histogram_samples.h" 135 #include "base/metrics/histogram_samples.h"
135 #include "base/metrics/sparse_histogram.h" 136 #include "base/metrics/sparse_histogram.h"
136 #include "base/metrics/statistics_recorder.h" 137 #include "base/metrics/statistics_recorder.h"
137 #include "base/prefs/pref_registry_simple.h" 138 #include "base/prefs/pref_registry_simple.h"
138 #include "base/prefs/pref_service.h" 139 #include "base/prefs/pref_service.h"
139 #include "base/rand_util.h" 140 #include "base/rand_util.h"
140 #include "base/single_thread_task_runner.h" 141 #include "base/single_thread_task_runner.h"
141 #include "base/strings/string_number_conversions.h" 142 #include "base/strings/string_number_conversions.h"
142 #include "base/strings/utf_string_conversions.h" 143 #include "base/strings/utf_string_conversions.h"
143 #include "base/thread_task_runner_handle.h" 144 #include "base/thread_task_runner_handle.h"
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
235 236
236 int probability; 237 int probability;
237 // In case specified sampling rate is invalid. 238 // In case specified sampling rate is invalid.
238 if (!base::StringToInt(probability_str, &probability)) 239 if (!base::StringToInt(probability_str, &probability))
239 return true; 240 return true;
240 return base::RandInt(1, 100) <= probability; 241 return base::RandInt(1, 100) <= probability;
241 } 242 }
242 243
243 } // namespace 244 } // namespace
244 245
246 MetricsService::PersistentHistogramIterator::PersistentHistogramIterator(
247 AllocatorSet& allocators,
248 AllocatorSet::iterator pos)
249 : allocators_(allocators),
250 allocator_iter_(pos) {
251 if (pos != allocators_.end()) {
252 (*allocator_iter_)->CreateIterator(&histogram_iter_);
253 // Have to call ++ to advance iterator to the first persistent histogram.
254 operator++();
255 }
256 }
257
258 MetricsService::PersistentHistogramIterator&
259 MetricsService::PersistentHistogramIterator::operator++() {
260 if (allocator_iter_ != allocators_.end()) {
261 for (;;) {
262 base::HistogramBase* h = base::GetNextPersistentHistogram(
263 *allocator_iter_, &histogram_iter_);
264 if (h) {
265 current_histogram_ = new HistogramPointer(h);
266 break;
267 }
268 allocator_iter_++;
269 if (allocator_iter_ == allocators_.end()) {
270 histogram_iter_.clear(); // Clear so it matches end() iterator.
271 current_histogram_ = nullptr;
272 break;
273 }
274 (*allocator_iter_)->CreateIterator(&histogram_iter_);
275 }
276 }
277 return *this;
278 }
279
245 // static 280 // static
246 MetricsService::ShutdownCleanliness MetricsService::clean_shutdown_status_ = 281 MetricsService::ShutdownCleanliness MetricsService::clean_shutdown_status_ =
247 MetricsService::CLEANLY_SHUTDOWN; 282 MetricsService::CLEANLY_SHUTDOWN;
248 283
249 MetricsService::ExecutionPhase MetricsService::execution_phase_ = 284 MetricsService::ExecutionPhase MetricsService::execution_phase_ =
250 MetricsService::UNINITIALIZED_PHASE; 285 MetricsService::UNINITIALIZED_PHASE;
251 286
252 // static 287 // static
253 void MetricsService::RegisterPrefs(PrefRegistrySimple* registry) { 288 void MetricsService::RegisterPrefs(PrefRegistrySimple* registry) {
254 DCHECK(IsSingleThreaded()); 289 DCHECK(IsSingleThreaded());
(...skipping 276 matching lines...) Expand 10 before | Expand all | Expand 10 after
531 UNINITIALIZED_PHASE); 566 UNINITIALIZED_PHASE);
532 local_state_->SetInteger(prefs::kStabilityIncompleteSessionEndCount, 0); 567 local_state_->SetInteger(prefs::kStabilityIncompleteSessionEndCount, 0);
533 local_state_->SetInteger(prefs::kStabilityLaunchCount, 0); 568 local_state_->SetInteger(prefs::kStabilityLaunchCount, 0);
534 local_state_->SetBoolean(prefs::kStabilitySessionEndCompleted, true); 569 local_state_->SetBoolean(prefs::kStabilitySessionEndCompleted, true);
535 } 570 }
536 571
537 void MetricsService::PushExternalLog(const std::string& log) { 572 void MetricsService::PushExternalLog(const std::string& log) {
538 log_manager_.StoreLog(log, MetricsLog::ONGOING_LOG); 573 log_manager_.StoreLog(log, MetricsLog::ONGOING_LOG);
539 } 574 }
540 575
576 void MetricsService::AddPersistentMemorySegment(
577 base::PersistentMemoryAllocator* allocator) {
578 DCHECK(allocators_.find(allocator) == allocators_.end());
579 allocators_.insert(allocator);
580 }
581
582 void MetricsService::RemovePersistentMemorySegment(
583 base::PersistentMemoryAllocator* allocator) {
584 DCHECK(allocators_.find(allocator) != allocators_.end());
585 allocators_.erase(allocator);
586 }
587
541 //------------------------------------------------------------------------------ 588 //------------------------------------------------------------------------------
542 // private methods 589 // private methods
543 //------------------------------------------------------------------------------ 590 //------------------------------------------------------------------------------
544 591
545 592
546 //------------------------------------------------------------------------------ 593 //------------------------------------------------------------------------------
547 // Initialization methods 594 // Initialization methods
548 595
549 void MetricsService::InitializeMetricsState() { 596 void MetricsService::InitializeMetricsState() {
550 const int64 buildtime = MetricsLog::GetBuildTime(); 597 const int64 buildtime = MetricsLog::GetBuildTime();
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after
730 777
731 // TODO(jar): Integrate bounds on log recording more consistently, so that we 778 // TODO(jar): Integrate bounds on log recording more consistently, so that we
732 // can stop recording logs that are too big much sooner. 779 // can stop recording logs that are too big much sooner.
733 if (log_manager_.current_log()->num_events() > kEventLimit) { 780 if (log_manager_.current_log()->num_events() > kEventLimit) {
734 UMA_HISTOGRAM_COUNTS("UMA.Discarded Log Events", 781 UMA_HISTOGRAM_COUNTS("UMA.Discarded Log Events",
735 log_manager_.current_log()->num_events()); 782 log_manager_.current_log()->num_events());
736 log_manager_.DiscardCurrentLog(); 783 log_manager_.DiscardCurrentLog();
737 OpenNewLog(); // Start trivial log to hold our histograms. 784 OpenNewLog(); // Start trivial log to hold our histograms.
738 } 785 }
739 786
787 base::PersistentMemoryAllocator* allocator =
788 base::GetPersistentHistogramMemoryAllocator();
789 if (allocator)
790 allocator->UpdateStaticHistograms();
791
740 // Put incremental data (histogram deltas, and realtime stats deltas) at the 792 // Put incremental data (histogram deltas, and realtime stats deltas) at the
741 // end of all log transmissions (initial log handles this separately). 793 // end of all log transmissions (initial log handles this separately).
742 // RecordIncrementalStabilityElements only exists on the derived 794 // RecordIncrementalStabilityElements only exists on the derived
743 // MetricsLog class. 795 // MetricsLog class.
744 MetricsLog* current_log = log_manager_.current_log(); 796 MetricsLog* current_log = log_manager_.current_log();
745 DCHECK(current_log); 797 DCHECK(current_log);
746 RecordCurrentEnvironment(current_log); 798 RecordCurrentEnvironment(current_log);
747 base::TimeDelta incremental_uptime; 799 base::TimeDelta incremental_uptime;
748 base::TimeDelta uptime; 800 base::TimeDelta uptime;
749 GetUptimes(local_state_, &incremental_uptime, &uptime); 801 GetUptimes(local_state_, &incremental_uptime, &uptime);
(...skipping 340 matching lines...) Expand 10 before | Expand all | Expand 10 after
1090 void MetricsService::RecordCurrentEnvironment(MetricsLog* log) { 1142 void MetricsService::RecordCurrentEnvironment(MetricsLog* log) {
1091 std::vector<variations::ActiveGroupId> synthetic_trials; 1143 std::vector<variations::ActiveGroupId> synthetic_trials;
1092 GetSyntheticFieldTrialsOlderThan(log->creation_time(), &synthetic_trials); 1144 GetSyntheticFieldTrialsOlderThan(log->creation_time(), &synthetic_trials);
1093 log->RecordEnvironment(metrics_providers_.get(), synthetic_trials, 1145 log->RecordEnvironment(metrics_providers_.get(), synthetic_trials,
1094 GetInstallDate(), GetMetricsReportingEnabledDate()); 1146 GetInstallDate(), GetMetricsReportingEnabledDate());
1095 } 1147 }
1096 1148
1097 void MetricsService::RecordCurrentHistograms() { 1149 void MetricsService::RecordCurrentHistograms() {
1098 DCHECK(log_manager_.current_log()); 1150 DCHECK(log_manager_.current_log());
1099 histogram_snapshot_manager_.PrepareDeltas( 1151 histogram_snapshot_manager_.PrepareDeltas(
1152 base::StatisticsRecorder::begin(true), base::StatisticsRecorder::end(),
1153 base::Histogram::kNoFlags, base::Histogram::kUmaTargetedHistogramFlag);
1154 histogram_snapshot_manager_.PrepareDeltas(
1155 persistent_begin(), persistent_end(),
1100 base::Histogram::kNoFlags, base::Histogram::kUmaTargetedHistogramFlag); 1156 base::Histogram::kNoFlags, base::Histogram::kUmaTargetedHistogramFlag);
1101 } 1157 }
1102 1158
1103 void MetricsService::RecordCurrentStabilityHistograms() { 1159 void MetricsService::RecordCurrentStabilityHistograms() {
1104 DCHECK(log_manager_.current_log()); 1160 DCHECK(log_manager_.current_log());
1105 histogram_snapshot_manager_.PrepareDeltas( 1161 histogram_snapshot_manager_.PrepareDeltas(
1162 base::StatisticsRecorder::begin(true), base::StatisticsRecorder::end(),
1106 base::Histogram::kNoFlags, base::Histogram::kUmaStabilityHistogramFlag); 1163 base::Histogram::kNoFlags, base::Histogram::kUmaStabilityHistogramFlag);
1107 } 1164 }
1108 1165
1109 void MetricsService::LogCleanShutdown() { 1166 void MetricsService::LogCleanShutdown() {
1110 // Redundant setting to assure that we always reset this value at shutdown 1167 // Redundant setting to assure that we always reset this value at shutdown
1111 // (and that we don't use some alternate path, and not call LogCleanShutdown). 1168 // (and that we don't use some alternate path, and not call LogCleanShutdown).
1112 clean_shutdown_status_ = CLEANLY_SHUTDOWN; 1169 clean_shutdown_status_ = CLEANLY_SHUTDOWN;
1113 1170
1114 clean_exit_beacon_.WriteBeaconValue(true); 1171 clean_exit_beacon_.WriteBeaconValue(true);
1115 RecordCurrentState(local_state_); 1172 RecordCurrentState(local_state_);
(...skipping 18 matching lines...) Expand all
1134 pref->SetInt64(prefs::kStabilityLastTimestampSec, 1191 pref->SetInt64(prefs::kStabilityLastTimestampSec,
1135 base::Time::Now().ToTimeT()); 1192 base::Time::Now().ToTimeT());
1136 } 1193 }
1137 1194
1138 void MetricsService::SkipAndDiscardUpload() { 1195 void MetricsService::SkipAndDiscardUpload() {
1139 log_manager_.DiscardStagedLog(); 1196 log_manager_.DiscardStagedLog();
1140 scheduler_->UploadCancelled(); 1197 scheduler_->UploadCancelled();
1141 log_upload_in_progress_ = false; 1198 log_upload_in_progress_ = false;
1142 } 1199 }
1143 1200
1201 MetricsService::PersistentHistogramIterator MetricsService::persistent_begin() {
1202 return PersistentHistogramIterator(allocators_, allocators_.begin());
1203 }
1204
1205 MetricsService::PersistentHistogramIterator MetricsService::persistent_end() {
1206 return PersistentHistogramIterator(allocators_, allocators_.end());
1207 }
1208
1144 } // namespace metrics 1209 } // namespace metrics
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698