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 <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <map> | 10 #include <map> |
11 #include <memory> | 11 #include <memory> |
12 #include <set> | 12 #include <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_dump_request_args.h" | 21 #include "base/trace_event/memory_dump_request_args.h" |
22 #include "base/trace_event/process_memory_dump.h" | 22 #include "base/trace_event/process_memory_dump.h" |
23 #include "base/trace_event/trace_event.h" | 23 #include "base/trace_event/trace_event.h" |
24 | 24 |
| 25 // Forward declare |MemoryDumpManagerDelegateImplTest| so that we can make it a |
| 26 // friend of |MemoryDumpManager| and give it access to |SetInstanceForTesting|. |
| 27 namespace memory_instrumentation { |
| 28 |
| 29 class MemoryDumpManagerDelegateImplTest; |
| 30 |
| 31 } // namespace memory_instrumentation |
| 32 |
25 namespace base { | 33 namespace base { |
26 | 34 |
27 class SingleThreadTaskRunner; | 35 class SingleThreadTaskRunner; |
28 class Thread; | 36 class Thread; |
29 | 37 |
30 namespace trace_event { | 38 namespace trace_event { |
31 | 39 |
32 class MemoryDumpManagerDelegate; | 40 class MemoryDumpManagerDelegate; |
33 class MemoryDumpProvider; | 41 class MemoryDumpProvider; |
34 class MemoryDumpSessionState; | 42 class MemoryDumpSessionState; |
(...skipping 12 matching lines...) Expand all Loading... |
47 static const uint64_t kInvalidTracingProcessId; | 55 static const uint64_t kInvalidTracingProcessId; |
48 | 56 |
49 static MemoryDumpManager* GetInstance(); | 57 static MemoryDumpManager* GetInstance(); |
50 | 58 |
51 // Invoked once per process to listen to trace begin / end events. | 59 // Invoked once per process to listen to trace begin / end events. |
52 // Initialization can happen after (Un)RegisterMemoryDumpProvider() calls | 60 // Initialization can happen after (Un)RegisterMemoryDumpProvider() calls |
53 // and the MemoryDumpManager guarantees to support this. | 61 // and the MemoryDumpManager guarantees to support this. |
54 // On the other side, the MemoryDumpManager will not be fully operational | 62 // On the other side, the MemoryDumpManager will not be fully operational |
55 // (i.e. will NACK any RequestGlobalMemoryDump()) until initialized. | 63 // (i.e. will NACK any RequestGlobalMemoryDump()) until initialized. |
56 // Arguments: | 64 // Arguments: |
57 // is_coordinator: if true this MemoryDumpManager instance will act as a | |
58 // coordinator and schedule periodic dumps (if enabled via TraceConfig); | |
59 // false when the MemoryDumpManager is initialized in a slave process. | |
60 // delegate: inversion-of-control interface for embedder-specific behaviors | 65 // delegate: inversion-of-control interface for embedder-specific behaviors |
61 // (multiprocess handshaking). See the lifetime and thread-safety | 66 // (multiprocess handshaking). See the lifetime and thread-safety |
62 // requirements in the |MemoryDumpManagerDelegate| docstring. | 67 // requirements in the |MemoryDumpManagerDelegate| docstring. |
63 void Initialize(MemoryDumpManagerDelegate* delegate, bool is_coordinator); | 68 void Initialize(MemoryDumpManagerDelegate* delegate); |
64 | 69 |
65 // (Un)Registers a MemoryDumpProvider instance. | 70 // (Un)Registers a MemoryDumpProvider instance. |
66 // Args: | 71 // Args: |
67 // - mdp: the MemoryDumpProvider instance to be registered. MemoryDumpManager | 72 // - mdp: the MemoryDumpProvider instance to be registered. MemoryDumpManager |
68 // does NOT take memory ownership of |mdp|, which is expected to either | 73 // does NOT take memory ownership of |mdp|, which is expected to either |
69 // be a singleton or unregister itself. | 74 // be a singleton or unregister itself. |
70 // - name: a friendly name (duplicates allowed). Used for debugging and | 75 // - name: a friendly name (duplicates allowed). Used for debugging and |
71 // run-time profiling of memory-infra internals. Must be a long-lived | 76 // run-time profiling of memory-infra internals. Must be a long-lived |
72 // C string. | 77 // C string. |
73 // - task_runner: either a SingleThreadTaskRunner or SequencedTaskRunner. All | 78 // - task_runner: either a SingleThreadTaskRunner or SequencedTaskRunner. All |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 // session lifetime. | 133 // session lifetime. |
129 const scoped_refptr<MemoryDumpSessionState>& session_state_for_testing() | 134 const scoped_refptr<MemoryDumpSessionState>& session_state_for_testing() |
130 const { | 135 const { |
131 return session_state_; | 136 return session_state_; |
132 } | 137 } |
133 | 138 |
134 // Returns a unique id for identifying the processes. The id can be | 139 // Returns a unique id for identifying the processes. The id can be |
135 // retrieved by child processes only when tracing is enabled. This is | 140 // retrieved by child processes only when tracing is enabled. This is |
136 // intended to express cross-process sharing of memory dumps on the | 141 // intended to express cross-process sharing of memory dumps on the |
137 // child-process side, without having to know its own child process id. | 142 // child-process side, without having to know its own child process id. |
138 uint64_t GetTracingProcessId() const; | 143 uint64_t GetTracingProcessId() const { return tracing_process_id_; } |
| 144 void set_tracing_process_id(uint64_t tracing_process_id) { |
| 145 tracing_process_id_ = tracing_process_id; |
| 146 } |
139 | 147 |
140 // Returns the name for a the allocated_objects dump. Use this to declare | 148 // Returns the name for a the allocated_objects dump. Use this to declare |
141 // suballocator dumps from other dump providers. | 149 // suballocator dumps from other dump providers. |
142 // It will return nullptr if there is no dump provider for the system | 150 // It will return nullptr if there is no dump provider for the system |
143 // allocator registered (which is currently the case for Mac OS). | 151 // allocator registered (which is currently the case for Mac OS). |
144 const char* system_allocator_pool_name() const { | 152 const char* system_allocator_pool_name() const { |
145 return kSystemAllocatorPoolName; | 153 return kSystemAllocatorPoolName; |
146 }; | 154 }; |
147 | 155 |
148 // When set to true, calling |RegisterMemoryDumpProvider| is a no-op. | 156 // When set to true, calling |RegisterMemoryDumpProvider| is a no-op. |
149 void set_dumper_registrations_ignored_for_testing(bool ignored) { | 157 void set_dumper_registrations_ignored_for_testing(bool ignored) { |
150 dumper_registrations_ignored_for_testing_ = ignored; | 158 dumper_registrations_ignored_for_testing_ = ignored; |
151 } | 159 } |
152 | 160 |
153 private: | 161 private: |
154 friend std::default_delete<MemoryDumpManager>; // For the testing instance. | 162 friend std::default_delete<MemoryDumpManager>; // For the testing instance. |
155 friend struct DefaultSingletonTraits<MemoryDumpManager>; | 163 friend struct DefaultSingletonTraits<MemoryDumpManager>; |
156 friend class MemoryDumpManagerDelegate; | 164 friend class MemoryDumpManagerDelegate; |
157 friend class MemoryDumpManagerTest; | 165 friend class MemoryDumpManagerTest; |
158 friend class MemoryDumpScheduler; | 166 friend class MemoryDumpScheduler; |
| 167 friend class memory_instrumentation::MemoryDumpManagerDelegateImplTest; |
159 | 168 |
160 // Descriptor used to hold information about registered MDPs. | 169 // Descriptor used to hold information about registered MDPs. |
161 // Some important considerations about lifetime of this object: | 170 // Some important considerations about lifetime of this object: |
162 // - In nominal conditions, all the MemoryDumpProviderInfo instances live in | 171 // - In nominal conditions, all the MemoryDumpProviderInfo instances live in |
163 // the |dump_providers_| collection (% unregistration while dumping). | 172 // the |dump_providers_| collection (% unregistration while dumping). |
164 // - Upon each dump they (actually their scoped_refptr-s) are copied into | 173 // - Upon each dump they (actually their scoped_refptr-s) are copied into |
165 // the ProcessMemoryDumpAsyncState. This is to allow removal (see below). | 174 // the ProcessMemoryDumpAsyncState. This is to allow removal (see below). |
166 // - When the MDP.OnMemoryDump() is invoked, the corresponding MDPInfo copy | 175 // - When the MDP.OnMemoryDump() is invoked, the corresponding MDPInfo copy |
167 // inside ProcessMemoryDumpAsyncState is removed. | 176 // inside ProcessMemoryDumpAsyncState is removed. |
168 // - In most cases, the MDPInfo is destroyed within UnregisterDumpProvider(). | 177 // - In most cases, the MDPInfo is destroyed within UnregisterDumpProvider(). |
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
343 // Shared among all the PMDs to keep state scoped to the tracing session. | 352 // Shared among all the PMDs to keep state scoped to the tracing session. |
344 scoped_refptr<MemoryDumpSessionState> session_state_; | 353 scoped_refptr<MemoryDumpSessionState> session_state_; |
345 | 354 |
346 // The list of names of dump providers that are blacklisted from strict thread | 355 // The list of names of dump providers that are blacklisted from strict thread |
347 // affinity check on unregistration. | 356 // affinity check on unregistration. |
348 std::unordered_set<StringPiece, StringPieceHash> | 357 std::unordered_set<StringPiece, StringPieceHash> |
349 strict_thread_check_blacklist_; | 358 strict_thread_check_blacklist_; |
350 | 359 |
351 MemoryDumpManagerDelegate* delegate_; // Not owned. | 360 MemoryDumpManagerDelegate* delegate_; // Not owned. |
352 | 361 |
353 // When true, this instance is in charge of coordinating periodic dumps. | |
354 bool is_coordinator_; | |
355 | |
356 // Protects from concurrent accesses to the |dump_providers_*| and |delegate_| | 362 // Protects from concurrent accesses to the |dump_providers_*| and |delegate_| |
357 // to guard against disabling logging while dumping on another thread. | 363 // to guard against disabling logging while dumping on another thread. |
358 Lock lock_; | 364 Lock lock_; |
359 | 365 |
360 // Optimization to avoid attempting any memory dump (i.e. to not walk an empty | 366 // Optimization to avoid attempting any memory dump (i.e. to not walk an empty |
361 // dump_providers_enabled_ list) when tracing is not enabled. | 367 // dump_providers_enabled_ list) when tracing is not enabled. |
362 subtle::AtomicWord memory_tracing_enabled_; | 368 subtle::AtomicWord memory_tracing_enabled_; |
363 | 369 |
364 // For triggering memory dumps. | 370 // For triggering memory dumps. |
365 std::unique_ptr<MemoryDumpScheduler> dump_scheduler_; | 371 std::unique_ptr<MemoryDumpScheduler> dump_scheduler_; |
(...skipping 15 matching lines...) Expand all Loading... |
381 DISALLOW_COPY_AND_ASSIGN(MemoryDumpManager); | 387 DISALLOW_COPY_AND_ASSIGN(MemoryDumpManager); |
382 }; | 388 }; |
383 | 389 |
384 // The delegate is supposed to be long lived (read: a Singleton) and thread | 390 // The delegate is supposed to be long lived (read: a Singleton) and thread |
385 // safe (i.e. should expect calls from any thread and handle thread hopping). | 391 // safe (i.e. should expect calls from any thread and handle thread hopping). |
386 class BASE_EXPORT MemoryDumpManagerDelegate { | 392 class BASE_EXPORT MemoryDumpManagerDelegate { |
387 public: | 393 public: |
388 virtual void RequestGlobalMemoryDump(const MemoryDumpRequestArgs& args, | 394 virtual void RequestGlobalMemoryDump(const MemoryDumpRequestArgs& args, |
389 const MemoryDumpCallback& callback) = 0; | 395 const MemoryDumpCallback& callback) = 0; |
390 | 396 |
391 // Returns tracing process id of the current process. This is used by | 397 virtual bool IsCoordinator() const = 0; |
392 // MemoryDumpManager::GetTracingProcessId. | |
393 virtual uint64_t GetTracingProcessId() const = 0; | |
394 | 398 |
395 protected: | 399 protected: |
396 MemoryDumpManagerDelegate() {} | 400 MemoryDumpManagerDelegate() {} |
397 virtual ~MemoryDumpManagerDelegate() {} | 401 virtual ~MemoryDumpManagerDelegate() {} |
398 | 402 |
399 void CreateProcessDump(const MemoryDumpRequestArgs& args, | 403 void CreateProcessDump(const MemoryDumpRequestArgs& args, |
400 const MemoryDumpCallback& callback) { | 404 const MemoryDumpCallback& callback) { |
401 MemoryDumpManager::GetInstance()->CreateProcessDump(args, callback); | 405 MemoryDumpManager::GetInstance()->CreateProcessDump(args, callback); |
402 } | 406 } |
403 | 407 |
404 private: | 408 private: |
405 DISALLOW_COPY_AND_ASSIGN(MemoryDumpManagerDelegate); | 409 DISALLOW_COPY_AND_ASSIGN(MemoryDumpManagerDelegate); |
406 }; | 410 }; |
407 | 411 |
408 } // namespace trace_event | 412 } // namespace trace_event |
409 } // namespace base | 413 } // namespace base |
410 | 414 |
411 #endif // BASE_TRACE_EVENT_MEMORY_DUMP_MANAGER_H_ | 415 #endif // BASE_TRACE_EVENT_MEMORY_DUMP_MANAGER_H_ |
OLD | NEW |