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

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

Issue 1618703004: [MemoryInfra] Support dump providers running on SequencedTaskRunner (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixes. 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
« no previous file with comments | « no previous file | base/trace_event/memory_dump_manager.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef BASE_TRACE_EVENT_MEMORY_DUMP_MANAGER_H_ 5 #ifndef BASE_TRACE_EVENT_MEMORY_DUMP_MANAGER_H_
6 #define BASE_TRACE_EVENT_MEMORY_DUMP_MANAGER_H_ 6 #define BASE_TRACE_EVENT_MEMORY_DUMP_MANAGER_H_
7 7
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <map> 10 #include <map>
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
62 void Initialize(MemoryDumpManagerDelegate* delegate, bool is_coordinator); 62 void Initialize(MemoryDumpManagerDelegate* delegate, bool is_coordinator);
63 63
64 // (Un)Registers a MemoryDumpProvider instance. 64 // (Un)Registers a MemoryDumpProvider instance.
65 // Args: 65 // Args:
66 // - mdp: the MemoryDumpProvider instance to be registered. MemoryDumpManager 66 // - mdp: the MemoryDumpProvider instance to be registered. MemoryDumpManager
67 // does NOT take memory ownership of |mdp|, which is expected to either 67 // does NOT take memory ownership of |mdp|, which is expected to either
68 // be a singleton or unregister itself. 68 // be a singleton or unregister itself.
69 // - name: a friendly name (duplicates allowed). Used for debugging and 69 // - name: a friendly name (duplicates allowed). Used for debugging and
70 // run-time profiling of memory-infra internals. Must be a long-lived 70 // run-time profiling of memory-infra internals. Must be a long-lived
71 // C string. 71 // C string.
72 // - task_runner: if non-null, all the calls to |mdp| will be 72 // - task_runner: either a SingleThreadTaskRunner or SequencedTaskRunner. All
73 // issued on the given thread. Otherwise, |mdp| should be able to 73 // the calls to |mdp| will be run on the given |task_runner|. If passed
74 // handle calls on arbitrary threads. 74 // null |mdp| should be able to handle calls on arbitrary threads.
75 // - options: extra optional arguments. See memory_dump_provider.h. 75 // - options: extra optional arguments. See memory_dump_provider.h.
76 void RegisterDumpProvider( 76 void RegisterDumpProvider(
77 MemoryDumpProvider* mdp, 77 MemoryDumpProvider* mdp,
78 const char* name, 78 const char* name,
79 const scoped_refptr<SingleThreadTaskRunner>& task_runner); 79 const scoped_refptr<SingleThreadTaskRunner>& task_runner);
80 void RegisterDumpProvider( 80 void RegisterDumpProvider(
81 MemoryDumpProvider* mdp, 81 MemoryDumpProvider* mdp,
82 const char* name, 82 const char* name,
83 const scoped_refptr<SingleThreadTaskRunner>& task_runner, 83 const scoped_refptr<SingleThreadTaskRunner>& task_runner,
84 const MemoryDumpProvider::Options& options); 84 MemoryDumpProvider::Options options);
85 void RegisterDumpProviderWithSequencedTaskRunner(
86 MemoryDumpProvider* mdp,
87 const char* name,
88 const scoped_refptr<SequencedTaskRunner>& task_runner,
89 MemoryDumpProvider::Options options);
85 void UnregisterDumpProvider(MemoryDumpProvider* mdp); 90 void UnregisterDumpProvider(MemoryDumpProvider* mdp);
86 91
87 // Unregisters an unbound dump provider and takes care about its deletion 92 // Unregisters an unbound dump provider and takes care about its deletion
88 // asynchronously. Can be used only for for dump providers with no 93 // asynchronously. Can be used only for for dump providers with no
89 // task-runner affinity. 94 // task-runner affinity.
90 // This method takes ownership of the dump provider and guarantees that: 95 // This method takes ownership of the dump provider and guarantees that:
91 // - The |mdp| will be deleted at some point in the near future. 96 // - The |mdp| will be deleted at some point in the near future.
92 // - Its deletion will not happen concurrently with the OnMemoryDump() call. 97 // - Its deletion will not happen concurrently with the OnMemoryDump() call.
93 // Note that OnMemoryDump() calls can still happen after this method returns. 98 // Note that OnMemoryDump() calls can still happen after this method returns.
94 void UnregisterAndDeleteDumpProviderSoon(scoped_ptr<MemoryDumpProvider> mdp); 99 void UnregisterAndDeleteDumpProviderSoon(scoped_ptr<MemoryDumpProvider> mdp);
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
146 // Descriptor used to hold information about registered MDPs. 151 // Descriptor used to hold information about registered MDPs.
147 // Some important considerations about lifetime of this object: 152 // Some important considerations about lifetime of this object:
148 // - In nominal conditions, all the MemoryDumpProviderInfo instances live in 153 // - In nominal conditions, all the MemoryDumpProviderInfo instances live in
149 // the |dump_providers_| collection (% unregistration while dumping). 154 // the |dump_providers_| collection (% unregistration while dumping).
150 // - Upon each dump they (actually their scoped_refptr-s) are copied into 155 // - Upon each dump they (actually their scoped_refptr-s) are copied into
151 // the ProcessMemoryDumpAsyncState. This is to allow removal (see below). 156 // the ProcessMemoryDumpAsyncState. This is to allow removal (see below).
152 // - When the MDP.OnMemoryDump() is invoked, the corresponding MDPInfo copy 157 // - When the MDP.OnMemoryDump() is invoked, the corresponding MDPInfo copy
153 // inside ProcessMemoryDumpAsyncState is removed. 158 // inside ProcessMemoryDumpAsyncState is removed.
154 // - In most cases, the MDPInfo is destroyed within UnregisterDumpProvider(). 159 // - In most cases, the MDPInfo is destroyed within UnregisterDumpProvider().
155 // - If UnregisterDumpProvider() is called while a dump is in progress, the 160 // - If UnregisterDumpProvider() is called while a dump is in progress, the
156 // MDPInfo is destroyed in the epilogue of ContinueAsyncProcessDump(), when 161 // MDPInfo is destroyed in SetupNextMemoryDump() or InvokeOnMemoryDump(),
157 // the copy inside ProcessMemoryDumpAsyncState is erase()-d. 162 // when the copy inside ProcessMemoryDumpAsyncState is erase()-d.
158 // - The non-const fields of MemoryDumpProviderInfo are safe to access only 163 // - The non-const fields of MemoryDumpProviderInfo are safe to access only
159 // in the |task_runner| thread, unless the thread has been destroyed. 164 // on tasks running in the |task_runner|, unless the thread has been
165 // destroyed.
160 struct MemoryDumpProviderInfo 166 struct MemoryDumpProviderInfo
161 : public RefCountedThreadSafe<MemoryDumpProviderInfo> { 167 : public RefCountedThreadSafe<MemoryDumpProviderInfo> {
162 // Define a total order based on the thread (i.e. |task_runner|) affinity, 168 // Define a total order based on the |task_runner| affinity, so that MDPs
163 // so that all MDP belonging to the same thread are adjacent in the set. 169 // belonging to the same SequencedTaskRunner are adjacent in the set.
164 struct Comparator { 170 struct Comparator {
165 bool operator()(const scoped_refptr<MemoryDumpProviderInfo>& a, 171 bool operator()(const scoped_refptr<MemoryDumpProviderInfo>& a,
166 const scoped_refptr<MemoryDumpProviderInfo>& b) const; 172 const scoped_refptr<MemoryDumpProviderInfo>& b) const;
167 }; 173 };
168 using OrderedSet = 174 using OrderedSet =
169 std::set<scoped_refptr<MemoryDumpProviderInfo>, Comparator>; 175 std::set<scoped_refptr<MemoryDumpProviderInfo>, Comparator>;
170 176
171 MemoryDumpProviderInfo( 177 MemoryDumpProviderInfo(
172 MemoryDumpProvider* dump_provider, 178 MemoryDumpProvider* dump_provider,
173 const char* name, 179 const char* name,
174 const scoped_refptr<SingleThreadTaskRunner>& task_runner, 180 const scoped_refptr<SequencedTaskRunner>& task_runner,
175 const MemoryDumpProvider::Options& options); 181 const MemoryDumpProvider::Options& options);
176 182
177 MemoryDumpProvider* const dump_provider; 183 MemoryDumpProvider* const dump_provider;
178 184
179 // Used to transfer ownership for UnregisterAndDeleteDumpProviderSoon(). 185 // Used to transfer ownership for UnregisterAndDeleteDumpProviderSoon().
180 // nullptr in all other cases. 186 // nullptr in all other cases.
181 scoped_ptr<MemoryDumpProvider> owned_dump_provider; 187 scoped_ptr<MemoryDumpProvider> owned_dump_provider;
182 188
183 // Human readable name, for debugging and testing. Not necessarily unique. 189 // Human readable name, for debugging and testing. Not necessarily unique.
184 const char* const name; 190 const char* const name;
185 191
186 // The task_runner affinity. Can be nullptr, in which case the dump provider 192 // The task runner affinity. Can be nullptr, in which case the dump provider
187 // will be invoked on |dump_thread_|. 193 // will be invoked on |dump_thread_|.
188 const scoped_refptr<SingleThreadTaskRunner> task_runner; 194 const scoped_refptr<SequencedTaskRunner> task_runner;
189 195
190 // The |options| arg passed to RegisterDumpProvider(). 196 // The |options| arg passed to RegisterDumpProvider().
191 const MemoryDumpProvider::Options options; 197 const MemoryDumpProvider::Options options;
192 198
193 // For fail-safe logic (auto-disable failing MDPs). 199 // For fail-safe logic (auto-disable failing MDPs).
194 int consecutive_failures; 200 int consecutive_failures;
195 201
196 // Flagged either by the auto-disable logic or during unregistration. 202 // Flagged either by the auto-disable logic or during unregistration.
197 bool disabled; 203 bool disabled;
198 204
199 private: 205 private:
200 friend class base::RefCountedThreadSafe<MemoryDumpProviderInfo>; 206 friend class base::RefCountedThreadSafe<MemoryDumpProviderInfo>;
201 ~MemoryDumpProviderInfo(); 207 ~MemoryDumpProviderInfo();
202 208
203 DISALLOW_COPY_AND_ASSIGN(MemoryDumpProviderInfo); 209 DISALLOW_COPY_AND_ASSIGN(MemoryDumpProviderInfo);
204 }; 210 };
205 211
206 // Holds the state of a process memory dump that needs to be carried over 212 // Holds the state of a process memory dump that needs to be carried over
207 // across threads in order to fulfil an asynchronous CreateProcessDump() 213 // across task runners in order to fulfil an asynchronous CreateProcessDump()
208 // request. At any time exactly one thread owns a ProcessMemoryDumpAsyncState. 214 // request. At any time exactly one task runner owns a
215 // ProcessMemoryDumpAsyncState.
209 struct ProcessMemoryDumpAsyncState { 216 struct ProcessMemoryDumpAsyncState {
210 ProcessMemoryDumpAsyncState( 217 ProcessMemoryDumpAsyncState(
211 MemoryDumpRequestArgs req_args, 218 MemoryDumpRequestArgs req_args,
212 const MemoryDumpProviderInfo::OrderedSet& dump_providers, 219 const MemoryDumpProviderInfo::OrderedSet& dump_providers,
213 const scoped_refptr<MemoryDumpSessionState>& session_state, 220 const scoped_refptr<MemoryDumpSessionState>& session_state,
214 MemoryDumpCallback callback, 221 MemoryDumpCallback callback,
215 const scoped_refptr<SingleThreadTaskRunner>& dump_thread_task_runner); 222 const scoped_refptr<SingleThreadTaskRunner>& dump_thread_task_runner);
216 ~ProcessMemoryDumpAsyncState(); 223 ~ProcessMemoryDumpAsyncState();
217 224
218 // Gets or creates the memory dump container for the given target process. 225 // Gets or creates the memory dump container for the given target process.
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
270 // Enable heap profiling if kEnableHeapProfiling is specified. 277 // Enable heap profiling if kEnableHeapProfiling is specified.
271 void EnableHeapProfilingIfNeeded(); 278 void EnableHeapProfilingIfNeeded();
272 279
273 // Internal, used only by MemoryDumpManagerDelegate. 280 // Internal, used only by MemoryDumpManagerDelegate.
274 // Creates a memory dump for the current process and appends it to the trace. 281 // Creates a memory dump for the current process and appends it to the trace.
275 // |callback| will be invoked asynchronously upon completion on the same 282 // |callback| will be invoked asynchronously upon completion on the same
276 // thread on which CreateProcessDump() was called. 283 // thread on which CreateProcessDump() was called.
277 void CreateProcessDump(const MemoryDumpRequestArgs& args, 284 void CreateProcessDump(const MemoryDumpRequestArgs& args,
278 const MemoryDumpCallback& callback); 285 const MemoryDumpCallback& callback);
279 286
280 // Continues the ProcessMemoryDump started by CreateProcessDump(), hopping 287 // Calls InvokeOnMemoryDump() for the next MDP on the task runner specified by
281 // across threads as needed as specified by MDPs in RegisterDumpProvider(). 288 // the MDP while registration. On failure to do so, skips and continues to
282 void ContinueAsyncProcessDump( 289 // next MDP.
283 ProcessMemoryDumpAsyncState* owned_pmd_async_state); 290 void SetupNextMemoryDump(
291 scoped_ptr<ProcessMemoryDumpAsyncState> pmd_async_state);
292
293 // Invokes OnMemoryDump() of the next MDP and calls SetupNextMemoryDump() at
294 // the end to continue the ProcessMemoryDump. Should be called on the MDP task
295 // runner.
296 void InvokeOnMemoryDump(ProcessMemoryDumpAsyncState* owned_pmd_async_state);
297
298 // Helper for RegierDumpProvider* functions.
299 void RegisterDumpProviderInternal(
300 MemoryDumpProvider* mdp,
301 const char* name,
302 const scoped_refptr<SequencedTaskRunner>& task_runner,
303 const MemoryDumpProvider::Options& options);
284 304
285 // Helper for the public UnregisterDumpProvider* functions. 305 // Helper for the public UnregisterDumpProvider* functions.
286 void UnregisterDumpProviderInternal(MemoryDumpProvider* mdp, 306 void UnregisterDumpProviderInternal(MemoryDumpProvider* mdp,
287 bool take_mdp_ownership_and_delete_async); 307 bool take_mdp_ownership_and_delete_async);
288 308
289 // An ordererd set of registered MemoryDumpProviderInfo(s), sorted by thread 309 // An ordererd set of registered MemoryDumpProviderInfo(s), sorted by task
290 // affinity (MDPs belonging to the same thread are adjacent). 310 // runner affinity (MDPs belonging to the same task runners are adjacent).
291 MemoryDumpProviderInfo::OrderedSet dump_providers_; 311 MemoryDumpProviderInfo::OrderedSet dump_providers_;
292 312
293 // Shared among all the PMDs to keep state scoped to the tracing session. 313 // Shared among all the PMDs to keep state scoped to the tracing session.
294 scoped_refptr<MemoryDumpSessionState> session_state_; 314 scoped_refptr<MemoryDumpSessionState> session_state_;
295 315
296 MemoryDumpManagerDelegate* delegate_; // Not owned. 316 MemoryDumpManagerDelegate* delegate_; // Not owned.
297 317
298 // When true, this instance is in charge of coordinating periodic dumps. 318 // When true, this instance is in charge of coordinating periodic dumps.
299 bool is_coordinator_; 319 bool is_coordinator_;
300 320
301 // Protects from concurrent accesses to the |dump_providers_*| and |delegate_| 321 // Protects from concurrent accesses to the |dump_providers_*| and |delegate_|
302 // to guard against disabling logging while dumping on another thread. 322 // to guard against disabling logging while dumping on another thread.
303 Lock lock_; 323 Lock lock_;
304 324
305 // Optimization to avoid attempting any memory dump (i.e. to not walk an empty 325 // Optimization to avoid attempting any memory dump (i.e. to not walk an empty
306 // dump_providers_enabled_ list) when tracing is not enabled. 326 // dump_providers_enabled_ list) when tracing is not enabled.
307 subtle::AtomicWord memory_tracing_enabled_; 327 subtle::AtomicWord memory_tracing_enabled_;
308 328
309 // For time-triggered periodic dumps. 329 // For time-triggered periodic dumps.
310 RepeatingTimer periodic_dump_timer_; 330 RepeatingTimer periodic_dump_timer_;
311 331
312 // Thread used for MemoryDumpProviders which don't specify a thread affinity. 332 // Thread used for MemoryDumpProviders which don't specify a task runner
333 // affinity.
313 scoped_ptr<Thread> dump_thread_; 334 scoped_ptr<Thread> dump_thread_;
314 335
315 // The unique id of the child process. This is created only for tracing and is 336 // The unique id of the child process. This is created only for tracing and is
316 // expected to be valid only when tracing is enabled. 337 // expected to be valid only when tracing is enabled.
317 uint64_t tracing_process_id_; 338 uint64_t tracing_process_id_;
318 339
319 // When true, calling |RegisterMemoryDumpProvider| is a no-op. 340 // When true, calling |RegisterMemoryDumpProvider| is a no-op.
320 bool dumper_registrations_ignored_for_testing_; 341 bool dumper_registrations_ignored_for_testing_;
321 342
322 // Whether new memory dump providers should be told to enable heap profiling. 343 // Whether new memory dump providers should be told to enable heap profiling.
(...skipping 23 matching lines...) Expand all
346 } 367 }
347 368
348 private: 369 private:
349 DISALLOW_COPY_AND_ASSIGN(MemoryDumpManagerDelegate); 370 DISALLOW_COPY_AND_ASSIGN(MemoryDumpManagerDelegate);
350 }; 371 };
351 372
352 } // namespace trace_event 373 } // namespace trace_event
353 } // namespace base 374 } // namespace base
354 375
355 #endif // BASE_TRACE_EVENT_MEMORY_DUMP_MANAGER_H_ 376 #endif // BASE_TRACE_EVENT_MEMORY_DUMP_MANAGER_H_
OLDNEW
« no previous file with comments | « no previous file | base/trace_event/memory_dump_manager.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698