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 <map> | 8 #include <map> |
9 #include <memory> | 9 #include <memory> |
10 #include <set> | 10 #include <set> |
11 #include <vector> | |
12 | 11 |
13 #include "base/atomicops.h" | 12 #include "base/atomicops.h" |
14 #include "base/containers/hash_tables.h" | 13 #include "base/containers/hash_tables.h" |
15 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
16 #include "base/memory/singleton.h" | 15 #include "base/memory/singleton.h" |
17 #include "base/synchronization/lock.h" | 16 #include "base/synchronization/lock.h" |
18 #include "base/timer/timer.h" | 17 #include "base/timer/timer.h" |
19 #include "base/trace_event/memory_dump_request_args.h" | 18 #include "base/trace_event/memory_dump_request_args.h" |
20 #include "base/trace_event/process_memory_dump.h" | 19 #include "base/trace_event/process_memory_dump.h" |
21 #include "base/trace_event/trace_event.h" | 20 #include "base/trace_event/trace_event.h" |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
124 void set_dumper_registrations_ignored_for_testing(bool ignored) { | 123 void set_dumper_registrations_ignored_for_testing(bool ignored) { |
125 dumper_registrations_ignored_for_testing_ = ignored; | 124 dumper_registrations_ignored_for_testing_ = ignored; |
126 } | 125 } |
127 | 126 |
128 private: | 127 private: |
129 friend std::default_delete<MemoryDumpManager>; // For the testing instance. | 128 friend std::default_delete<MemoryDumpManager>; // For the testing instance. |
130 friend struct DefaultSingletonTraits<MemoryDumpManager>; | 129 friend struct DefaultSingletonTraits<MemoryDumpManager>; |
131 friend class MemoryDumpManagerDelegate; | 130 friend class MemoryDumpManagerDelegate; |
132 friend class MemoryDumpManagerTest; | 131 friend class MemoryDumpManagerTest; |
133 | 132 |
134 // Descriptor used to hold information about registered MDPs. | 133 // Descriptor struct used to hold information about registered MDPs. It is |
135 // Some important considerations about lifetime of this object: | 134 // deliberately copyable, in order to allow it to be used as std::set value. |
136 // - In nominal conditions, all the MemoryDumpProviderInfo instances live in | 135 struct MemoryDumpProviderInfo { |
137 // the |dump_providers_| collection (% unregistration while dumping). | |
138 // - Upon each dump they (actually their scoped_refptr-s) are copied into | |
139 // the ProcessMemoryDumpAsyncState. This is to allow removal (see below). | |
140 // - When the MDP.OnMemoryDump() is invoked, the corresponding MDPInfo copy | |
141 // inside ProcessMemoryDumpAsyncState is removed. | |
142 // - In nominal conditions, the MDPInfo is destroyed in the | |
143 // UnregisterDumpProvider() call. | |
144 // - If UnregisterDumpProvider() is called while a dump is in progress, the | |
145 // MDPInfo is destroyed in the epilogue of ContinueAsyncProcessDump(), when | |
146 // the copy inside ProcessMemoryDumpAsyncState is erase()-d. | |
147 // - The non-const fields of MemoryDumpProviderInfo are safe to access only | |
148 // in the |task_runner| thread, unless the thread has been destroyed. | |
149 struct MemoryDumpProviderInfo | |
150 : public RefCountedThreadSafe<MemoryDumpProviderInfo> { | |
151 // Define a total order based on the thread (i.e. |task_runner|) affinity, | |
152 // so that all MDP belonging to the same thread are adjacent in the set. | |
153 struct Comparator { | |
154 bool operator()(const scoped_refptr<MemoryDumpProviderInfo>& a, | |
155 const scoped_refptr<MemoryDumpProviderInfo>& b) const; | |
156 }; | |
157 using OrderedSet = | |
158 std::set<scoped_refptr<MemoryDumpProviderInfo>, Comparator>; | |
159 | |
160 MemoryDumpProviderInfo( | 136 MemoryDumpProviderInfo( |
161 MemoryDumpProvider* dump_provider, | 137 MemoryDumpProvider* dump_provider, |
162 const char* name, | 138 const char* name, |
163 const scoped_refptr<SingleThreadTaskRunner>& task_runner, | 139 const scoped_refptr<SingleThreadTaskRunner>& task_runner, |
164 const MemoryDumpProvider::Options& options); | 140 const MemoryDumpProvider::Options& options); |
| 141 ~MemoryDumpProviderInfo(); |
| 142 |
| 143 // Define a total order based on the thread (i.e. |task_runner|) affinity, |
| 144 // so that all MDP belonging to the same thread are adjacent in the set. |
| 145 bool operator<(const MemoryDumpProviderInfo& other) const; |
165 | 146 |
166 MemoryDumpProvider* const dump_provider; | 147 MemoryDumpProvider* const dump_provider; |
167 | |
168 // Human readable name, for debugging and testing. Not necessarily unique. | |
169 const char* const name; | 148 const char* const name; |
170 | 149 |
171 // The task_runner affinity. Can be nullptr, in which case the dump provider | 150 // The task_runner affinity. Can be nullptr, in which case the dump provider |
172 // will be invoked on |dump_thread_|. | 151 // will be invoked on |dump_thread_|. |
173 const scoped_refptr<SingleThreadTaskRunner> task_runner; | 152 scoped_refptr<SingleThreadTaskRunner> task_runner; |
174 | 153 |
175 // The |options| arg passed to RegisterDumpProvider(). | 154 // The |options| arg passed to RegisterDumpProvider(). |
176 const MemoryDumpProvider::Options options; | 155 const MemoryDumpProvider::Options options; |
177 | 156 |
178 // For fail-safe logic (auto-disable failing MDPs). | 157 // For fail-safe logic (auto-disable failing MDPs). These fields are mutable |
179 int consecutive_failures; | 158 // as can be safely changed without impacting the order within the set. |
| 159 mutable int consecutive_failures; |
| 160 mutable bool disabled; |
180 | 161 |
181 // Flagged either by the auto-disable logic or during unregistration. | 162 // When a dump provider unregisters, it is flagged as |unregistered| and it |
182 bool disabled; | 163 // is removed only upon the next memory dump. This is to avoid altering the |
| 164 // |dump_providers_| collection while a dump is in progress. |
| 165 mutable bool unregistered; |
| 166 }; |
183 | 167 |
184 private: | 168 using MemoryDumpProviderInfoSet = std::set<MemoryDumpProviderInfo>; |
185 friend class base::RefCountedThreadSafe<MemoryDumpProviderInfo>; | |
186 ~MemoryDumpProviderInfo(); | |
187 | |
188 DISALLOW_COPY_AND_ASSIGN(MemoryDumpProviderInfo); | |
189 }; | |
190 | 169 |
191 // Holds the state of a process memory dump that needs to be carried over | 170 // Holds the state of a process memory dump that needs to be carried over |
192 // across threads in order to fulfil an asynchronous CreateProcessDump() | 171 // across threads in order to fulfil an asynchronous CreateProcessDump() |
193 // request. At any time exactly one thread owns a ProcessMemoryDumpAsyncState. | 172 // request. At any time exactly one thread owns a ProcessMemoryDumpAsyncState. |
194 struct ProcessMemoryDumpAsyncState { | 173 struct ProcessMemoryDumpAsyncState { |
195 ProcessMemoryDumpAsyncState( | 174 ProcessMemoryDumpAsyncState( |
196 MemoryDumpRequestArgs req_args, | 175 MemoryDumpRequestArgs req_args, |
197 const MemoryDumpProviderInfo::OrderedSet& dump_providers, | 176 MemoryDumpProviderInfoSet::iterator next_dump_provider, |
198 const scoped_refptr<MemoryDumpSessionState>& session_state, | 177 const scoped_refptr<MemoryDumpSessionState>& session_state, |
199 MemoryDumpCallback callback, | 178 MemoryDumpCallback callback, |
200 const scoped_refptr<SingleThreadTaskRunner>& dump_thread_task_runner); | 179 const scoped_refptr<SingleThreadTaskRunner>& dump_thread_task_runner); |
201 ~ProcessMemoryDumpAsyncState(); | 180 ~ProcessMemoryDumpAsyncState(); |
202 | 181 |
203 // Gets or creates the memory dump container for the given target process. | 182 // Gets or creates the memory dump container for the given target process. |
204 ProcessMemoryDump* GetOrCreateMemoryDumpContainerForProcess(ProcessId pid); | 183 ProcessMemoryDump* GetOrCreateMemoryDumpContainerForProcess(ProcessId pid); |
205 | 184 |
206 // A map of ProcessId -> ProcessMemoryDump, one for each target process | 185 // A map of ProcessId -> ProcessMemoryDump, one for each target process |
207 // being dumped from the current process. Typically each process dumps only | 186 // being dumped from the current process. Typically each process dumps only |
208 // for itself, unless dump providers specify a different |target_process| in | 187 // for itself, unless dump providers specify a different |target_process| in |
209 // MemoryDumpProvider::Options. | 188 // MemoryDumpProvider::Options. |
210 std::map<ProcessId, scoped_ptr<ProcessMemoryDump>> process_dumps; | 189 std::map<ProcessId, scoped_ptr<ProcessMemoryDump>> process_dumps; |
211 | 190 |
212 // The arguments passed to the initial CreateProcessDump() request. | 191 // The arguments passed to the initial CreateProcessDump() request. |
213 const MemoryDumpRequestArgs req_args; | 192 const MemoryDumpRequestArgs req_args; |
214 | 193 |
215 // An ordered sequence of dump providers that have to be invoked to complete | 194 // The |dump_providers_| iterator to the next dump provider that should be |
216 // the dump. This is a copy of |dump_providers_| at the beginning of a dump | 195 // invoked (or dump_providers_.end() if at the end of the sequence). |
217 // and becomes empty at the end, when all dump providers have been invoked. | 196 MemoryDumpProviderInfoSet::iterator next_dump_provider; |
218 std::vector<scoped_refptr<MemoryDumpProviderInfo>> pending_dump_providers; | |
219 | 197 |
220 // The trace-global session state. | 198 // The trace-global session state. |
221 scoped_refptr<MemoryDumpSessionState> session_state; | 199 scoped_refptr<MemoryDumpSessionState> session_state; |
222 | 200 |
223 // Callback passed to the initial call to CreateProcessDump(). | 201 // Callback passed to the initial call to CreateProcessDump(). |
224 MemoryDumpCallback callback; | 202 MemoryDumpCallback callback; |
225 | 203 |
226 // The thread on which FinalizeDumpAndAddToTrace() (and hence |callback|) | 204 // The thread on which FinalizeDumpAndAddToTrace() (and hence |callback|) |
227 // should be invoked. This is the thread on which the initial | 205 // should be invoked. This is the thread on which the initial |
228 // CreateProcessDump() request was called. | 206 // CreateProcessDump() request was called. |
(...skipping 12 matching lines...) Expand all Loading... |
241 | 219 |
242 static const int kMaxConsecutiveFailuresCount; | 220 static const int kMaxConsecutiveFailuresCount; |
243 static const char* const kSystemAllocatorPoolName; | 221 static const char* const kSystemAllocatorPoolName; |
244 | 222 |
245 MemoryDumpManager(); | 223 MemoryDumpManager(); |
246 ~MemoryDumpManager() override; | 224 ~MemoryDumpManager() override; |
247 | 225 |
248 static void SetInstanceForTesting(MemoryDumpManager* instance); | 226 static void SetInstanceForTesting(MemoryDumpManager* instance); |
249 static void FinalizeDumpAndAddToTrace( | 227 static void FinalizeDumpAndAddToTrace( |
250 scoped_ptr<ProcessMemoryDumpAsyncState> pmd_async_state); | 228 scoped_ptr<ProcessMemoryDumpAsyncState> pmd_async_state); |
| 229 static void AbortDumpLocked(MemoryDumpCallback callback, |
| 230 scoped_refptr<SingleThreadTaskRunner> task_runner, |
| 231 uint64_t dump_guid); |
251 | 232 |
252 // Internal, used only by MemoryDumpManagerDelegate. | 233 // Internal, used only by MemoryDumpManagerDelegate. |
253 // Creates a memory dump for the current process and appends it to the trace. | 234 // Creates a memory dump for the current process and appends it to the trace. |
254 // |callback| will be invoked asynchronously upon completion on the same | 235 // |callback| will be invoked asynchronously upon completion on the same |
255 // thread on which CreateProcessDump() was called. | 236 // thread on which CreateProcessDump() was called. |
256 void CreateProcessDump(const MemoryDumpRequestArgs& args, | 237 void CreateProcessDump(const MemoryDumpRequestArgs& args, |
257 const MemoryDumpCallback& callback); | 238 const MemoryDumpCallback& callback); |
258 | 239 |
259 // Continues the ProcessMemoryDump started by CreateProcessDump(), hopping | 240 // Continues the ProcessMemoryDump started by CreateProcessDump(), hopping |
260 // across threads as needed as specified by MDPs in RegisterDumpProvider(). | 241 // across threads as needed as specified by MDPs in RegisterDumpProvider(). |
261 void ContinueAsyncProcessDump( | 242 void ContinueAsyncProcessDump( |
262 ProcessMemoryDumpAsyncState* owned_pmd_async_state); | 243 scoped_ptr<ProcessMemoryDumpAsyncState> pmd_async_state); |
263 | 244 |
264 // An ordererd set of registered MemoryDumpProviderInfo(s), sorted by thread | 245 // An ordererd set of registered MemoryDumpProviderInfo(s), sorted by thread |
265 // affinity (MDPs belonging to the same thread are adjacent). | 246 // affinity (MDPs belonging to the same thread are adjacent). |
266 MemoryDumpProviderInfo::OrderedSet dump_providers_; | 247 MemoryDumpProviderInfoSet dump_providers_; |
267 | 248 |
268 // Shared among all the PMDs to keep state scoped to the tracing session. | 249 // Shared among all the PMDs to keep state scoped to the tracing session. |
269 scoped_refptr<MemoryDumpSessionState> session_state_; | 250 scoped_refptr<MemoryDumpSessionState> session_state_; |
270 | 251 |
271 MemoryDumpManagerDelegate* delegate_; // Not owned. | 252 MemoryDumpManagerDelegate* delegate_; // Not owned. |
272 | 253 |
273 // When true, this instance is in charge of coordinating periodic dumps. | 254 // When true, this instance is in charge of coordinating periodic dumps. |
274 bool is_coordinator_; | 255 bool is_coordinator_; |
275 | 256 |
276 // Protects from concurrent accesses to the |dump_providers_*| and |delegate_| | 257 // Protects from concurrent accesses to the |dump_providers_*| and |delegate_| |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
321 } | 302 } |
322 | 303 |
323 private: | 304 private: |
324 DISALLOW_COPY_AND_ASSIGN(MemoryDumpManagerDelegate); | 305 DISALLOW_COPY_AND_ASSIGN(MemoryDumpManagerDelegate); |
325 }; | 306 }; |
326 | 307 |
327 } // namespace trace_event | 308 } // namespace trace_event |
328 } // namespace base | 309 } // namespace base |
329 | 310 |
330 #endif // BASE_TRACE_EVENT_MEMORY_DUMP_MANAGER_H_ | 311 #endif // BASE_TRACE_EVENT_MEMORY_DUMP_MANAGER_H_ |
OLD | NEW |