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 #include "chrome/browser/performance_monitor/performance_monitor.h" | 5 #include "chrome/browser/performance_monitor/performance_monitor.h" |
6 | 6 |
7 #include "base/memory/singleton.h" | 7 #include "base/memory/singleton.h" |
| 8 #include "base/metrics/field_trial.h" |
| 9 #include "base/metrics/histogram_base.h" |
| 10 #include "base/metrics/statistics_recorder.h" |
8 #include "base/process/process_iterator.h" | 11 #include "base/process/process_iterator.h" |
9 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
10 #include "base/time/time.h" | 13 #include "base/time/time.h" |
| 14 #include "chrome/browser/browser_process.h" |
| 15 #include "chrome/browser/tracing/crash_service_uploader.h" |
| 16 #include "content/browser/tracing/background_tracing_endpoint.h" |
| 17 #include "content/browser/tracing/background_tracing_manager.h" |
11 #include "content/public/browser/browser_child_process_host.h" | 18 #include "content/public/browser/browser_child_process_host.h" |
12 #include "content/public/browser/browser_child_process_host_iterator.h" | 19 #include "content/public/browser/browser_child_process_host_iterator.h" |
13 #include "content/public/browser/browser_thread.h" | 20 #include "content/public/browser/browser_thread.h" |
14 #include "content/public/browser/child_process_data.h" | 21 #include "content/public/browser/child_process_data.h" |
15 #include "content/public/browser/render_process_host.h" | 22 #include "content/public/browser/render_process_host.h" |
16 #include "content/public/common/content_constants.h" | 23 #include "content/public/common/content_constants.h" |
17 | 24 |
18 #if defined(ENABLE_EXTENSIONS) | 25 #if defined(ENABLE_EXTENSIONS) |
19 #include "extensions/browser/extension_host.h" | 26 #include "extensions/browser/extension_host.h" |
20 #include "extensions/browser/extension_registry.h" | 27 #include "extensions/browser/extension_registry.h" |
21 #include "extensions/common/manifest_handlers/background_info.h" | 28 #include "extensions/common/manifest_handlers/background_info.h" |
22 #endif | 29 #endif |
23 | 30 |
24 using content::BrowserThread; | 31 using content::BrowserThread; |
25 | 32 |
26 namespace { | 33 namespace { |
27 | 34 |
28 // The default interval at which PerformanceMonitor performs its timed | 35 // The default interval at which PerformanceMonitor performs its timed |
29 // collections. | 36 // collections. |
30 const int kGatherIntervalInSeconds = 120; | 37 const int kGatherIntervalInSeconds = 120; |
31 | 38 |
| 39 class CrashBackendEndpoint : public BackgroundTracingEndpoint { |
| 40 public: |
| 41 CrashBackendEndpoint() { |
| 42 uploader_ = scoped_ptr<TraceCrashServiceUploader>( |
| 43 new TraceCrashServiceUploader( |
| 44 g_browser_process->system_request_context())); |
| 45 } |
| 46 |
| 47 void ReceiveTraceFinalContents(const std::string& file_contents) override { |
| 48 printf("Size: %lu\n", file_contents.size()); |
| 49 printf("CrashBackendEndpoint: Uploading.\n"); |
| 50 |
| 51 uploader_->DoUpload( |
| 52 file_contents, |
| 53 base::Bind(&CrashBackendEndpoint::OnUploadProgress, |
| 54 this), |
| 55 base::Bind(&CrashBackendEndpoint::OnUploadComplete, |
| 56 this)); |
| 57 } |
| 58 |
| 59 class CrashBackendEndpointFactory : public BackgroundTracingManagerEndpointFac
tory { |
| 60 public: |
| 61 BackgroundTracingEndpoint* CreateFromString(const std::string&) const overri
de { |
| 62 return new CrashBackendEndpoint(); |
| 63 } |
| 64 std::string GetClassName() const override { |
| 65 return "crash_backend_endpoint"; |
| 66 } |
| 67 }; |
| 68 |
| 69 static BackgroundTracingManagerEndpointFactory* GetFactory() { |
| 70 return new CrashBackendEndpointFactory(); |
| 71 } |
| 72 |
| 73 std::string ToString() const override { |
| 74 return "crash_backend_endpoint"; |
| 75 } |
| 76 |
| 77 private: |
| 78 ~CrashBackendEndpoint() override { |
| 79 } |
| 80 |
| 81 void OnUploadProgress(int64 current, int64 total) { |
| 82 } |
| 83 |
| 84 void OnUploadComplete(bool success, const std::string& feedback) { |
| 85 printf("CrashBackendEndpoint: Finished: %s\n", feedback.c_str()); |
| 86 done_callback_.Run(); |
| 87 } |
| 88 |
| 89 scoped_ptr<TraceCrashServiceUploader> uploader_; |
| 90 }; |
| 91 |
| 92 |
| 93 class FakeEndpoint : public BackgroundTracingEndpoint { |
| 94 public: |
| 95 FakeEndpoint() { |
| 96 } |
| 97 |
| 98 void ReceiveTraceFinalContents(const std::string& file_contents) override { |
| 99 printf("Size: %lu\n", file_contents.size()); |
| 100 printf("FakeEndpoint: Not going to do anything.\n"); |
| 101 content::BrowserThread::PostDelayedTask( |
| 102 content::BrowserThread::UI, |
| 103 FROM_HERE, |
| 104 base::Bind(done_callback_), |
| 105 base::TimeDelta::FromSeconds(5)); |
| 106 } |
| 107 |
| 108 class FakeEndpointFactory : public BackgroundTracingManagerEndpointFactory { |
| 109 public: |
| 110 BackgroundTracingEndpoint* CreateFromString(const std::string&) const overri
de { |
| 111 return new FakeEndpoint(); |
| 112 } |
| 113 std::string GetClassName() const override { |
| 114 return "fake_endpoint"; |
| 115 } |
| 116 }; |
| 117 |
| 118 static BackgroundTracingManagerEndpointFactory* GetFactory() { |
| 119 return new FakeEndpointFactory(); |
| 120 } |
| 121 |
| 122 std::string ToString() const override { |
| 123 return "fake_endpoint"; |
| 124 } |
| 125 |
| 126 private: |
| 127 ~FakeEndpoint() override { |
| 128 } |
| 129 }; |
| 130 |
| 131 class PerformanceMonitorTrigger : public BackgroundTracingScenario { |
| 132 public: |
| 133 PerformanceMonitorTrigger(scoped_refptr<BackgroundTracingEndpoint> e) : |
| 134 BackgroundTracingScenario(e) { |
| 135 base::HistogramBase* histogram = base::StatisticsRecorder::FindHistogram( |
| 136 "Navigation.TimeToURLJobStart"); |
| 137 if (histogram) |
| 138 histogram->SetCallback( |
| 139 base::Bind(&PerformanceMonitorTrigger::onUMAHistogramChanged, |
| 140 base::Unretained(this))); |
| 141 } |
| 142 |
| 143 class PerformanceMonitorTriggerFactory : public BackgroundTracingManagerScenar
ioFactory { |
| 144 public: |
| 145 BackgroundTracingScenario* CreateFromString( |
| 146 const std::string& contents, |
| 147 scoped_refptr<BackgroundTracingEndpoint> endpoint) const override { |
| 148 return new PerformanceMonitorTrigger(endpoint); |
| 149 } |
| 150 std::string GetClassName() const override { |
| 151 return "perf_mon_trigger"; |
| 152 } |
| 153 }; |
| 154 |
| 155 static BackgroundTracingManagerScenarioFactory* GetFactory() { |
| 156 return new PerformanceMonitorTriggerFactory(); |
| 157 } |
| 158 |
| 159 std::string ToString() const override { |
| 160 return "perf_mon_trigger"; |
| 161 } |
| 162 |
| 163 protected: |
| 164 |
| 165 ~PerformanceMonitorTrigger() override {} |
| 166 |
| 167 void onUMAHistogramChanged() { |
| 168 printf("THIS IS A CLALBACK FROM HISTOGRAM\n"); |
| 169 BackgroundTracingManager::GetInstance()-> |
| 170 TryFinalizingBackgroundTracing( |
| 171 BackgroundTracingManager::StartedFinalizingCallback()); |
| 172 } |
| 173 }; |
| 174 |
32 } // namespace | 175 } // namespace |
33 | 176 |
34 namespace performance_monitor { | 177 namespace performance_monitor { |
35 | 178 |
36 PerformanceMonitor::PerformanceMonitor() { | 179 PerformanceMonitor::PerformanceMonitor() { |
37 } | 180 } |
38 | 181 |
39 PerformanceMonitor::~PerformanceMonitor() { | 182 PerformanceMonitor::~PerformanceMonitor() { |
40 } | 183 } |
41 | 184 |
42 // static | 185 // static |
43 PerformanceMonitor* PerformanceMonitor::GetInstance() { | 186 PerformanceMonitor* PerformanceMonitor::GetInstance() { |
44 return Singleton<PerformanceMonitor>::get(); | 187 return Singleton<PerformanceMonitor>::get(); |
45 } | 188 } |
46 | 189 |
| 190 void CreateTestUMATrigger() { |
| 191 BackgroundTracingManager::GetInstance()->RegisterFactory( |
| 192 PerformanceMonitorTrigger::GetFactory()); |
| 193 BackgroundTracingManager::GetInstance()->RegisterFactory( |
| 194 FakeEndpoint::GetFactory()); |
| 195 BackgroundTracingManager::GetInstance()->RegisterFactory( |
| 196 CrashBackendEndpoint::GetFactory()); |
| 197 BackgroundTracingManager::GetInstance()->SetActiveScenario( |
| 198 BackgroundTracingManager::GetInstance()->CreateFromString( |
| 199 "crash_backend_endpoint|perf_mon_trigger")); |
| 200 } |
| 201 |
47 void PerformanceMonitor::StartGatherCycle() { | 202 void PerformanceMonitor::StartGatherCycle() { |
48 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 203 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
49 repeating_timer_.Start(FROM_HERE, | 204 repeating_timer_.Start(FROM_HERE, |
50 base::TimeDelta::FromSeconds(kGatherIntervalInSeconds), | 205 base::TimeDelta::FromSeconds(kGatherIntervalInSeconds), |
51 this, &PerformanceMonitor::GatherMetricsMapOnUIThread); | 206 this, &PerformanceMonitor::GatherMetricsMapOnUIThread); |
| 207 |
| 208 if (base::FieldTrialList::FindFullName("SlowReportsContinualTracing") == |
| 209 "Enable" || 1) { |
| 210 |
| 211 content::BrowserThread::PostDelayedTask( |
| 212 content::BrowserThread::UI, |
| 213 FROM_HERE, |
| 214 base::Bind(&CreateTestUMATrigger), |
| 215 base::TimeDelta::FromSeconds(1)); |
| 216 } |
52 } | 217 } |
53 | 218 |
54 namespace { | 219 namespace { |
55 | 220 |
56 void GatherMetricsForRenderProcess(content::RenderProcessHost* host, | 221 void GatherMetricsForRenderProcess(content::RenderProcessHost* host, |
57 ProcessMetricsMetadata& data) { | 222 ProcessMetricsMetadata& data) { |
58 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 223 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
59 #if defined(ENABLE_EXTENSIONS) | 224 #if defined(ENABLE_EXTENSIONS) |
60 content::BrowserContext* browser_context = host->GetBrowserContext(); | 225 content::BrowserContext* browser_context = host->GetBrowserContext(); |
61 extensions::ProcessMap* extension_process_map = | 226 extensions::ProcessMap* extension_process_map = |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
167 // Not touched this iteration; let's get rid of it. | 332 // Not touched this iteration; let's get rid of it. |
168 metrics_map_.erase(iter++); | 333 metrics_map_.erase(iter++); |
169 } else { | 334 } else { |
170 process_metrics.SampleMetrics(); | 335 process_metrics.SampleMetrics(); |
171 ++iter; | 336 ++iter; |
172 } | 337 } |
173 } | 338 } |
174 } | 339 } |
175 | 340 |
176 } // namespace performance_monitor | 341 } // namespace performance_monitor |
OLD | NEW |