Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(85)

Side by Side Diff: base/trace_event/memory_dump_manager.h

Issue 1647803004: Move base to DEPS (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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.
4
5 #ifndef BASE_TRACE_EVENT_MEMORY_DUMP_MANAGER_H_
6 #define BASE_TRACE_EVENT_MEMORY_DUMP_MANAGER_H_
7
8 #include <set>
9
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"
19
20 namespace base {
21
22 class SingleThreadTaskRunner;
23
24 namespace trace_event {
25
26 class MemoryDumpManagerDelegate;
27 class MemoryDumpProvider;
28 class MemoryDumpSessionState;
29
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;
37
38 static MemoryDumpManager* GetInstance();
39
40 // Invoked once per process to register the TraceLog observer.
41 void Initialize();
42
43 // See the lifetime and thread-safety requirements on the delegate below in
44 // the |MemoryDumpManagerDelegate| docstring.
45 void SetDelegate(MemoryDumpManagerDelegate* delegate);
46
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);
57
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);
66
67 // Same as above (still asynchronous), but without callback.
68 void RequestGlobalDump(MemoryDumpType dump_type);
69
70 // TraceLog::EnabledStateObserver implementation.
71 void OnTraceLogEnabled() override;
72 void OnTraceLogDisabled() override;
73
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_;
79 }
80
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);
90
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_; }
96
97 private:
98 friend struct DefaultDeleter<MemoryDumpManager>; // For the testing instance.
99 friend struct DefaultSingletonTraits<MemoryDumpManager>;
100 friend class MemoryDumpManagerDelegate;
101 friend class MemoryDumpManagerTest;
102 FRIEND_TEST_ALL_PREFIXES(MemoryDumpManagerTest, DisableFailingDumpers);
103
104 // Descriptor struct used to hold information about registered MDPs. It is
105 // deliberately copyable, in order to allow it to be used as std::set value.
106 struct MemoryDumpProviderInfo {
107 MemoryDumpProviderInfo(
108 MemoryDumpProvider* dump_provider,
109 const scoped_refptr<SingleThreadTaskRunner>& task_runner);
110 ~MemoryDumpProviderInfo();
111
112 // Define a total order based on the thread (i.e. |task_runner|) affinity,
113 // so that all MDP belonging to the same thread are adjacent in the set.
114 bool operator<(const MemoryDumpProviderInfo& other) const;
115
116 MemoryDumpProvider* const dump_provider;
117 scoped_refptr<SingleThreadTaskRunner> task_runner; // Optional.
118
119 // For fail-safe logic (auto-disable failing MDPs). These fields are mutable
120 // as can be safely changed without impacting the order within the set.
121 mutable int consecutive_failures;
122 mutable bool disabled;
123 };
124
125 using MemoryDumpProviderInfoSet = std::set<MemoryDumpProviderInfo>;
126
127 // Holds the state of a process memory dump that needs to be carried over
128 // across threads in order to fulfil an asynchronous CreateProcessDump()
129 // request. At any time exactly one thread owns a ProcessMemoryDumpAsyncState.
130 struct ProcessMemoryDumpAsyncState {
131 ProcessMemoryDumpAsyncState(
132 MemoryDumpRequestArgs req_args,
133 MemoryDumpProviderInfoSet::iterator next_dump_provider,
134 const scoped_refptr<MemoryDumpSessionState>& session_state,
135 MemoryDumpCallback callback);
136 ~ProcessMemoryDumpAsyncState();
137
138 // The ProcessMemoryDump container, where each dump provider will dump its
139 // own MemoryAllocatorDump(s) upon the OnMemoryDump() call.
140 ProcessMemoryDump process_memory_dump;
141
142 // The arguments passed to the initial CreateProcessDump() request.
143 const MemoryDumpRequestArgs req_args;
144
145 // The |dump_providers_| iterator to the next dump provider that should be
146 // invoked (or dump_providers_.end() if at the end of the sequence).
147 MemoryDumpProviderInfoSet::iterator next_dump_provider;
148
149 // Callback passed to the initial call to CreateProcessDump().
150 MemoryDumpCallback callback;
151
152 // The thread on which FinalizeDumpAndAddToTrace() (and hence |callback|)
153 // should be invoked. This is the thread on which the initial
154 // CreateProcessDump() request was called.
155 const scoped_refptr<SingleThreadTaskRunner> task_runner;
156
157 private:
158 DISALLOW_COPY_AND_ASSIGN(ProcessMemoryDumpAsyncState);
159 };
160
161 static const int kMaxConsecutiveFailuresCount;
162
163 MemoryDumpManager();
164 ~MemoryDumpManager() override;
165
166 static void SetInstanceForTesting(MemoryDumpManager* instance);
167 static void FinalizeDumpAndAddToTrace(
168 scoped_ptr<ProcessMemoryDumpAsyncState> pmd_async_state);
169 static void AbortDumpLocked(MemoryDumpCallback callback,
170 scoped_refptr<SingleThreadTaskRunner> task_runner,
171 uint64 dump_guid);
172
173 // Internal, used only by MemoryDumpManagerDelegate.
174 // Creates a memory dump for the current process and appends it to the trace.
175 // |callback| will be invoked asynchronously upon completion on the same
176 // thread on which CreateProcessDump() was called.
177 void CreateProcessDump(const MemoryDumpRequestArgs& args,
178 const MemoryDumpCallback& callback);
179
180 // Continues the ProcessMemoryDump started by CreateProcessDump(), hopping
181 // across threads as needed as specified by MDPs in RegisterDumpProvider().
182 void ContinueAsyncProcessDump(
183 scoped_ptr<ProcessMemoryDumpAsyncState> pmd_async_state);
184
185 // Pass kInvalidTracingProcessId to invalidate the id.
186 void set_tracing_process_id(uint64 id) {
187 DCHECK(tracing_process_id_ == kInvalidTracingProcessId ||
188 id == kInvalidTracingProcessId || tracing_process_id_ == id);
189 tracing_process_id_ = id;
190 }
191
192 // An ordererd set of registered MemoryDumpProviderInfo(s), sorted by thread
193 // affinity (MDPs belonging to the same thread are adjacent).
194 MemoryDumpProviderInfoSet dump_providers_;
195
196 // Flag used to signal that some provider was removed from |dump_providers_|
197 // and therefore the current memory dump (if any) should be aborted.
198 bool did_unregister_dump_provider_;
199
200 // Shared among all the PMDs to keep state scoped to the tracing session.
201 scoped_refptr<MemoryDumpSessionState> session_state_;
202
203 MemoryDumpManagerDelegate* delegate_; // Not owned.
204
205 // Protects from concurrent accesses to the |dump_providers_*| and |delegate_|
206 // to guard against disabling logging while dumping on another thread.
207 Lock lock_;
208
209 // Optimization to avoid attempting any memory dump (i.e. to not walk an empty
210 // dump_providers_enabled_ list) when tracing is not enabled.
211 subtle::AtomicWord memory_tracing_enabled_;
212
213 // For time-triggered periodic dumps.
214 RepeatingTimer<MemoryDumpManager> periodic_dump_timer_;
215
216 // The unique id of the child process. This is created only for tracing and is
217 // expected to be valid only when tracing is enabled.
218 uint64 tracing_process_id_;
219
220 // Skips the auto-registration of the core dumpers during Initialize().
221 bool skip_core_dumpers_auto_registration_for_testing_;
222
223 DISALLOW_COPY_AND_ASSIGN(MemoryDumpManager);
224 };
225
226 // The delegate is supposed to be long lived (read: a Singleton) and thread
227 // safe (i.e. should expect calls from any thread and handle thread hopping).
228 class BASE_EXPORT MemoryDumpManagerDelegate {
229 public:
230 virtual void RequestGlobalMemoryDump(const MemoryDumpRequestArgs& args,
231 const MemoryDumpCallback& callback) = 0;
232
233 // Determines whether the MemoryDumpManager instance should be the master
234 // (the ones which initiates and coordinates the multiprocess dumps) or not.
235 virtual bool IsCoordinatorProcess() const = 0;
236
237 protected:
238 MemoryDumpManagerDelegate() {}
239 virtual ~MemoryDumpManagerDelegate() {}
240
241 void CreateProcessDump(const MemoryDumpRequestArgs& args,
242 const MemoryDumpCallback& callback) {
243 MemoryDumpManager::GetInstance()->CreateProcessDump(args, callback);
244 }
245
246 void set_tracing_process_id(uint64 id) {
247 MemoryDumpManager::GetInstance()->set_tracing_process_id(id);
248 }
249
250 private:
251 DISALLOW_COPY_AND_ASSIGN(MemoryDumpManagerDelegate);
252 };
253
254 } // namespace trace_event
255 } // namespace base
256
257 #endif // BASE_TRACE_EVENT_MEMORY_DUMP_MANAGER_H_
OLDNEW
« no previous file with comments | « base/trace_event/memory_allocator_dump_unittest.cc ('k') | base/trace_event/memory_dump_manager.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698