| 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 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 201 #include "components/metrics/metrics_service_client.h" | 201 #include "components/metrics/metrics_service_client.h" |
| 202 #include "components/variations/entropy_provider.h" | 202 #include "components/variations/entropy_provider.h" |
| 203 #include "net/base/load_flags.h" | 203 #include "net/base/load_flags.h" |
| 204 #include "net/url_request/url_fetcher.h" | 204 #include "net/url_request/url_fetcher.h" |
| 205 | 205 |
| 206 #if defined(ENABLE_PLUGINS) | 206 #if defined(ENABLE_PLUGINS) |
| 207 // TODO(asvitkine): Move this out of MetricsService. | 207 // TODO(asvitkine): Move this out of MetricsService. |
| 208 #include "chrome/browser/metrics/plugin_metrics_provider.h" | 208 #include "chrome/browser/metrics/plugin_metrics_provider.h" |
| 209 #endif | 209 #endif |
| 210 | 210 |
| 211 #if defined(OS_CHROMEOS) | |
| 212 #include "chrome/browser/chromeos/settings/cros_settings.h" | |
| 213 #include "chrome/browser/metrics/chromeos_metrics_provider.h" | |
| 214 #include "chromeos/system/statistics_provider.h" | |
| 215 #endif | |
| 216 | |
| 217 #if defined(OS_WIN) | 211 #if defined(OS_WIN) |
| 218 #include "chrome/browser/metrics/google_update_metrics_provider_win.h" | 212 #include "chrome/browser/metrics/google_update_metrics_provider_win.h" |
| 219 #endif | 213 #endif |
| 220 | 214 |
| 221 #if defined(OS_ANDROID) | 215 #if defined(OS_ANDROID) |
| 222 // TODO(asvitkine): Move this out of MetricsService. | 216 // TODO(asvitkine): Move this out of MetricsService. |
| 223 #include "chrome/browser/metrics/android_metrics_provider.h" | 217 #include "chrome/browser/metrics/android_metrics_provider.h" |
| 224 #endif | 218 #endif |
| 225 | 219 |
| 226 using base::Time; | 220 using base::Time; |
| 227 using content::BrowserThread; | |
| 228 using metrics::MetricsLogManager; | 221 using metrics::MetricsLogManager; |
| 229 | 222 |
| 230 namespace { | 223 namespace { |
| 231 | 224 |
| 232 // Check to see that we're being called on only one thread. | 225 // Check to see that we're being called on only one thread. |
| 233 bool IsSingleThreaded() { | 226 bool IsSingleThreaded() { |
| 234 static base::PlatformThreadId thread_id = 0; | 227 static base::PlatformThreadId thread_id = 0; |
| 235 if (!thread_id) | 228 if (!thread_id) |
| 236 thread_id = base::PlatformThread::CurrentId(); | 229 thread_id = base::PlatformThread::CurrentId(); |
| 237 return base::PlatformThread::CurrentId() == thread_id; | 230 return base::PlatformThread::CurrentId() == thread_id; |
| (...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 410 RegisterMetricsProvider(scoped_ptr<metrics::MetricsProvider>( | 403 RegisterMetricsProvider(scoped_ptr<metrics::MetricsProvider>( |
| 411 google_update_metrics_provider_)); | 404 google_update_metrics_provider_)); |
| 412 #endif | 405 #endif |
| 413 | 406 |
| 414 #if defined(ENABLE_PLUGINS) | 407 #if defined(ENABLE_PLUGINS) |
| 415 plugin_metrics_provider_ = new PluginMetricsProvider(local_state_); | 408 plugin_metrics_provider_ = new PluginMetricsProvider(local_state_); |
| 416 RegisterMetricsProvider(scoped_ptr<metrics::MetricsProvider>( | 409 RegisterMetricsProvider(scoped_ptr<metrics::MetricsProvider>( |
| 417 plugin_metrics_provider_)); | 410 plugin_metrics_provider_)); |
| 418 #endif | 411 #endif |
| 419 | 412 |
| 420 #if defined(OS_CHROMEOS) | |
| 421 RegisterMetricsProvider( | |
| 422 scoped_ptr<metrics::MetricsProvider>(new ChromeOSMetricsProvider)); | |
| 423 #endif | |
| 424 } | 413 } |
| 425 | 414 |
| 426 MetricsService::~MetricsService() { | 415 MetricsService::~MetricsService() { |
| 427 DisableRecording(); | 416 DisableRecording(); |
| 428 } | 417 } |
| 429 | 418 |
| 430 void MetricsService::InitializeMetricsRecordingState() { | 419 void MetricsService::InitializeMetricsRecordingState() { |
| 431 InitializeMetricsState(); | 420 InitializeMetricsState(); |
| 432 | 421 |
| 433 base::Closure callback = base::Bind(&MetricsService::StartScheduledUpload, | 422 base::Closure callback = base::Bind(&MetricsService::StartScheduledUpload, |
| (...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 691 local_state_->SetInt64(prefs::kStabilityLaunchTimeSec, Time::Now().ToTimeT()); | 680 local_state_->SetInt64(prefs::kStabilityLaunchTimeSec, Time::Now().ToTimeT()); |
| 692 | 681 |
| 693 // Bookkeeping for the uninstall metrics. | 682 // Bookkeeping for the uninstall metrics. |
| 694 IncrementLongPrefsValue(prefs::kUninstallLaunchCount); | 683 IncrementLongPrefsValue(prefs::kUninstallLaunchCount); |
| 695 | 684 |
| 696 // Kick off the process of saving the state (so the uptime numbers keep | 685 // Kick off the process of saving the state (so the uptime numbers keep |
| 697 // getting updated) every n minutes. | 686 // getting updated) every n minutes. |
| 698 ScheduleNextStateSave(); | 687 ScheduleNextStateSave(); |
| 699 } | 688 } |
| 700 | 689 |
| 701 // static | 690 void MetricsService::OnInitTaskGotHardwareClass() { |
| 702 void MetricsService::InitTaskGetHardwareClass( | |
| 703 base::WeakPtr<MetricsService> self, | |
| 704 base::MessageLoopProxy* target_loop) { | |
| 705 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 706 | |
| 707 std::string hardware_class; | |
| 708 #if defined(OS_CHROMEOS) | |
| 709 chromeos::system::StatisticsProvider::GetInstance()->GetMachineStatistic( | |
| 710 "hardware_class", &hardware_class); | |
| 711 #endif // OS_CHROMEOS | |
| 712 | |
| 713 target_loop->PostTask(FROM_HERE, | |
| 714 base::Bind(&MetricsService::OnInitTaskGotHardwareClass, | |
| 715 self, hardware_class)); | |
| 716 } | |
| 717 | |
| 718 void MetricsService::OnInitTaskGotHardwareClass( | |
| 719 const std::string& hardware_class) { | |
| 720 DCHECK_EQ(INIT_TASK_SCHEDULED, state_); | 691 DCHECK_EQ(INIT_TASK_SCHEDULED, state_); |
| 721 hardware_class_ = hardware_class; | |
| 722 | 692 |
| 723 const base::Closure got_plugin_info_callback = | 693 const base::Closure got_plugin_info_callback = |
| 724 base::Bind(&MetricsService::OnInitTaskGotPluginInfo, | 694 base::Bind(&MetricsService::OnInitTaskGotPluginInfo, |
| 725 self_ptr_factory_.GetWeakPtr()); | 695 self_ptr_factory_.GetWeakPtr()); |
| 726 | 696 |
| 727 #if defined(ENABLE_PLUGINS) | 697 #if defined(ENABLE_PLUGINS) |
| 728 plugin_metrics_provider_->GetPluginInformation(got_plugin_info_callback); | 698 plugin_metrics_provider_->GetPluginInformation(got_plugin_info_callback); |
| 729 #else | 699 #else |
| 730 got_plugin_info_callback.Run(); | 700 got_plugin_info_callback.Run(); |
| 731 #endif | 701 #endif |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 849 void MetricsService::OpenNewLog() { | 819 void MetricsService::OpenNewLog() { |
| 850 DCHECK(!log_manager_.current_log()); | 820 DCHECK(!log_manager_.current_log()); |
| 851 | 821 |
| 852 log_manager_.BeginLoggingWithLog( | 822 log_manager_.BeginLoggingWithLog( |
| 853 CreateLog(MetricsLog::ONGOING_LOG).PassAs<metrics::MetricsLogBase>()); | 823 CreateLog(MetricsLog::ONGOING_LOG).PassAs<metrics::MetricsLogBase>()); |
| 854 NotifyOnDidCreateMetricsLog(); | 824 NotifyOnDidCreateMetricsLog(); |
| 855 if (state_ == INITIALIZED) { | 825 if (state_ == INITIALIZED) { |
| 856 // We only need to schedule that run once. | 826 // We only need to schedule that run once. |
| 857 state_ = INIT_TASK_SCHEDULED; | 827 state_ = INIT_TASK_SCHEDULED; |
| 858 | 828 |
| 859 // TODO(blundell): Change the callback to be | 829 content::BrowserThread::PostDelayedTask( |
| 860 // FinishedReceivingProfilerData() when the initial metrics gathering is | 830 content::BrowserThread::UI, |
| 861 // moved to ChromeMetricsServiceClient. | |
| 862 client_->StartGatheringMetrics(base::Bind(&base::DoNothing)); | |
| 863 | |
| 864 // Schedules a task on the file thread for execution of slower | |
| 865 // initialization steps (such as plugin list generation) necessary | |
| 866 // for sending the initial log. This avoids blocking the main UI | |
| 867 // thread. | |
| 868 BrowserThread::PostDelayedTask( | |
| 869 BrowserThread::FILE, | |
| 870 FROM_HERE, | 831 FROM_HERE, |
| 871 base::Bind(&MetricsService::InitTaskGetHardwareClass, | 832 base::Bind(&MetricsService::StartGatheringMetrics, |
| 872 self_ptr_factory_.GetWeakPtr(), | 833 self_ptr_factory_.GetWeakPtr()), |
| 873 base::MessageLoop::current()->message_loop_proxy()), | |
| 874 base::TimeDelta::FromSeconds(kInitializationDelaySeconds)); | 834 base::TimeDelta::FromSeconds(kInitializationDelaySeconds)); |
| 875 } | 835 } |
| 876 } | 836 } |
| 877 | 837 |
| 838 void MetricsService::StartGatheringMetrics() { |
| 839 // TODO(blundell): Move all initial metrics gathering to |
| 840 // ChromeMetricsServiceClient. |
| 841 client_->StartGatheringMetrics( |
| 842 base::Bind(&MetricsService::OnInitTaskGotHardwareClass, |
| 843 self_ptr_factory_.GetWeakPtr())); |
| 844 } |
| 845 |
| 878 void MetricsService::CloseCurrentLog() { | 846 void MetricsService::CloseCurrentLog() { |
| 879 if (!log_manager_.current_log()) | 847 if (!log_manager_.current_log()) |
| 880 return; | 848 return; |
| 881 | 849 |
| 882 // TODO(jar): Integrate bounds on log recording more consistently, so that we | 850 // TODO(jar): Integrate bounds on log recording more consistently, so that we |
| 883 // can stop recording logs that are too big much sooner. | 851 // can stop recording logs that are too big much sooner. |
| 884 if (log_manager_.current_log()->num_events() > kEventLimit) { | 852 if (log_manager_.current_log()->num_events() > kEventLimit) { |
| 885 UMA_HISTOGRAM_COUNTS("UMA.Discarded Log Events", | 853 UMA_HISTOGRAM_COUNTS("UMA.Discarded Log Events", |
| 886 log_manager_.current_log()->num_events()); | 854 log_manager_.current_log()->num_events()); |
| 887 log_manager_.DiscardCurrentLog(); | 855 log_manager_.DiscardCurrentLog(); |
| 888 OpenNewLog(); // Start trivial log to hold our histograms. | 856 OpenNewLog(); // Start trivial log to hold our histograms. |
| 889 } | 857 } |
| 890 | 858 |
| 891 // Adds to ongoing logs. | |
| 892 log_manager_.current_log()->set_hardware_class(hardware_class_); | |
| 893 | |
| 894 // Put incremental data (histogram deltas, and realtime stats deltas) at the | 859 // Put incremental data (histogram deltas, and realtime stats deltas) at the |
| 895 // end of all log transmissions (initial log handles this separately). | 860 // end of all log transmissions (initial log handles this separately). |
| 896 // RecordIncrementalStabilityElements only exists on the derived | 861 // RecordIncrementalStabilityElements only exists on the derived |
| 897 // MetricsLog class. | 862 // MetricsLog class. |
| 898 MetricsLog* current_log = | 863 MetricsLog* current_log = |
| 899 static_cast<MetricsLog*>(log_manager_.current_log()); | 864 static_cast<MetricsLog*>(log_manager_.current_log()); |
| 900 DCHECK(current_log); | 865 DCHECK(current_log); |
| 901 std::vector<variations::ActiveGroupId> synthetic_trials; | 866 std::vector<variations::ActiveGroupId> synthetic_trials; |
| 902 GetCurrentSyntheticFieldTrials(&synthetic_trials); | 867 GetCurrentSyntheticFieldTrials(&synthetic_trials); |
| 903 current_log->RecordEnvironment(metrics_providers_.get(), synthetic_trials); | 868 current_log->RecordEnvironment(metrics_providers_.get(), synthetic_trials); |
| (...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1105 | 1070 |
| 1106 // Store unsent logs, including the stability log that was just saved, so | 1071 // Store unsent logs, including the stability log that was just saved, so |
| 1107 // that they're not lost in case of a crash before upload time. | 1072 // that they're not lost in case of a crash before upload time. |
| 1108 log_manager_.PersistUnsentLogs(); | 1073 log_manager_.PersistUnsentLogs(); |
| 1109 | 1074 |
| 1110 has_initial_stability_log_ = true; | 1075 has_initial_stability_log_ = true; |
| 1111 } | 1076 } |
| 1112 | 1077 |
| 1113 void MetricsService::PrepareInitialMetricsLog() { | 1078 void MetricsService::PrepareInitialMetricsLog() { |
| 1114 DCHECK(state_ == INIT_TASK_DONE || state_ == SENDING_INITIAL_STABILITY_LOG); | 1079 DCHECK(state_ == INIT_TASK_DONE || state_ == SENDING_INITIAL_STABILITY_LOG); |
| 1115 initial_metrics_log_->set_hardware_class(hardware_class_); | |
| 1116 | 1080 |
| 1117 std::vector<variations::ActiveGroupId> synthetic_trials; | 1081 std::vector<variations::ActiveGroupId> synthetic_trials; |
| 1118 GetCurrentSyntheticFieldTrials(&synthetic_trials); | 1082 GetCurrentSyntheticFieldTrials(&synthetic_trials); |
| 1119 initial_metrics_log_->RecordEnvironment(metrics_providers_.get(), | 1083 initial_metrics_log_->RecordEnvironment(metrics_providers_.get(), |
| 1120 synthetic_trials); | 1084 synthetic_trials); |
| 1121 base::TimeDelta incremental_uptime; | 1085 base::TimeDelta incremental_uptime; |
| 1122 base::TimeDelta uptime; | 1086 base::TimeDelta uptime; |
| 1123 GetUptimes(local_state_, &incremental_uptime, &uptime); | 1087 GetUptimes(local_state_, &incremental_uptime, &uptime); |
| 1124 | 1088 |
| 1125 // Histograms only get written to the current log, so make the new log current | 1089 // Histograms only get written to the current log, so make the new log current |
| (...skipping 284 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1410 RecordCurrentState(local_state_); | 1374 RecordCurrentState(local_state_); |
| 1411 } | 1375 } |
| 1412 | 1376 |
| 1413 void MetricsService::RecordCurrentState(PrefService* pref) { | 1377 void MetricsService::RecordCurrentState(PrefService* pref) { |
| 1414 pref->SetInt64(prefs::kStabilityLastTimestampSec, Time::Now().ToTimeT()); | 1378 pref->SetInt64(prefs::kStabilityLastTimestampSec, Time::Now().ToTimeT()); |
| 1415 | 1379 |
| 1416 #if defined(ENABLE_PLUGINS) | 1380 #if defined(ENABLE_PLUGINS) |
| 1417 plugin_metrics_provider_->RecordPluginChanges(); | 1381 plugin_metrics_provider_->RecordPluginChanges(); |
| 1418 #endif | 1382 #endif |
| 1419 } | 1383 } |
| OLD | NEW |