| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 | 10 // A MetricsService instance is typically created at application startup. It |
| (...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 257 // load. | 257 // load. |
| 258 int instances; | 258 int instances; |
| 259 | 259 |
| 260 ChildProcessInfo::ProcessType process_type; | 260 ChildProcessInfo::ProcessType process_type; |
| 261 }; | 261 }; |
| 262 | 262 |
| 263 // Handles asynchronous fetching of memory details. | 263 // Handles asynchronous fetching of memory details. |
| 264 // Will run the provided task after finished. | 264 // Will run the provided task after finished. |
| 265 class MetricsMemoryDetails : public MemoryDetails { | 265 class MetricsMemoryDetails : public MemoryDetails { |
| 266 public: | 266 public: |
| 267 explicit MetricsMemoryDetails(Task* completion) : completion_(completion) {} | 267 explicit MetricsMemoryDetails(const base::Closure& callback) |
| 268 : callback_(callback) {} |
| 268 | 269 |
| 269 virtual void OnDetailsAvailable() { | 270 virtual void OnDetailsAvailable() { |
| 270 MessageLoop::current()->PostTask(FROM_HERE, completion_); | 271 MessageLoop::current()->PostTask(FROM_HERE, callback_); |
| 271 } | 272 } |
| 272 | 273 |
| 273 private: | 274 private: |
| 274 ~MetricsMemoryDetails() {} | 275 ~MetricsMemoryDetails() {} |
| 275 | 276 |
| 276 Task* completion_; | 277 base::Closure callback_; |
| 277 DISALLOW_COPY_AND_ASSIGN(MetricsMemoryDetails); | 278 DISALLOW_COPY_AND_ASSIGN(MetricsMemoryDetails); |
| 278 }; | 279 }; |
| 279 | 280 |
| 280 // static | 281 // static |
| 281 void MetricsService::RegisterPrefs(PrefService* local_state) { | 282 void MetricsService::RegisterPrefs(PrefService* local_state) { |
| 282 DCHECK(IsSingleThreaded()); | 283 DCHECK(IsSingleThreaded()); |
| 283 local_state->RegisterStringPref(prefs::kMetricsClientID, ""); | 284 local_state->RegisterStringPref(prefs::kMetricsClientID, ""); |
| 284 local_state->RegisterInt64Pref(prefs::kMetricsClientIDTimestamp, 0); | 285 local_state->RegisterInt64Pref(prefs::kMetricsClientIDTimestamp, 0); |
| 285 local_state->RegisterInt64Pref(prefs::kStabilityLaunchTimeSec, 0); | 286 local_state->RegisterInt64Pref(prefs::kStabilityLaunchTimeSec, 0); |
| 286 local_state->RegisterInt64Pref(prefs::kStabilityLastTimestampSec, 0); | 287 local_state->RegisterInt64Pref(prefs::kStabilityLastTimestampSec, 0); |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 358 } | 359 } |
| 359 | 360 |
| 360 MetricsService::MetricsService() | 361 MetricsService::MetricsService() |
| 361 : recording_active_(false), | 362 : recording_active_(false), |
| 362 reporting_active_(false), | 363 reporting_active_(false), |
| 363 state_(INITIALIZED), | 364 state_(INITIALIZED), |
| 364 current_fetch_(NULL), | 365 current_fetch_(NULL), |
| 365 io_thread_(NULL), | 366 io_thread_(NULL), |
| 366 idle_since_last_transmission_(false), | 367 idle_since_last_transmission_(false), |
| 367 next_window_id_(0), | 368 next_window_id_(0), |
| 368 ALLOW_THIS_IN_INITIALIZER_LIST(log_sender_factory_(this)), | 369 ALLOW_THIS_IN_INITIALIZER_LIST(log_sender_weak_factory_(this)), |
| 369 ALLOW_THIS_IN_INITIALIZER_LIST(state_saver_factory_(this)), | 370 ALLOW_THIS_IN_INITIALIZER_LIST(state_saver_weak_factory_(this)), |
| 370 waiting_for_asynchronus_reporting_step_(false) { | 371 waiting_for_asynchronus_reporting_step_(false) { |
| 371 DCHECK(IsSingleThreaded()); | 372 DCHECK(IsSingleThreaded()); |
| 372 InitializeMetricsState(); | 373 InitializeMetricsState(); |
| 373 | 374 |
| 374 base::Closure callback = base::Bind(&MetricsService::StartScheduledUpload, | 375 base::Closure callback = base::Bind(&MetricsService::StartScheduledUpload, |
| 375 base::Unretained(this)); | 376 base::Unretained(this)); |
| 376 scheduler_.reset(new MetricsReportingScheduler(callback)); | 377 scheduler_.reset(new MetricsReportingScheduler(callback)); |
| 377 log_manager_.set_log_serializer(new MetricsLogSerializer()); | 378 log_manager_.set_log_serializer(new MetricsLogSerializer()); |
| 378 log_manager_.set_max_ongoing_log_store_size(kUploadLogAvoidRetransmitSize); | 379 log_manager_.set_max_ongoing_log_store_size(kUploadLogAvoidRetransmitSize); |
| 379 } | 380 } |
| (...skipping 373 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 753 } | 754 } |
| 754 | 755 |
| 755 std::string MetricsService::GenerateClientID() { | 756 std::string MetricsService::GenerateClientID() { |
| 756 return guid::GenerateGUID(); | 757 return guid::GenerateGUID(); |
| 757 } | 758 } |
| 758 | 759 |
| 759 //------------------------------------------------------------------------------ | 760 //------------------------------------------------------------------------------ |
| 760 // State save methods | 761 // State save methods |
| 761 | 762 |
| 762 void MetricsService::ScheduleNextStateSave() { | 763 void MetricsService::ScheduleNextStateSave() { |
| 763 state_saver_factory_.RevokeAll(); | 764 state_saver_weak_factory_.InvalidateWeakPtrs(); |
| 764 | 765 |
| 765 MessageLoop::current()->PostDelayedTask(FROM_HERE, | 766 MessageLoop::current()->PostDelayedTask( |
| 766 state_saver_factory_.NewRunnableMethod(&MetricsService::SaveLocalState), | 767 FROM_HERE, |
| 768 base::Bind(&MetricsService::SaveLocalState, |
| 769 state_saver_weak_factory_.GetWeakPtr()), |
| 767 kSaveStateInterval * 1000); | 770 kSaveStateInterval * 1000); |
| 768 } | 771 } |
| 769 | 772 |
| 770 void MetricsService::SaveLocalState() { | 773 void MetricsService::SaveLocalState() { |
| 771 PrefService* pref = g_browser_process->local_state(); | 774 PrefService* pref = g_browser_process->local_state(); |
| 772 if (!pref) { | 775 if (!pref) { |
| 773 NOTREACHED(); | 776 NOTREACHED(); |
| 774 return; | 777 return; |
| 775 } | 778 } |
| 776 | 779 |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 867 // If reporting has been turned off, the scheduler doesn't need to run. | 870 // If reporting has been turned off, the scheduler doesn't need to run. |
| 868 if (!reporting_active() || !recording_active()) { | 871 if (!reporting_active() || !recording_active()) { |
| 869 scheduler_->Stop(); | 872 scheduler_->Stop(); |
| 870 scheduler_->UploadCancelled(); | 873 scheduler_->UploadCancelled(); |
| 871 return; | 874 return; |
| 872 } | 875 } |
| 873 | 876 |
| 874 DCHECK(!waiting_for_asynchronus_reporting_step_); | 877 DCHECK(!waiting_for_asynchronus_reporting_step_); |
| 875 waiting_for_asynchronus_reporting_step_ = true; | 878 waiting_for_asynchronus_reporting_step_ = true; |
| 876 | 879 |
| 877 Task* task = log_sender_factory_. | 880 base::Closure callback = |
| 878 NewRunnableMethod(&MetricsService::OnMemoryDetailCollectionDone); | 881 base::Bind(&MetricsService::OnMemoryDetailCollectionDone, |
| 882 log_sender_weak_factory_.GetWeakPtr()); |
| 879 | 883 |
| 880 scoped_refptr<MetricsMemoryDetails> details(new MetricsMemoryDetails(task)); | 884 scoped_refptr<MetricsMemoryDetails> details( |
| 885 new MetricsMemoryDetails(callback)); |
| 881 details->StartFetch(); | 886 details->StartFetch(); |
| 882 | 887 |
| 883 // Collect WebCore cache information to put into a histogram. | 888 // Collect WebCore cache information to put into a histogram. |
| 884 for (content::RenderProcessHost::iterator i( | 889 for (content::RenderProcessHost::iterator i( |
| 885 content::RenderProcessHost::AllHostsIterator()); | 890 content::RenderProcessHost::AllHostsIterator()); |
| 886 !i.IsAtEnd(); i.Advance()) | 891 !i.IsAtEnd(); i.Advance()) |
| 887 i.GetCurrentValue()->Send(new ChromeViewMsg_GetCacheResourceStats()); | 892 i.GetCurrentValue()->Send(new ChromeViewMsg_GetCacheResourceStats()); |
| 888 } | 893 } |
| 889 | 894 |
| 890 void MetricsService::OnMemoryDetailCollectionDone() { | 895 void MetricsService::OnMemoryDetailCollectionDone() { |
| 891 DCHECK(IsSingleThreaded()); | 896 DCHECK(IsSingleThreaded()); |
| 892 // This function should only be called as the callback from an ansynchronous | 897 // This function should only be called as the callback from an ansynchronous |
| 893 // step. | 898 // step. |
| 894 DCHECK(waiting_for_asynchronus_reporting_step_); | 899 DCHECK(waiting_for_asynchronus_reporting_step_); |
| 895 | 900 |
| 896 // Right before the UMA transmission gets started, there's one more thing we'd | 901 // Right before the UMA transmission gets started, there's one more thing we'd |
| 897 // like to record: the histogram of memory usage, so we spawn a task to | 902 // like to record: the histogram of memory usage, so we spawn a task to |
| 898 // collect the memory details and when that task is finished, it will call | 903 // collect the memory details and when that task is finished, it will call |
| 899 // OnMemoryDetailCollectionDone, which will call HistogramSynchronization to | 904 // OnMemoryDetailCollectionDone, which will call HistogramSynchronization to |
| 900 // collect histograms from all renderers and then we will call | 905 // collect histograms from all renderers and then we will call |
| 901 // OnHistogramSynchronizationDone to continue processing. | 906 // OnHistogramSynchronizationDone to continue processing. |
| 902 | 907 |
| 903 // Create a callback_task for OnHistogramSynchronizationDone. | 908 // Create a callback_task for OnHistogramSynchronizationDone. |
| 904 Task* callback_task = log_sender_factory_.NewRunnableMethod( | 909 base::Closure callback = base::Bind( |
| 905 &MetricsService::OnHistogramSynchronizationDone); | 910 &MetricsService::OnHistogramSynchronizationDone, |
| 911 log_sender_weak_factory_.GetWeakPtr()); |
| 906 | 912 |
| 907 base::StatisticsRecorder::CollectHistogramStats("Browser"); | 913 base::StatisticsRecorder::CollectHistogramStats("Browser"); |
| 908 | 914 |
| 909 // Set up the callback to task to call after we receive histograms from all | 915 // Set up the callback to task to call after we receive histograms from all |
| 910 // renderer processes. Wait time specifies how long to wait before absolutely | 916 // renderer processes. Wait time specifies how long to wait before absolutely |
| 911 // calling us back on the task. | 917 // calling us back on the task. |
| 912 HistogramSynchronizer::FetchRendererHistogramsAsynchronously( | 918 HistogramSynchronizer::FetchRendererHistogramsAsynchronously( |
| 913 MessageLoop::current(), callback_task, | 919 MessageLoop::current(), callback, |
| 914 kMaxHistogramGatheringWaitDuration); | 920 kMaxHistogramGatheringWaitDuration); |
| 915 } | 921 } |
| 916 | 922 |
| 917 void MetricsService::OnHistogramSynchronizationDone() { | 923 void MetricsService::OnHistogramSynchronizationDone() { |
| 918 DCHECK(IsSingleThreaded()); | 924 DCHECK(IsSingleThreaded()); |
| 919 | 925 |
| 920 // If somehow there is a fetch in progress, we return and hope things work | 926 // If somehow there is a fetch in progress, we return and hope things work |
| 921 // out. The scheduler isn't informed since if this happens, the scheduler | 927 // out. The scheduler isn't informed since if this happens, the scheduler |
| 922 // will get a response from the upload. | 928 // will get a response from the upload. |
| 923 DCHECK(!current_fetch_.get()); | 929 DCHECK(!current_fetch_.get()); |
| (...skipping 623 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1547 if (local_state) { | 1553 if (local_state) { |
| 1548 const PrefService::Preference* uma_pref = | 1554 const PrefService::Preference* uma_pref = |
| 1549 local_state->FindPreference(prefs::kMetricsReportingEnabled); | 1555 local_state->FindPreference(prefs::kMetricsReportingEnabled); |
| 1550 if (uma_pref) { | 1556 if (uma_pref) { |
| 1551 bool success = uma_pref->GetValue()->GetAsBoolean(&result); | 1557 bool success = uma_pref->GetValue()->GetAsBoolean(&result); |
| 1552 DCHECK(success); | 1558 DCHECK(success); |
| 1553 } | 1559 } |
| 1554 } | 1560 } |
| 1555 return result; | 1561 return result; |
| 1556 } | 1562 } |
| OLD | NEW |