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 |
| 41 // Stop now if this was called without an allocator, typically because | |
| 42 // GetSubprocessHistogramAllocatorOnIOThread exited early and returned | |
| 43 // null. | |
| 44 if (!allocator) | |
| 45 return; | |
| 46 | |
| 32 // Map is "MapOwnPointer" so transfer ownership to it. | 47 // Map is "MapOwnPointer" so transfer ownership to it. |
| 33 allocators_by_id_.AddWithID(allocator.release(), id); | 48 allocators_by_id_.AddWithID(allocator.release(), id); |
| 34 } | 49 } |
| 35 | 50 |
| 36 void SubprocessMetricsProvider::DeregisterSubprocessAllocator(int id) { | 51 void SubprocessMetricsProvider::DeregisterSubprocessAllocator(int id) { |
| 37 DCHECK(thread_checker_.CalledOnValidThread()); | 52 DCHECK(thread_checker_.CalledOnValidThread()); |
| 38 | 53 |
| 39 if (!allocators_by_id_.Lookup(id)) | 54 if (!allocators_by_id_.Lookup(id)) |
| 40 return; | 55 return; |
| 41 | 56 |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 76 iter.Advance()) { | 91 iter.Advance()) { |
| 77 MergeHistogramDeltasFromAllocator(iter.GetCurrentKey(), | 92 MergeHistogramDeltasFromAllocator(iter.GetCurrentKey(), |
| 78 iter.GetCurrentValue()); | 93 iter.GetCurrentValue()); |
| 79 } | 94 } |
| 80 | 95 |
| 81 UMA_HISTOGRAM_COUNTS_100( | 96 UMA_HISTOGRAM_COUNTS_100( |
| 82 "UMA.SubprocessMetricsProvider.SubprocessCount", | 97 "UMA.SubprocessMetricsProvider.SubprocessCount", |
| 83 allocators_by_id_.size()); | 98 allocators_by_id_.size()); |
| 84 } | 99 } |
| 85 | 100 |
| 101 void SubprocessMetricsProvider::BrowserChildProcessHostConnected( | |
| 102 const content::ChildProcessData& data) { | |
| 103 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 104 | |
| 105 // It's necessary to access the BrowserChildProcessHost object that is | |
| 106 // managing the child in order to extract the metrics memory from it. | |
| 107 // Unfortunately, the required lookup can only be performed on the IO | |
| 108 // thread so do the necessary dance. | |
| 109 content::BrowserThread::PostTaskAndReplyWithResult( | |
| 110 content::BrowserThread::IO, FROM_HERE, | |
| 111 base::Bind(&SubprocessMetricsProvider:: | |
| 112 GetSubprocessHistogramAllocatorOnIOThread, | |
| 113 data.id), | |
| 114 base::Bind(&SubprocessMetricsProvider:: | |
| 115 RegisterSubprocessAllocator, | |
| 116 weak_ptr_factory_.GetWeakPtr(), data.id)); | |
| 117 } | |
| 118 | |
| 119 void SubprocessMetricsProvider::BrowserChildProcessHostDisconnected( | |
| 120 const content::ChildProcessData& data) { | |
| 121 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 122 DeregisterSubprocessAllocator(data.id); | |
| 123 } | |
| 124 | |
| 125 void SubprocessMetricsProvider::BrowserChildProcessCrashed( | |
| 126 const content::ChildProcessData& data, | |
| 127 int exit_code) { | |
| 128 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 129 DeregisterSubprocessAllocator(data.id); | |
| 130 } | |
| 131 | |
| 132 void SubprocessMetricsProvider::BrowserChildProcessKilled( | |
| 133 const content::ChildProcessData& data, | |
| 134 int exit_code) { | |
| 135 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 136 DeregisterSubprocessAllocator(data.id); | |
| 137 } | |
| 138 | |
| 86 void SubprocessMetricsProvider::Observe( | 139 void SubprocessMetricsProvider::Observe( |
| 87 int type, | 140 int type, |
| 88 const content::NotificationSource& source, | 141 const content::NotificationSource& source, |
| 89 const content::NotificationDetails& details) { | 142 const content::NotificationDetails& details) { |
| 90 DCHECK(thread_checker_.CalledOnValidThread()); | 143 DCHECK(thread_checker_.CalledOnValidThread()); |
| 91 DCHECK_EQ(content::NOTIFICATION_RENDERER_PROCESS_CREATED, type); | 144 DCHECK_EQ(content::NOTIFICATION_RENDERER_PROCESS_CREATED, type); |
| 92 | 145 |
| 93 content::RenderProcessHost* host = | 146 content::RenderProcessHost* host = |
| 94 content::Source<content::RenderProcessHost>(source).ptr(); | 147 content::Source<content::RenderProcessHost>(source).ptr(); |
| 95 | 148 |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 128 content::RenderProcessHost* host) { | 181 content::RenderProcessHost* host) { |
| 129 DCHECK(thread_checker_.CalledOnValidThread()); | 182 DCHECK(thread_checker_.CalledOnValidThread()); |
| 130 | 183 |
| 131 // It's possible for a Renderer to terminate without RenderProcessExited | 184 // It's possible for a Renderer to terminate without RenderProcessExited |
| 132 // (above) being called so it's necessary to de-register also upon the | 185 // (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. | 186 // destruction of the host. If both get called, no harm is done. |
| 134 | 187 |
| 135 DeregisterSubprocessAllocator(host->GetID()); | 188 DeregisterSubprocessAllocator(host->GetID()); |
| 136 scoped_observer_.Remove(host); | 189 scoped_observer_.Remove(host); |
| 137 } | 190 } |
| 191 | |
| 192 // static | |
| 193 std::unique_ptr<base::PersistentHistogramAllocator> | |
| 194 SubprocessMetricsProvider::GetSubprocessHistogramAllocatorOnIOThread(int id) { | |
|
Alexei Svitkine (slow)
2016/08/18 15:57:28
Nit: Make this a free-standing function in the ano
| |
| 195 // See if the new process has a memory allocator and take control of it if so. | |
| 196 // This call can only be made on the browser's IO thread. | |
| 197 content::BrowserChildProcessHost* host = | |
| 198 content::BrowserChildProcessHost::FromID(id); | |
| 199 if (!host) | |
| 200 return nullptr; | |
| 201 | |
| 202 std::unique_ptr<base::SharedPersistentMemoryAllocator> allocator = | |
| 203 host->TakeMetricsAllocator(); | |
| 204 if (!allocator) | |
| 205 return nullptr; | |
| 206 | |
| 207 return base::MakeUnique<base::PersistentHistogramAllocator>( | |
| 208 std::move(allocator)); | |
| 209 } | |
| OLD | NEW |