| 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 <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/atomicops.h" | 10 #include "base/atomicops.h" |
| 11 #include "base/containers/hash_tables.h" | 11 #include "base/containers/hash_tables.h" |
| 12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
| 13 #include "base/memory/singleton.h" | 13 #include "base/memory/singleton.h" |
| 14 #include "base/synchronization/lock.h" | 14 #include "base/synchronization/lock.h" |
| 15 #include "base/timer/timer.h" | 15 #include "base/timer/timer.h" |
| 16 #include "base/trace_event/memory_dump_request_args.h" | 16 #include "base/trace_event/memory_dump_request_args.h" |
| 17 #include "base/trace_event/trace_event.h" | 17 #include "base/trace_event/trace_event.h" |
| 18 | 18 |
| 19 namespace base { | 19 namespace base { |
| 20 |
| 21 class SingleThreadTaskRunner; |
| 22 |
| 20 namespace trace_event { | 23 namespace trace_event { |
| 21 | 24 |
| 22 namespace { | 25 namespace { |
| 23 class ProcessMemoryDumpHolder; | 26 class ProcessMemoryDumpHolder; |
| 24 } | 27 } |
| 25 | 28 |
| 26 class MemoryDumpManagerDelegate; | 29 class MemoryDumpManagerDelegate; |
| 27 class MemoryDumpProvider; | 30 class MemoryDumpProvider; |
| 28 class ProcessMemoryDump; | 31 class ProcessMemoryDump; |
| 29 class MemoryDumpSessionState; | 32 class MemoryDumpSessionState; |
| 30 | 33 |
| 31 // This is the interface exposed to the rest of the codebase to deal with | 34 // This is the interface exposed to the rest of the codebase to deal with |
| 32 // memory tracing. The main entry point for clients is represented by | 35 // memory tracing. The main entry point for clients is represented by |
| 33 // RequestDumpPoint(). The extension by Un(RegisterDumpProvider). | 36 // RequestDumpPoint(). The extension by Un(RegisterDumpProvider). |
| 34 class BASE_EXPORT MemoryDumpManager : public TraceLog::EnabledStateObserver { | 37 class BASE_EXPORT MemoryDumpManager : public TraceLog::EnabledStateObserver { |
| 35 public: | 38 public: |
| 36 static const char* const kTraceCategoryForTesting; | 39 static const char* const kTraceCategoryForTesting; |
| 37 | 40 |
| 38 static MemoryDumpManager* GetInstance(); | 41 static MemoryDumpManager* GetInstance(); |
| 39 | 42 |
| 40 // Invoked once per process to register the TraceLog observer. | 43 // Invoked once per process to register the TraceLog observer. |
| 41 void Initialize(); | 44 void Initialize(); |
| 42 | 45 |
| 43 // See the lifetime and thread-safety requirements on the delegate below in | 46 // See the lifetime and thread-safety requirements on the delegate below in |
| 44 // the |MemoryDumpManagerDelegate| docstring. | 47 // the |MemoryDumpManagerDelegate| docstring. |
| 45 void SetDelegate(MemoryDumpManagerDelegate* delegate); | 48 void SetDelegate(MemoryDumpManagerDelegate* delegate); |
| 46 | 49 |
| 47 // MemoryDumpManager does NOT take memory ownership of |mdp|, which is | 50 // MemoryDumpManager does NOT take memory ownership of |mdp|, which is |
| 48 // expected to either be a singleton or unregister itself. | 51 // expected to either be a singleton or unregister itself. |
| 52 // 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 |
| 54 // able to handle calls on arbitrary threads. |
| 55 void RegisterDumpProvider( |
| 56 MemoryDumpProvider* mdp, |
| 57 const scoped_refptr<SingleThreadTaskRunner>& task_runner); |
| 49 void RegisterDumpProvider(MemoryDumpProvider* mdp); | 58 void RegisterDumpProvider(MemoryDumpProvider* mdp); |
| 50 void UnregisterDumpProvider(MemoryDumpProvider* mdp); | 59 void UnregisterDumpProvider(MemoryDumpProvider* mdp); |
| 51 | 60 |
| 52 // Requests a memory dump. The dump might happen or not depending on the | 61 // Requests a memory dump. The dump might happen or not depending on the |
| 53 // filters and categories specified when enabling tracing. | 62 // filters and categories specified when enabling tracing. |
| 54 // The optional |callback| is executed asynchronously, on an arbitrary thread, | 63 // The optional |callback| is executed asynchronously, on an arbitrary thread, |
| 55 // to notify about the completion of the global dump (i.e. after all the | 64 // to notify about the completion of the global dump (i.e. after all the |
| 56 // processes have dumped) and its success (true iff all the dumps were | 65 // processes have dumped) and its success (true iff all the dumps were |
| 57 // successful). | 66 // successful). |
| 58 void RequestGlobalDump(MemoryDumpType dump_type, | 67 void RequestGlobalDump(MemoryDumpType dump_type, |
| 59 const MemoryDumpCallback& callback); | 68 const MemoryDumpCallback& callback); |
| 60 | 69 |
| 61 // Same as above (still asynchronous), but without callback. | 70 // Same as above (still asynchronous), but without callback. |
| 62 void RequestGlobalDump(MemoryDumpType dump_type); | 71 void RequestGlobalDump(MemoryDumpType dump_type); |
| 63 | 72 |
| 64 // TraceLog::EnabledStateObserver implementation. | 73 // TraceLog::EnabledStateObserver implementation. |
| 65 void OnTraceLogEnabled() override; | 74 void OnTraceLogEnabled() override; |
| 66 void OnTraceLogDisabled() override; | 75 void OnTraceLogDisabled() override; |
| 67 | 76 |
| 68 // Returns the MemoryDumpProvider which is currently being dumping into a | |
| 69 // ProcessMemoryDump via DumpInto(...) if any, nullptr otherwise. | |
| 70 MemoryDumpProvider* dump_provider_currently_active() const { | |
| 71 return dump_provider_currently_active_; | |
| 72 } | |
| 73 | |
| 74 // Returns the MemoryDumpSessionState object, which is shared by all the | 77 // Returns the MemoryDumpSessionState object, which is shared by all the |
| 75 // ProcessMemoryDump and MemoryAllocatorDump instances through all the tracing | 78 // ProcessMemoryDump and MemoryAllocatorDump instances through all the tracing |
| 76 // session lifetime. | 79 // session lifetime. |
| 77 const scoped_refptr<MemoryDumpSessionState>& session_state() const { | 80 const scoped_refptr<MemoryDumpSessionState>& session_state() const { |
| 78 return session_state_; | 81 return session_state_; |
| 79 } | 82 } |
| 80 | 83 |
| 81 private: | 84 private: |
| 85 // Descriptor struct used to hold information about registered MDPs. It is |
| 86 // deliberately copyable, in order to allow to be used as hash_map value. |
| 87 struct MemoryDumpProviderInfo { |
| 88 MemoryDumpProviderInfo( |
| 89 const scoped_refptr<SingleThreadTaskRunner>& task_runner); |
| 90 ~MemoryDumpProviderInfo(); |
| 91 |
| 92 scoped_refptr<SingleThreadTaskRunner> task_runner; // Optional. |
| 93 bool disabled; // For fail-safe logic (auto-disable failing MDPs). |
| 94 }; |
| 95 |
| 82 friend struct DefaultDeleter<MemoryDumpManager>; // For the testing instance. | 96 friend struct DefaultDeleter<MemoryDumpManager>; // For the testing instance. |
| 83 friend struct DefaultSingletonTraits<MemoryDumpManager>; | 97 friend struct DefaultSingletonTraits<MemoryDumpManager>; |
| 84 friend class MemoryDumpManagerDelegate; | 98 friend class MemoryDumpManagerDelegate; |
| 85 friend class MemoryDumpManagerTest; | 99 friend class MemoryDumpManagerTest; |
| 86 | 100 |
| 87 static void SetInstanceForTesting(MemoryDumpManager* instance); | 101 static void SetInstanceForTesting(MemoryDumpManager* instance); |
| 88 | 102 |
| 89 MemoryDumpManager(); | 103 MemoryDumpManager(); |
| 90 virtual ~MemoryDumpManager(); | 104 virtual ~MemoryDumpManager(); |
| 91 | 105 |
| 92 // Internal, used only by MemoryDumpManagerDelegate. | 106 // Internal, used only by MemoryDumpManagerDelegate. |
| 93 // Creates a memory dump for the current process and appends it to the trace. | 107 // Creates a memory dump for the current process and appends it to the trace. |
| 94 // |callback| will be invoked asynchronously upon completion on the same | 108 // |callback| will be invoked asynchronously upon completion on the same |
| 95 // thread on which CreateProcessDump() was called. | 109 // thread on which CreateProcessDump() was called. |
| 96 void CreateProcessDump(const MemoryDumpRequestArgs& args, | 110 void CreateProcessDump(const MemoryDumpRequestArgs& args, |
| 97 const MemoryDumpCallback& callback); | 111 const MemoryDumpCallback& callback); |
| 98 | 112 |
| 99 bool InvokeDumpProviderLocked(MemoryDumpProvider* mdp, | 113 bool InvokeDumpProviderLocked(MemoryDumpProvider* mdp, |
| 100 ProcessMemoryDump* pmd); | 114 ProcessMemoryDump* pmd); |
| 101 void ContinueAsyncProcessDump( | 115 void ContinueAsyncProcessDump( |
| 102 MemoryDumpProvider* mdp, | 116 MemoryDumpProvider* mdp, |
| 103 scoped_refptr<ProcessMemoryDumpHolder> pmd_holder); | 117 scoped_refptr<ProcessMemoryDumpHolder> pmd_holder); |
| 104 | 118 |
| 105 hash_set<MemoryDumpProvider*> dump_providers_registered_; // Not owned. | 119 hash_map<MemoryDumpProvider*, MemoryDumpProviderInfo> dump_providers_; |
| 106 hash_set<MemoryDumpProvider*> dump_providers_enabled_; // Not owned. | |
| 107 | |
| 108 // TODO(primiano): this is required only until crbug.com/466121 gets fixed. | |
| 109 MemoryDumpProvider* dump_provider_currently_active_; // Not owned. | |
| 110 | 120 |
| 111 // Shared among all the PMDs to keep state scoped to the tracing session. | 121 // Shared among all the PMDs to keep state scoped to the tracing session. |
| 112 scoped_refptr<MemoryDumpSessionState> session_state_; | 122 scoped_refptr<MemoryDumpSessionState> session_state_; |
| 113 | 123 |
| 114 MemoryDumpManagerDelegate* delegate_; // Not owned. | 124 MemoryDumpManagerDelegate* delegate_; // Not owned. |
| 115 | 125 |
| 116 // Protects from concurrent accesses to the |dump_providers_*| and |delegate_| | 126 // Protects from concurrent accesses to the |dump_providers_*| and |delegate_| |
| 117 // to guard against disabling logging while dumping on another thread. | 127 // to guard against disabling logging while dumping on another thread. |
| 118 Lock lock_; | 128 Lock lock_; |
| 119 | 129 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 151 } | 161 } |
| 152 | 162 |
| 153 private: | 163 private: |
| 154 DISALLOW_COPY_AND_ASSIGN(MemoryDumpManagerDelegate); | 164 DISALLOW_COPY_AND_ASSIGN(MemoryDumpManagerDelegate); |
| 155 }; | 165 }; |
| 156 | 166 |
| 157 } // namespace trace_event | 167 } // namespace trace_event |
| 158 } // namespace base | 168 } // namespace base |
| 159 | 169 |
| 160 #endif // BASE_TRACE_EVENT_MEMORY_DUMP_MANAGER_H_ | 170 #endif // BASE_TRACE_EVENT_MEMORY_DUMP_MANAGER_H_ |
| OLD | NEW |