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/trace_event.h"
21 class SingleThreadTaskRunner
;
23 namespace trace_event
{
26 class ProcessMemoryDumpHolder
;
29 class MemoryDumpManagerDelegate
;
30 class MemoryDumpProvider
;
31 class ProcessMemoryDump
;
32 class MemoryDumpSessionState
;
34 // This is the interface exposed to the rest of the codebase to deal with
35 // memory tracing. The main entry point for clients is represented by
36 // RequestDumpPoint(). The extension by Un(RegisterDumpProvider).
37 class BASE_EXPORT MemoryDumpManager
: public TraceLog::EnabledStateObserver
{
39 static const char* const kTraceCategoryForTesting
;
41 static MemoryDumpManager
* GetInstance();
43 // Invoked once per process to register the TraceLog observer.
46 // See the lifetime and thread-safety requirements on the delegate below in
47 // the |MemoryDumpManagerDelegate| docstring.
48 void SetDelegate(MemoryDumpManagerDelegate
* delegate
);
50 // MemoryDumpManager does NOT take memory ownership of |mdp|, which is
51 // expected to either be a singleton or unregister itself.
52 // If the optional |task_runner| argument is non-null, all the calls to the
53 // |mdp| will be issues on the given thread. Otherwise, the |mdp| should be
54 // able to handle calls on arbitrary threads.
55 void RegisterDumpProvider(
56 MemoryDumpProvider
* mdp
,
57 const scoped_refptr
<SingleThreadTaskRunner
>& task_runner
);
58 void RegisterDumpProvider(MemoryDumpProvider
* mdp
);
59 void UnregisterDumpProvider(MemoryDumpProvider
* mdp
);
61 // Requests a memory dump. The dump might happen or not depending on the
62 // filters and categories specified when enabling tracing.
63 // The optional |callback| is executed asynchronously, on an arbitrary thread,
64 // to notify about the completion of the global dump (i.e. after all the
65 // processes have dumped) and its success (true iff all the dumps were
67 void RequestGlobalDump(MemoryDumpType dump_type
,
68 const MemoryDumpCallback
& callback
);
70 // Same as above (still asynchronous), but without callback.
71 void RequestGlobalDump(MemoryDumpType dump_type
);
73 // TraceLog::EnabledStateObserver implementation.
74 void OnTraceLogEnabled() override
;
75 void OnTraceLogDisabled() override
;
77 // Returns the MemoryDumpSessionState object, which is shared by all the
78 // ProcessMemoryDump and MemoryAllocatorDump instances through all the tracing
80 const scoped_refptr
<MemoryDumpSessionState
>& session_state() const {
81 return session_state_
;
85 // Descriptor struct used to hold information about registered MDPs. It is
86 // deliberately copyable, in order to allow to be used as hash_map value.
87 struct MemoryDumpProviderInfo
{
88 MemoryDumpProviderInfo(
89 const scoped_refptr
<SingleThreadTaskRunner
>& task_runner
);
90 ~MemoryDumpProviderInfo();
92 scoped_refptr
<SingleThreadTaskRunner
> task_runner
; // Optional.
93 bool disabled
; // For fail-safe logic (auto-disable failing MDPs).
96 friend struct DefaultDeleter
<MemoryDumpManager
>; // For the testing instance.
97 friend struct DefaultSingletonTraits
<MemoryDumpManager
>;
98 friend class MemoryDumpManagerDelegate
;
99 friend class MemoryDumpManagerTest
;
101 static void SetInstanceForTesting(MemoryDumpManager
* instance
);
104 virtual ~MemoryDumpManager();
106 // Internal, used only by MemoryDumpManagerDelegate.
107 // Creates a memory dump for the current process and appends it to the trace.
108 // |callback| will be invoked asynchronously upon completion on the same
109 // thread on which CreateProcessDump() was called.
110 void CreateProcessDump(const MemoryDumpRequestArgs
& args
,
111 const MemoryDumpCallback
& callback
);
113 bool InvokeDumpProviderLocked(MemoryDumpProvider
* mdp
,
114 ProcessMemoryDump
* pmd
);
115 void ContinueAsyncProcessDump(
116 MemoryDumpProvider
* mdp
,
117 scoped_refptr
<ProcessMemoryDumpHolder
> pmd_holder
);
119 hash_map
<MemoryDumpProvider
*, MemoryDumpProviderInfo
> dump_providers_
;
121 // Shared among all the PMDs to keep state scoped to the tracing session.
122 scoped_refptr
<MemoryDumpSessionState
> session_state_
;
124 MemoryDumpManagerDelegate
* delegate_
; // Not owned.
126 // Protects from concurrent accesses to the |dump_providers_*| and |delegate_|
127 // to guard against disabling logging while dumping on another thread.
130 // Optimization to avoid attempting any memory dump (i.e. to not walk an empty
131 // dump_providers_enabled_ list) when tracing is not enabled.
132 subtle::AtomicWord memory_tracing_enabled_
;
134 // For time-triggered periodic dumps.
135 RepeatingTimer
<MemoryDumpManager
> periodic_dump_timer_
;
137 // Skips the auto-registration of the core dumpers during Initialize().
138 bool skip_core_dumpers_auto_registration_for_testing_
;
140 DISALLOW_COPY_AND_ASSIGN(MemoryDumpManager
);
143 // The delegate is supposed to be long lived (read: a Singleton) and thread
144 // safe (i.e. should expect calls from any thread and handle thread hopping).
145 class BASE_EXPORT MemoryDumpManagerDelegate
{
147 virtual void RequestGlobalMemoryDump(const MemoryDumpRequestArgs
& args
,
148 const MemoryDumpCallback
& callback
) = 0;
150 // Determines whether the MemoryDumpManager instance should be the master
151 // (the ones which initiates and coordinates the multiprocess dumps) or not.
152 virtual bool IsCoordinatorProcess() const = 0;
155 MemoryDumpManagerDelegate() {}
156 virtual ~MemoryDumpManagerDelegate() {}
158 void CreateProcessDump(const MemoryDumpRequestArgs
& args
,
159 const MemoryDumpCallback
& callback
) {
160 MemoryDumpManager::GetInstance()->CreateProcessDump(args
, callback
);
164 DISALLOW_COPY_AND_ASSIGN(MemoryDumpManagerDelegate
);
167 } // namespace trace_event
170 #endif // BASE_TRACE_EVENT_MEMORY_DUMP_MANAGER_H_