| 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(std::unique_ptr<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 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 131 // session lifetime. | 136 // session lifetime. |
| 132 const scoped_refptr<MemoryDumpSessionState>& session_state_for_testing() | 137 const scoped_refptr<MemoryDumpSessionState>& session_state_for_testing() |
| 133 const { | 138 const { |
| 134 return session_state_; | 139 return session_state_; |
| 135 } | 140 } |
| 136 | 141 |
| 137 // Returns a unique id for identifying the processes. The id can be | 142 // Returns a unique id for identifying the processes. The id can be |
| 138 // retrieved by child processes only when tracing is enabled. This is | 143 // retrieved by child processes only when tracing is enabled. This is |
| 139 // intended to express cross-process sharing of memory dumps on the | 144 // intended to express cross-process sharing of memory dumps on the |
| 140 // child-process side, without having to know its own child process id. | 145 // child-process side, without having to know its own child process id. |
| 141 uint64_t GetTracingProcessId() const; | 146 uint64_t GetTracingProcessId() const { return tracing_process_id_; } |
| 147 void set_tracing_process_id(uint64_t tracing_process_id) { |
| 148 tracing_process_id_ = tracing_process_id; |
| 149 } |
| 142 | 150 |
| 143 // Returns the name for a the allocated_objects dump. Use this to declare | 151 // Returns the name for a the allocated_objects dump. Use this to declare |
| 144 // suballocator dumps from other dump providers. | 152 // suballocator dumps from other dump providers. |
| 145 // It will return nullptr if there is no dump provider for the system | 153 // It will return nullptr if there is no dump provider for the system |
| 146 // allocator registered (which is currently the case for Mac OS). | 154 // allocator registered (which is currently the case for Mac OS). |
| 147 const char* system_allocator_pool_name() const { | 155 const char* system_allocator_pool_name() const { |
| 148 return kSystemAllocatorPoolName; | 156 return kSystemAllocatorPoolName; |
| 149 }; | 157 }; |
| 150 | 158 |
| 151 // When set to true, calling |RegisterMemoryDumpProvider| is a no-op. | 159 // When set to true, calling |RegisterMemoryDumpProvider| is a no-op. |
| 152 void set_dumper_registrations_ignored_for_testing(bool ignored) { | 160 void set_dumper_registrations_ignored_for_testing(bool ignored) { |
| 153 dumper_registrations_ignored_for_testing_ = ignored; | 161 dumper_registrations_ignored_for_testing_ = ignored; |
| 154 } | 162 } |
| 155 | 163 |
| 156 private: | 164 private: |
| 157 friend std::default_delete<MemoryDumpManager>; // For the testing instance. | 165 friend std::default_delete<MemoryDumpManager>; // For the testing instance. |
| 158 friend struct DefaultSingletonTraits<MemoryDumpManager>; | 166 friend struct DefaultSingletonTraits<MemoryDumpManager>; |
| 159 friend class MemoryDumpManagerDelegate; | 167 friend class MemoryDumpManagerDelegate; |
| 160 friend class MemoryDumpManagerTest; | 168 friend class MemoryDumpManagerTest; |
| 161 friend class MemoryDumpScheduler; | 169 friend class MemoryDumpScheduler; |
| 170 friend class memory_instrumentation::MemoryDumpManagerDelegateImplTest; |
| 162 | 171 |
| 163 // Descriptor used to hold information about registered MDPs. | 172 // Descriptor used to hold information about registered MDPs. |
| 164 // Some important considerations about lifetime of this object: | 173 // Some important considerations about lifetime of this object: |
| 165 // - In nominal conditions, all the MemoryDumpProviderInfo instances live in | 174 // - In nominal conditions, all the MemoryDumpProviderInfo instances live in |
| 166 // the |dump_providers_| collection (% unregistration while dumping). | 175 // the |dump_providers_| collection (% unregistration while dumping). |
| 167 // - Upon each dump they (actually their scoped_refptr-s) are copied into | 176 // - Upon each dump they (actually their scoped_refptr-s) are copied into |
| 168 // the ProcessMemoryDumpAsyncState. This is to allow removal (see below). | 177 // the ProcessMemoryDumpAsyncState. This is to allow removal (see below). |
| 169 // - When the MDP.OnMemoryDump() is invoked, the corresponding MDPInfo copy | 178 // - When the MDP.OnMemoryDump() is invoked, the corresponding MDPInfo copy |
| 170 // inside ProcessMemoryDumpAsyncState is removed. | 179 // inside ProcessMemoryDumpAsyncState is removed. |
| 171 // - In most cases, the MDPInfo is destroyed within UnregisterDumpProvider(). | 180 // - In most cases, the MDPInfo is destroyed within UnregisterDumpProvider(). |
| (...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 344 MemoryDumpProviderInfo::OrderedSet dump_providers_for_polling_; | 353 MemoryDumpProviderInfo::OrderedSet dump_providers_for_polling_; |
| 345 | 354 |
| 346 // Shared among all the PMDs to keep state scoped to the tracing session. | 355 // Shared among all the PMDs to keep state scoped to the tracing session. |
| 347 scoped_refptr<MemoryDumpSessionState> session_state_; | 356 scoped_refptr<MemoryDumpSessionState> session_state_; |
| 348 | 357 |
| 349 // The list of names of dump providers that are blacklisted from strict thread | 358 // The list of names of dump providers that are blacklisted from strict thread |
| 350 // affinity check on unregistration. | 359 // affinity check on unregistration. |
| 351 std::unordered_set<StringPiece, StringPieceHash> | 360 std::unordered_set<StringPiece, StringPieceHash> |
| 352 strict_thread_check_blacklist_; | 361 strict_thread_check_blacklist_; |
| 353 | 362 |
| 354 MemoryDumpManagerDelegate* delegate_; // Not owned. | 363 std::unique_ptr<MemoryDumpManagerDelegate> delegate_; |
| 355 | |
| 356 // When true, this instance is in charge of coordinating periodic dumps. | |
| 357 bool is_coordinator_; | |
| 358 | 364 |
| 359 // Protects from concurrent accesses to the |dump_providers_*| and |delegate_| | 365 // Protects from concurrent accesses to the |dump_providers_*| and |delegate_| |
| 360 // to guard against disabling logging while dumping on another thread. | 366 // to guard against disabling logging while dumping on another thread. |
| 361 Lock lock_; | 367 Lock lock_; |
| 362 | 368 |
| 363 // Optimization to avoid attempting any memory dump (i.e. to not walk an empty | 369 // Optimization to avoid attempting any memory dump (i.e. to not walk an empty |
| 364 // dump_providers_enabled_ list) when tracing is not enabled. | 370 // dump_providers_enabled_ list) when tracing is not enabled. |
| 365 subtle::AtomicWord memory_tracing_enabled_; | 371 subtle::AtomicWord memory_tracing_enabled_; |
| 366 | 372 |
| 367 // For triggering memory dumps. | 373 // For triggering memory dumps. |
| (...skipping 13 matching lines...) Expand all Loading... |
| 381 // Whether new memory dump providers should be told to enable heap profiling. | 387 // Whether new memory dump providers should be told to enable heap profiling. |
| 382 bool heap_profiling_enabled_; | 388 bool heap_profiling_enabled_; |
| 383 | 389 |
| 384 DISALLOW_COPY_AND_ASSIGN(MemoryDumpManager); | 390 DISALLOW_COPY_AND_ASSIGN(MemoryDumpManager); |
| 385 }; | 391 }; |
| 386 | 392 |
| 387 // The delegate is supposed to be long lived (read: a Singleton) and thread | 393 // The delegate is supposed to be long lived (read: a Singleton) and thread |
| 388 // safe (i.e. should expect calls from any thread and handle thread hopping). | 394 // safe (i.e. should expect calls from any thread and handle thread hopping). |
| 389 class BASE_EXPORT MemoryDumpManagerDelegate { | 395 class BASE_EXPORT MemoryDumpManagerDelegate { |
| 390 public: | 396 public: |
| 397 MemoryDumpManagerDelegate() {} |
| 398 virtual ~MemoryDumpManagerDelegate() {} |
| 399 |
| 391 virtual void RequestGlobalMemoryDump(const MemoryDumpRequestArgs& args, | 400 virtual void RequestGlobalMemoryDump(const MemoryDumpRequestArgs& args, |
| 392 const MemoryDumpCallback& callback) = 0; | 401 const MemoryDumpCallback& callback) = 0; |
| 393 | 402 |
| 394 // Returns tracing process id of the current process. This is used by | 403 virtual bool IsCoordinator() const = 0; |
| 395 // MemoryDumpManager::GetTracingProcessId. | |
| 396 virtual uint64_t GetTracingProcessId() const = 0; | |
| 397 | 404 |
| 398 protected: | 405 protected: |
| 399 MemoryDumpManagerDelegate() {} | |
| 400 virtual ~MemoryDumpManagerDelegate() {} | |
| 401 | |
| 402 void CreateProcessDump(const MemoryDumpRequestArgs& args, | 406 void CreateProcessDump(const MemoryDumpRequestArgs& args, |
| 403 const MemoryDumpCallback& callback) { | 407 const MemoryDumpCallback& callback) { |
| 404 MemoryDumpManager::GetInstance()->CreateProcessDump(args, callback); | 408 MemoryDumpManager::GetInstance()->CreateProcessDump(args, callback); |
| 405 } | 409 } |
| 406 | 410 |
| 407 private: | 411 private: |
| 408 DISALLOW_COPY_AND_ASSIGN(MemoryDumpManagerDelegate); | 412 DISALLOW_COPY_AND_ASSIGN(MemoryDumpManagerDelegate); |
| 409 }; | 413 }; |
| 410 | 414 |
| 411 } // namespace trace_event | 415 } // namespace trace_event |
| 412 } // namespace base | 416 } // namespace base |
| 413 | 417 |
| 414 #endif // BASE_TRACE_EVENT_MEMORY_DUMP_MANAGER_H_ | 418 #endif // BASE_TRACE_EVENT_MEMORY_DUMP_MANAGER_H_ |
| OLD | NEW |