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