| 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" |
| (...skipping 18 matching lines...) Expand all Loading... |
| 29 class MemoryDumpManagerDelegate; | 29 class MemoryDumpManagerDelegate; |
| 30 class MemoryDumpProvider; | 30 class MemoryDumpProvider; |
| 31 class ProcessMemoryDump; | 31 class ProcessMemoryDump; |
| 32 class MemoryDumpSessionState; | 32 class MemoryDumpSessionState; |
| 33 | 33 |
| 34 // 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 |
| 35 // memory tracing. The main entry point for clients is represented by | 35 // memory tracing. The main entry point for clients is represented by |
| 36 // RequestDumpPoint(). The extension by Un(RegisterDumpProvider). | 36 // RequestDumpPoint(). The extension by Un(RegisterDumpProvider). |
| 37 class BASE_EXPORT MemoryDumpManager : public TraceLog::EnabledStateObserver { | 37 class BASE_EXPORT MemoryDumpManager : public TraceLog::EnabledStateObserver { |
| 38 public: | 38 public: |
| 39 static const int kInvalidTracingProcessId; | |
| 40 static const char* const kTraceCategoryForTesting; | 39 static const char* const kTraceCategoryForTesting; |
| 41 | 40 |
| 42 static MemoryDumpManager* GetInstance(); | 41 static MemoryDumpManager* GetInstance(); |
| 43 | 42 |
| 44 // Invoked once per process to register the TraceLog observer. | 43 // Invoked once per process to register the TraceLog observer. |
| 45 void Initialize(); | 44 void Initialize(); |
| 46 | 45 |
| 47 // 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 |
| 48 // the |MemoryDumpManagerDelegate| docstring. | 47 // the |MemoryDumpManagerDelegate| docstring. |
| 49 void SetDelegate(MemoryDumpManagerDelegate* delegate); | 48 void SetDelegate(MemoryDumpManagerDelegate* delegate); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 75 void OnTraceLogEnabled() override; | 74 void OnTraceLogEnabled() override; |
| 76 void OnTraceLogDisabled() override; | 75 void OnTraceLogDisabled() override; |
| 77 | 76 |
| 78 // Returns the MemoryDumpSessionState object, which is shared by all the | 77 // Returns the MemoryDumpSessionState object, which is shared by all the |
| 79 // ProcessMemoryDump and MemoryAllocatorDump instances through all the tracing | 78 // ProcessMemoryDump and MemoryAllocatorDump instances through all the tracing |
| 80 // session lifetime. | 79 // session lifetime. |
| 81 const scoped_refptr<MemoryDumpSessionState>& session_state() const { | 80 const scoped_refptr<MemoryDumpSessionState>& session_state() const { |
| 82 return session_state_; | 81 return session_state_; |
| 83 } | 82 } |
| 84 | 83 |
| 85 // Derives a tracing process id from a child process id. Child process ids | |
| 86 // cannot be used directly in tracing for security reasons (see: discussion in | |
| 87 // crrev.com/1173263004). This method is meant to be used when dumping | |
| 88 // cross-process shared memory from a process which knows the child process id | |
| 89 // of its endpoints. The value returned by this method is guaranteed to be | |
| 90 // equal to the value returned by tracing_process_id() in the corresponding | |
| 91 // child process. | |
| 92 static int ChildProcessIdToTracingProcessId(int child_id); | |
| 93 | |
| 94 // Returns a unique id for the current process. The id can be retrieved only | |
| 95 // by child processes and only when tracing is enabled. This is intended to | |
| 96 // express cross-process sharing of memory dumps on the child-process side, | |
| 97 // without having to know its own child process id. | |
| 98 int tracing_process_id() const { | |
| 99 DCHECK_NE(kInvalidTracingProcessId, tracing_process_id_); | |
| 100 return tracing_process_id_; | |
| 101 } | |
| 102 | |
| 103 private: | 84 private: |
| 104 // Descriptor struct used to hold information about registered MDPs. It is | 85 // Descriptor struct used to hold information about registered MDPs. It is |
| 105 // deliberately copyable, in order to allow to be used as hash_map value. | 86 // deliberately copyable, in order to allow to be used as hash_map value. |
| 106 struct MemoryDumpProviderInfo { | 87 struct MemoryDumpProviderInfo { |
| 107 MemoryDumpProviderInfo( | 88 MemoryDumpProviderInfo( |
| 108 const scoped_refptr<SingleThreadTaskRunner>& task_runner); | 89 const scoped_refptr<SingleThreadTaskRunner>& task_runner); |
| 109 ~MemoryDumpProviderInfo(); | 90 ~MemoryDumpProviderInfo(); |
| 110 | 91 |
| 111 scoped_refptr<SingleThreadTaskRunner> task_runner; // Optional. | 92 scoped_refptr<SingleThreadTaskRunner> task_runner; // Optional. |
| 112 int consecutive_failures; // Number of times the provider failed (to | 93 int consecutive_failures; // Number of times the provider failed (to |
| (...skipping 20 matching lines...) Expand all Loading... |
| 133 // thread on which CreateProcessDump() was called. | 114 // thread on which CreateProcessDump() was called. |
| 134 void CreateProcessDump(const MemoryDumpRequestArgs& args, | 115 void CreateProcessDump(const MemoryDumpRequestArgs& args, |
| 135 const MemoryDumpCallback& callback); | 116 const MemoryDumpCallback& callback); |
| 136 | 117 |
| 137 bool InvokeDumpProviderLocked(MemoryDumpProvider* mdp, | 118 bool InvokeDumpProviderLocked(MemoryDumpProvider* mdp, |
| 138 ProcessMemoryDump* pmd); | 119 ProcessMemoryDump* pmd); |
| 139 void ContinueAsyncProcessDump( | 120 void ContinueAsyncProcessDump( |
| 140 MemoryDumpProvider* mdp, | 121 MemoryDumpProvider* mdp, |
| 141 scoped_refptr<ProcessMemoryDumpHolder> pmd_holder); | 122 scoped_refptr<ProcessMemoryDumpHolder> pmd_holder); |
| 142 | 123 |
| 143 // Pass kInvalidTracingProcessId for invalidating the id. | |
| 144 void set_tracing_process_id(int id) { | |
| 145 DCHECK(tracing_process_id_ == kInvalidTracingProcessId || | |
| 146 id == kInvalidTracingProcessId || | |
| 147 tracing_process_id_ == id); | |
| 148 tracing_process_id_ = id; | |
| 149 } | |
| 150 | |
| 151 hash_map<MemoryDumpProvider*, MemoryDumpProviderInfo> dump_providers_; | 124 hash_map<MemoryDumpProvider*, MemoryDumpProviderInfo> dump_providers_; |
| 152 | 125 |
| 153 // Shared among all the PMDs to keep state scoped to the tracing session. | 126 // Shared among all the PMDs to keep state scoped to the tracing session. |
| 154 scoped_refptr<MemoryDumpSessionState> session_state_; | 127 scoped_refptr<MemoryDumpSessionState> session_state_; |
| 155 | 128 |
| 156 MemoryDumpManagerDelegate* delegate_; // Not owned. | 129 MemoryDumpManagerDelegate* delegate_; // Not owned. |
| 157 | 130 |
| 158 // Protects from concurrent accesses to the |dump_providers_*| and |delegate_| | 131 // Protects from concurrent accesses to the |dump_providers_*| and |delegate_| |
| 159 // to guard against disabling logging while dumping on another thread. | 132 // to guard against disabling logging while dumping on another thread. |
| 160 Lock lock_; | 133 Lock lock_; |
| 161 | 134 |
| 162 // Optimization to avoid attempting any memory dump (i.e. to not walk an empty | 135 // Optimization to avoid attempting any memory dump (i.e. to not walk an empty |
| 163 // dump_providers_enabled_ list) when tracing is not enabled. | 136 // dump_providers_enabled_ list) when tracing is not enabled. |
| 164 subtle::AtomicWord memory_tracing_enabled_; | 137 subtle::AtomicWord memory_tracing_enabled_; |
| 165 | 138 |
| 166 // For time-triggered periodic dumps. | 139 // For time-triggered periodic dumps. |
| 167 RepeatingTimer<MemoryDumpManager> periodic_dump_timer_; | 140 RepeatingTimer<MemoryDumpManager> periodic_dump_timer_; |
| 168 | 141 |
| 169 // The unique id of the child process. This is created only for tracing and is | |
| 170 // expected to be valid only when tracing is enabled. | |
| 171 int tracing_process_id_; | |
| 172 | |
| 173 // Skips the auto-registration of the core dumpers during Initialize(). | 142 // Skips the auto-registration of the core dumpers during Initialize(). |
| 174 bool skip_core_dumpers_auto_registration_for_testing_; | 143 bool skip_core_dumpers_auto_registration_for_testing_; |
| 175 | 144 |
| 176 DISALLOW_COPY_AND_ASSIGN(MemoryDumpManager); | 145 DISALLOW_COPY_AND_ASSIGN(MemoryDumpManager); |
| 177 }; | 146 }; |
| 178 | 147 |
| 179 // The delegate is supposed to be long lived (read: a Singleton) and thread | 148 // The delegate is supposed to be long lived (read: a Singleton) and thread |
| 180 // safe (i.e. should expect calls from any thread and handle thread hopping). | 149 // safe (i.e. should expect calls from any thread and handle thread hopping). |
| 181 class BASE_EXPORT MemoryDumpManagerDelegate { | 150 class BASE_EXPORT MemoryDumpManagerDelegate { |
| 182 public: | 151 public: |
| 183 virtual void RequestGlobalMemoryDump(const MemoryDumpRequestArgs& args, | 152 virtual void RequestGlobalMemoryDump(const MemoryDumpRequestArgs& args, |
| 184 const MemoryDumpCallback& callback) = 0; | 153 const MemoryDumpCallback& callback) = 0; |
| 185 | 154 |
| 186 // Determines whether the MemoryDumpManager instance should be the master | 155 // Determines whether the MemoryDumpManager instance should be the master |
| 187 // (the ones which initiates and coordinates the multiprocess dumps) or not. | 156 // (the ones which initiates and coordinates the multiprocess dumps) or not. |
| 188 virtual bool IsCoordinatorProcess() const = 0; | 157 virtual bool IsCoordinatorProcess() const = 0; |
| 189 | 158 |
| 190 protected: | 159 protected: |
| 191 MemoryDumpManagerDelegate() {} | 160 MemoryDumpManagerDelegate() {} |
| 192 virtual ~MemoryDumpManagerDelegate() {} | 161 virtual ~MemoryDumpManagerDelegate() {} |
| 193 | 162 |
| 194 void CreateProcessDump(const MemoryDumpRequestArgs& args, | 163 void CreateProcessDump(const MemoryDumpRequestArgs& args, |
| 195 const MemoryDumpCallback& callback) { | 164 const MemoryDumpCallback& callback) { |
| 196 MemoryDumpManager::GetInstance()->CreateProcessDump(args, callback); | 165 MemoryDumpManager::GetInstance()->CreateProcessDump(args, callback); |
| 197 } | 166 } |
| 198 | 167 |
| 199 void set_tracing_process_id(int id) { | |
| 200 MemoryDumpManager::GetInstance()->set_tracing_process_id(id); | |
| 201 } | |
| 202 | |
| 203 private: | 168 private: |
| 204 DISALLOW_COPY_AND_ASSIGN(MemoryDumpManagerDelegate); | 169 DISALLOW_COPY_AND_ASSIGN(MemoryDumpManagerDelegate); |
| 205 }; | 170 }; |
| 206 | 171 |
| 207 } // namespace trace_event | 172 } // namespace trace_event |
| 208 } // namespace base | 173 } // namespace base |
| 209 | 174 |
| 210 #endif // BASE_TRACE_EVENT_MEMORY_DUMP_MANAGER_H_ | 175 #endif // BASE_TRACE_EVENT_MEMORY_DUMP_MANAGER_H_ |
| OLD | NEW |