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