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