Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/metrics/subprocess_metrics_provider.h" | 5 #include "chrome/browser/metrics/subprocess_metrics_provider.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
| 9 #include "base/metrics/histogram_base.h" | 9 #include "base/metrics/histogram_base.h" |
| 10 #include "base/metrics/histogram_macros.h" | 10 #include "base/metrics/histogram_macros.h" |
| 11 #include "base/metrics/persistent_histogram_allocator.h" | 11 #include "base/metrics/persistent_histogram_allocator.h" |
| 12 #include "base/metrics/persistent_memory_allocator.h" | 12 #include "base/metrics/persistent_memory_allocator.h" |
| 13 #include "components/metrics/metrics_service.h" | 13 #include "components/metrics/metrics_service.h" |
| 14 #include "content/public/browser/browser_child_process_host.h" | |
| 15 #include "content/public/browser/browser_thread.h" | |
| 16 #include "content/public/browser/child_process_data.h" | |
| 14 #include "content/public/browser/notification_service.h" | 17 #include "content/public/browser/notification_service.h" |
| 15 #include "content/public/browser/notification_types.h" | 18 #include "content/public/browser/notification_types.h" |
| 16 #include "content/public/browser/render_process_host.h" | 19 #include "content/public/browser/render_process_host.h" |
| 17 | 20 |
| 18 SubprocessMetricsProvider::SubprocessMetricsProvider() | 21 SubprocessMetricsProvider::SubprocessMetricsProvider() |
| 19 : scoped_observer_(this) { | 22 : scoped_observer_(this), weak_ptr_factory_(this) {} |
| 23 | |
| 24 SubprocessMetricsProvider::~SubprocessMetricsProvider() { | |
| 25 // Safe even if this object has never been added as an observer. | |
| 26 content::BrowserChildProcessObserver::Remove(this); | |
| 27 } | |
| 28 | |
| 29 void SubprocessMetricsProvider::EnableSubprocessTracking() { | |
| 30 content::BrowserChildProcessObserver::Add(this); | |
| 20 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CREATED, | 31 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CREATED, |
| 21 content::NotificationService::AllBrowserContextsAndSources()); | 32 content::NotificationService::AllBrowserContextsAndSources()); |
| 22 } | 33 } |
| 23 | 34 |
| 24 SubprocessMetricsProvider::~SubprocessMetricsProvider() {} | |
| 25 | |
| 26 void SubprocessMetricsProvider::RegisterSubprocessAllocator( | 35 void SubprocessMetricsProvider::RegisterSubprocessAllocator( |
| 27 int id, | 36 int id, |
| 28 std::unique_ptr<base::PersistentHistogramAllocator> allocator) { | 37 std::unique_ptr<base::PersistentHistogramAllocator> allocator) { |
| 29 DCHECK(thread_checker_.CalledOnValidThread()); | 38 DCHECK(thread_checker_.CalledOnValidThread()); |
| 30 DCHECK(!allocators_by_id_.Lookup(id)); | 39 DCHECK(!allocators_by_id_.Lookup(id)); |
| 31 | 40 |
| 32 // Map is "MapOwnPointer" so transfer ownership to it. | 41 // Map is "MapOwnPointer" so transfer ownership to it. |
| 33 allocators_by_id_.AddWithID(allocator.release(), id); | 42 allocators_by_id_.AddWithID(allocator.release(), id); |
| 34 } | 43 } |
| 35 | 44 |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 76 iter.Advance()) { | 85 iter.Advance()) { |
| 77 MergeHistogramDeltasFromAllocator(iter.GetCurrentKey(), | 86 MergeHistogramDeltasFromAllocator(iter.GetCurrentKey(), |
| 78 iter.GetCurrentValue()); | 87 iter.GetCurrentValue()); |
| 79 } | 88 } |
| 80 | 89 |
| 81 UMA_HISTOGRAM_COUNTS_100( | 90 UMA_HISTOGRAM_COUNTS_100( |
| 82 "UMA.SubprocessMetricsProvider.SubprocessCount", | 91 "UMA.SubprocessMetricsProvider.SubprocessCount", |
| 83 allocators_by_id_.size()); | 92 allocators_by_id_.size()); |
| 84 } | 93 } |
| 85 | 94 |
| 95 void SubprocessMetricsProvider::BrowserChildProcessHostConnected( | |
| 96 const content::ChildProcessData& data) { | |
| 97 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 98 | |
| 99 // It's necessary to access the BrowserChildProcessHost object that is | |
| 100 // managing the child in order to extract the metrics memory from it. | |
| 101 // Unfortunately, the required lookup can only be performed on the IO | |
| 102 // thread so do the necessary dance. | |
| 103 content::BrowserThread::PostTask( | |
| 104 content::BrowserThread::IO, FROM_HERE, | |
| 105 base::Bind(&SubprocessMetricsProvider:: | |
| 106 RegisterBrowserChildProcessAllocatorFromIOThread, | |
| 107 weak_ptr_factory_.GetWeakPtr(), data.id)); | |
| 108 } | |
| 109 | |
| 110 void SubprocessMetricsProvider::BrowserChildProcessHostDisconnected( | |
| 111 const content::ChildProcessData& data) { | |
| 112 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 113 DeregisterSubprocessAllocator(data.id); | |
| 114 } | |
| 115 | |
| 116 void SubprocessMetricsProvider::BrowserChildProcessCrashed( | |
| 117 const content::ChildProcessData& data, | |
| 118 int exit_code) { | |
| 119 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 120 DeregisterSubprocessAllocator(data.id); | |
| 121 } | |
| 122 | |
| 123 void SubprocessMetricsProvider::BrowserChildProcessKilled( | |
| 124 const content::ChildProcessData& data, | |
| 125 int exit_code) { | |
| 126 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 127 DeregisterSubprocessAllocator(data.id); | |
| 128 } | |
| 129 | |
| 86 void SubprocessMetricsProvider::Observe( | 130 void SubprocessMetricsProvider::Observe( |
| 87 int type, | 131 int type, |
| 88 const content::NotificationSource& source, | 132 const content::NotificationSource& source, |
| 89 const content::NotificationDetails& details) { | 133 const content::NotificationDetails& details) { |
| 90 DCHECK(thread_checker_.CalledOnValidThread()); | 134 DCHECK(thread_checker_.CalledOnValidThread()); |
| 91 DCHECK_EQ(content::NOTIFICATION_RENDERER_PROCESS_CREATED, type); | 135 DCHECK_EQ(content::NOTIFICATION_RENDERER_PROCESS_CREATED, type); |
| 92 | 136 |
| 93 content::RenderProcessHost* host = | 137 content::RenderProcessHost* host = |
| 94 content::Source<content::RenderProcessHost>(source).ptr(); | 138 content::Source<content::RenderProcessHost>(source).ptr(); |
| 95 | 139 |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 128 content::RenderProcessHost* host) { | 172 content::RenderProcessHost* host) { |
| 129 DCHECK(thread_checker_.CalledOnValidThread()); | 173 DCHECK(thread_checker_.CalledOnValidThread()); |
| 130 | 174 |
| 131 // It's possible for a Renderer to terminate without RenderProcessExited | 175 // It's possible for a Renderer to terminate without RenderProcessExited |
| 132 // (above) being called so it's necessary to de-register also upon the | 176 // (above) being called so it's necessary to de-register also upon the |
| 133 // destruction of the host. If both get called, no harm is done. | 177 // destruction of the host. If both get called, no harm is done. |
| 134 | 178 |
| 135 DeregisterSubprocessAllocator(host->GetID()); | 179 DeregisterSubprocessAllocator(host->GetID()); |
| 136 scoped_observer_.Remove(host); | 180 scoped_observer_.Remove(host); |
| 137 } | 181 } |
| 182 | |
| 183 // static | |
| 184 void SubprocessMetricsProvider:: | |
| 185 RegisterBrowserChildProcessAllocatorFromIOThread( | |
| 186 base::WeakPtr<SubprocessMetricsProvider> provider, | |
| 187 int id) { | |
| 188 // See if the new process has a memory allocator and take control of it if so. | |
| 189 // This call can only be made on the browser's IO thread. | |
| 190 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | |
| 191 content::BrowserChildProcessHost* host = | |
| 192 content::BrowserChildProcessHost::FromID(id); | |
| 193 if (!host) | |
| 194 return; | |
| 195 std::unique_ptr<base::SharedPersistentMemoryAllocator> allocator = | |
| 196 host->TakeMetricsAllocator(); | |
| 197 if (!allocator) | |
| 198 return; | |
| 199 | |
| 200 // Register the new allocator so metrics will be extracted from it. This has | |
| 201 // to be done on the UI thread. | |
| 202 content::BrowserThread::PostTask( | |
|
Alexei Svitkine (slow)
2016/08/12 23:14:36
This seems overly complicated.
I think you can si
bcwhite
2016/08/15 19:43:09
Done.
| |
| 203 content::BrowserThread::UI, | |
| 204 FROM_HERE, | |
| 205 base::Bind( | |
| 206 &SubprocessMetricsProvider::RegisterSubprocessAllocator, | |
| 207 provider, | |
| 208 id, | |
| 209 base::Passed( | |
| 210 WrapUnique(new base::PersistentHistogramAllocator( | |
|
Alexei Svitkine (slow)
2016/08/12 23:14:36
Use base::MakeUnique.
bcwhite
2016/08/15 19:43:10
Done.
| |
| 211 std::move(allocator)))))); | |
| 212 } | |
| OLD | NEW |