Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / base / trace_event / memory_dump_manager.h
blobfbc35fdedb89eecf6ae85ea8990352bd00155296
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 <set>
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"
20 namespace base {
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 {
34 public:
35 static const uint64 kInvalidTracingProcessId;
36 static const char* const kTraceCategoryForTesting;
38 static MemoryDumpManager* GetInstance();
40 // Invoked once per process to register the TraceLog observer.
41 void Initialize();
43 // See the lifetime and thread-safety requirements on the delegate below in
44 // the |MemoryDumpManagerDelegate| docstring.
45 void SetDelegate(MemoryDumpManagerDelegate* delegate);
47 // MemoryDumpManager does NOT take memory ownership of |mdp|, which is
48 // expected to either be a singleton or unregister itself.
49 // If the optional |task_runner| argument is non-null, all the calls to the
50 // |mdp| will be issues on the given thread. Otherwise, the |mdp| should be
51 // able to handle calls on arbitrary threads.
52 void RegisterDumpProvider(
53 MemoryDumpProvider* mdp,
54 const scoped_refptr<SingleThreadTaskRunner>& task_runner);
55 void RegisterDumpProvider(MemoryDumpProvider* mdp);
56 void UnregisterDumpProvider(MemoryDumpProvider* mdp);
58 // Requests a memory dump. The dump might happen or not depending on the
59 // filters and categories specified when enabling tracing.
60 // The optional |callback| is executed asynchronously, on an arbitrary thread,
61 // to notify about the completion of the global dump (i.e. after all the
62 // processes have dumped) and its success (true iff all the dumps were
63 // successful).
64 void RequestGlobalDump(MemoryDumpType dump_type,
65 const MemoryDumpCallback& callback);
67 // Same as above (still asynchronous), but without callback.
68 void RequestGlobalDump(MemoryDumpType dump_type);
70 // TraceLog::EnabledStateObserver implementation.
71 void OnTraceLogEnabled() override;
72 void OnTraceLogDisabled() override;
74 // Returns the MemoryDumpSessionState object, which is shared by all the
75 // ProcessMemoryDump and MemoryAllocatorDump instances through all the tracing
76 // session lifetime.
77 const scoped_refptr<MemoryDumpSessionState>& session_state() const {
78 return session_state_;
81 // Derives a tracing process id from a child process id. Child process ids
82 // cannot be used directly in tracing for security reasons (see: discussion in
83 // crrev.com/1173263004). This method is meant to be used when dumping
84 // cross-process shared memory from a process which knows the child process id
85 // of its endpoints. The value returned by this method is guaranteed to be
86 // equal to the value returned by tracing_process_id() in the corresponding
87 // child process.
88 // This will never return kInvalidTracingProcessId.
89 static uint64 ChildProcessIdToTracingProcessId(int child_id);
91 // Returns a unique id for the current process. The id can be retrieved only
92 // by child processes and only when tracing is enabled. This is intended to
93 // express cross-process sharing of memory dumps on the child-process side,
94 // without having to know its own child process id.
95 uint64 tracing_process_id() const { return tracing_process_id_; }
97 // Returns the name for a the allocated_objects dump. Use this to declare
98 // suballocator dumps from other dump providers.
99 // It should not return nullptr after the manager has been initialized.
100 const char* system_allocator_pool_name() const {
101 return system_allocator_pool_name_;
104 private:
105 friend struct DefaultDeleter<MemoryDumpManager>; // For the testing instance.
106 friend struct DefaultSingletonTraits<MemoryDumpManager>;
107 friend class MemoryDumpManagerDelegate;
108 friend class MemoryDumpManagerTest;
109 FRIEND_TEST_ALL_PREFIXES(MemoryDumpManagerTest, DisableFailingDumpers);
111 // Descriptor struct used to hold information about registered MDPs. It is
112 // deliberately copyable, in order to allow it to be used as std::set value.
113 struct MemoryDumpProviderInfo {
114 MemoryDumpProviderInfo(
115 MemoryDumpProvider* dump_provider,
116 const scoped_refptr<SingleThreadTaskRunner>& task_runner);
117 ~MemoryDumpProviderInfo();
119 // Define a total order based on the thread (i.e. |task_runner|) affinity,
120 // so that all MDP belonging to the same thread are adjacent in the set.
121 bool operator<(const MemoryDumpProviderInfo& other) const;
123 MemoryDumpProvider* const dump_provider;
124 scoped_refptr<SingleThreadTaskRunner> task_runner; // Optional.
126 // For fail-safe logic (auto-disable failing MDPs). These fields are mutable
127 // as can be safely changed without impacting the order within the set.
128 mutable int consecutive_failures;
129 mutable bool disabled;
132 using MemoryDumpProviderInfoSet = std::set<MemoryDumpProviderInfo>;
134 // Holds the state of a process memory dump that needs to be carried over
135 // across threads in order to fulfil an asynchronous CreateProcessDump()
136 // request. At any time exactly one thread owns a ProcessMemoryDumpAsyncState.
137 struct ProcessMemoryDumpAsyncState {
138 ProcessMemoryDumpAsyncState(
139 MemoryDumpRequestArgs req_args,
140 MemoryDumpProviderInfoSet::iterator next_dump_provider,
141 const scoped_refptr<MemoryDumpSessionState>& session_state,
142 MemoryDumpCallback callback);
143 ~ProcessMemoryDumpAsyncState();
145 // The ProcessMemoryDump container, where each dump provider will dump its
146 // own MemoryAllocatorDump(s) upon the OnMemoryDump() call.
147 ProcessMemoryDump process_memory_dump;
149 // The arguments passed to the initial CreateProcessDump() request.
150 const MemoryDumpRequestArgs req_args;
152 // The |dump_providers_| iterator to the next dump provider that should be
153 // invoked (or dump_providers_.end() if at the end of the sequence).
154 MemoryDumpProviderInfoSet::iterator next_dump_provider;
156 // Callback passed to the initial call to CreateProcessDump().
157 MemoryDumpCallback callback;
159 // The thread on which FinalizeDumpAndAddToTrace() (and hence |callback|)
160 // should be invoked. This is the thread on which the initial
161 // CreateProcessDump() request was called.
162 const scoped_refptr<SingleThreadTaskRunner> task_runner;
164 private:
165 DISALLOW_COPY_AND_ASSIGN(ProcessMemoryDumpAsyncState);
168 static const int kMaxConsecutiveFailuresCount;
170 MemoryDumpManager();
171 ~MemoryDumpManager() override;
173 static void SetInstanceForTesting(MemoryDumpManager* instance);
174 static void FinalizeDumpAndAddToTrace(
175 scoped_ptr<ProcessMemoryDumpAsyncState> pmd_async_state);
176 static void AbortDumpLocked(MemoryDumpCallback callback,
177 scoped_refptr<SingleThreadTaskRunner> task_runner,
178 uint64 dump_guid);
180 // Internal, used only by MemoryDumpManagerDelegate.
181 // Creates a memory dump for the current process and appends it to the trace.
182 // |callback| will be invoked asynchronously upon completion on the same
183 // thread on which CreateProcessDump() was called.
184 void CreateProcessDump(const MemoryDumpRequestArgs& args,
185 const MemoryDumpCallback& callback);
187 // Continues the ProcessMemoryDump started by CreateProcessDump(), hopping
188 // across threads as needed as specified by MDPs in RegisterDumpProvider().
189 void ContinueAsyncProcessDump(
190 scoped_ptr<ProcessMemoryDumpAsyncState> pmd_async_state);
192 // Pass kInvalidTracingProcessId to invalidate the id.
193 void set_tracing_process_id(uint64 id) {
194 DCHECK(tracing_process_id_ == kInvalidTracingProcessId ||
195 id == kInvalidTracingProcessId || tracing_process_id_ == id);
196 tracing_process_id_ = id;
199 // An ordererd set of registered MemoryDumpProviderInfo(s), sorted by thread
200 // affinity (MDPs belonging to the same thread are adjacent).
201 MemoryDumpProviderInfoSet dump_providers_;
203 // Flag used to signal that some provider was removed from |dump_providers_|
204 // and therefore the current memory dump (if any) should be aborted.
205 bool did_unregister_dump_provider_;
207 // Shared among all the PMDs to keep state scoped to the tracing session.
208 scoped_refptr<MemoryDumpSessionState> session_state_;
210 MemoryDumpManagerDelegate* delegate_; // Not owned.
212 // Protects from concurrent accesses to the |dump_providers_*| and |delegate_|
213 // to guard against disabling logging while dumping on another thread.
214 Lock lock_;
216 // Optimization to avoid attempting any memory dump (i.e. to not walk an empty
217 // dump_providers_enabled_ list) when tracing is not enabled.
218 subtle::AtomicWord memory_tracing_enabled_;
220 // For time-triggered periodic dumps.
221 RepeatingTimer<MemoryDumpManager> periodic_dump_timer_;
223 // The unique id of the child process. This is created only for tracing and is
224 // expected to be valid only when tracing is enabled.
225 uint64 tracing_process_id_;
227 // Name of the allocated_objects dump.
228 const char* system_allocator_pool_name_;
230 // Skips the auto-registration of the core dumpers during Initialize().
231 bool skip_core_dumpers_auto_registration_for_testing_;
233 DISALLOW_COPY_AND_ASSIGN(MemoryDumpManager);
236 // The delegate is supposed to be long lived (read: a Singleton) and thread
237 // safe (i.e. should expect calls from any thread and handle thread hopping).
238 class BASE_EXPORT MemoryDumpManagerDelegate {
239 public:
240 virtual void RequestGlobalMemoryDump(const MemoryDumpRequestArgs& args,
241 const MemoryDumpCallback& callback) = 0;
243 // Determines whether the MemoryDumpManager instance should be the master
244 // (the ones which initiates and coordinates the multiprocess dumps) or not.
245 virtual bool IsCoordinatorProcess() const = 0;
247 protected:
248 MemoryDumpManagerDelegate() {}
249 virtual ~MemoryDumpManagerDelegate() {}
251 void CreateProcessDump(const MemoryDumpRequestArgs& args,
252 const MemoryDumpCallback& callback) {
253 MemoryDumpManager::GetInstance()->CreateProcessDump(args, callback);
256 void set_tracing_process_id(uint64 id) {
257 MemoryDumpManager::GetInstance()->set_tracing_process_id(id);
260 private:
261 DISALLOW_COPY_AND_ASSIGN(MemoryDumpManagerDelegate);
264 } // namespace trace_event
265 } // namespace base
267 #endif // BASE_TRACE_EVENT_MEMORY_DUMP_MANAGER_H_