Commit 06a20b56 authored by Erik Chen's avatar Erik Chen Committed by Commit Bot

Clean up components/heap_profiling.

This CL has no intended behavior change.

This CL cleans up test structure and updates some comments.

Change-Id: I3b5ed07813ec416dd67098d9f93b702a115cee09
Bug: 827545
Reviewed-on: https://chromium-review.googlesource.com/1014371
Commit-Queue: Erik Chen <erikchen@chromium.org>
Reviewed-by: default avatarNico Weber <thakis@chromium.org>
Cr-Commit-Position: refs/heads/master@{#551418}
parent 04bc5ee9
...@@ -136,7 +136,7 @@ void ProfilingProcessHost::SaveTraceWithHeapDumpToFile( ...@@ -136,7 +136,7 @@ void ProfilingProcessHost::SaveTraceWithHeapDumpToFile(
}, },
std::move(dest), std::move(done)); std::move(dest), std::move(done));
Supervisor::GetInstance()->RequestTraceWithHeapDump( Supervisor::GetInstance()->RequestTraceWithHeapDump(
std::move(finish_trace_callback), false /* anonymize */); std::move(finish_trace_callback), /*anonymize=*/false);
} }
void ProfilingProcessHost::RequestProcessReport(std::string trigger_name) { void ProfilingProcessHost::RequestProcessReport(std::string trigger_name) {
......
...@@ -251,6 +251,7 @@ void MemoryInternalsDOMHandler::HandleReportProcess( ...@@ -251,6 +251,7 @@ void MemoryInternalsDOMHandler::HandleReportProcess(
void MemoryInternalsDOMHandler::HandleStartProfiling( void MemoryInternalsDOMHandler::HandleStartProfiling(
const base::ListValue* args) { const base::ListValue* args) {
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
if (!args->is_list() || args->GetList().size() != 1) if (!args->is_list() || args->GetList().size() != 1)
return; return;
...@@ -294,6 +295,8 @@ void MemoryInternalsDOMHandler::GetProfiledPids( ...@@ -294,6 +295,8 @@ void MemoryInternalsDOMHandler::GetProfiledPids(
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
heap_profiling::Supervisor* supervisor = heap_profiling::Supervisor* supervisor =
heap_profiling::Supervisor::GetInstance(); heap_profiling::Supervisor::GetInstance();
// The supervisor hasn't started, so return an empty list.
if (!supervisor->HasStarted()) { if (!supervisor->HasStarted()) {
content::BrowserThread::PostTask( content::BrowserThread::PostTask(
content::BrowserThread::UI, FROM_HERE, content::BrowserThread::UI, FROM_HERE,
...@@ -302,6 +305,7 @@ void MemoryInternalsDOMHandler::GetProfiledPids( ...@@ -302,6 +305,7 @@ void MemoryInternalsDOMHandler::GetProfiledPids(
std::vector<base::ProcessId>())); std::vector<base::ProcessId>()));
return; return;
} }
supervisor->GetProfiledPids( supervisor->GetProfiledPids(
base::BindOnce(&MemoryInternalsDOMHandler::ReturnProcessListOnUIThread, base::BindOnce(&MemoryInternalsDOMHandler::ReturnProcessListOnUIThread,
weak_factory_.GetWeakPtr(), std::move(children))); weak_factory_.GetWeakPtr(), std::move(children)));
......
...@@ -560,7 +560,7 @@ bool TestDriver::RunTest(const Options& options) { ...@@ -560,7 +560,7 @@ bool TestDriver::RunTest(const Options& options) {
} }
if (running_on_ui_thread_) { if (running_on_ui_thread_) {
if (!CheckOrStartProfiling()) if (!CheckOrStartProfilingOnUIThreadWithNestedRunLoops())
return false; return false;
Supervisor::GetInstance()->SetKeepSmallAllocations(true); Supervisor::GetInstance()->SetKeepSmallAllocations(true);
if (ShouldProfileRenderer()) if (ShouldProfileRenderer())
...@@ -629,7 +629,8 @@ void TestDriver::GetHasStartedOnUIThread() { ...@@ -629,7 +629,8 @@ void TestDriver::GetHasStartedOnUIThread() {
void TestDriver::CheckOrStartProfilingOnUIThreadAndSignal() { void TestDriver::CheckOrStartProfilingOnUIThreadAndSignal() {
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
initialization_success_ = CheckOrStartProfiling(); initialization_success_ =
CheckOrStartProfilingOnUIThreadWithAsyncSignalling();
// If the flag is true, then the WaitableEvent will be signaled after // If the flag is true, then the WaitableEvent will be signaled after
// profiling has started. // profiling has started.
...@@ -643,25 +644,19 @@ void TestDriver::SetKeepSmallAllocationsOnUIThreadAndSignal() { ...@@ -643,25 +644,19 @@ void TestDriver::SetKeepSmallAllocationsOnUIThreadAndSignal() {
wait_for_ui_thread_.Signal(); wait_for_ui_thread_.Signal();
} }
bool TestDriver::CheckOrStartProfiling() { bool TestDriver::CheckOrStartProfilingOnUIThreadWithAsyncSignalling() {
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
if (options_.profiling_already_started) { if (options_.profiling_already_started) {
if (Supervisor::GetInstance()->HasStarted()) { if (!Supervisor::GetInstance()->HasStarted()) {
LOG(ERROR) << "Profiling should have been started, but wasn't";
return false;
}
// Even if profiling has started, it's possible that the allocator shim // Even if profiling has started, it's possible that the allocator shim
// has not yet been initialized. Wait for it. // has not yet been initialized. Wait for it.
if (ShouldProfileBrowser()) { if (ShouldProfileBrowser()) {
bool already_initialized = false; bool already_initialized = SetOnInitAllocatorShimCallbackForTesting(
std::unique_ptr<base::RunLoop> run_loop;
if (running_on_ui_thread_) {
run_loop.reset(new base::RunLoop);
already_initialized = SetOnInitAllocatorShimCallbackForTesting(
run_loop->QuitClosure(), base::ThreadTaskRunnerHandle::Get());
if (!already_initialized)
run_loop->Run();
} else {
already_initialized = SetOnInitAllocatorShimCallbackForTesting(
base::Bind(&base::WaitableEvent::Signal, base::Bind(&base::WaitableEvent::Signal,
base::Unretained(&wait_for_ui_thread_)), base::Unretained(&wait_for_ui_thread_)),
base::ThreadTaskRunnerHandle::Get()); base::ThreadTaskRunnerHandle::Get());
...@@ -669,13 +664,63 @@ bool TestDriver::CheckOrStartProfiling() { ...@@ -669,13 +664,63 @@ bool TestDriver::CheckOrStartProfiling() {
wait_for_profiling_to_start_ = true; wait_for_profiling_to_start_ = true;
} }
} }
}
return true; return true;
} }
content::ServiceManagerConnection* connection =
content::ServiceManagerConnection::GetForProcess();
if (!connection) {
LOG(ERROR) << "A ServiceManagerConnection was not available for the "
"current process.";
return false;
}
wait_for_profiling_to_start_ = true;
base::OnceClosure start_callback;
// If we're going to profile the browser, then wait for the allocator shim to
// start. Otherwise, wait for the Supervisor to start.
if (ShouldProfileBrowser()) {
SetOnInitAllocatorShimCallbackForTesting(
base::Bind(&base::WaitableEvent::Signal,
base::Unretained(&wait_for_ui_thread_)),
base::ThreadTaskRunnerHandle::Get());
} else {
start_callback = base::BindOnce(&base::WaitableEvent::Signal,
base::Unretained(&wait_for_ui_thread_));
}
uint32_t sampling_rate = options_.should_sample
? (options_.sample_everything ? 2 : kSampleRate)
: 1;
Supervisor::GetInstance()->Start(connection, options_.mode,
options_.stack_mode, sampling_rate,
std::move(start_callback));
return true;
}
bool TestDriver::CheckOrStartProfilingOnUIThreadWithNestedRunLoops() {
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
if (options_.profiling_already_started) {
if (!Supervisor::GetInstance()->HasStarted()) {
LOG(ERROR) << "Profiling should have been started, but wasn't"; LOG(ERROR) << "Profiling should have been started, but wasn't";
return false; return false;
} }
// Even if profiling has started, it's possible that the allocator shim
// has not yet been initialized. Wait for it.
if (ShouldProfileBrowser()) {
std::unique_ptr<base::RunLoop> run_loop(new base::RunLoop);
bool already_initialized = SetOnInitAllocatorShimCallbackForTesting(
run_loop->QuitClosure(), base::ThreadTaskRunnerHandle::Get());
if (!already_initialized)
run_loop->Run();
}
return true;
}
content::ServiceManagerConnection* connection = content::ServiceManagerConnection* connection =
content::ServiceManagerConnection::GetForProcess(); content::ServiceManagerConnection::GetForProcess();
if (!connection) { if (!connection) {
...@@ -686,32 +731,16 @@ bool TestDriver::CheckOrStartProfiling() { ...@@ -686,32 +731,16 @@ bool TestDriver::CheckOrStartProfiling() {
// When this is not-null, initialization should wait for the QuitClosure to be // When this is not-null, initialization should wait for the QuitClosure to be
// called. // called.
std::unique_ptr<base::RunLoop> run_loop; std::unique_ptr<base::RunLoop> run_loop(new base::RunLoop);
base::OnceClosure start_callback; base::OnceClosure start_callback;
// If we're going to profile the browser, then wait for the allocator shim to // If we're going to profile the browser, then wait for the allocator shim to
// start. Otherwise, wait for the Supervisor to start. // start. Otherwise, wait for the Supervisor to start.
if (ShouldProfileBrowser()) { if (ShouldProfileBrowser()) {
if (running_on_ui_thread_) {
run_loop.reset(new base::RunLoop);
SetOnInitAllocatorShimCallbackForTesting( SetOnInitAllocatorShimCallbackForTesting(
run_loop->QuitClosure(), base::ThreadTaskRunnerHandle::Get()); run_loop->QuitClosure(), base::ThreadTaskRunnerHandle::Get());
} else { } else {
wait_for_profiling_to_start_ = true;
SetOnInitAllocatorShimCallbackForTesting(
base::Bind(&base::WaitableEvent::Signal,
base::Unretained(&wait_for_ui_thread_)),
base::ThreadTaskRunnerHandle::Get());
}
} else {
if (running_on_ui_thread_) {
run_loop.reset(new base::RunLoop);
start_callback = run_loop->QuitClosure(); start_callback = run_loop->QuitClosure();
} else {
wait_for_profiling_to_start_ = true;
start_callback = base::BindOnce(&base::WaitableEvent::Signal,
base::Unretained(&wait_for_ui_thread_));
}
} }
uint32_t sampling_rate = options_.should_sample uint32_t sampling_rate = options_.should_sample
...@@ -721,7 +750,6 @@ bool TestDriver::CheckOrStartProfiling() { ...@@ -721,7 +750,6 @@ bool TestDriver::CheckOrStartProfiling() {
options_.stack_mode, sampling_rate, options_.stack_mode, sampling_rate,
std::move(start_callback)); std::move(start_callback));
if (run_loop)
run_loop->Run(); run_loop->Run();
return true; return true;
......
...@@ -89,7 +89,15 @@ class TestDriver { ...@@ -89,7 +89,15 @@ class TestDriver {
// If profiling is expected to already be started, confirm it. // If profiling is expected to already be started, confirm it.
// Otherwise, start profiling with the given mode. // Otherwise, start profiling with the given mode.
bool CheckOrStartProfiling(); // This method must only be called on platforms that supported nested run
// loops on the UI thread.
bool CheckOrStartProfilingOnUIThreadWithNestedRunLoops();
// If profiling is expected to already be started, confirm it.
// Otherwise, start profiling with the given mode.
// This method must only be called on platforms that are running the
// TestDriver from a non-UI thread, which allows for async signalling.
bool CheckOrStartProfilingOnUIThreadWithAsyncSignalling();
// Performs allocations. These are expected to be profiled. // Performs allocations. These are expected to be profiled.
void MakeTestAllocations(); void MakeTestAllocations();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment