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 #ifndef BASE_TRACE_EVENT_MEMORY_DUMP_MANAGER_H_
6 #define BASE_TRACE_EVENT_MEMORY_DUMP_MANAGER_H_
10 #include "base/atomicops.h"
11 #include "base/containers/hash_tables.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/singleton.h"
14 #include "base/synchronization/lock.h"
15 #include "base/timer/timer.h"
16 #include "base/trace_event/memory_dump_request_args.h"
17 #include "base/trace_event/process_memory_dump.h"
18 #include "base/trace_event/trace_event.h"
22 class SingleThreadTaskRunner
;
24 namespace trace_event
{
26 class MemoryDumpManagerDelegate
;
27 class MemoryDumpProvider
;
28 class MemoryDumpSessionState
;
30 // This is the interface exposed to the rest of the codebase to deal with
31 // memory tracing. The main entry point for clients is represented by
32 // RequestDumpPoint(). The extension by Un(RegisterDumpProvider).
33 class BASE_EXPORT MemoryDumpManager
: public TraceLog::EnabledStateObserver
{
35 static const char* const kTraceCategory
;
37 // This value is returned as the tracing id of the child processes by
38 // GetTracingProcessId() when tracing is not enabled.
39 static const uint64 kInvalidTracingProcessId
;
41 static MemoryDumpManager
* GetInstance();
43 // Invoked once per process to listen to trace begin / end events.
44 // Initialization can happen after (Un)RegisterMemoryDumpProvider() calls
45 // and the MemoryDumpManager guarantees to support this.
46 // On the other side, the MemoryDumpManager will not be fully operational
47 // (i.e. will NACK any RequestGlobalMemoryDump()) until initialized.
49 // is_coordinator: if true this MemoryDumpManager instance will act as a
50 // coordinator and schedule periodic dumps (if enabled via TraceConfig);
51 // false when the MemoryDumpManager is initialized in a slave process.
52 // delegate: inversion-of-control interface for embedder-specific behaviors
53 // (multiprocess handshaking). See the lifetime and thread-safety
54 // requirements in the |MemoryDumpManagerDelegate| docstring.
55 void Initialize(MemoryDumpManagerDelegate
* delegate
, bool is_coordinator
);
57 // MemoryDumpManager does NOT take memory ownership of |mdp|, which is
58 // expected to either be a singleton or unregister itself.
59 // If the optional |task_runner| argument is non-null, all the calls to the
60 // |mdp| will be issues on the given thread. Otherwise, the |mdp| should be
61 // able to handle calls on arbitrary threads.
62 void RegisterDumpProvider(
63 MemoryDumpProvider
* mdp
,
64 const scoped_refptr
<SingleThreadTaskRunner
>& task_runner
);
65 void RegisterDumpProvider(MemoryDumpProvider
* mdp
);
66 void UnregisterDumpProvider(MemoryDumpProvider
* mdp
);
68 // Requests a memory dump. The dump might happen or not depending on the
69 // filters and categories specified when enabling tracing.
70 // The |dump_args| is used to specify the dump's level of detail.
71 // The optional |callback| is executed asynchronously, on an arbitrary thread,
72 // to notify about the completion of the global dump (i.e. after all the
73 // processes have dumped) and its success (true iff all the dumps were
75 void RequestGlobalDump(MemoryDumpType dump_type
,
76 const MemoryDumpArgs
& dump_args
,
77 const MemoryDumpCallback
& callback
);
79 // Same as above (still asynchronous), but without callback.
80 void RequestGlobalDump(MemoryDumpType dump_type
,
81 const MemoryDumpArgs
& dump_args
);
83 // TraceLog::EnabledStateObserver implementation.
84 void OnTraceLogEnabled() override
;
85 void OnTraceLogDisabled() override
;
87 // Returns the MemoryDumpSessionState object, which is shared by all the
88 // ProcessMemoryDump and MemoryAllocatorDump instances through all the tracing
90 const scoped_refptr
<MemoryDumpSessionState
>& session_state() const {
91 return session_state_
;
94 // Returns a unique id for identifying the processes. The id can be
95 // retrieved by child processes only when tracing is enabled. This is
96 // intended to express cross-process sharing of memory dumps on the
97 // child-process side, without having to know its own child process id.
98 uint64
GetTracingProcessId() const;
100 // Returns the name for a the allocated_objects dump. Use this to declare
101 // suballocator dumps from other dump providers.
102 // It will return nullptr if there is no dump provider for the system
103 // allocator registered (which is currently the case for Mac OS).
104 const char* system_allocator_pool_name() const {
105 return kSystemAllocatorPoolName
;
108 // Tells the initialization phase to skip scheduling periodic memory dumps.
109 void DisablePeriodicDumpsForTesting() {
110 disable_periodic_dumps_for_testing_
= true;
114 friend struct DefaultDeleter
<MemoryDumpManager
>; // For the testing instance.
115 friend struct DefaultSingletonTraits
<MemoryDumpManager
>;
116 friend class MemoryDumpManagerDelegate
;
117 friend class MemoryDumpManagerTest
;
119 // Descriptor struct used to hold information about registered MDPs. It is
120 // deliberately copyable, in order to allow it to be used as std::set value.
121 struct MemoryDumpProviderInfo
{
122 MemoryDumpProviderInfo(
123 MemoryDumpProvider
* dump_provider
,
124 const scoped_refptr
<SingleThreadTaskRunner
>& task_runner
);
125 ~MemoryDumpProviderInfo();
127 // Define a total order based on the thread (i.e. |task_runner|) affinity,
128 // so that all MDP belonging to the same thread are adjacent in the set.
129 bool operator<(const MemoryDumpProviderInfo
& other
) const;
131 MemoryDumpProvider
* const dump_provider
;
132 scoped_refptr
<SingleThreadTaskRunner
> task_runner
; // Optional.
134 // For fail-safe logic (auto-disable failing MDPs). These fields are mutable
135 // as can be safely changed without impacting the order within the set.
136 mutable int consecutive_failures
;
137 mutable bool disabled
;
139 // When a dump provider unregisters, it is flagged as |unregistered| and it
140 // is removed only upon the next memory dump. This is to avoid altering the
141 // |dump_providers_| collection while a dump is in progress.
142 mutable bool unregistered
;
145 using MemoryDumpProviderInfoSet
= std::set
<MemoryDumpProviderInfo
>;
147 // Holds the state of a process memory dump that needs to be carried over
148 // across threads in order to fulfil an asynchronous CreateProcessDump()
149 // request. At any time exactly one thread owns a ProcessMemoryDumpAsyncState.
150 struct ProcessMemoryDumpAsyncState
{
151 ProcessMemoryDumpAsyncState(
152 MemoryDumpRequestArgs req_args
,
153 MemoryDumpProviderInfoSet::iterator next_dump_provider
,
154 const scoped_refptr
<MemoryDumpSessionState
>& session_state
,
155 MemoryDumpCallback callback
);
156 ~ProcessMemoryDumpAsyncState();
158 // The ProcessMemoryDump container, where each dump provider will dump its
159 // own MemoryAllocatorDump(s) upon the OnMemoryDump() call.
160 ProcessMemoryDump process_memory_dump
;
162 // The arguments passed to the initial CreateProcessDump() request.
163 const MemoryDumpRequestArgs req_args
;
165 // The |dump_providers_| iterator to the next dump provider that should be
166 // invoked (or dump_providers_.end() if at the end of the sequence).
167 MemoryDumpProviderInfoSet::iterator next_dump_provider
;
169 // Callback passed to the initial call to CreateProcessDump().
170 MemoryDumpCallback callback
;
172 // The thread on which FinalizeDumpAndAddToTrace() (and hence |callback|)
173 // should be invoked. This is the thread on which the initial
174 // CreateProcessDump() request was called.
175 const scoped_refptr
<SingleThreadTaskRunner
> task_runner
;
178 DISALLOW_COPY_AND_ASSIGN(ProcessMemoryDumpAsyncState
);
181 static const int kMaxConsecutiveFailuresCount
;
182 static const char* const kSystemAllocatorPoolName
;
185 ~MemoryDumpManager() override
;
187 static void SetInstanceForTesting(MemoryDumpManager
* instance
);
188 static void FinalizeDumpAndAddToTrace(
189 scoped_ptr
<ProcessMemoryDumpAsyncState
> pmd_async_state
);
190 static void AbortDumpLocked(MemoryDumpCallback callback
,
191 scoped_refptr
<SingleThreadTaskRunner
> task_runner
,
194 // Internal, used only by MemoryDumpManagerDelegate.
195 // Creates a memory dump for the current process and appends it to the trace.
196 // |callback| will be invoked asynchronously upon completion on the same
197 // thread on which CreateProcessDump() was called.
198 void CreateProcessDump(const MemoryDumpRequestArgs
& args
,
199 const MemoryDumpCallback
& callback
);
201 // Continues the ProcessMemoryDump started by CreateProcessDump(), hopping
202 // across threads as needed as specified by MDPs in RegisterDumpProvider().
203 void ContinueAsyncProcessDump(
204 scoped_ptr
<ProcessMemoryDumpAsyncState
> pmd_async_state
);
206 // An ordererd set of registered MemoryDumpProviderInfo(s), sorted by thread
207 // affinity (MDPs belonging to the same thread are adjacent).
208 MemoryDumpProviderInfoSet dump_providers_
;
210 // Shared among all the PMDs to keep state scoped to the tracing session.
211 scoped_refptr
<MemoryDumpSessionState
> session_state_
;
213 MemoryDumpManagerDelegate
* delegate_
; // Not owned.
215 // When true, this instance is in charge of coordinating periodic dumps.
216 bool is_coordinator_
;
218 // Protects from concurrent accesses to the |dump_providers_*| and |delegate_|
219 // to guard against disabling logging while dumping on another thread.
222 // Optimization to avoid attempting any memory dump (i.e. to not walk an empty
223 // dump_providers_enabled_ list) when tracing is not enabled.
224 subtle::AtomicWord memory_tracing_enabled_
;
226 // For time-triggered periodic dumps.
227 RepeatingTimer
<MemoryDumpManager
> periodic_dump_timer_
;
229 // The unique id of the child process. This is created only for tracing and is
230 // expected to be valid only when tracing is enabled.
231 uint64 tracing_process_id_
;
233 // Skips the auto-registration of the core dumpers during Initialize().
234 bool skip_core_dumpers_auto_registration_for_testing_
;
236 // When true, the initialization phase does not start the periodic memory
238 // TODO(primiano): This should go into TraceConfig. https://goo.gl/5Hj3o0.
239 bool disable_periodic_dumps_for_testing_
;
241 DISALLOW_COPY_AND_ASSIGN(MemoryDumpManager
);
244 // The delegate is supposed to be long lived (read: a Singleton) and thread
245 // safe (i.e. should expect calls from any thread and handle thread hopping).
246 class BASE_EXPORT MemoryDumpManagerDelegate
{
248 virtual void RequestGlobalMemoryDump(const MemoryDumpRequestArgs
& args
,
249 const MemoryDumpCallback
& callback
) = 0;
251 // Returns tracing process id of the current process. This is used by
252 // MemoryDumpManager::GetTracingProcessId.
253 virtual uint64
GetTracingProcessId() const = 0;
256 MemoryDumpManagerDelegate() {}
257 virtual ~MemoryDumpManagerDelegate() {}
259 void CreateProcessDump(const MemoryDumpRequestArgs
& args
,
260 const MemoryDumpCallback
& callback
) {
261 MemoryDumpManager::GetInstance()->CreateProcessDump(args
, callback
);
265 DISALLOW_COPY_AND_ASSIGN(MemoryDumpManagerDelegate
);
268 } // namespace trace_event
271 #endif // BASE_TRACE_EVENT_MEMORY_DUMP_MANAGER_H_