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

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

Issue 2819413002: [memory-infra] Remove MemoryDumpManagerDelegate (Closed)
Patch Set: Add export macros Created 3 years, 8 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
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>
11 #include <memory> 11 #include <memory>
12 #include <unordered_set> 12 #include <unordered_set>
13 #include <vector> 13 #include <vector>
14 14
15 #include "base/atomicops.h" 15 #include "base/atomicops.h"
16 #include "base/containers/hash_tables.h" 16 #include "base/containers/hash_tables.h"
17 #include "base/macros.h" 17 #include "base/macros.h"
18 #include "base/memory/ref_counted.h" 18 #include "base/memory/ref_counted.h"
19 #include "base/memory/singleton.h" 19 #include "base/memory/singleton.h"
20 #include "base/synchronization/lock.h" 20 #include "base/synchronization/lock.h"
21 #include "base/trace_event/memory_allocator_dump.h" 21 #include "base/trace_event/memory_allocator_dump.h"
22 #include "base/trace_event/memory_dump_provider_info.h" 22 #include "base/trace_event/memory_dump_provider_info.h"
23 #include "base/trace_event/memory_dump_request_args.h" 23 #include "base/trace_event/memory_dump_request_args.h"
24 #include "base/trace_event/process_memory_dump.h" 24 #include "base/trace_event/process_memory_dump.h"
25 #include "base/trace_event/trace_event.h" 25 #include "base/trace_event/trace_event.h"
26 26
27 // Forward declare |MemoryDumpManagerDelegateImplTest| so that we can make it a 27 // Forward declare |ProcessLocalDumpManagerImplTest| so that we can make it a
28 // friend of |MemoryDumpManager| and give it access to |SetInstanceForTesting|. 28 // friend of |MemoryDumpManager| and give it access to |SetInstanceForTesting|.
29 namespace memory_instrumentation { 29 namespace memory_instrumentation {
30 30
31 class MemoryDumpManagerDelegateImplTest; 31 class ProcessLocalDumpManagerImplTest;
32 32
33 } // namespace memory_instrumentation 33 } // namespace memory_instrumentation
34 34
35 namespace base { 35 namespace base {
36 36
37 class SingleThreadTaskRunner; 37 class SingleThreadTaskRunner;
38 class Thread; 38 class Thread;
39 39
40 namespace trace_event { 40 namespace trace_event {
41 41
42 class MemoryDumpManagerDelegate;
43 class MemoryDumpProvider; 42 class MemoryDumpProvider;
44 class MemoryDumpSessionState; 43 class MemoryDumpSessionState;
45 44
46 // This is the interface exposed to the rest of the codebase to deal with 45 // This is the interface exposed to the rest of the codebase to deal with
47 // memory tracing. The main entry point for clients is represented by 46 // memory tracing. The main entry point for clients is represented by
48 // RequestDumpPoint(). The extension by Un(RegisterDumpProvider). 47 // RequestDumpPoint(). The extension by Un(RegisterDumpProvider).
49 class BASE_EXPORT MemoryDumpManager : public TraceLog::EnabledStateObserver { 48 class BASE_EXPORT MemoryDumpManager : public TraceLog::EnabledStateObserver {
50 public: 49 public:
50 using RequestGlobalDumpFunction =
51 RepeatingCallback<void(const MemoryDumpRequestArgs& args,
52 const GlobalMemoryDumpCallback& callback)>;
53
51 static const char* const kTraceCategory; 54 static const char* const kTraceCategory;
52 static const char* const kLogPrefix; 55 static const char* const kLogPrefix;
53 56
54 // This value is returned as the tracing id of the child processes by 57 // This value is returned as the tracing id of the child processes by
55 // GetTracingProcessId() when tracing is not enabled. 58 // GetTracingProcessId() when tracing is not enabled.
56 static const uint64_t kInvalidTracingProcessId; 59 static const uint64_t kInvalidTracingProcessId;
57 60
58 static MemoryDumpManager* GetInstance(); 61 static MemoryDumpManager* GetInstance();
59 62
60 // Invoked once per process to listen to trace begin / end events. 63 // Invoked once per process to listen to trace begin / end events.
61 // Initialization can happen after (Un)RegisterMemoryDumpProvider() calls 64 // Initialization can happen after (Un)RegisterMemoryDumpProvider() calls
62 // and the MemoryDumpManager guarantees to support this. 65 // and the MemoryDumpManager guarantees to support this.
63 // On the other side, the MemoryDumpManager will not be fully operational 66 // On the other side, the MemoryDumpManager will not be fully operational
64 // (i.e. will NACK any RequestGlobalMemoryDump()) until initialized. 67 // (i.e. will NACK any RequestGlobalMemoryDump()) until initialized.
65 // Arguments: 68 // Arguments:
66 // delegate: inversion-of-control interface for embedder-specific behaviors 69 // request_dump_function: Function to invoke a global dump. Global dump
67 // (multiprocess handshaking). See the lifetime and thread-safety 70 // involves embedder-specific behaviors like multiprocess handshaking.
68 // requirements in the |MemoryDumpManagerDelegate| docstring. 71 // is_coordinator: True when current process coodinates the periodic dump
69 void Initialize(std::unique_ptr<MemoryDumpManagerDelegate> delegate); 72 // triggering.
73 void Initialize(RequestGlobalDumpFunction request_dump_function,
74 bool is_coordinator);
70 75
71 // (Un)Registers a MemoryDumpProvider instance. 76 // (Un)Registers a MemoryDumpProvider instance.
72 // Args: 77 // Args:
73 // - mdp: the MemoryDumpProvider instance to be registered. MemoryDumpManager 78 // - mdp: the MemoryDumpProvider instance to be registered. MemoryDumpManager
74 // does NOT take memory ownership of |mdp|, which is expected to either 79 // does NOT take memory ownership of |mdp|, which is expected to either
75 // be a singleton or unregister itself. 80 // be a singleton or unregister itself.
76 // - name: a friendly name (duplicates allowed). Used for debugging and 81 // - name: a friendly name (duplicates allowed). Used for debugging and
77 // run-time profiling of memory-infra internals. Must be a long-lived 82 // run-time profiling of memory-infra internals. Must be a long-lived
78 // C string. 83 // C string.
79 // - task_runner: either a SingleThreadTaskRunner or SequencedTaskRunner. All 84 // - task_runner: either a SingleThreadTaskRunner or SequencedTaskRunner. All
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
112 // processes have dumped) and its success (true iff all the dumps were 117 // processes have dumped) and its success (true iff all the dumps were
113 // successful). 118 // successful).
114 void RequestGlobalDump(MemoryDumpType dump_type, 119 void RequestGlobalDump(MemoryDumpType dump_type,
115 MemoryDumpLevelOfDetail level_of_detail, 120 MemoryDumpLevelOfDetail level_of_detail,
116 const GlobalMemoryDumpCallback& callback); 121 const GlobalMemoryDumpCallback& callback);
117 122
118 // Same as above (still asynchronous), but without callback. 123 // Same as above (still asynchronous), but without callback.
119 void RequestGlobalDump(MemoryDumpType dump_type, 124 void RequestGlobalDump(MemoryDumpType dump_type,
120 MemoryDumpLevelOfDetail level_of_detail); 125 MemoryDumpLevelOfDetail level_of_detail);
121 126
127 // NOTE: Use RequestGlobalDump() to create memory dumps. Creates a memory dump
128 // for the current process and appends it to the trace. |callback| will be
129 // invoked asynchronously upon completion on the same thread on which
130 // CreateProcessDump() was called. This method should only be used by the
131 // embedder while creating a global memory dump.
132 void CreateProcessDump(const MemoryDumpRequestArgs& args,
133 const ProcessMemoryDumpCallback& callback);
134
122 // TraceLog::EnabledStateObserver implementation. 135 // TraceLog::EnabledStateObserver implementation.
123 void OnTraceLogEnabled() override; 136 void OnTraceLogEnabled() override;
124 void OnTraceLogDisabled() override; 137 void OnTraceLogDisabled() override;
125 138
126 // Enable heap profiling if kEnableHeapProfiling is specified. 139 // Enable heap profiling if kEnableHeapProfiling is specified.
127 void EnableHeapProfilingIfNeeded(); 140 void EnableHeapProfilingIfNeeded();
128 141
129 // Returns true if the dump mode is allowed for current tracing session. 142 // Returns true if the dump mode is allowed for current tracing session.
130 bool IsDumpModeAllowed(MemoryDumpLevelOfDetail dump_mode); 143 bool IsDumpModeAllowed(MemoryDumpLevelOfDetail dump_mode);
131 144
(...skipping 26 matching lines...) Expand all
158 }; 171 };
159 172
160 // When set to true, calling |RegisterMemoryDumpProvider| is a no-op. 173 // When set to true, calling |RegisterMemoryDumpProvider| is a no-op.
161 void set_dumper_registrations_ignored_for_testing(bool ignored) { 174 void set_dumper_registrations_ignored_for_testing(bool ignored) {
162 dumper_registrations_ignored_for_testing_ = ignored; 175 dumper_registrations_ignored_for_testing_ = ignored;
163 } 176 }
164 177
165 private: 178 private:
166 friend std::default_delete<MemoryDumpManager>; // For the testing instance. 179 friend std::default_delete<MemoryDumpManager>; // For the testing instance.
167 friend struct DefaultSingletonTraits<MemoryDumpManager>; 180 friend struct DefaultSingletonTraits<MemoryDumpManager>;
168 friend class MemoryDumpManagerDelegate;
169 friend class MemoryDumpManagerTest; 181 friend class MemoryDumpManagerTest;
170 friend class memory_instrumentation::MemoryDumpManagerDelegateImplTest; 182 friend class memory_instrumentation::ProcessLocalDumpManagerImplTest;
171 183
172 // Holds the state of a process memory dump that needs to be carried over 184 // Holds the state of a process memory dump that needs to be carried over
173 // across task runners in order to fulfil an asynchronous CreateProcessDump() 185 // across task runners in order to fulfil an asynchronous CreateProcessDump()
174 // request. At any time exactly one task runner owns a 186 // request. At any time exactly one task runner owns a
175 // ProcessMemoryDumpAsyncState. 187 // ProcessMemoryDumpAsyncState.
176 struct ProcessMemoryDumpAsyncState { 188 struct ProcessMemoryDumpAsyncState {
177 ProcessMemoryDumpAsyncState( 189 ProcessMemoryDumpAsyncState(
178 MemoryDumpRequestArgs req_args, 190 MemoryDumpRequestArgs req_args,
179 const MemoryDumpProviderInfo::OrderedSet& dump_providers, 191 const MemoryDumpProviderInfo::OrderedSet& dump_providers,
180 scoped_refptr<MemoryDumpSessionState> session_state, 192 scoped_refptr<MemoryDumpSessionState> session_state,
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
230 static const char* const kSystemAllocatorPoolName; 242 static const char* const kSystemAllocatorPoolName;
231 243
232 MemoryDumpManager(); 244 MemoryDumpManager();
233 ~MemoryDumpManager() override; 245 ~MemoryDumpManager() override;
234 246
235 static void SetInstanceForTesting(MemoryDumpManager* instance); 247 static void SetInstanceForTesting(MemoryDumpManager* instance);
236 static uint32_t GetDumpsSumKb(const std::string&, const ProcessMemoryDump*); 248 static uint32_t GetDumpsSumKb(const std::string&, const ProcessMemoryDump*);
237 static void FinalizeDumpAndAddToTrace( 249 static void FinalizeDumpAndAddToTrace(
238 std::unique_ptr<ProcessMemoryDumpAsyncState> pmd_async_state); 250 std::unique_ptr<ProcessMemoryDumpAsyncState> pmd_async_state);
239 251
240 // Internal, used only by MemoryDumpManagerDelegate.
241 // Creates a memory dump for the current process and appends it to the trace.
242 // |callback| will be invoked asynchronously upon completion on the same
243 // thread on which CreateProcessDump() was called.
244 void CreateProcessDump(const MemoryDumpRequestArgs& args,
245 const ProcessMemoryDumpCallback& callback);
246
247 // Calls InvokeOnMemoryDump() for the next MDP on the task runner specified by 252 // Calls InvokeOnMemoryDump() for the next MDP on the task runner specified by
248 // the MDP while registration. On failure to do so, skips and continues to 253 // the MDP while registration. On failure to do so, skips and continues to
249 // next MDP. 254 // next MDP.
250 void SetupNextMemoryDump( 255 void SetupNextMemoryDump(
251 std::unique_ptr<ProcessMemoryDumpAsyncState> pmd_async_state); 256 std::unique_ptr<ProcessMemoryDumpAsyncState> pmd_async_state);
252 257
253 // Invokes OnMemoryDump() of the next MDP and calls SetupNextMemoryDump() at 258 // Invokes OnMemoryDump() of the next MDP and calls SetupNextMemoryDump() at
254 // the end to continue the ProcessMemoryDump. Should be called on the MDP task 259 // the end to continue the ProcessMemoryDump. Should be called on the MDP task
255 // runner. 260 // runner.
256 void InvokeOnMemoryDump(ProcessMemoryDumpAsyncState* owned_pmd_async_state); 261 void InvokeOnMemoryDump(ProcessMemoryDumpAsyncState* owned_pmd_async_state);
(...skipping 19 matching lines...) Expand all
276 MemoryDumpProviderInfo::OrderedSet dump_providers_; 281 MemoryDumpProviderInfo::OrderedSet dump_providers_;
277 282
278 // Shared among all the PMDs to keep state scoped to the tracing session. 283 // Shared among all the PMDs to keep state scoped to the tracing session.
279 scoped_refptr<MemoryDumpSessionState> session_state_; 284 scoped_refptr<MemoryDumpSessionState> session_state_;
280 285
281 // The list of names of dump providers that are blacklisted from strict thread 286 // The list of names of dump providers that are blacklisted from strict thread
282 // affinity check on unregistration. 287 // affinity check on unregistration.
283 std::unordered_set<StringPiece, StringPieceHash> 288 std::unordered_set<StringPiece, StringPieceHash>
284 strict_thread_check_blacklist_; 289 strict_thread_check_blacklist_;
285 290
286 std::unique_ptr<MemoryDumpManagerDelegate> delegate_; 291 // Function provided by the embedder to handle global dump requests.
292 RequestGlobalDumpFunction request_dump_function_;
287 293
288 // Protects from concurrent accesses to the |dump_providers_*| and |delegate_| 294 // True when current process coordinates the periodic dump triggering.
289 // to guard against disabling logging while dumping on another thread. 295 bool is_coordinator_;
296
297 // Protects from concurrent accesses to the local state, eg: to guard against
298 // disabling logging while dumping on another thread.
290 Lock lock_; 299 Lock lock_;
291 300
292 // Optimization to avoid attempting any memory dump (i.e. to not walk an empty 301 // Optimization to avoid attempting any memory dump (i.e. to not walk an empty
293 // dump_providers_enabled_ list) when tracing is not enabled. 302 // dump_providers_enabled_ list) when tracing is not enabled.
294 subtle::AtomicWord memory_tracing_enabled_; 303 subtle::AtomicWord memory_tracing_enabled_;
295 304
296 // Thread used for MemoryDumpProviders which don't specify a task runner 305 // Thread used for MemoryDumpProviders which don't specify a task runner
297 // affinity. 306 // affinity.
298 std::unique_ptr<Thread> dump_thread_; 307 std::unique_ptr<Thread> dump_thread_;
299 308
300 // The unique id of the child process. This is created only for tracing and is 309 // The unique id of the child process. This is created only for tracing and is
301 // expected to be valid only when tracing is enabled. 310 // expected to be valid only when tracing is enabled.
302 uint64_t tracing_process_id_; 311 uint64_t tracing_process_id_;
303 312
304 // When true, calling |RegisterMemoryDumpProvider| is a no-op. 313 // When true, calling |RegisterMemoryDumpProvider| is a no-op.
305 bool dumper_registrations_ignored_for_testing_; 314 bool dumper_registrations_ignored_for_testing_;
306 315
307 // Whether new memory dump providers should be told to enable heap profiling. 316 // Whether new memory dump providers should be told to enable heap profiling.
308 bool heap_profiling_enabled_; 317 bool heap_profiling_enabled_;
309 318
310 DISALLOW_COPY_AND_ASSIGN(MemoryDumpManager); 319 DISALLOW_COPY_AND_ASSIGN(MemoryDumpManager);
311 }; 320 };
312 321
313 // The delegate is supposed to be long lived (read: a Singleton) and thread
314 // safe (i.e. should expect calls from any thread and handle thread hopping).
315 class BASE_EXPORT MemoryDumpManagerDelegate {
316 public:
317 MemoryDumpManagerDelegate() {}
318 virtual ~MemoryDumpManagerDelegate() {}
319
320 virtual void RequestGlobalMemoryDump(
321 const MemoryDumpRequestArgs& args,
322 const GlobalMemoryDumpCallback& callback) = 0;
323
324 virtual bool IsCoordinator() const = 0;
325
326 protected:
327 void CreateProcessDump(const MemoryDumpRequestArgs& args,
328 const ProcessMemoryDumpCallback& callback) {
329 MemoryDumpManager::GetInstance()->CreateProcessDump(args, callback);
330 }
331
332 private:
333 DISALLOW_COPY_AND_ASSIGN(MemoryDumpManagerDelegate);
334 };
335
336 } // namespace trace_event 322 } // namespace trace_event
337 } // namespace base 323 } // namespace base
338 324
339 #endif // BASE_TRACE_EVENT_MEMORY_DUMP_MANAGER_H_ 325 #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') | services/resource_coordinator/BUILD.gn » ('J')

Powered by Google App Engine
This is Rietveld 408576698