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 <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <map> | 10 #include <map> |
11 #include <memory> | 11 #include <memory> |
12 #include <set> | 12 #include <set> |
13 #include <vector> | 13 #include <vector> |
14 | 14 |
15 #include "base/atomicops.h" | 15 #include "base/atomicops.h" |
16 #include "base/containers/hash_tables.h" | 16 #include "base/containers/hash_tables.h" |
17 #include "base/macros.h" | 17 #include "base/macros.h" |
18 #include "base/memory/ref_counted.h" | 18 #include "base/memory/ref_counted.h" |
19 #include "base/memory/singleton.h" | 19 #include "base/memory/singleton.h" |
20 #include "base/synchronization/lock.h" | 20 #include "base/synchronization/lock.h" |
21 #include "base/timer/timer.h" | |
22 #include "base/trace_event/memory_dump_request_args.h" | 21 #include "base/trace_event/memory_dump_request_args.h" |
23 #include "base/trace_event/process_memory_dump.h" | 22 #include "base/trace_event/process_memory_dump.h" |
24 #include "base/trace_event/trace_event.h" | 23 #include "base/trace_event/trace_event.h" |
25 | 24 |
26 namespace base { | 25 namespace base { |
27 | 26 |
28 class SingleThreadTaskRunner; | 27 class SingleThreadTaskRunner; |
29 class Thread; | 28 class Thread; |
30 | 29 |
31 namespace trace_event { | 30 namespace trace_event { |
32 | 31 |
33 class MemoryDumpManagerDelegate; | 32 class MemoryDumpManagerDelegate; |
34 class MemoryDumpProvider; | 33 class MemoryDumpProvider; |
35 class MemoryDumpSessionState; | 34 class MemoryDumpSessionState; |
| 35 class MemoryDumpScheduler; |
36 | 36 |
37 // This is the interface exposed to the rest of the codebase to deal with | 37 // This is the interface exposed to the rest of the codebase to deal with |
38 // memory tracing. The main entry point for clients is represented by | 38 // memory tracing. The main entry point for clients is represented by |
39 // RequestDumpPoint(). The extension by Un(RegisterDumpProvider). | 39 // RequestDumpPoint(). The extension by Un(RegisterDumpProvider). |
40 class BASE_EXPORT MemoryDumpManager : public TraceLog::EnabledStateObserver { | 40 class BASE_EXPORT MemoryDumpManager : public TraceLog::EnabledStateObserver { |
41 public: | 41 public: |
42 static const char* const kTraceCategory; | 42 static const char* const kTraceCategory; |
43 static const char* const kLogPrefix; | 43 static const char* const kLogPrefix; |
44 | 44 |
45 // This value is returned as the tracing id of the child processes by | 45 // This value is returned as the tracing id of the child processes by |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
148 // When set to true, calling |RegisterMemoryDumpProvider| is a no-op. | 148 // When set to true, calling |RegisterMemoryDumpProvider| is a no-op. |
149 void set_dumper_registrations_ignored_for_testing(bool ignored) { | 149 void set_dumper_registrations_ignored_for_testing(bool ignored) { |
150 dumper_registrations_ignored_for_testing_ = ignored; | 150 dumper_registrations_ignored_for_testing_ = ignored; |
151 } | 151 } |
152 | 152 |
153 private: | 153 private: |
154 friend std::default_delete<MemoryDumpManager>; // For the testing instance. | 154 friend std::default_delete<MemoryDumpManager>; // For the testing instance. |
155 friend struct DefaultSingletonTraits<MemoryDumpManager>; | 155 friend struct DefaultSingletonTraits<MemoryDumpManager>; |
156 friend class MemoryDumpManagerDelegate; | 156 friend class MemoryDumpManagerDelegate; |
157 friend class MemoryDumpManagerTest; | 157 friend class MemoryDumpManagerTest; |
| 158 friend class MemoryDumpScheduler; |
158 | 159 |
159 // Descriptor used to hold information about registered MDPs. | 160 // Descriptor used to hold information about registered MDPs. |
160 // Some important considerations about lifetime of this object: | 161 // Some important considerations about lifetime of this object: |
161 // - In nominal conditions, all the MemoryDumpProviderInfo instances live in | 162 // - In nominal conditions, all the MemoryDumpProviderInfo instances live in |
162 // the |dump_providers_| collection (% unregistration while dumping). | 163 // the |dump_providers_| collection (% unregistration while dumping). |
163 // - Upon each dump they (actually their scoped_refptr-s) are copied into | 164 // - Upon each dump they (actually their scoped_refptr-s) are copied into |
164 // the ProcessMemoryDumpAsyncState. This is to allow removal (see below). | 165 // the ProcessMemoryDumpAsyncState. This is to allow removal (see below). |
165 // - When the MDP.OnMemoryDump() is invoked, the corresponding MDPInfo copy | 166 // - When the MDP.OnMemoryDump() is invoked, the corresponding MDPInfo copy |
166 // inside ProcessMemoryDumpAsyncState is removed. | 167 // inside ProcessMemoryDumpAsyncState is removed. |
167 // - In most cases, the MDPInfo is destroyed within UnregisterDumpProvider(). | 168 // - In most cases, the MDPInfo is destroyed within UnregisterDumpProvider(). |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
270 // This is essentially |dump_thread_|.task_runner() but needs to be kept | 271 // This is essentially |dump_thread_|.task_runner() but needs to be kept |
271 // as a separate variable as it needs to be accessed by arbitrary dumpers' | 272 // as a separate variable as it needs to be accessed by arbitrary dumpers' |
272 // threads outside of the lock_ to avoid races when disabling tracing. | 273 // threads outside of the lock_ to avoid races when disabling tracing. |
273 // It is immutable for all the duration of a tracing session. | 274 // It is immutable for all the duration of a tracing session. |
274 const scoped_refptr<SingleThreadTaskRunner> dump_thread_task_runner; | 275 const scoped_refptr<SingleThreadTaskRunner> dump_thread_task_runner; |
275 | 276 |
276 private: | 277 private: |
277 DISALLOW_COPY_AND_ASSIGN(ProcessMemoryDumpAsyncState); | 278 DISALLOW_COPY_AND_ASSIGN(ProcessMemoryDumpAsyncState); |
278 }; | 279 }; |
279 | 280 |
280 // Sets up periodic memory dump timers to start global dump requests based on | |
281 // the dump triggers from trace config. | |
282 class BASE_EXPORT PeriodicGlobalDumpTimer { | |
283 public: | |
284 PeriodicGlobalDumpTimer(); | |
285 ~PeriodicGlobalDumpTimer(); | |
286 | |
287 void Start(const std::vector<TraceConfig::MemoryDumpConfig::Trigger>& | |
288 triggers_list); | |
289 void Stop(); | |
290 | |
291 bool IsRunning(); | |
292 | |
293 private: | |
294 // Periodically called by the timer. | |
295 void RequestPeriodicGlobalDump(); | |
296 | |
297 RepeatingTimer timer_; | |
298 uint32_t periodic_dumps_count_; | |
299 uint32_t light_dump_rate_; | |
300 uint32_t heavy_dump_rate_; | |
301 | |
302 DISALLOW_COPY_AND_ASSIGN(PeriodicGlobalDumpTimer); | |
303 }; | |
304 | |
305 static const int kMaxConsecutiveFailuresCount; | 281 static const int kMaxConsecutiveFailuresCount; |
306 static const char* const kSystemAllocatorPoolName; | 282 static const char* const kSystemAllocatorPoolName; |
307 | 283 |
308 MemoryDumpManager(); | 284 MemoryDumpManager(); |
309 ~MemoryDumpManager() override; | 285 ~MemoryDumpManager() override; |
310 | 286 |
311 static void SetInstanceForTesting(MemoryDumpManager* instance); | 287 static void SetInstanceForTesting(MemoryDumpManager* instance); |
312 static void FinalizeDumpAndAddToTrace( | 288 static void FinalizeDumpAndAddToTrace( |
313 std::unique_ptr<ProcessMemoryDumpAsyncState> pmd_async_state); | 289 std::unique_ptr<ProcessMemoryDumpAsyncState> pmd_async_state); |
314 | 290 |
(...skipping 13 matching lines...) Expand all Loading... |
328 // Invokes OnMemoryDump() of the next MDP and calls SetupNextMemoryDump() at | 304 // Invokes OnMemoryDump() of the next MDP and calls SetupNextMemoryDump() at |
329 // the end to continue the ProcessMemoryDump. Should be called on the MDP task | 305 // the end to continue the ProcessMemoryDump. Should be called on the MDP task |
330 // runner. | 306 // runner. |
331 void InvokeOnMemoryDump(ProcessMemoryDumpAsyncState* owned_pmd_async_state); | 307 void InvokeOnMemoryDump(ProcessMemoryDumpAsyncState* owned_pmd_async_state); |
332 | 308 |
333 // Records a quick total memory usage in |memory_total|. This is used to track | 309 // Records a quick total memory usage in |memory_total|. This is used to track |
334 // and detect peaks in the memory usage of the process without having to | 310 // and detect peaks in the memory usage of the process without having to |
335 // record all data from dump providers. This value is approximate to trade-off | 311 // record all data from dump providers. This value is approximate to trade-off |
336 // speed, and not consistent with the rest of the memory-infra metrics. Must | 312 // speed, and not consistent with the rest of the memory-infra metrics. Must |
337 // be called on the dump thread. | 313 // be called on the dump thread. |
338 void PollFastMemoryTotal(uint64_t* memory_total); | 314 // Returns true if |memory_total| was updated by polling at least 1 MDP. |
| 315 bool PollFastMemoryTotal(uint64_t* memory_total); |
339 | 316 |
340 // Helper for RegierDumpProvider* functions. | 317 // Helper for RegierDumpProvider* functions. |
341 void RegisterDumpProviderInternal( | 318 void RegisterDumpProviderInternal( |
342 MemoryDumpProvider* mdp, | 319 MemoryDumpProvider* mdp, |
343 const char* name, | 320 const char* name, |
344 scoped_refptr<SequencedTaskRunner> task_runner, | 321 scoped_refptr<SequencedTaskRunner> task_runner, |
345 const MemoryDumpProvider::Options& options); | 322 const MemoryDumpProvider::Options& options); |
346 | 323 |
347 // Helper for the public UnregisterDumpProvider* functions. | 324 // Helper for the public UnregisterDumpProvider* functions. |
348 void UnregisterDumpProviderInternal(MemoryDumpProvider* mdp, | 325 void UnregisterDumpProviderInternal(MemoryDumpProvider* mdp, |
(...skipping 28 matching lines...) Expand all Loading... |
377 bool is_coordinator_; | 354 bool is_coordinator_; |
378 | 355 |
379 // Protects from concurrent accesses to the |dump_providers_*| and |delegate_| | 356 // Protects from concurrent accesses to the |dump_providers_*| and |delegate_| |
380 // to guard against disabling logging while dumping on another thread. | 357 // to guard against disabling logging while dumping on another thread. |
381 Lock lock_; | 358 Lock lock_; |
382 | 359 |
383 // Optimization to avoid attempting any memory dump (i.e. to not walk an empty | 360 // Optimization to avoid attempting any memory dump (i.e. to not walk an empty |
384 // dump_providers_enabled_ list) when tracing is not enabled. | 361 // dump_providers_enabled_ list) when tracing is not enabled. |
385 subtle::AtomicWord memory_tracing_enabled_; | 362 subtle::AtomicWord memory_tracing_enabled_; |
386 | 363 |
387 // For time-triggered periodic dumps. | 364 // For triggering memory dumps. |
388 PeriodicGlobalDumpTimer periodic_dump_timer_; | 365 std::unique_ptr<MemoryDumpScheduler> dump_scheduler_; |
389 | 366 |
390 // Thread used for MemoryDumpProviders which don't specify a task runner | 367 // Thread used for MemoryDumpProviders which don't specify a task runner |
391 // affinity. | 368 // affinity. |
392 std::unique_ptr<Thread> dump_thread_; | 369 std::unique_ptr<Thread> dump_thread_; |
393 | 370 |
394 // The unique id of the child process. This is created only for tracing and is | 371 // The unique id of the child process. This is created only for tracing and is |
395 // expected to be valid only when tracing is enabled. | 372 // expected to be valid only when tracing is enabled. |
396 uint64_t tracing_process_id_; | 373 uint64_t tracing_process_id_; |
397 | 374 |
398 // When true, calling |RegisterMemoryDumpProvider| is a no-op. | 375 // When true, calling |RegisterMemoryDumpProvider| is a no-op. |
(...skipping 26 matching lines...) Expand all Loading... |
425 } | 402 } |
426 | 403 |
427 private: | 404 private: |
428 DISALLOW_COPY_AND_ASSIGN(MemoryDumpManagerDelegate); | 405 DISALLOW_COPY_AND_ASSIGN(MemoryDumpManagerDelegate); |
429 }; | 406 }; |
430 | 407 |
431 } // namespace trace_event | 408 } // namespace trace_event |
432 } // namespace base | 409 } // namespace base |
433 | 410 |
434 #endif // BASE_TRACE_EVENT_MEMORY_DUMP_MANAGER_H_ | 411 #endif // BASE_TRACE_EVENT_MEMORY_DUMP_MANAGER_H_ |
OLD | NEW |