[tracing] Introduce MemoryDumpSessionState
[chromium-blink-merge.git] / base / trace_event / memory_dump_manager.h
blob6fc2341e40e98a9964fc9574b9f56a134d47b8de
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_
8 #include <vector>
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/trace_event/memory_dump_request_args.h"
16 #include "base/trace_event/trace_event.h"
18 namespace base {
19 namespace trace_event {
21 namespace {
22 class ProcessMemoryDumpHolder;
25 class MemoryDumpManagerDelegate;
26 class MemoryDumpProvider;
27 class ProcessMemoryDump;
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 {
34 public:
35 static const char* const kTraceCategoryForTesting;
37 static MemoryDumpManager* GetInstance();
39 // Invoked once per process to register the TraceLog observer.
40 void Initialize();
42 // See the lifetime and thread-safety requirements on the delegate below in
43 // the |MemoryDumpManagerDelegate| docstring.
44 void SetDelegate(MemoryDumpManagerDelegate* delegate);
46 // MemoryDumpManager does NOT take memory ownership of |mdp|, which is
47 // expected to either be a singleton or unregister itself.
48 void RegisterDumpProvider(MemoryDumpProvider* mdp);
49 void UnregisterDumpProvider(MemoryDumpProvider* mdp);
51 // Requests a memory dump. The dump might happen or not depending on the
52 // filters and categories specified when enabling tracing.
53 // The optional |callback| is executed asynchronously, on an arbitrary thread,
54 // to notify about the completion of the global dump (i.e. after all the
55 // processes have dumped) and its success (true iff all the dumps were
56 // successful).
57 void RequestGlobalDump(MemoryDumpType dump_type,
58 const MemoryDumpCallback& callback);
60 // Same as above (still asynchronous), but without callback.
61 void RequestGlobalDump(MemoryDumpType dump_type);
63 // TraceLog::EnabledStateObserver implementation.
64 void OnTraceLogEnabled() override;
65 void OnTraceLogDisabled() override;
67 // Returns the MemoryDumpProvider which is currently being dumping into a
68 // ProcessMemoryDump via DumpInto(...) if any, nullptr otherwise.
69 MemoryDumpProvider* dump_provider_currently_active() const {
70 return dump_provider_currently_active_;
73 // Returns the MemoryDumpSessionState object, which is shared by all the
74 // ProcessMemoryDump and MemoryAllocatorDump instances through all the tracing
75 // session lifetime.
76 const scoped_refptr<MemoryDumpSessionState>& session_state() const {
77 return session_state_;
80 private:
81 friend struct DefaultDeleter<MemoryDumpManager>; // For the testing instance.
82 friend struct DefaultSingletonTraits<MemoryDumpManager>;
83 friend class MemoryDumpManagerDelegate;
84 friend class MemoryDumpManagerTest;
86 static void SetInstanceForTesting(MemoryDumpManager* instance);
88 MemoryDumpManager();
89 virtual ~MemoryDumpManager();
91 // Internal, used only by MemoryDumpManagerDelegate.
92 // Creates a memory dump for the current process and appends it to the trace.
93 // |callback| will be invoked asynchronously upon completion on the same
94 // thread on which CreateProcessDump() was called.
95 void CreateProcessDump(const MemoryDumpRequestArgs& args,
96 const MemoryDumpCallback& callback);
98 bool InvokeDumpProviderLocked(MemoryDumpProvider* mdp,
99 ProcessMemoryDump* pmd);
100 void ContinueAsyncProcessDump(
101 MemoryDumpProvider* mdp,
102 scoped_refptr<ProcessMemoryDumpHolder> pmd_holder);
104 hash_set<MemoryDumpProvider*> dump_providers_registered_; // Not owned.
105 hash_set<MemoryDumpProvider*> dump_providers_enabled_; // Not owned.
107 // TODO(primiano): this is required only until crbug.com/466121 gets fixed.
108 MemoryDumpProvider* dump_provider_currently_active_; // Not owned.
110 // Shared among all the PMDs to keep state scoped to the tracing session.
111 scoped_refptr<MemoryDumpSessionState> session_state_;
113 MemoryDumpManagerDelegate* delegate_; // Not owned.
115 // Protects from concurrent accesses to the |dump_providers_*| and |delegate_|
116 // to guard against disabling logging while dumping on another thread.
117 Lock lock_;
119 // Optimization to avoid attempting any memory dump (i.e. to not walk an empty
120 // dump_providers_enabled_ list) when tracing is not enabled.
121 subtle::AtomicWord memory_tracing_enabled_;
123 DISALLOW_COPY_AND_ASSIGN(MemoryDumpManager);
126 // The delegate is supposed to be long lived (read: a Singleton) and thread
127 // safe (i.e. should expect calls from any thread and handle thread hopping).
128 class BASE_EXPORT MemoryDumpManagerDelegate {
129 public:
130 virtual void RequestGlobalMemoryDump(const MemoryDumpRequestArgs& args,
131 const MemoryDumpCallback& callback) = 0;
133 protected:
134 MemoryDumpManagerDelegate() {}
135 virtual ~MemoryDumpManagerDelegate() {}
137 void CreateProcessDump(const MemoryDumpRequestArgs& args,
138 const MemoryDumpCallback& callback) {
139 MemoryDumpManager::GetInstance()->CreateProcessDump(args, callback);
142 private:
143 DISALLOW_COPY_AND_ASSIGN(MemoryDumpManagerDelegate);
146 } // namespace trace_event
147 } // namespace base
149 #endif // BASE_TRACE_EVENT_MEMORY_DUMP_MANAGER_H_