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/memory/singleton.h"
12 #include "base/synchronization/lock.h"
13 #include "base/trace_event/memory_dump_request_args.h"
14 #include "base/trace_event/trace_event.h"
17 namespace trace_event
{
19 class MemoryDumpManagerDelegate
;
20 class MemoryDumpProvider
;
22 // This is the interface exposed to the rest of the codebase to deal with
23 // memory tracing. The main entry point for clients is represented by
24 // RequestDumpPoint(). The extension by Un(RegisterDumpProvider).
25 class BASE_EXPORT MemoryDumpManager
: public TraceLog::EnabledStateObserver
{
27 static MemoryDumpManager
* GetInstance();
29 // Invoked once per process to register the TraceLog observer.
32 // See the lifetime and thread-safety requirements on the delegate below in
33 // the |MemoryDumpManagerDelegate| docstring.
34 void SetDelegate(MemoryDumpManagerDelegate
* delegate
);
36 // MemoryDumpManager does NOT take memory ownership of |mdp|, which is
37 // expected to be a singleton.
38 void RegisterDumpProvider(MemoryDumpProvider
* mdp
);
39 void UnregisterDumpProvider(MemoryDumpProvider
* mdp
);
41 // Requests a memory dump. The dump might happen or not depending on the
42 // filters and categories specified when enabling tracing.
43 // The optional |callback| is executed asynchronously, on an arbitrary thread,
44 // to notify about the completion of the global dump (i.e. after all the
45 // processes have dumped) and its success (true iff all the dumps were
47 void RequestGlobalDump(MemoryDumpType dump_type
,
48 const MemoryDumpCallback
& callback
);
50 // Same as above, but without callback.
51 void RequestGlobalDump(MemoryDumpType dump_type
);
53 // Creates a memory dump for the current process and appends it to the trace.
54 void CreateProcessDump(const MemoryDumpRequestArgs
& args
);
56 // TraceLog::EnabledStateObserver implementation.
57 void OnTraceLogEnabled() override
;
58 void OnTraceLogDisabled() override
;
60 // Returns the MemoryDumpProvider which is currently being dumping into a
61 // ProcessMemoryDump via DumpInto(...) if any, nullptr otherwise.
62 MemoryDumpProvider
* dump_provider_currently_active() const {
63 return dump_provider_currently_active_
;
67 friend struct DefaultDeleter
<MemoryDumpManager
>; // For the testing instance.
68 friend struct DefaultSingletonTraits
<MemoryDumpManager
>;
69 friend class MemoryDumpManagerTest
;
71 static const char kTraceCategory
[];
73 static void SetInstanceForTesting(MemoryDumpManager
* instance
);
76 virtual ~MemoryDumpManager();
78 std::vector
<MemoryDumpProvider
*> dump_providers_registered_
; // Not owned.
79 std::vector
<MemoryDumpProvider
*> dump_providers_enabled_
; // Not owned.
81 // TODO(primiano): this is required only until crbug.com/466121 gets fixed.
82 MemoryDumpProvider
* dump_provider_currently_active_
; // Not owned.
84 MemoryDumpManagerDelegate
* delegate_
; // Not owned.
86 // Protects from concurrent accesses to the |dump_providers_*| and |delegate_|
87 // to guard against disabling logging while dumping on another thread.
90 // Optimization to avoid attempting any memory dump (i.e. to not walk an empty
91 // dump_providers_enabled_ list) when tracing is not enabled.
92 subtle::AtomicWord memory_tracing_enabled_
;
94 DISALLOW_COPY_AND_ASSIGN(MemoryDumpManager
);
97 // The delegate is supposed to be long lived (read: a Singleton) and thread
98 // safe (i.e. should expect calls from any thread and handle thread hopping).
99 class BASE_EXPORT MemoryDumpManagerDelegate
{
101 virtual void RequestGlobalMemoryDump(const MemoryDumpRequestArgs
& args
,
102 const MemoryDumpCallback
& callback
) = 0;
105 MemoryDumpManagerDelegate() {}
106 virtual ~MemoryDumpManagerDelegate() {}
109 DISALLOW_COPY_AND_ASSIGN(MemoryDumpManagerDelegate
);
112 } // namespace trace_event
115 #endif // BASE_TRACE_EVENT_MEMORY_DUMP_MANAGER_H_