| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "components/tracing/child/child_memory_dump_manager_delegate_impl.h" | |
| 6 | |
| 7 #include "base/single_thread_task_runner.h" | |
| 8 #include "build/build_config.h" | |
| 9 #include "components/tracing/child/child_trace_message_filter.h" | |
| 10 #include "components/tracing/common/process_metrics_memory_dump_provider.h" | |
| 11 | |
| 12 namespace tracing { | |
| 13 | |
| 14 namespace { | |
| 15 void AbortDumpRequest(const base::trace_event::MemoryDumpRequestArgs& args, | |
| 16 const base::trace_event::MemoryDumpCallback& callback) { | |
| 17 if (!callback.is_null()) | |
| 18 callback.Run(args.dump_guid, false /* success */); | |
| 19 } | |
| 20 } // namespace | |
| 21 | |
| 22 // static | |
| 23 ChildMemoryDumpManagerDelegateImpl* | |
| 24 ChildMemoryDumpManagerDelegateImpl::GetInstance() { | |
| 25 return base::Singleton< | |
| 26 ChildMemoryDumpManagerDelegateImpl, | |
| 27 base::LeakySingletonTraits<ChildMemoryDumpManagerDelegateImpl>>::get(); | |
| 28 } | |
| 29 | |
| 30 ChildMemoryDumpManagerDelegateImpl::ChildMemoryDumpManagerDelegateImpl() | |
| 31 : ctmf_(nullptr), | |
| 32 tracing_process_id_( | |
| 33 base::trace_event::MemoryDumpManager::kInvalidTracingProcessId) { | |
| 34 } | |
| 35 | |
| 36 ChildMemoryDumpManagerDelegateImpl::~ChildMemoryDumpManagerDelegateImpl() {} | |
| 37 | |
| 38 void ChildMemoryDumpManagerDelegateImpl::SetChildTraceMessageFilter( | |
| 39 ChildTraceMessageFilter* ctmf) { | |
| 40 auto* task_runner = ctmf ? (ctmf->ipc_task_runner()) : nullptr; | |
| 41 // Check that we are either registering the CTMF or tearing it down, but not | |
| 42 // replacing a valid instance with another one (should never happen). | |
| 43 DCHECK(!ctmf_ || (!ctmf && ctmf_task_runner_)); | |
| 44 ctmf_ = ctmf; | |
| 45 | |
| 46 { | |
| 47 base::AutoLock lock(lock_); | |
| 48 ctmf_task_runner_ = task_runner; | |
| 49 } | |
| 50 | |
| 51 if (ctmf) { | |
| 52 base::trace_event::MemoryDumpManager::GetInstance()->Initialize( | |
| 53 this /* delegate */, false /* is_coordinator */); | |
| 54 | |
| 55 #if !defined(OS_LINUX) && !defined(OS_NACL) | |
| 56 // On linux the browser process takes care of dumping process metrics. | |
| 57 // The child process is not allowed to do so due to BPF sandbox. | |
| 58 tracing::ProcessMetricsMemoryDumpProvider::RegisterForProcess( | |
| 59 base::kNullProcessId); | |
| 60 #endif | |
| 61 } | |
| 62 } | |
| 63 | |
| 64 // Invoked in child processes by the MemoryDumpManager. | |
| 65 void ChildMemoryDumpManagerDelegateImpl::RequestGlobalMemoryDump( | |
| 66 const base::trace_event::MemoryDumpRequestArgs& args, | |
| 67 const base::trace_event::MemoryDumpCallback& callback) { | |
| 68 // RequestGlobalMemoryDump can be called on any thread, cannot access | |
| 69 // ctmf_task_runner_ as it could be racy. | |
| 70 scoped_refptr<base::SingleThreadTaskRunner> ctmf_task_runner; | |
| 71 { | |
| 72 base::AutoLock lock(lock_); | |
| 73 ctmf_task_runner = ctmf_task_runner_; | |
| 74 } | |
| 75 | |
| 76 // Bail out if we receive a dump request from the manager before the | |
| 77 // ChildTraceMessageFilter has been initialized. | |
| 78 if (!ctmf_task_runner) { | |
| 79 VLOG(1) << base::trace_event::MemoryDumpManager::kLogPrefix | |
| 80 << " failed because child trace message filter hasn't been" | |
| 81 << " initialized"; | |
| 82 return AbortDumpRequest(args, callback); | |
| 83 } | |
| 84 | |
| 85 // Make sure we access |ctmf_| only on the thread where it lives to avoid | |
| 86 // races on shutdown. | |
| 87 if (!ctmf_task_runner->BelongsToCurrentThread()) { | |
| 88 const bool did_post_task = ctmf_task_runner->PostTask( | |
| 89 FROM_HERE, | |
| 90 base::Bind(&ChildMemoryDumpManagerDelegateImpl::RequestGlobalMemoryDump, | |
| 91 base::Unretained(this), args, callback)); | |
| 92 if (!did_post_task) | |
| 93 return AbortDumpRequest(args, callback); | |
| 94 return; | |
| 95 } | |
| 96 | |
| 97 // The ChildTraceMessageFilter could have been destroyed while hopping on the | |
| 98 // right thread. If this is the case, bail out. | |
| 99 if (!ctmf_) { | |
| 100 VLOG(1) << base::trace_event::MemoryDumpManager::kLogPrefix | |
| 101 << " failed because child trace message filter was" | |
| 102 << " destroyed while switching threads"; | |
| 103 return AbortDumpRequest(args, callback); | |
| 104 } | |
| 105 | |
| 106 // Send the request up to the browser process' MessageDumpmanager. | |
| 107 ctmf_->SendGlobalMemoryDumpRequest(args, callback); | |
| 108 } | |
| 109 | |
| 110 uint64_t ChildMemoryDumpManagerDelegateImpl::GetTracingProcessId() const { | |
| 111 return tracing_process_id_; | |
| 112 } | |
| 113 | |
| 114 } // namespace tracing | |
| OLD | NEW |