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
3 // found in the LICENSE file.
5 #ifndef BASE_TRACE_EVENT_MEMORY_DUMP_MANAGER_H_
6 #define BASE_TRACE_EVENT_MEMORY_DUMP_MANAGER_H_
10 #include "base/atomicops.h"
11 #include "base/memory/singleton.h"
12 #include "base/synchronization/lock.h"
13 #include "base/trace_event/trace_event.h"
16 namespace trace_event
{
18 class MemoryDumpProvider
;
20 // Captures the reason why a dump point is being requested. This is to allow
21 // selective enabling of dump points, filtering and post-processing.
22 enum class DumpPointType
{
23 TASK_BEGIN
, // Dumping memory at the beginning of a message-loop task.
24 TASK_END
, // Dumping memory at the ending of a message-loop task.
25 PERIODIC_INTERVAL
, // Dumping memory at periodic intervals.
26 EXPLICITLY_TRIGGERED
, // Non maskable dump request.
29 // This is the interface exposed to the rest of the codebase to deal with
30 // memory tracing. The main entry point for clients is represented by
31 // RequestDumpPoint(). The extension by Un(RegisterDumpProvider).
32 class BASE_EXPORT MemoryDumpManager
: public TraceLog::EnabledStateObserver
{
34 static MemoryDumpManager
* GetInstance();
36 // Invoked once per process to register the TraceLog observer.
39 // MemoryDumpManager does NOT take memory ownership of |mdp|, which is
40 // expected to be a singleton.
41 void RegisterDumpProvider(MemoryDumpProvider
* mdp
);
42 void UnregisterDumpProvider(MemoryDumpProvider
* mdp
);
44 // Requests a memory dump. The dump might happen or not depending on the
45 // filters and categories specified when enabling tracing.
46 void RequestDumpPoint(DumpPointType dump_point_type
);
48 // TraceLog::EnabledStateObserver implementation.
49 void OnTraceLogEnabled() override
;
50 void OnTraceLogDisabled() override
;
52 // Returns the MemoryDumpProvider which is currently being dumping into a
53 // ProcessMemoryDump via DumpInto(...) if any, nullptr otherwise.
54 MemoryDumpProvider
* dump_provider_currently_active() const {
55 return dump_provider_currently_active_
;
59 friend struct DefaultDeleter
<MemoryDumpManager
>; // For the testing instance.
60 friend struct DefaultSingletonTraits
<MemoryDumpManager
>;
61 friend class MemoryDumpManagerTest
;
63 static const char kTraceCategory
[];
65 static void SetInstanceForTesting(MemoryDumpManager
* instance
);
68 virtual ~MemoryDumpManager();
70 // Broadcasts the dump requests to the other processes.
71 void BroadcastDumpRequest();
73 // Creates a dump point for the current process and appends it to the trace.
74 void CreateLocalDumpPoint(DumpPointType dump_point_type
, uint64 guid
);
76 std::vector
<MemoryDumpProvider
*> dump_providers_registered_
; // Not owned.
77 std::vector
<MemoryDumpProvider
*> dump_providers_enabled_
; // Not owned.
79 // TODO(primiano): this is required only until crbug.com/466121 gets fixed.
80 MemoryDumpProvider
* dump_provider_currently_active_
; // Now owned.
82 // Protects from concurrent accesses to the |dump_providers_*|, e.g., tearing
83 // down logging while creating a dump point on another thread.
86 // Optimization to avoid attempting any dump point (i.e. to not walk an empty
87 // dump_providers_enabled_ list) when tracing is not enabled.
88 subtle::AtomicWord memory_tracing_enabled_
;
90 DISALLOW_COPY_AND_ASSIGN(MemoryDumpManager
);
93 } // namespace trace_event
96 #endif // BASE_TRACE_EVENT_MEMORY_DUMP_MANAGER_H_