| OLD | NEW |
| 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 //------------------------------------------------------------------------------ | 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 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 174 #include "base/metrics/statistics_recorder.h" | 174 #include "base/metrics/statistics_recorder.h" |
| 175 #include "base/prefs/pref_registry_simple.h" | 175 #include "base/prefs/pref_registry_simple.h" |
| 176 #include "base/prefs/pref_service.h" | 176 #include "base/prefs/pref_service.h" |
| 177 #include "base/strings/string_number_conversions.h" | 177 #include "base/strings/string_number_conversions.h" |
| 178 #include "base/strings/utf_string_conversions.h" | 178 #include "base/strings/utf_string_conversions.h" |
| 179 #include "base/threading/platform_thread.h" | 179 #include "base/threading/platform_thread.h" |
| 180 #include "base/threading/thread.h" | 180 #include "base/threading/thread.h" |
| 181 #include "base/threading/thread_restrictions.h" | 181 #include "base/threading/thread_restrictions.h" |
| 182 #include "base/tracked_objects.h" | 182 #include "base/tracked_objects.h" |
| 183 #include "base/values.h" | 183 #include "base/values.h" |
| 184 #include "chrome/browser/metrics/chrome_stability_metrics_provider.h" | |
| 185 #include "chrome/browser/metrics/gpu_metrics_provider.h" | |
| 186 #include "chrome/browser/metrics/network_metrics_provider.h" | |
| 187 #include "chrome/browser/metrics/omnibox_metrics_provider.h" | |
| 188 #include "chrome/browser/metrics/profiler_metrics_provider.h" | |
| 189 #include "chrome/browser/metrics/tracking_synchronizer.h" | |
| 190 #include "chrome/common/pref_names.h" | 184 #include "chrome/common/pref_names.h" |
| 191 #include "components/metrics/metrics_log.h" | 185 #include "components/metrics/metrics_log.h" |
| 192 #include "components/metrics/metrics_log_manager.h" | 186 #include "components/metrics/metrics_log_manager.h" |
| 193 #include "components/metrics/metrics_log_uploader.h" | 187 #include "components/metrics/metrics_log_uploader.h" |
| 194 #include "components/metrics/metrics_pref_names.h" | 188 #include "components/metrics/metrics_pref_names.h" |
| 195 #include "components/metrics/metrics_reporting_scheduler.h" | 189 #include "components/metrics/metrics_reporting_scheduler.h" |
| 196 #include "components/metrics/metrics_service_client.h" | 190 #include "components/metrics/metrics_service_client.h" |
| 197 #include "components/metrics/metrics_state_manager.h" | 191 #include "components/metrics/metrics_state_manager.h" |
| 198 #include "components/variations/entropy_provider.h" | 192 #include "components/variations/entropy_provider.h" |
| 199 #include "content/public/browser/browser_thread.h" | 193 #include "content/public/browser/browser_thread.h" |
| 200 | 194 |
| 201 #if defined(ENABLE_PLUGINS) | |
| 202 // TODO(asvitkine): Move this out of MetricsService. | |
| 203 #include "chrome/browser/metrics/plugin_metrics_provider.h" | |
| 204 #endif | |
| 205 | |
| 206 #if defined(OS_WIN) | |
| 207 #include "chrome/browser/metrics/google_update_metrics_provider_win.h" | |
| 208 #endif | |
| 209 | |
| 210 #if defined(OS_ANDROID) | |
| 211 // TODO(asvitkine): Move this out of MetricsService. | |
| 212 #include "chrome/browser/metrics/android_metrics_provider.h" | |
| 213 #endif | |
| 214 | |
| 215 using base::Time; | 195 using base::Time; |
| 216 using metrics::MetricsLogManager; | 196 using metrics::MetricsLogManager; |
| 217 | 197 |
| 218 namespace { | 198 namespace { |
| 219 | 199 |
| 220 // Check to see that we're being called on only one thread. | 200 // Check to see that we're being called on only one thread. |
| 221 bool IsSingleThreaded() { | 201 bool IsSingleThreaded() { |
| 222 static base::PlatformThreadId thread_id = 0; | 202 static base::PlatformThreadId thread_id = 0; |
| 223 if (!thread_id) | 203 if (!thread_id) |
| 224 thread_id = base::PlatformThread::CurrentId(); | 204 thread_id = base::PlatformThread::CurrentId(); |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 319 registry->RegisterIntegerPref(metrics::prefs::kMetricsSessionID, -1); | 299 registry->RegisterIntegerPref(metrics::prefs::kMetricsSessionID, -1); |
| 320 | 300 |
| 321 registry->RegisterListPref(metrics::prefs::kMetricsInitialLogs); | 301 registry->RegisterListPref(metrics::prefs::kMetricsInitialLogs); |
| 322 registry->RegisterListPref(metrics::prefs::kMetricsOngoingLogs); | 302 registry->RegisterListPref(metrics::prefs::kMetricsOngoingLogs); |
| 323 | 303 |
| 324 registry->RegisterInt64Pref(prefs::kInstallDate, 0); | 304 registry->RegisterInt64Pref(prefs::kInstallDate, 0); |
| 325 registry->RegisterInt64Pref(prefs::kUninstallLaunchCount, 0); | 305 registry->RegisterInt64Pref(prefs::kUninstallLaunchCount, 0); |
| 326 registry->RegisterInt64Pref(prefs::kUninstallMetricsUptimeSec, 0); | 306 registry->RegisterInt64Pref(prefs::kUninstallMetricsUptimeSec, 0); |
| 327 registry->RegisterInt64Pref(prefs::kUninstallLastLaunchTimeSec, 0); | 307 registry->RegisterInt64Pref(prefs::kUninstallLastLaunchTimeSec, 0); |
| 328 registry->RegisterInt64Pref(prefs::kUninstallLastObservedRunTimeSec, 0); | 308 registry->RegisterInt64Pref(prefs::kUninstallLastObservedRunTimeSec, 0); |
| 329 | |
| 330 // TODO(asvitkine): Move this out of here. | |
| 331 ChromeStabilityMetricsProvider::RegisterPrefs(registry); | |
| 332 | |
| 333 #if defined(OS_ANDROID) | |
| 334 // TODO(asvitkine): Move this out of here. | |
| 335 AndroidMetricsProvider::RegisterPrefs(registry); | |
| 336 #endif // defined(OS_ANDROID) | |
| 337 | |
| 338 #if defined(ENABLE_PLUGINS) | |
| 339 // TODO(asvitkine): Move this out of here. | |
| 340 PluginMetricsProvider::RegisterPrefs(registry); | |
| 341 #endif | |
| 342 } | 309 } |
| 343 | 310 |
| 344 MetricsService::MetricsService(metrics::MetricsStateManager* state_manager, | 311 MetricsService::MetricsService(metrics::MetricsStateManager* state_manager, |
| 345 metrics::MetricsServiceClient* client, | 312 metrics::MetricsServiceClient* client, |
| 346 PrefService* local_state) | 313 PrefService* local_state) |
| 347 : log_manager_(local_state, kUploadLogAvoidRetransmitSize), | 314 : log_manager_(local_state, kUploadLogAvoidRetransmitSize), |
| 348 histogram_snapshot_manager_(this), | 315 histogram_snapshot_manager_(this), |
| 349 state_manager_(state_manager), | 316 state_manager_(state_manager), |
| 350 client_(client), | 317 client_(client), |
| 351 local_state_(local_state), | 318 local_state_(local_state), |
| 352 recording_active_(false), | 319 recording_active_(false), |
| 353 reporting_active_(false), | 320 reporting_active_(false), |
| 354 test_mode_active_(false), | 321 test_mode_active_(false), |
| 355 state_(INITIALIZED), | 322 state_(INITIALIZED), |
| 356 has_initial_stability_log_(false), | 323 has_initial_stability_log_(false), |
| 357 log_upload_in_progress_(false), | 324 log_upload_in_progress_(false), |
| 358 idle_since_last_transmission_(false), | 325 idle_since_last_transmission_(false), |
| 359 session_id_(-1), | 326 session_id_(-1), |
| 360 self_ptr_factory_(this), | 327 self_ptr_factory_(this), |
| 361 state_saver_factory_(this) { | 328 state_saver_factory_(this) { |
| 362 DCHECK(IsSingleThreaded()); | 329 DCHECK(IsSingleThreaded()); |
| 363 DCHECK(state_manager_); | 330 DCHECK(state_manager_); |
| 364 DCHECK(client_); | 331 DCHECK(client_); |
| 365 DCHECK(local_state_); | 332 DCHECK(local_state_); |
| 366 | |
| 367 #if defined(OS_ANDROID) | |
| 368 // TODO(asvitkine): Move this out of MetricsService. | |
| 369 RegisterMetricsProvider( | |
| 370 scoped_ptr<metrics::MetricsProvider>(new AndroidMetricsProvider( | |
| 371 local_state_))); | |
| 372 #endif // defined(OS_ANDROID) | |
| 373 | |
| 374 // TODO(asvitkine): Move these out of MetricsService. | |
| 375 RegisterMetricsProvider( | |
| 376 scoped_ptr<metrics::MetricsProvider>(new NetworkMetricsProvider)); | |
| 377 RegisterMetricsProvider( | |
| 378 scoped_ptr<metrics::MetricsProvider>(new OmniboxMetricsProvider)); | |
| 379 RegisterMetricsProvider( | |
| 380 scoped_ptr<metrics::MetricsProvider>(new ChromeStabilityMetricsProvider)); | |
| 381 RegisterMetricsProvider( | |
| 382 scoped_ptr<metrics::MetricsProvider>(new GPUMetricsProvider())); | |
| 383 profiler_metrics_provider_ = new ProfilerMetricsProvider; | |
| 384 RegisterMetricsProvider( | |
| 385 scoped_ptr<metrics::MetricsProvider>(profiler_metrics_provider_)); | |
| 386 | |
| 387 #if defined(OS_WIN) | |
| 388 google_update_metrics_provider_ = new GoogleUpdateMetricsProviderWin; | |
| 389 RegisterMetricsProvider(scoped_ptr<metrics::MetricsProvider>( | |
| 390 google_update_metrics_provider_)); | |
| 391 #endif | |
| 392 | |
| 393 #if defined(ENABLE_PLUGINS) | |
| 394 plugin_metrics_provider_ = new PluginMetricsProvider(local_state_); | |
| 395 RegisterMetricsProvider(scoped_ptr<metrics::MetricsProvider>( | |
| 396 plugin_metrics_provider_)); | |
| 397 #endif | |
| 398 | |
| 399 } | 333 } |
| 400 | 334 |
| 401 MetricsService::~MetricsService() { | 335 MetricsService::~MetricsService() { |
| 402 DisableRecording(); | 336 DisableRecording(); |
| 403 } | 337 } |
| 404 | 338 |
| 405 void MetricsService::InitializeMetricsRecordingState() { | 339 void MetricsService::InitializeMetricsRecordingState() { |
| 406 InitializeMetricsState(); | 340 InitializeMetricsState(); |
| 407 | 341 |
| 408 base::Closure callback = base::Bind(&MetricsService::StartScheduledUpload, | 342 base::Closure callback = base::Bind(&MetricsService::StartScheduledUpload, |
| (...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 666 local_state_->SetInt64(prefs::kStabilityLaunchTimeSec, Time::Now().ToTimeT()); | 600 local_state_->SetInt64(prefs::kStabilityLaunchTimeSec, Time::Now().ToTimeT()); |
| 667 | 601 |
| 668 // Bookkeeping for the uninstall metrics. | 602 // Bookkeeping for the uninstall metrics. |
| 669 IncrementLongPrefsValue(prefs::kUninstallLaunchCount); | 603 IncrementLongPrefsValue(prefs::kUninstallLaunchCount); |
| 670 | 604 |
| 671 // Kick off the process of saving the state (so the uptime numbers keep | 605 // Kick off the process of saving the state (so the uptime numbers keep |
| 672 // getting updated) every n minutes. | 606 // getting updated) every n minutes. |
| 673 ScheduleNextStateSave(); | 607 ScheduleNextStateSave(); |
| 674 } | 608 } |
| 675 | 609 |
| 676 void MetricsService::OnInitTaskGotHardwareClass() { | |
| 677 DCHECK_EQ(INIT_TASK_SCHEDULED, state_); | |
| 678 | |
| 679 const base::Closure got_plugin_info_callback = | |
| 680 base::Bind(&MetricsService::OnInitTaskGotPluginInfo, | |
| 681 self_ptr_factory_.GetWeakPtr()); | |
| 682 | |
| 683 #if defined(ENABLE_PLUGINS) | |
| 684 plugin_metrics_provider_->GetPluginInformation(got_plugin_info_callback); | |
| 685 #else | |
| 686 got_plugin_info_callback.Run(); | |
| 687 #endif | |
| 688 } | |
| 689 | |
| 690 void MetricsService::OnInitTaskGotPluginInfo() { | |
| 691 DCHECK_EQ(INIT_TASK_SCHEDULED, state_); | |
| 692 | |
| 693 const base::Closure got_metrics_callback = | |
| 694 base::Bind(&MetricsService::OnInitTaskGotGoogleUpdateData, | |
| 695 self_ptr_factory_.GetWeakPtr()); | |
| 696 | |
| 697 #if defined(OS_WIN) && defined(GOOGLE_CHROME_BUILD) | |
| 698 google_update_metrics_provider_->GetGoogleUpdateData(got_metrics_callback); | |
| 699 #else | |
| 700 got_metrics_callback.Run(); | |
| 701 #endif | |
| 702 } | |
| 703 | |
| 704 void MetricsService::OnInitTaskGotGoogleUpdateData() { | |
| 705 DCHECK_EQ(INIT_TASK_SCHEDULED, state_); | |
| 706 | |
| 707 // Start the next part of the init task: fetching performance data. This will | |
| 708 // call into |FinishedReceivingProfilerData()| when the task completes. | |
| 709 chrome_browser_metrics::TrackingSynchronizer::FetchProfilerDataAsynchronously( | |
| 710 self_ptr_factory_.GetWeakPtr()); | |
| 711 } | |
| 712 | |
| 713 void MetricsService::OnUserAction(const std::string& action) { | 610 void MetricsService::OnUserAction(const std::string& action) { |
| 714 if (!ShouldLogEvents()) | 611 if (!ShouldLogEvents()) |
| 715 return; | 612 return; |
| 716 | 613 |
| 717 log_manager_.current_log()->RecordUserAction(action); | 614 log_manager_.current_log()->RecordUserAction(action); |
| 718 HandleIdleSinceLastTransmission(false); | 615 HandleIdleSinceLastTransmission(false); |
| 719 } | 616 } |
| 720 | 617 |
| 721 void MetricsService::ReceivedProfilerData( | 618 void MetricsService::FinishedGatheringInitialMetrics() { |
| 722 const tracked_objects::ProcessDataSnapshot& process_data, | |
| 723 int process_type) { | |
| 724 DCHECK_EQ(INIT_TASK_SCHEDULED, state_); | |
| 725 | |
| 726 profiler_metrics_provider_->RecordProfilerData(process_data, process_type); | |
| 727 } | |
| 728 | |
| 729 void MetricsService::FinishedReceivingProfilerData() { | |
| 730 DCHECK_EQ(INIT_TASK_SCHEDULED, state_); | 619 DCHECK_EQ(INIT_TASK_SCHEDULED, state_); |
| 731 state_ = INIT_TASK_DONE; | 620 state_ = INIT_TASK_DONE; |
| 732 | 621 |
| 733 // Create the initial log. | 622 // Create the initial log. |
| 734 if (!initial_metrics_log_.get()) { | 623 if (!initial_metrics_log_.get()) { |
| 735 initial_metrics_log_ = CreateLog(MetricsLog::ONGOING_LOG); | 624 initial_metrics_log_ = CreateLog(MetricsLog::ONGOING_LOG); |
| 736 NotifyOnDidCreateMetricsLog(); | 625 NotifyOnDidCreateMetricsLog(); |
| 737 } | 626 } |
| 738 | 627 |
| 739 scheduler_->InitTaskComplete(); | 628 scheduler_->InitTaskComplete(); |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 814 content::BrowserThread::PostDelayedTask( | 703 content::BrowserThread::PostDelayedTask( |
| 815 content::BrowserThread::UI, | 704 content::BrowserThread::UI, |
| 816 FROM_HERE, | 705 FROM_HERE, |
| 817 base::Bind(&MetricsService::StartGatheringMetrics, | 706 base::Bind(&MetricsService::StartGatheringMetrics, |
| 818 self_ptr_factory_.GetWeakPtr()), | 707 self_ptr_factory_.GetWeakPtr()), |
| 819 base::TimeDelta::FromSeconds(kInitializationDelaySeconds)); | 708 base::TimeDelta::FromSeconds(kInitializationDelaySeconds)); |
| 820 } | 709 } |
| 821 } | 710 } |
| 822 | 711 |
| 823 void MetricsService::StartGatheringMetrics() { | 712 void MetricsService::StartGatheringMetrics() { |
| 824 // TODO(blundell): Move all initial metrics gathering to | |
| 825 // ChromeMetricsServiceClient. | |
| 826 client_->StartGatheringMetrics( | 713 client_->StartGatheringMetrics( |
| 827 base::Bind(&MetricsService::OnInitTaskGotHardwareClass, | 714 base::Bind(&MetricsService::FinishedGatheringInitialMetrics, |
| 828 self_ptr_factory_.GetWeakPtr())); | 715 self_ptr_factory_.GetWeakPtr())); |
| 829 } | 716 } |
| 830 | 717 |
| 831 void MetricsService::CloseCurrentLog() { | 718 void MetricsService::CloseCurrentLog() { |
| 832 if (!log_manager_.current_log()) | 719 if (!log_manager_.current_log()) |
| 833 return; | 720 return; |
| 834 | 721 |
| 835 // TODO(jar): Integrate bounds on log recording more consistently, so that we | 722 // TODO(jar): Integrate bounds on log recording more consistently, so that we |
| 836 // can stop recording logs that are too big much sooner. | 723 // can stop recording logs that are too big much sooner. |
| 837 if (log_manager_.current_log()->num_events() > kEventLimit) { | 724 if (log_manager_.current_log()->num_events() > kEventLimit) { |
| (...skipping 449 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1287 | 1174 |
| 1288 // Redundant setting to assure that we always reset this value at shutdown | 1175 // Redundant setting to assure that we always reset this value at shutdown |
| 1289 // (and that we don't use some alternate path, and not call LogCleanShutdown). | 1176 // (and that we don't use some alternate path, and not call LogCleanShutdown). |
| 1290 clean_shutdown_status_ = CLEANLY_SHUTDOWN; | 1177 clean_shutdown_status_ = CLEANLY_SHUTDOWN; |
| 1291 | 1178 |
| 1292 RecordBooleanPrefValue(prefs::kStabilityExitedCleanly, true); | 1179 RecordBooleanPrefValue(prefs::kStabilityExitedCleanly, true); |
| 1293 local_state_->SetInteger(prefs::kStabilityExecutionPhase, | 1180 local_state_->SetInteger(prefs::kStabilityExecutionPhase, |
| 1294 MetricsService::SHUTDOWN_COMPLETE); | 1181 MetricsService::SHUTDOWN_COMPLETE); |
| 1295 } | 1182 } |
| 1296 | 1183 |
| 1297 void MetricsService::LogPluginLoadingError(const base::FilePath& plugin_path) { | |
| 1298 #if defined(ENABLE_PLUGINS) | |
| 1299 plugin_metrics_provider_->LogPluginLoadingError(plugin_path); | |
| 1300 #endif | |
| 1301 } | |
| 1302 | |
| 1303 bool MetricsService::ShouldLogEvents() { | 1184 bool MetricsService::ShouldLogEvents() { |
| 1304 // We simply don't log events to UMA if there is a single incognito | 1185 // We simply don't log events to UMA if there is a single incognito |
| 1305 // session visible. The problem is that we always notify using the orginal | 1186 // session visible. The problem is that we always notify using the orginal |
| 1306 // profile in order to simplify notification processing. | 1187 // profile in order to simplify notification processing. |
| 1307 return !client_->IsOffTheRecordSessionActive(); | 1188 return !client_->IsOffTheRecordSessionActive(); |
| 1308 } | 1189 } |
| 1309 | 1190 |
| 1310 void MetricsService::RecordBooleanPrefValue(const char* path, bool value) { | 1191 void MetricsService::RecordBooleanPrefValue(const char* path, bool value) { |
| 1311 DCHECK(IsSingleThreaded()); | 1192 DCHECK(IsSingleThreaded()); |
| 1312 local_state_->SetBoolean(path, value); | 1193 local_state_->SetBoolean(path, value); |
| 1313 RecordCurrentState(local_state_); | 1194 RecordCurrentState(local_state_); |
| 1314 } | 1195 } |
| 1315 | 1196 |
| 1316 void MetricsService::RecordCurrentState(PrefService* pref) { | 1197 void MetricsService::RecordCurrentState(PrefService* pref) { |
| 1317 pref->SetInt64(prefs::kStabilityLastTimestampSec, Time::Now().ToTimeT()); | 1198 pref->SetInt64(prefs::kStabilityLastTimestampSec, Time::Now().ToTimeT()); |
| 1318 | 1199 |
| 1319 for (size_t i = 0; i < metrics_providers_.size(); ++i) | 1200 for (size_t i = 0; i < metrics_providers_.size(); ++i) |
| 1320 metrics_providers_[i]->RecordCurrentState(); | 1201 metrics_providers_[i]->RecordCurrentState(); |
| 1321 } | 1202 } |
| OLD | NEW |