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