| 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 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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/browser_process.h" | 184 #include "chrome/browser/browser_process.h" |
| 185 #include "chrome/browser/chrome_notification_types.h" | 185 #include "chrome/browser/chrome_notification_types.h" |
| 186 #include "chrome/browser/io_thread.h" | 186 #include "chrome/browser/io_thread.h" |
| 187 #include "chrome/browser/metrics/chrome_stability_metrics_provider.h" | |
| 188 #include "chrome/browser/metrics/gpu_metrics_provider.h" | |
| 189 #include "chrome/browser/metrics/metrics_log.h" | 187 #include "chrome/browser/metrics/metrics_log.h" |
| 190 #include "chrome/browser/metrics/metrics_state_manager.h" | 188 #include "chrome/browser/metrics/metrics_state_manager.h" |
| 191 #include "chrome/browser/metrics/network_metrics_provider.h" | |
| 192 #include "chrome/browser/metrics/omnibox_metrics_provider.h" | |
| 193 #include "chrome/browser/metrics/profiler_metrics_provider.h" | |
| 194 #include "chrome/browser/metrics/tracking_synchronizer.h" | |
| 195 #include "chrome/common/pref_names.h" | 189 #include "chrome/common/pref_names.h" |
| 196 #include "chrome/common/variations/variations_util.h" | 190 #include "chrome/common/variations/variations_util.h" |
| 197 #include "components/metrics/metrics_log_base.h" | 191 #include "components/metrics/metrics_log_base.h" |
| 198 #include "components/metrics/metrics_log_manager.h" | 192 #include "components/metrics/metrics_log_manager.h" |
| 199 #include "components/metrics/metrics_log_uploader.h" | 193 #include "components/metrics/metrics_log_uploader.h" |
| 200 #include "components/metrics/metrics_pref_names.h" | 194 #include "components/metrics/metrics_pref_names.h" |
| 201 #include "components/metrics/metrics_reporting_scheduler.h" | 195 #include "components/metrics/metrics_reporting_scheduler.h" |
| 202 #include "components/metrics/metrics_service_client.h" | 196 #include "components/metrics/metrics_service_client.h" |
| 203 #include "components/variations/entropy_provider.h" | 197 #include "components/variations/entropy_provider.h" |
| 204 | 198 |
| 205 #if defined(ENABLE_PLUGINS) | |
| 206 // TODO(asvitkine): Move this out of MetricsService. | |
| 207 #include "chrome/browser/metrics/plugin_metrics_provider.h" | |
| 208 #endif | |
| 209 | |
| 210 #if defined(OS_WIN) | |
| 211 #include "chrome/browser/metrics/google_update_metrics_provider_win.h" | |
| 212 #endif | |
| 213 | |
| 214 #if defined(OS_ANDROID) | |
| 215 // TODO(asvitkine): Move this out of MetricsService. | |
| 216 #include "chrome/browser/metrics/android_metrics_provider.h" | |
| 217 #endif | |
| 218 | |
| 219 using base::Time; | 199 using base::Time; |
| 220 using metrics::MetricsLogManager; | 200 using metrics::MetricsLogManager; |
| 221 | 201 |
| 222 namespace { | 202 namespace { |
| 223 | 203 |
| 224 // Check to see that we're being called on only one thread. | 204 // Check to see that we're being called on only one thread. |
| 225 bool IsSingleThreaded() { | 205 bool IsSingleThreaded() { |
| 226 static base::PlatformThreadId thread_id = 0; | 206 static base::PlatformThreadId thread_id = 0; |
| 227 if (!thread_id) | 207 if (!thread_id) |
| 228 thread_id = base::PlatformThread::CurrentId(); | 208 thread_id = base::PlatformThread::CurrentId(); |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 335 std::string()); | 315 std::string()); |
| 336 | 316 |
| 337 registry->RegisterListPref(metrics::prefs::kMetricsInitialLogs); | 317 registry->RegisterListPref(metrics::prefs::kMetricsInitialLogs); |
| 338 registry->RegisterListPref(metrics::prefs::kMetricsOngoingLogs); | 318 registry->RegisterListPref(metrics::prefs::kMetricsOngoingLogs); |
| 339 | 319 |
| 340 registry->RegisterInt64Pref(prefs::kInstallDate, 0); | 320 registry->RegisterInt64Pref(prefs::kInstallDate, 0); |
| 341 registry->RegisterInt64Pref(prefs::kUninstallLaunchCount, 0); | 321 registry->RegisterInt64Pref(prefs::kUninstallLaunchCount, 0); |
| 342 registry->RegisterInt64Pref(prefs::kUninstallMetricsUptimeSec, 0); | 322 registry->RegisterInt64Pref(prefs::kUninstallMetricsUptimeSec, 0); |
| 343 registry->RegisterInt64Pref(prefs::kUninstallLastLaunchTimeSec, 0); | 323 registry->RegisterInt64Pref(prefs::kUninstallLastLaunchTimeSec, 0); |
| 344 registry->RegisterInt64Pref(prefs::kUninstallLastObservedRunTimeSec, 0); | 324 registry->RegisterInt64Pref(prefs::kUninstallLastObservedRunTimeSec, 0); |
| 345 | |
| 346 // TODO(asvitkine): Move this out of here. | |
| 347 ChromeStabilityMetricsProvider::RegisterPrefs(registry); | |
| 348 | |
| 349 #if defined(OS_ANDROID) | |
| 350 // TODO(asvitkine): Move this out of here. | |
| 351 AndroidMetricsProvider::RegisterPrefs(registry); | |
| 352 #endif // defined(OS_ANDROID) | |
| 353 | |
| 354 #if defined(ENABLE_PLUGINS) | |
| 355 // TODO(asvitkine): Move this out of here. | |
| 356 PluginMetricsProvider::RegisterPrefs(registry); | |
| 357 #endif | |
| 358 } | 325 } |
| 359 | 326 |
| 360 MetricsService::MetricsService(metrics::MetricsStateManager* state_manager, | 327 MetricsService::MetricsService(metrics::MetricsStateManager* state_manager, |
| 361 metrics::MetricsServiceClient* client, | 328 metrics::MetricsServiceClient* client, |
| 362 PrefService* local_state) | 329 PrefService* local_state) |
| 363 : log_manager_(local_state, kUploadLogAvoidRetransmitSize), | 330 : log_manager_(local_state, kUploadLogAvoidRetransmitSize), |
| 364 histogram_snapshot_manager_(this), | 331 histogram_snapshot_manager_(this), |
| 365 state_manager_(state_manager), | 332 state_manager_(state_manager), |
| 366 client_(client), | 333 client_(client), |
| 367 local_state_(local_state), | 334 local_state_(local_state), |
| 368 recording_active_(false), | 335 recording_active_(false), |
| 369 reporting_active_(false), | 336 reporting_active_(false), |
| 370 test_mode_active_(false), | 337 test_mode_active_(false), |
| 371 state_(INITIALIZED), | 338 state_(INITIALIZED), |
| 372 has_initial_stability_log_(false), | 339 has_initial_stability_log_(false), |
| 373 log_upload_in_progress_(false), | 340 log_upload_in_progress_(false), |
| 374 idle_since_last_transmission_(false), | 341 idle_since_last_transmission_(false), |
| 375 session_id_(-1), | 342 session_id_(-1), |
| 376 self_ptr_factory_(this), | 343 self_ptr_factory_(this), |
| 377 state_saver_factory_(this) { | 344 state_saver_factory_(this) { |
| 378 DCHECK(IsSingleThreaded()); | 345 DCHECK(IsSingleThreaded()); |
| 379 DCHECK(state_manager_); | 346 DCHECK(state_manager_); |
| 380 DCHECK(client_); | 347 DCHECK(client_); |
| 381 DCHECK(local_state_); | 348 DCHECK(local_state_); |
| 382 | |
| 383 #if defined(OS_ANDROID) | |
| 384 // TODO(asvitkine): Move this out of MetricsService. | |
| 385 RegisterMetricsProvider( | |
| 386 scoped_ptr<metrics::MetricsProvider>(new AndroidMetricsProvider( | |
| 387 local_state_))); | |
| 388 #endif // defined(OS_ANDROID) | |
| 389 | |
| 390 // TODO(asvitkine): Move these out of MetricsService. | |
| 391 RegisterMetricsProvider( | |
| 392 scoped_ptr<metrics::MetricsProvider>(new NetworkMetricsProvider)); | |
| 393 RegisterMetricsProvider( | |
| 394 scoped_ptr<metrics::MetricsProvider>(new OmniboxMetricsProvider)); | |
| 395 RegisterMetricsProvider( | |
| 396 scoped_ptr<metrics::MetricsProvider>(new ChromeStabilityMetricsProvider)); | |
| 397 RegisterMetricsProvider( | |
| 398 scoped_ptr<metrics::MetricsProvider>(new GPUMetricsProvider())); | |
| 399 profiler_metrics_provider_ = new ProfilerMetricsProvider; | |
| 400 RegisterMetricsProvider( | |
| 401 scoped_ptr<metrics::MetricsProvider>(profiler_metrics_provider_)); | |
| 402 | |
| 403 #if defined(OS_WIN) | |
| 404 google_update_metrics_provider_ = new GoogleUpdateMetricsProviderWin; | |
| 405 RegisterMetricsProvider(scoped_ptr<metrics::MetricsProvider>( | |
| 406 google_update_metrics_provider_)); | |
| 407 #endif | |
| 408 | |
| 409 #if defined(ENABLE_PLUGINS) | |
| 410 plugin_metrics_provider_ = new PluginMetricsProvider(local_state_); | |
| 411 RegisterMetricsProvider(scoped_ptr<metrics::MetricsProvider>( | |
| 412 plugin_metrics_provider_)); | |
| 413 #endif | |
| 414 | |
| 415 } | 349 } |
| 416 | 350 |
| 417 MetricsService::~MetricsService() { | 351 MetricsService::~MetricsService() { |
| 418 DisableRecording(); | 352 DisableRecording(); |
| 419 } | 353 } |
| 420 | 354 |
| 421 void MetricsService::InitializeMetricsRecordingState() { | 355 void MetricsService::InitializeMetricsRecordingState() { |
| 422 InitializeMetricsState(); | 356 InitializeMetricsState(); |
| 423 | 357 |
| 424 base::Closure callback = base::Bind(&MetricsService::StartScheduledUpload, | 358 base::Closure callback = base::Bind(&MetricsService::StartScheduledUpload, |
| (...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 682 local_state_->SetInt64(prefs::kStabilityLaunchTimeSec, Time::Now().ToTimeT()); | 616 local_state_->SetInt64(prefs::kStabilityLaunchTimeSec, Time::Now().ToTimeT()); |
| 683 | 617 |
| 684 // Bookkeeping for the uninstall metrics. | 618 // Bookkeeping for the uninstall metrics. |
| 685 IncrementLongPrefsValue(prefs::kUninstallLaunchCount); | 619 IncrementLongPrefsValue(prefs::kUninstallLaunchCount); |
| 686 | 620 |
| 687 // Kick off the process of saving the state (so the uptime numbers keep | 621 // Kick off the process of saving the state (so the uptime numbers keep |
| 688 // getting updated) every n minutes. | 622 // getting updated) every n minutes. |
| 689 ScheduleNextStateSave(); | 623 ScheduleNextStateSave(); |
| 690 } | 624 } |
| 691 | 625 |
| 692 void MetricsService::OnInitTaskGotHardwareClass() { | |
| 693 DCHECK_EQ(INIT_TASK_SCHEDULED, state_); | |
| 694 | |
| 695 const base::Closure got_plugin_info_callback = | |
| 696 base::Bind(&MetricsService::OnInitTaskGotPluginInfo, | |
| 697 self_ptr_factory_.GetWeakPtr()); | |
| 698 | |
| 699 #if defined(ENABLE_PLUGINS) | |
| 700 plugin_metrics_provider_->GetPluginInformation(got_plugin_info_callback); | |
| 701 #else | |
| 702 got_plugin_info_callback.Run(); | |
| 703 #endif | |
| 704 } | |
| 705 | |
| 706 void MetricsService::OnInitTaskGotPluginInfo() { | |
| 707 DCHECK_EQ(INIT_TASK_SCHEDULED, state_); | |
| 708 | |
| 709 const base::Closure got_metrics_callback = | |
| 710 base::Bind(&MetricsService::OnInitTaskGotGoogleUpdateData, | |
| 711 self_ptr_factory_.GetWeakPtr()); | |
| 712 | |
| 713 #if defined(OS_WIN) && defined(GOOGLE_CHROME_BUILD) | |
| 714 google_update_metrics_provider_->GetGoogleUpdateData(got_metrics_callback); | |
| 715 #else | |
| 716 got_metrics_callback.Run(); | |
| 717 #endif | |
| 718 } | |
| 719 | |
| 720 void MetricsService::OnInitTaskGotGoogleUpdateData() { | |
| 721 DCHECK_EQ(INIT_TASK_SCHEDULED, state_); | |
| 722 | |
| 723 // Start the next part of the init task: fetching performance data. This will | |
| 724 // call into |FinishedReceivingProfilerData()| when the task completes. | |
| 725 chrome_browser_metrics::TrackingSynchronizer::FetchProfilerDataAsynchronously( | |
| 726 self_ptr_factory_.GetWeakPtr()); | |
| 727 } | |
| 728 | |
| 729 void MetricsService::OnUserAction(const std::string& action) { | 626 void MetricsService::OnUserAction(const std::string& action) { |
| 730 if (!ShouldLogEvents()) | 627 if (!ShouldLogEvents()) |
| 731 return; | 628 return; |
| 732 | 629 |
| 733 log_manager_.current_log()->RecordUserAction(action); | 630 log_manager_.current_log()->RecordUserAction(action); |
| 734 HandleIdleSinceLastTransmission(false); | 631 HandleIdleSinceLastTransmission(false); |
| 735 } | 632 } |
| 736 | 633 |
| 737 void MetricsService::ReceivedProfilerData( | 634 void MetricsService::FinishedGatheringInitialMetrics() { |
| 738 const tracked_objects::ProcessDataSnapshot& process_data, | |
| 739 int process_type) { | |
| 740 DCHECK_EQ(INIT_TASK_SCHEDULED, state_); | |
| 741 | |
| 742 profiler_metrics_provider_->RecordProfilerData(process_data, process_type); | |
| 743 } | |
| 744 | |
| 745 void MetricsService::FinishedReceivingProfilerData() { | |
| 746 DCHECK_EQ(INIT_TASK_SCHEDULED, state_); | 635 DCHECK_EQ(INIT_TASK_SCHEDULED, state_); |
| 747 state_ = INIT_TASK_DONE; | 636 state_ = INIT_TASK_DONE; |
| 748 | 637 |
| 749 // Create the initial log. | 638 // Create the initial log. |
| 750 DCHECK(!initial_metrics_log_.get()); | 639 DCHECK(!initial_metrics_log_.get()); |
| 751 initial_metrics_log_ = CreateLog(MetricsLog::ONGOING_LOG); | 640 initial_metrics_log_ = CreateLog(MetricsLog::ONGOING_LOG); |
| 752 NotifyOnDidCreateMetricsLog(); | 641 NotifyOnDidCreateMetricsLog(); |
| 753 | 642 |
| 754 scheduler_->InitTaskComplete(); | 643 scheduler_->InitTaskComplete(); |
| 755 } | 644 } |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 830 content::BrowserThread::PostDelayedTask( | 719 content::BrowserThread::PostDelayedTask( |
| 831 content::BrowserThread::UI, | 720 content::BrowserThread::UI, |
| 832 FROM_HERE, | 721 FROM_HERE, |
| 833 base::Bind(&MetricsService::StartGatheringMetrics, | 722 base::Bind(&MetricsService::StartGatheringMetrics, |
| 834 self_ptr_factory_.GetWeakPtr()), | 723 self_ptr_factory_.GetWeakPtr()), |
| 835 base::TimeDelta::FromSeconds(kInitializationDelaySeconds)); | 724 base::TimeDelta::FromSeconds(kInitializationDelaySeconds)); |
| 836 } | 725 } |
| 837 } | 726 } |
| 838 | 727 |
| 839 void MetricsService::StartGatheringMetrics() { | 728 void MetricsService::StartGatheringMetrics() { |
| 840 // TODO(blundell): Move all initial metrics gathering to | |
| 841 // ChromeMetricsServiceClient. | |
| 842 client_->StartGatheringMetrics( | 729 client_->StartGatheringMetrics( |
| 843 base::Bind(&MetricsService::OnInitTaskGotHardwareClass, | 730 base::Bind(&MetricsService::FinishedGatheringInitialMetrics, |
| 844 self_ptr_factory_.GetWeakPtr())); | 731 self_ptr_factory_.GetWeakPtr())); |
| 845 } | 732 } |
| 846 | 733 |
| 847 void MetricsService::CloseCurrentLog() { | 734 void MetricsService::CloseCurrentLog() { |
| 848 if (!log_manager_.current_log()) | 735 if (!log_manager_.current_log()) |
| 849 return; | 736 return; |
| 850 | 737 |
| 851 // TODO(jar): Integrate bounds on log recording more consistently, so that we | 738 // TODO(jar): Integrate bounds on log recording more consistently, so that we |
| 852 // can stop recording logs that are too big much sooner. | 739 // can stop recording logs that are too big much sooner. |
| 853 if (log_manager_.current_log()->num_events() > kEventLimit) { | 740 if (log_manager_.current_log()->num_events() > kEventLimit) { |
| (...skipping 453 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1307 | 1194 |
| 1308 // Redundant setting to assure that we always reset this value at shutdown | 1195 // Redundant setting to assure that we always reset this value at shutdown |
| 1309 // (and that we don't use some alternate path, and not call LogCleanShutdown). | 1196 // (and that we don't use some alternate path, and not call LogCleanShutdown). |
| 1310 clean_shutdown_status_ = CLEANLY_SHUTDOWN; | 1197 clean_shutdown_status_ = CLEANLY_SHUTDOWN; |
| 1311 | 1198 |
| 1312 RecordBooleanPrefValue(prefs::kStabilityExitedCleanly, true); | 1199 RecordBooleanPrefValue(prefs::kStabilityExitedCleanly, true); |
| 1313 local_state_->SetInteger(prefs::kStabilityExecutionPhase, | 1200 local_state_->SetInteger(prefs::kStabilityExecutionPhase, |
| 1314 MetricsService::SHUTDOWN_COMPLETE); | 1201 MetricsService::SHUTDOWN_COMPLETE); |
| 1315 } | 1202 } |
| 1316 | 1203 |
| 1317 void MetricsService::LogPluginLoadingError(const base::FilePath& plugin_path) { | |
| 1318 #if defined(ENABLE_PLUGINS) | |
| 1319 plugin_metrics_provider_->LogPluginLoadingError(plugin_path); | |
| 1320 #endif | |
| 1321 } | |
| 1322 | |
| 1323 bool MetricsService::ShouldLogEvents() { | 1204 bool MetricsService::ShouldLogEvents() { |
| 1324 // We simply don't log events to UMA if there is a single incognito | 1205 // We simply don't log events to UMA if there is a single incognito |
| 1325 // session visible. The problem is that we always notify using the orginal | 1206 // session visible. The problem is that we always notify using the orginal |
| 1326 // profile in order to simplify notification processing. | 1207 // profile in order to simplify notification processing. |
| 1327 return !client_->IsOffTheRecordSessionActive(); | 1208 return !client_->IsOffTheRecordSessionActive(); |
| 1328 } | 1209 } |
| 1329 | 1210 |
| 1330 void MetricsService::RecordBooleanPrefValue(const char* path, bool value) { | 1211 void MetricsService::RecordBooleanPrefValue(const char* path, bool value) { |
| 1331 DCHECK(IsSingleThreaded()); | 1212 DCHECK(IsSingleThreaded()); |
| 1332 local_state_->SetBoolean(path, value); | 1213 local_state_->SetBoolean(path, value); |
| 1333 RecordCurrentState(local_state_); | 1214 RecordCurrentState(local_state_); |
| 1334 } | 1215 } |
| 1335 | 1216 |
| 1336 void MetricsService::RecordCurrentState(PrefService* pref) { | 1217 void MetricsService::RecordCurrentState(PrefService* pref) { |
| 1337 pref->SetInt64(prefs::kStabilityLastTimestampSec, Time::Now().ToTimeT()); | 1218 pref->SetInt64(prefs::kStabilityLastTimestampSec, Time::Now().ToTimeT()); |
| 1338 | 1219 |
| 1339 for (size_t i = 0; i < metrics_providers_.size(); ++i) | 1220 for (size_t i = 0; i < metrics_providers_.size(); ++i) |
| 1340 metrics_providers_[i]->RecordCurrentState(); | 1221 metrics_providers_[i]->RecordCurrentState(); |
| 1341 } | 1222 } |
| OLD | NEW |