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