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_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_trace_message_filter.h" | |
10 #include "components/tracing/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 const 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 return AbortDumpRequest(args, callback); | |
80 | |
81 // Make sure we access |ctmf_| only on the thread where it lives to avoid | |
82 // races on shutdown. | |
83 if (!ctmf_task_runner->BelongsToCurrentThread()) { | |
84 const bool did_post_task = ctmf_task_runner->PostTask( | |
85 FROM_HERE, | |
86 base::Bind(&ChildMemoryDumpManagerDelegateImpl::RequestGlobalMemoryDump, | |
87 base::Unretained(this), args, callback)); | |
88 if (!did_post_task) | |
89 return AbortDumpRequest(args, callback); | |
90 return; | |
91 } | |
92 | |
93 // The ChildTraceMessageFilter could have been destroyed while hopping on the | |
94 // right thread. If this is the case, bail out. | |
95 if (!ctmf_) | |
96 return AbortDumpRequest(args, callback); | |
97 | |
98 // Send the request up to the browser process' MessageDumpmanager. | |
99 ctmf_->SendGlobalMemoryDumpRequest(args, callback); | |
100 } | |
101 | |
102 uint64_t ChildMemoryDumpManagerDelegateImpl::GetTracingProcessId() const { | |
103 return tracing_process_id_; | |
104 } | |
105 | |
106 } // namespace tracing | |
OLD | NEW |