| 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; |
| 238 } | 231 } |
| 239 | 232 |
| 240 // The delay, in seconds, after starting recording before doing expensive | |
| 241 // initialization work. | |
| 242 #if defined(OS_ANDROID) || defined(OS_IOS) | |
| 243 // On mobile devices, a significant portion of sessions last less than a minute. | |
| 244 // Use a shorter timer on these platforms to avoid losing data. | |
| 245 // TODO(dfalcantara): To avoid delaying startup, tighten up initialization so | |
| 246 // that it occurs after the user gets their initial page. | |
| 247 const int kInitializationDelaySeconds = 5; | |
| 248 #else | |
| 249 const int kInitializationDelaySeconds = 30; | |
| 250 #endif | |
| 251 | |
| 252 // The maximum number of events in a log uploaded to the UMA server. | 233 // The maximum number of events in a log uploaded to the UMA server. |
| 253 const int kEventLimit = 2400; | 234 const int kEventLimit = 2400; |
| 254 | 235 |
| 255 // If an upload fails, and the transmission was over this byte count, then we | 236 // If an upload fails, and the transmission was over this byte count, then we |
| 256 // will discard the log, and not try to retransmit it. We also don't persist | 237 // will discard the log, and not try to retransmit it. We also don't persist |
| 257 // the log to the prefs for transmission during the next chrome session if this | 238 // the log to the prefs for transmission during the next chrome session if this |
| 258 // limit is exceeded. | 239 // limit is exceeded. |
| 259 const size_t kUploadLogAvoidRetransmitSize = 50000; | 240 const size_t kUploadLogAvoidRetransmitSize = 50000; |
| 260 | 241 |
| 261 // Interval, in minutes, between state saves. | 242 // Interval, in minutes, between state saves. |
| (...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 411 RegisterMetricsProvider(scoped_ptr<metrics::MetricsProvider>( | 392 RegisterMetricsProvider(scoped_ptr<metrics::MetricsProvider>( |
| 412 google_update_metrics_provider_)); | 393 google_update_metrics_provider_)); |
| 413 #endif | 394 #endif |
| 414 | 395 |
| 415 #if defined(ENABLE_PLUGINS) | 396 #if defined(ENABLE_PLUGINS) |
| 416 plugin_metrics_provider_ = new PluginMetricsProvider(local_state_); | 397 plugin_metrics_provider_ = new PluginMetricsProvider(local_state_); |
| 417 RegisterMetricsProvider(scoped_ptr<metrics::MetricsProvider>( | 398 RegisterMetricsProvider(scoped_ptr<metrics::MetricsProvider>( |
| 418 plugin_metrics_provider_)); | 399 plugin_metrics_provider_)); |
| 419 #endif | 400 #endif |
| 420 | 401 |
| 421 #if defined(OS_CHROMEOS) | |
| 422 RegisterMetricsProvider( | |
| 423 scoped_ptr<metrics::MetricsProvider>(new ChromeOSMetricsProvider)); | |
| 424 #endif | |
| 425 } | 402 } |
| 426 | 403 |
| 427 MetricsService::~MetricsService() { | 404 MetricsService::~MetricsService() { |
| 428 DisableRecording(); | 405 DisableRecording(); |
| 429 } | 406 } |
| 430 | 407 |
| 431 void MetricsService::InitializeMetricsRecordingState() { | 408 void MetricsService::InitializeMetricsRecordingState() { |
| 432 InitializeMetricsState(); | 409 InitializeMetricsState(); |
| 433 | 410 |
| 434 base::Closure callback = base::Bind(&MetricsService::StartScheduledUpload, | 411 base::Closure callback = base::Bind(&MetricsService::StartScheduledUpload, |
| (...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 692 local_state_->SetInt64(prefs::kStabilityLaunchTimeSec, Time::Now().ToTimeT()); | 669 local_state_->SetInt64(prefs::kStabilityLaunchTimeSec, Time::Now().ToTimeT()); |
| 693 | 670 |
| 694 // Bookkeeping for the uninstall metrics. | 671 // Bookkeeping for the uninstall metrics. |
| 695 IncrementLongPrefsValue(prefs::kUninstallLaunchCount); | 672 IncrementLongPrefsValue(prefs::kUninstallLaunchCount); |
| 696 | 673 |
| 697 // Kick off the process of saving the state (so the uptime numbers keep | 674 // Kick off the process of saving the state (so the uptime numbers keep |
| 698 // getting updated) every n minutes. | 675 // getting updated) every n minutes. |
| 699 ScheduleNextStateSave(); | 676 ScheduleNextStateSave(); |
| 700 } | 677 } |
| 701 | 678 |
| 702 // static | 679 void MetricsService::OnInitTaskGotHardwareClass() { |
| 703 void MetricsService::InitTaskGetHardwareClass( | |
| 704 base::WeakPtr<MetricsService> self, | |
| 705 base::MessageLoopProxy* target_loop) { | |
| 706 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 707 | |
| 708 std::string hardware_class; | |
| 709 #if defined(OS_CHROMEOS) | |
| 710 chromeos::system::StatisticsProvider::GetInstance()->GetMachineStatistic( | |
| 711 "hardware_class", &hardware_class); | |
| 712 #endif // OS_CHROMEOS | |
| 713 | |
| 714 target_loop->PostTask(FROM_HERE, | |
| 715 base::Bind(&MetricsService::OnInitTaskGotHardwareClass, | |
| 716 self, hardware_class)); | |
| 717 } | |
| 718 | |
| 719 void MetricsService::OnInitTaskGotHardwareClass( | |
| 720 const std::string& hardware_class) { | |
| 721 DCHECK_EQ(INIT_TASK_SCHEDULED, state_); | 680 DCHECK_EQ(INIT_TASK_SCHEDULED, state_); |
| 722 hardware_class_ = hardware_class; | |
| 723 | 681 |
| 724 const base::Closure got_plugin_info_callback = | 682 const base::Closure got_plugin_info_callback = |
| 725 base::Bind(&MetricsService::OnInitTaskGotPluginInfo, | 683 base::Bind(&MetricsService::OnInitTaskGotPluginInfo, |
| 726 self_ptr_factory_.GetWeakPtr()); | 684 self_ptr_factory_.GetWeakPtr()); |
| 727 | 685 |
| 728 #if defined(ENABLE_PLUGINS) | 686 #if defined(ENABLE_PLUGINS) |
| 729 plugin_metrics_provider_->GetPluginInformation(got_plugin_info_callback); | 687 plugin_metrics_provider_->GetPluginInformation(got_plugin_info_callback); |
| 730 #else | 688 #else |
| 731 got_plugin_info_callback.Run(); | 689 got_plugin_info_callback.Run(); |
| 732 #endif | 690 #endif |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 850 void MetricsService::OpenNewLog() { | 808 void MetricsService::OpenNewLog() { |
| 851 DCHECK(!log_manager_.current_log()); | 809 DCHECK(!log_manager_.current_log()); |
| 852 | 810 |
| 853 log_manager_.BeginLoggingWithLog( | 811 log_manager_.BeginLoggingWithLog( |
| 854 CreateLog(MetricsLog::ONGOING_LOG).PassAs<metrics::MetricsLogBase>()); | 812 CreateLog(MetricsLog::ONGOING_LOG).PassAs<metrics::MetricsLogBase>()); |
| 855 NotifyOnDidCreateMetricsLog(); | 813 NotifyOnDidCreateMetricsLog(); |
| 856 if (state_ == INITIALIZED) { | 814 if (state_ == INITIALIZED) { |
| 857 // We only need to schedule that run once. | 815 // We only need to schedule that run once. |
| 858 state_ = INIT_TASK_SCHEDULED; | 816 state_ = INIT_TASK_SCHEDULED; |
| 859 | 817 |
| 860 // TODO(blundell): Change the callback to be | 818 // TODO(blundell): Move all initial metrics gathering to |
| 861 // FinishedReceivingProfilerData() when the initial metrics gathering is | 819 // ChromeMetricsServiceClient. |
| 862 // moved to ChromeMetricsServiceClient. | 820 client_->StartGatheringMetrics( |
| 863 client_->StartGatheringMetrics(base::Bind(&base::DoNothing)); | 821 base::Bind(&MetricsService::OnInitTaskGotHardwareClass, |
| 864 | 822 self_ptr_factory_.GetWeakPtr())); |
| 865 // Schedules a task on the file thread for execution of slower | |
| 866 // initialization steps (such as plugin list generation) necessary | |
| 867 // for sending the initial log. This avoids blocking the main UI | |
| 868 // thread. | |
| 869 BrowserThread::PostDelayedTask( | |
| 870 BrowserThread::FILE, | |
| 871 FROM_HERE, | |
| 872 base::Bind(&MetricsService::InitTaskGetHardwareClass, | |
| 873 self_ptr_factory_.GetWeakPtr(), | |
| 874 base::MessageLoop::current()->message_loop_proxy()), | |
| 875 base::TimeDelta::FromSeconds(kInitializationDelaySeconds)); | |
| 876 } | 823 } |
| 877 } | 824 } |
| 878 | 825 |
| 879 void MetricsService::CloseCurrentLog() { | 826 void MetricsService::CloseCurrentLog() { |
| 880 if (!log_manager_.current_log()) | 827 if (!log_manager_.current_log()) |
| 881 return; | 828 return; |
| 882 | 829 |
| 883 // TODO(jar): Integrate bounds on log recording more consistently, so that we | 830 // TODO(jar): Integrate bounds on log recording more consistently, so that we |
| 884 // can stop recording logs that are too big much sooner. | 831 // can stop recording logs that are too big much sooner. |
| 885 if (log_manager_.current_log()->num_events() > kEventLimit) { | 832 if (log_manager_.current_log()->num_events() > kEventLimit) { |
| 886 UMA_HISTOGRAM_COUNTS("UMA.Discarded Log Events", | 833 UMA_HISTOGRAM_COUNTS("UMA.Discarded Log Events", |
| 887 log_manager_.current_log()->num_events()); | 834 log_manager_.current_log()->num_events()); |
| 888 log_manager_.DiscardCurrentLog(); | 835 log_manager_.DiscardCurrentLog(); |
| 889 OpenNewLog(); // Start trivial log to hold our histograms. | 836 OpenNewLog(); // Start trivial log to hold our histograms. |
| 890 } | 837 } |
| 891 | 838 |
| 892 // Adds to ongoing logs. | |
| 893 log_manager_.current_log()->set_hardware_class(hardware_class_); | |
| 894 | |
| 895 // Put incremental data (histogram deltas, and realtime stats deltas) at the | 839 // Put incremental data (histogram deltas, and realtime stats deltas) at the |
| 896 // end of all log transmissions (initial log handles this separately). | 840 // end of all log transmissions (initial log handles this separately). |
| 897 // RecordIncrementalStabilityElements only exists on the derived | 841 // RecordIncrementalStabilityElements only exists on the derived |
| 898 // MetricsLog class. | 842 // MetricsLog class. |
| 899 MetricsLog* current_log = | 843 MetricsLog* current_log = |
| 900 static_cast<MetricsLog*>(log_manager_.current_log()); | 844 static_cast<MetricsLog*>(log_manager_.current_log()); |
| 901 DCHECK(current_log); | 845 DCHECK(current_log); |
| 902 std::vector<variations::ActiveGroupId> synthetic_trials; | 846 std::vector<variations::ActiveGroupId> synthetic_trials; |
| 903 GetCurrentSyntheticFieldTrials(&synthetic_trials); | 847 GetCurrentSyntheticFieldTrials(&synthetic_trials); |
| 904 current_log->RecordEnvironment(metrics_providers_.get(), synthetic_trials); | 848 current_log->RecordEnvironment(metrics_providers_.get(), synthetic_trials); |
| (...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1106 | 1050 |
| 1107 // Store unsent logs, including the stability log that was just saved, so | 1051 // Store unsent logs, including the stability log that was just saved, so |
| 1108 // that they're not lost in case of a crash before upload time. | 1052 // that they're not lost in case of a crash before upload time. |
| 1109 log_manager_.PersistUnsentLogs(); | 1053 log_manager_.PersistUnsentLogs(); |
| 1110 | 1054 |
| 1111 has_initial_stability_log_ = true; | 1055 has_initial_stability_log_ = true; |
| 1112 } | 1056 } |
| 1113 | 1057 |
| 1114 void MetricsService::PrepareInitialMetricsLog() { | 1058 void MetricsService::PrepareInitialMetricsLog() { |
| 1115 DCHECK(state_ == INIT_TASK_DONE || state_ == SENDING_INITIAL_STABILITY_LOG); | 1059 DCHECK(state_ == INIT_TASK_DONE || state_ == SENDING_INITIAL_STABILITY_LOG); |
| 1116 initial_metrics_log_->set_hardware_class(hardware_class_); | |
| 1117 | 1060 |
| 1118 std::vector<variations::ActiveGroupId> synthetic_trials; | 1061 std::vector<variations::ActiveGroupId> synthetic_trials; |
| 1119 GetCurrentSyntheticFieldTrials(&synthetic_trials); | 1062 GetCurrentSyntheticFieldTrials(&synthetic_trials); |
| 1120 initial_metrics_log_->RecordEnvironment(metrics_providers_.get(), | 1063 initial_metrics_log_->RecordEnvironment(metrics_providers_.get(), |
| 1121 synthetic_trials); | 1064 synthetic_trials); |
| 1122 base::TimeDelta incremental_uptime; | 1065 base::TimeDelta incremental_uptime; |
| 1123 base::TimeDelta uptime; | 1066 base::TimeDelta uptime; |
| 1124 GetUptimes(local_state_, &incremental_uptime, &uptime); | 1067 GetUptimes(local_state_, &incremental_uptime, &uptime); |
| 1125 | 1068 |
| 1126 // Histograms only get written to the current log, so make the new log current | 1069 // 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... |
| 1411 RecordCurrentState(local_state_); | 1354 RecordCurrentState(local_state_); |
| 1412 } | 1355 } |
| 1413 | 1356 |
| 1414 void MetricsService::RecordCurrentState(PrefService* pref) { | 1357 void MetricsService::RecordCurrentState(PrefService* pref) { |
| 1415 pref->SetInt64(prefs::kStabilityLastTimestampSec, Time::Now().ToTimeT()); | 1358 pref->SetInt64(prefs::kStabilityLastTimestampSec, Time::Now().ToTimeT()); |
| 1416 | 1359 |
| 1417 #if defined(ENABLE_PLUGINS) | 1360 #if defined(ENABLE_PLUGINS) |
| 1418 plugin_metrics_provider_->RecordPluginChanges(); | 1361 plugin_metrics_provider_->RecordPluginChanges(); |
| 1419 #endif | 1362 #endif |
| 1420 } | 1363 } |
| OLD | NEW |