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