[tracing] Fix MemoryDumpManager to support startup tracing
[chromium-blink-merge.git] / components / tracing / child_memory_dump_manager_delegate_impl.cc
blob2b537c98bef4adf95bd51800b88e48235932eee9
1 // Copyright 2015 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 "components/tracing/child_memory_dump_manager_delegate_impl.h"
7 #include "base/single_thread_task_runner.h"
8 #include "components/tracing/child_trace_message_filter.h"
10 namespace tracing {
12 // static
13 ChildMemoryDumpManagerDelegateImpl*
14 ChildMemoryDumpManagerDelegateImpl::GetInstance() {
15 return base::Singleton<
16 ChildMemoryDumpManagerDelegateImpl,
17 base::LeakySingletonTraits<ChildMemoryDumpManagerDelegateImpl>>::get();
20 ChildMemoryDumpManagerDelegateImpl::ChildMemoryDumpManagerDelegateImpl()
21 : ctmf_(nullptr),
22 tracing_process_id_(
23 base::trace_event::MemoryDumpManager::kInvalidTracingProcessId) {
24 base::trace_event::MemoryDumpManager::GetInstance()->Initialize(
25 this /* delegate */, false /* is_coordinator */);
28 ChildMemoryDumpManagerDelegateImpl::~ChildMemoryDumpManagerDelegateImpl() {}
30 void ChildMemoryDumpManagerDelegateImpl::SetChildTraceMessageFilter(
31 ChildTraceMessageFilter* ctmf) {
32 // Check that we are either registering the CTMF or tearing it down, but not
33 // replacing a valid instance with another one (should never happen).
34 DCHECK(ctmf_ == nullptr || (ctmf == nullptr && ctmf_task_runner_ != nullptr));
35 ctmf_ = ctmf;
36 ctmf_task_runner_ = ctmf ? (ctmf->ipc_task_runner()) : nullptr;
39 // Invoked in child processes by the MemoryDumpManager.
40 void ChildMemoryDumpManagerDelegateImpl::RequestGlobalMemoryDump(
41 const base::trace_event::MemoryDumpRequestArgs& args,
42 const base::trace_event::MemoryDumpCallback& callback) {
43 // Bail out if we receive a dump request from the manager before the
44 // ChildTraceMessageFilter has been initialized.
45 if (!ctmf_task_runner_) {
46 if (!callback.is_null())
47 callback.Run(args.dump_guid, false /* success */);
48 return;
51 // Make sure we access |ctmf_| only on the thread where it lives to avoid
52 // races on shutdown.
53 if (!ctmf_task_runner_->BelongsToCurrentThread()) {
54 ctmf_task_runner_->PostTask(
55 FROM_HERE,
56 base::Bind(&ChildMemoryDumpManagerDelegateImpl::RequestGlobalMemoryDump,
57 base::Unretained(this), args, callback));
58 return;
61 // The ChildTraceMessageFilter could have been destroyed while hopping on the
62 // right thread. If this is the case, bail out.
63 if (!ctmf_) {
64 if (!callback.is_null())
65 callback.Run(args.dump_guid, false /* success */);
66 return;
69 // Send the request up to the browser process' MessageDumpmanager.
70 ctmf_->SendGlobalMemoryDumpRequest(args, callback);
73 uint64 ChildMemoryDumpManagerDelegateImpl::GetTracingProcessId() const {
74 return tracing_process_id_;
77 } // namespace tracing