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