OLD | NEW |
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 <set> | 8 #include <set> |
9 | 9 |
10 #include "base/atomicops.h" | 10 #include "base/atomicops.h" |
(...skipping 22 matching lines...) Expand all Loading... |
33 class BASE_EXPORT MemoryDumpManager : public TraceLog::EnabledStateObserver { | 33 class BASE_EXPORT MemoryDumpManager : public TraceLog::EnabledStateObserver { |
34 public: | 34 public: |
35 static const char* const kTraceCategory; | 35 static const char* const kTraceCategory; |
36 | 36 |
37 // This value is returned as the tracing id of the child processes by | 37 // This value is returned as the tracing id of the child processes by |
38 // GetTracingProcessId() when tracing is not enabled. | 38 // GetTracingProcessId() when tracing is not enabled. |
39 static const uint64 kInvalidTracingProcessId; | 39 static const uint64 kInvalidTracingProcessId; |
40 | 40 |
41 static MemoryDumpManager* GetInstance(); | 41 static MemoryDumpManager* GetInstance(); |
42 | 42 |
43 // Invoked once per process to register the TraceLog observer. | 43 // Invoked once per process to listen to trace begin / end events. |
44 void Initialize(); | 44 // Initialization can happen after (Un)RegisterMemoryDumpProvider() calls |
45 | 45 // and the MemoryDumpManager guarantees to support this. |
46 // See the lifetime and thread-safety requirements on the delegate below in | 46 // On the other side, the MemoryDumpManager will not be fully operational |
47 // the |MemoryDumpManagerDelegate| docstring. | 47 // (i.e. will NACK any RequestGlobalMemoryDump()) until initialized. |
48 void SetDelegate(MemoryDumpManagerDelegate* delegate); | 48 // Arguments: |
| 49 // is_coordinator: if true this MemoryDumpManager instance will act as a |
| 50 // coordinator and schedule periodic dumps (if enabled via TraceConfig); |
| 51 // false when the MemoryDumpManager is initialized in a slave process. |
| 52 // delegate: inversion-of-control interface for embedder-specific behaviors |
| 53 // (multiprocess handshaking). See the lifetime and thread-safety |
| 54 // requirements in the |MemoryDumpManagerDelegate| docstring. |
| 55 void Initialize(MemoryDumpManagerDelegate* delegate, bool is_coordinator); |
49 | 56 |
50 // MemoryDumpManager does NOT take memory ownership of |mdp|, which is | 57 // MemoryDumpManager does NOT take memory ownership of |mdp|, which is |
51 // expected to either be a singleton or unregister itself. | 58 // expected to either be a singleton or unregister itself. |
52 // If the optional |task_runner| argument is non-null, all the calls to the | 59 // If the optional |task_runner| argument is non-null, all the calls to the |
53 // |mdp| will be issues on the given thread. Otherwise, the |mdp| should be | 60 // |mdp| will be issues on the given thread. Otherwise, the |mdp| should be |
54 // able to handle calls on arbitrary threads. | 61 // able to handle calls on arbitrary threads. |
55 void RegisterDumpProvider( | 62 void RegisterDumpProvider( |
56 MemoryDumpProvider* mdp, | 63 MemoryDumpProvider* mdp, |
57 const scoped_refptr<SingleThreadTaskRunner>& task_runner); | 64 const scoped_refptr<SingleThreadTaskRunner>& task_runner); |
58 void RegisterDumpProvider(MemoryDumpProvider* mdp); | 65 void RegisterDumpProvider(MemoryDumpProvider* mdp); |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
101 // Tells the initialization phase to skip scheduling periodic memory dumps. | 108 // Tells the initialization phase to skip scheduling periodic memory dumps. |
102 void DisablePeriodicDumpsForTesting() { | 109 void DisablePeriodicDumpsForTesting() { |
103 disable_periodic_dumps_for_testing_ = true; | 110 disable_periodic_dumps_for_testing_ = true; |
104 } | 111 } |
105 | 112 |
106 private: | 113 private: |
107 friend struct DefaultDeleter<MemoryDumpManager>; // For the testing instance. | 114 friend struct DefaultDeleter<MemoryDumpManager>; // For the testing instance. |
108 friend struct DefaultSingletonTraits<MemoryDumpManager>; | 115 friend struct DefaultSingletonTraits<MemoryDumpManager>; |
109 friend class MemoryDumpManagerDelegate; | 116 friend class MemoryDumpManagerDelegate; |
110 friend class MemoryDumpManagerTest; | 117 friend class MemoryDumpManagerTest; |
111 FRIEND_TEST_ALL_PREFIXES(MemoryDumpManagerTest, DisableFailingDumpers); | |
112 FRIEND_TEST_ALL_PREFIXES(MemoryDumpManagerTest, | |
113 UnregisterDumperFromThreadWhileDumping); | |
114 | 118 |
115 // Descriptor struct used to hold information about registered MDPs. It is | 119 // Descriptor struct used to hold information about registered MDPs. It is |
116 // deliberately copyable, in order to allow it to be used as std::set value. | 120 // deliberately copyable, in order to allow it to be used as std::set value. |
117 struct MemoryDumpProviderInfo { | 121 struct MemoryDumpProviderInfo { |
118 MemoryDumpProviderInfo( | 122 MemoryDumpProviderInfo( |
119 MemoryDumpProvider* dump_provider, | 123 MemoryDumpProvider* dump_provider, |
120 const scoped_refptr<SingleThreadTaskRunner>& task_runner); | 124 const scoped_refptr<SingleThreadTaskRunner>& task_runner); |
121 ~MemoryDumpProviderInfo(); | 125 ~MemoryDumpProviderInfo(); |
122 | 126 |
123 // Define a total order based on the thread (i.e. |task_runner|) affinity, | 127 // Define a total order based on the thread (i.e. |task_runner|) affinity, |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
201 | 205 |
202 // An ordererd set of registered MemoryDumpProviderInfo(s), sorted by thread | 206 // An ordererd set of registered MemoryDumpProviderInfo(s), sorted by thread |
203 // affinity (MDPs belonging to the same thread are adjacent). | 207 // affinity (MDPs belonging to the same thread are adjacent). |
204 MemoryDumpProviderInfoSet dump_providers_; | 208 MemoryDumpProviderInfoSet dump_providers_; |
205 | 209 |
206 // Shared among all the PMDs to keep state scoped to the tracing session. | 210 // Shared among all the PMDs to keep state scoped to the tracing session. |
207 scoped_refptr<MemoryDumpSessionState> session_state_; | 211 scoped_refptr<MemoryDumpSessionState> session_state_; |
208 | 212 |
209 MemoryDumpManagerDelegate* delegate_; // Not owned. | 213 MemoryDumpManagerDelegate* delegate_; // Not owned. |
210 | 214 |
| 215 // When true, this instance is in charge of coordinating periodic dumps. |
| 216 bool is_coordinator_; |
| 217 |
211 // Protects from concurrent accesses to the |dump_providers_*| and |delegate_| | 218 // Protects from concurrent accesses to the |dump_providers_*| and |delegate_| |
212 // to guard against disabling logging while dumping on another thread. | 219 // to guard against disabling logging while dumping on another thread. |
213 Lock lock_; | 220 Lock lock_; |
214 | 221 |
215 // Optimization to avoid attempting any memory dump (i.e. to not walk an empty | 222 // Optimization to avoid attempting any memory dump (i.e. to not walk an empty |
216 // dump_providers_enabled_ list) when tracing is not enabled. | 223 // dump_providers_enabled_ list) when tracing is not enabled. |
217 subtle::AtomicWord memory_tracing_enabled_; | 224 subtle::AtomicWord memory_tracing_enabled_; |
218 | 225 |
219 // For time-triggered periodic dumps. | 226 // For time-triggered periodic dumps. |
220 RepeatingTimer<MemoryDumpManager> periodic_dump_timer_; | 227 RepeatingTimer<MemoryDumpManager> periodic_dump_timer_; |
(...skipping 13 matching lines...) Expand all Loading... |
234 DISALLOW_COPY_AND_ASSIGN(MemoryDumpManager); | 241 DISALLOW_COPY_AND_ASSIGN(MemoryDumpManager); |
235 }; | 242 }; |
236 | 243 |
237 // The delegate is supposed to be long lived (read: a Singleton) and thread | 244 // The delegate is supposed to be long lived (read: a Singleton) and thread |
238 // safe (i.e. should expect calls from any thread and handle thread hopping). | 245 // safe (i.e. should expect calls from any thread and handle thread hopping). |
239 class BASE_EXPORT MemoryDumpManagerDelegate { | 246 class BASE_EXPORT MemoryDumpManagerDelegate { |
240 public: | 247 public: |
241 virtual void RequestGlobalMemoryDump(const MemoryDumpRequestArgs& args, | 248 virtual void RequestGlobalMemoryDump(const MemoryDumpRequestArgs& args, |
242 const MemoryDumpCallback& callback) = 0; | 249 const MemoryDumpCallback& callback) = 0; |
243 | 250 |
244 // Determines whether the MemoryDumpManager instance should be the master | |
245 // (the ones which initiates and coordinates the multiprocess dumps) or not. | |
246 virtual bool IsCoordinatorProcess() const = 0; | |
247 | |
248 // Returns tracing process id of the current process. This is used by | 251 // Returns tracing process id of the current process. This is used by |
249 // MemoryDumpManager::GetTracingProcessId. | 252 // MemoryDumpManager::GetTracingProcessId. |
250 virtual uint64 GetTracingProcessId() const = 0; | 253 virtual uint64 GetTracingProcessId() const = 0; |
251 | 254 |
252 protected: | 255 protected: |
253 MemoryDumpManagerDelegate() {} | 256 MemoryDumpManagerDelegate() {} |
254 virtual ~MemoryDumpManagerDelegate() {} | 257 virtual ~MemoryDumpManagerDelegate() {} |
255 | 258 |
256 void CreateProcessDump(const MemoryDumpRequestArgs& args, | 259 void CreateProcessDump(const MemoryDumpRequestArgs& args, |
257 const MemoryDumpCallback& callback) { | 260 const MemoryDumpCallback& callback) { |
258 MemoryDumpManager::GetInstance()->CreateProcessDump(args, callback); | 261 MemoryDumpManager::GetInstance()->CreateProcessDump(args, callback); |
259 } | 262 } |
260 | 263 |
261 private: | 264 private: |
262 DISALLOW_COPY_AND_ASSIGN(MemoryDumpManagerDelegate); | 265 DISALLOW_COPY_AND_ASSIGN(MemoryDumpManagerDelegate); |
263 }; | 266 }; |
264 | 267 |
265 } // namespace trace_event | 268 } // namespace trace_event |
266 } // namespace base | 269 } // namespace base |
267 | 270 |
268 #endif // BASE_TRACE_EVENT_MEMORY_DUMP_MANAGER_H_ | 271 #endif // BASE_TRACE_EVENT_MEMORY_DUMP_MANAGER_H_ |
OLD | NEW |