[email protected] | 702a12d | 2012-02-10 19:43:42 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 33047f1 | 2011-12-01 23:20:20 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "content/browser/profiler_controller_impl.h" |
| 6 | |
| 7 | #include "base/bind.h" |
[email protected] | 42a3ff57 | 2014-06-13 02:44:46 | [diff] [blame] | 8 | #include "base/process/process_handle.h" |
[email protected] | 1cb05db | 2012-04-13 00:39:26 | [diff] [blame] | 9 | #include "base/tracked_objects.h" |
[email protected] | 33047f1 | 2011-12-01 23:20:20 | [diff] [blame] | 10 | #include "content/common/child_process_messages.h" |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 11 | #include "content/public/browser/browser_child_process_host_iterator.h" |
[email protected] | 33047f1 | 2011-12-01 23:20:20 | [diff] [blame] | 12 | #include "content/public/browser/browser_thread.h" |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 13 | #include "content/public/browser/child_process_data.h" |
[email protected] | 33047f1 | 2011-12-01 23:20:20 | [diff] [blame] | 14 | #include "content/public/browser/profiler_subscriber.h" |
| 15 | #include "content/public/browser/render_process_host.h" |
[email protected] | 33047f1 | 2011-12-01 23:20:20 | [diff] [blame] | 16 | |
[email protected] | 33047f1 | 2011-12-01 23:20:20 | [diff] [blame] | 17 | namespace content { |
| 18 | |
[email protected] | 4648832 | 2012-10-30 03:22:20 | [diff] [blame] | 19 | ProfilerController* ProfilerController::GetInstance() { |
[email protected] | 33047f1 | 2011-12-01 23:20:20 | [diff] [blame] | 20 | return ProfilerControllerImpl::GetInstance(); |
| 21 | } |
| 22 | |
| 23 | ProfilerControllerImpl* ProfilerControllerImpl::GetInstance() { |
olli.raula | 36aa8be | 2015-09-10 11:14:22 | [diff] [blame] | 24 | return base::Singleton<ProfilerControllerImpl>::get(); |
[email protected] | 33047f1 | 2011-12-01 23:20:20 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | ProfilerControllerImpl::ProfilerControllerImpl() : subscriber_(NULL) { |
| 28 | } |
| 29 | |
| 30 | ProfilerControllerImpl::~ProfilerControllerImpl() { |
| 31 | } |
| 32 | |
| 33 | void ProfilerControllerImpl::OnPendingProcesses(int sequence_number, |
| 34 | int pending_processes, |
| 35 | bool end) { |
mostynb | 4c27d04 | 2015-03-18 21:47:47 | [diff] [blame] | 36 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 33047f1 | 2011-12-01 23:20:20 | [diff] [blame] | 37 | if (subscriber_) |
| 38 | subscriber_->OnPendingProcesses(sequence_number, pending_processes, end); |
| 39 | } |
| 40 | |
| 41 | void ProfilerControllerImpl::OnProfilerDataCollected( |
| 42 | int sequence_number, |
[email protected] | 1cb05db | 2012-04-13 00:39:26 | [diff] [blame] | 43 | const tracked_objects::ProcessDataSnapshot& profiler_data, |
vadimt | 379d7fe | 2015-04-01 00:09:35 | [diff] [blame] | 44 | content::ProcessType process_type) { |
[email protected] | 33047f1 | 2011-12-01 23:20:20 | [diff] [blame] | 45 | if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
| 46 | BrowserThread::PostTask( |
| 47 | BrowserThread::UI, FROM_HERE, |
| 48 | base::Bind(&ProfilerControllerImpl::OnProfilerDataCollected, |
| 49 | base::Unretained(this), |
| 50 | sequence_number, |
[email protected] | 1cb05db | 2012-04-13 00:39:26 | [diff] [blame] | 51 | profiler_data, |
| 52 | process_type)); |
[email protected] | 33047f1 | 2011-12-01 23:20:20 | [diff] [blame] | 53 | return; |
| 54 | } |
| 55 | |
mostynb | 4c27d04 | 2015-03-18 21:47:47 | [diff] [blame] | 56 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 1cb05db | 2012-04-13 00:39:26 | [diff] [blame] | 57 | if (subscriber_) { |
| 58 | subscriber_->OnProfilerDataCollected(sequence_number, profiler_data, |
| 59 | process_type); |
| 60 | } |
[email protected] | 33047f1 | 2011-12-01 23:20:20 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | void ProfilerControllerImpl::Register(ProfilerSubscriber* subscriber) { |
mostynb | 4c27d04 | 2015-03-18 21:47:47 | [diff] [blame] | 64 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 33047f1 | 2011-12-01 23:20:20 | [diff] [blame] | 65 | DCHECK(!subscriber_); |
| 66 | subscriber_ = subscriber; |
| 67 | } |
| 68 | |
[email protected] | 1cb05db | 2012-04-13 00:39:26 | [diff] [blame] | 69 | void ProfilerControllerImpl::Unregister(const ProfilerSubscriber* subscriber) { |
| 70 | DCHECK_EQ(subscriber_, subscriber); |
| 71 | subscriber_ = NULL; |
[email protected] | 33047f1 | 2011-12-01 23:20:20 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | void ProfilerControllerImpl::GetProfilerDataFromChildProcesses( |
vadimt | e2de473 | 2015-04-27 21:43:02 | [diff] [blame] | 75 | int sequence_number, |
| 76 | int current_profiling_phase) { |
mostynb | 4c27d04 | 2015-03-18 21:47:47 | [diff] [blame] | 77 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
[email protected] | 33047f1 | 2011-12-01 23:20:20 | [diff] [blame] | 78 | |
| 79 | int pending_processes = 0; |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 80 | for (BrowserChildProcessHostIterator iter; !iter.Done(); ++iter) { |
[email protected] | 42a3ff57 | 2014-06-13 02:44:46 | [diff] [blame] | 81 | // In some cases, there may be no child process of the given type (for |
| 82 | // example, the GPU process may not exist and there may instead just be a |
| 83 | // GPU thread in the browser process). If that's the case, then the process |
| 84 | // handle will be base::kNullProcessHandle and we shouldn't ask it for data. |
| 85 | if (iter.GetData().handle == base::kNullProcessHandle) |
[email protected] | 6b69032 | 2013-07-19 08:39:34 | [diff] [blame] | 86 | continue; |
[email protected] | 6b69032 | 2013-07-19 08:39:34 | [diff] [blame] | 87 | |
[email protected] | 33047f1 | 2011-12-01 23:20:20 | [diff] [blame] | 88 | ++pending_processes; |
vadimt | e2de473 | 2015-04-27 21:43:02 | [diff] [blame] | 89 | if (!iter.Send(new ChildProcessMsg_GetChildProfilerData( |
| 90 | sequence_number, current_profiling_phase))) { |
[email protected] | 33047f1 | 2011-12-01 23:20:20 | [diff] [blame] | 91 | --pending_processes; |
vadimt | e2de473 | 2015-04-27 21:43:02 | [diff] [blame] | 92 | } |
[email protected] | 33047f1 | 2011-12-01 23:20:20 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | BrowserThread::PostTask( |
| 96 | BrowserThread::UI, |
| 97 | FROM_HERE, |
| 98 | base::Bind( |
| 99 | &ProfilerControllerImpl::OnPendingProcesses, |
| 100 | base::Unretained(this), |
| 101 | sequence_number, |
| 102 | pending_processes, |
| 103 | true)); |
| 104 | } |
| 105 | |
vadimt | e2de473 | 2015-04-27 21:43:02 | [diff] [blame] | 106 | // static |
| 107 | void ProfilerControllerImpl::NotifyChildProcessesOfProfilingPhaseCompletion( |
| 108 | int profiling_phase) { |
| 109 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 110 | |
| 111 | for (BrowserChildProcessHostIterator iter; !iter.Done(); ++iter) { |
| 112 | // In some cases, there may be no child process of the given type (for |
| 113 | // example, the GPU process may not exist and there may instead just be a |
| 114 | // GPU thread in the browser process). If that's the case, then the process |
| 115 | // handle will be base::kNullProcessHandle and we shouldn't send it a |
| 116 | // message. |
| 117 | if (iter.GetData().handle == base::kNullProcessHandle) |
| 118 | continue; |
| 119 | |
| 120 | iter.Send(new ChildProcessMsg_ProfilingPhaseCompleted(profiling_phase)); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | void ProfilerControllerImpl::GetProfilerData(int sequence_number, |
| 125 | int current_profiling_phase) { |
mostynb | 4c27d04 | 2015-03-18 21:47:47 | [diff] [blame] | 126 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 33047f1 | 2011-12-01 23:20:20 | [diff] [blame] | 127 | |
vadimt | e2de473 | 2015-04-27 21:43:02 | [diff] [blame] | 128 | // Iterates through renderers in UI thread, and through other child processes |
| 129 | // in IO thread, and send them GetChildProfilerData message. Renderers have to |
| 130 | // be contacted from UI thread, and other processes - from IO thread. |
[email protected] | 33047f1 | 2011-12-01 23:20:20 | [diff] [blame] | 131 | int pending_processes = 0; |
[email protected] | 33047f1 | 2011-12-01 23:20:20 | [diff] [blame] | 132 | for (RenderProcessHost::iterator it(RenderProcessHost::AllHostsIterator()); |
| 133 | !it.IsAtEnd(); it.Advance()) { |
| 134 | ++pending_processes; |
vadimt | e2de473 | 2015-04-27 21:43:02 | [diff] [blame] | 135 | if (!it.GetCurrentValue()->Send(new ChildProcessMsg_GetChildProfilerData( |
| 136 | sequence_number, current_profiling_phase))) { |
[email protected] | 33047f1 | 2011-12-01 23:20:20 | [diff] [blame] | 137 | --pending_processes; |
| 138 | } |
| 139 | } |
| 140 | OnPendingProcesses(sequence_number, pending_processes, false); |
| 141 | |
| 142 | BrowserThread::PostTask( |
vadimt | e2de473 | 2015-04-27 21:43:02 | [diff] [blame] | 143 | BrowserThread::IO, FROM_HERE, |
[email protected] | 33047f1 | 2011-12-01 23:20:20 | [diff] [blame] | 144 | base::Bind(&ProfilerControllerImpl::GetProfilerDataFromChildProcesses, |
vadimt | e2de473 | 2015-04-27 21:43:02 | [diff] [blame] | 145 | base::Unretained(this), sequence_number, |
| 146 | current_profiling_phase)); |
| 147 | } |
| 148 | |
| 149 | void ProfilerControllerImpl::OnProfilingPhaseCompleted(int profiling_phase) { |
| 150 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 151 | |
| 152 | // Iterates through renderers in UI thread, and through other child processes |
| 153 | // in IO thread, and send them OnProfilingPhase message. Renderers have to be |
| 154 | // contacted from UI thread, and other processes - from IO thread. |
| 155 | for (RenderProcessHost::iterator it(RenderProcessHost::AllHostsIterator()); |
| 156 | !it.IsAtEnd(); it.Advance()) { |
| 157 | it.GetCurrentValue()->Send( |
| 158 | new ChildProcessMsg_ProfilingPhaseCompleted(profiling_phase)); |
| 159 | } |
| 160 | |
| 161 | BrowserThread::PostTask( |
| 162 | BrowserThread::IO, FROM_HERE, |
| 163 | base::Bind(&ProfilerControllerImpl:: |
| 164 | NotifyChildProcessesOfProfilingPhaseCompletion, |
| 165 | profiling_phase)); |
[email protected] | 33047f1 | 2011-12-01 23:20:20 | [diff] [blame] | 166 | } |
| 167 | |
[email protected] | 33047f1 | 2011-12-01 23:20:20 | [diff] [blame] | 168 | } // namespace content |