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
3 // found in the LICENSE file.
5 #include "content/browser/profiler_controller_impl.h"
8 #include "base/command_line.h"
9 #include "base/tracked_objects.h"
10 #include "content/common/child_process_messages.h"
11 #include "content/public/browser/browser_child_process_host_iterator.h"
12 #include "content/public/browser/browser_thread.h"
13 #include "content/public/browser/child_process_data.h"
14 #include "content/public/browser/profiler_subscriber.h"
15 #include "content/public/browser/render_process_host.h"
16 #include "content/public/common/content_switches.h"
20 ProfilerController
* ProfilerController::GetInstance() {
21 return ProfilerControllerImpl::GetInstance();
24 ProfilerControllerImpl
* ProfilerControllerImpl::GetInstance() {
25 return Singleton
<ProfilerControllerImpl
>::get();
28 ProfilerControllerImpl::ProfilerControllerImpl() : subscriber_(NULL
) {
31 ProfilerControllerImpl::~ProfilerControllerImpl() {
34 void ProfilerControllerImpl::OnPendingProcesses(int sequence_number
,
35 int pending_processes
,
37 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
39 subscriber_
->OnPendingProcesses(sequence_number
, pending_processes
, end
);
42 void ProfilerControllerImpl::OnProfilerDataCollected(
44 const tracked_objects::ProcessDataSnapshot
& profiler_data
,
46 if (!BrowserThread::CurrentlyOn(BrowserThread::UI
)) {
47 BrowserThread::PostTask(
48 BrowserThread::UI
, FROM_HERE
,
49 base::Bind(&ProfilerControllerImpl::OnProfilerDataCollected
,
50 base::Unretained(this),
57 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
59 subscriber_
->OnProfilerDataCollected(sequence_number
, profiler_data
,
64 void ProfilerControllerImpl::Register(ProfilerSubscriber
* subscriber
) {
65 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
67 subscriber_
= subscriber
;
70 void ProfilerControllerImpl::Unregister(const ProfilerSubscriber
* subscriber
) {
71 DCHECK_EQ(subscriber_
, subscriber
);
75 void ProfilerControllerImpl::GetProfilerDataFromChildProcesses(
76 int sequence_number
) {
77 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
79 int pending_processes
= 0;
80 for (BrowserChildProcessHostIterator iter
; !iter
.Done(); ++iter
) {
81 // Skips requesting profiler data from the "GPU Process" if we are using in
82 // process GPU. Those stats should be in the Browser-process's GPU thread.
83 if (iter
.GetData().process_type
== PROCESS_TYPE_GPU
&&
84 CommandLine::ForCurrentProcess()->HasSwitch(switches::kInProcessGPU
)) {
89 if (!iter
.Send(new ChildProcessMsg_GetChildProfilerData(sequence_number
)))
93 BrowserThread::PostTask(
97 &ProfilerControllerImpl::OnPendingProcesses
,
98 base::Unretained(this),
104 void ProfilerControllerImpl::GetProfilerData(int sequence_number
) {
105 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
107 int pending_processes
= 0;
108 for (RenderProcessHost::iterator
it(RenderProcessHost::AllHostsIterator());
109 !it
.IsAtEnd(); it
.Advance()) {
111 if (!it
.GetCurrentValue()->Send(
112 new ChildProcessMsg_GetChildProfilerData(sequence_number
))) {
116 OnPendingProcesses(sequence_number
, pending_processes
, false);
118 BrowserThread::PostTask(
121 base::Bind(&ProfilerControllerImpl::GetProfilerDataFromChildProcesses
,
122 base::Unretained(this),
126 } // namespace content