De-duplicate BASE_IMPLEMENTATION define in the GN build.
[chromium-blink-merge.git] / base / trace_event / memory_dump_manager.h
blob8a9b3b7e95a414dc72d3b17333199ff97d0bf21f
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_
8 #include <vector>
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"
15 namespace base {
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 {
33 public:
34 static MemoryDumpManager* GetInstance();
36 // Invoked once per process to register the TraceLog observer.
37 void Initialize();
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 private:
53 friend struct DefaultDeleter<MemoryDumpManager>; // For the testing instance.
54 friend struct DefaultSingletonTraits<MemoryDumpManager>;
55 friend class MemoryDumpManagerTest;
57 static const char kTraceCategory[];
59 static void SetInstanceForTesting(MemoryDumpManager* instance);
61 MemoryDumpManager();
62 virtual ~MemoryDumpManager();
64 // Broadcasts the dump requests to the other processes.
65 void BroadcastDumpRequest();
67 // Creates a dump point for the current process and appends it to the trace.
68 void CreateLocalDumpPoint(DumpPointType dump_point_type, uint64 guid);
70 std::vector<MemoryDumpProvider*> dump_providers_registered_; // Not owned.
71 std::vector<MemoryDumpProvider*> dump_providers_enabled_; // Not owned.
73 // Protects from concurrent accesses to the |dump_providers_*|, e.g., tearing
74 // down logging while creating a dump point on another thread.
75 Lock lock_;
77 // Optimization to avoid attempting any dump point (i.e. to not walk an empty
78 // dump_providers_enabled_ list) when tracing is not enabled.
79 subtle::AtomicWord memory_tracing_enabled_;
81 DISALLOW_COPY_AND_ASSIGN(MemoryDumpManager);
84 } // namespace trace_event
85 } // namespace base
87 #endif // BASE_TRACE_EVENT_MEMORY_DUMP_MANAGER_H_