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