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 <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/atomicops.h" | 10 #include "base/atomicops.h" |
| 11 #include "base/containers/hash_tables.h" |
| 12 #include "base/memory/ref_counted.h" |
11 #include "base/memory/singleton.h" | 13 #include "base/memory/singleton.h" |
12 #include "base/synchronization/lock.h" | 14 #include "base/synchronization/lock.h" |
| 15 #include "base/timer/timer.h" |
| 16 #include "base/trace_event/memory_dump_request_args.h" |
13 #include "base/trace_event/trace_event.h" | 17 #include "base/trace_event/trace_event.h" |
14 | 18 |
15 namespace base { | 19 namespace base { |
16 namespace trace_event { | 20 namespace trace_event { |
17 | 21 |
| 22 namespace { |
| 23 class ProcessMemoryDumpHolder; |
| 24 } |
| 25 |
| 26 class MemoryDumpManagerDelegate; |
18 class MemoryDumpProvider; | 27 class MemoryDumpProvider; |
19 | 28 class ProcessMemoryDump; |
20 // Captures the reason why a dump point is being requested. This is to allow | 29 class MemoryDumpSessionState; |
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. | |
27 }; | |
28 | 30 |
29 // This is the interface exposed to the rest of the codebase to deal with | 31 // 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 | 32 // memory tracing. The main entry point for clients is represented by |
31 // RequestDumpPoint(). The extension by Un(RegisterDumpProvider). | 33 // RequestDumpPoint(). The extension by Un(RegisterDumpProvider). |
32 class BASE_EXPORT MemoryDumpManager : public TraceLog::EnabledStateObserver { | 34 class BASE_EXPORT MemoryDumpManager : public TraceLog::EnabledStateObserver { |
33 public: | 35 public: |
| 36 static const char* const kTraceCategoryForTesting; |
| 37 |
34 static MemoryDumpManager* GetInstance(); | 38 static MemoryDumpManager* GetInstance(); |
35 | 39 |
36 // Invoked once per process to register the TraceLog observer. | 40 // Invoked once per process to register the TraceLog observer. |
37 void Initialize(); | 41 void Initialize(); |
38 | 42 |
| 43 // See the lifetime and thread-safety requirements on the delegate below in |
| 44 // the |MemoryDumpManagerDelegate| docstring. |
| 45 void SetDelegate(MemoryDumpManagerDelegate* delegate); |
| 46 |
39 // MemoryDumpManager does NOT take memory ownership of |mdp|, which is | 47 // MemoryDumpManager does NOT take memory ownership of |mdp|, which is |
40 // expected to be a singleton. | 48 // expected to either be a singleton or unregister itself. |
41 void RegisterDumpProvider(MemoryDumpProvider* mdp); | 49 void RegisterDumpProvider(MemoryDumpProvider* mdp); |
42 void UnregisterDumpProvider(MemoryDumpProvider* mdp); | 50 void UnregisterDumpProvider(MemoryDumpProvider* mdp); |
43 | 51 |
44 // Requests a memory dump. The dump might happen or not depending on the | 52 // Requests a memory dump. The dump might happen or not depending on the |
45 // filters and categories specified when enabling tracing. | 53 // filters and categories specified when enabling tracing. |
46 void RequestDumpPoint(DumpPointType dump_point_type); | 54 // The optional |callback| is executed asynchronously, on an arbitrary thread, |
| 55 // to notify about the completion of the global dump (i.e. after all the |
| 56 // processes have dumped) and its success (true iff all the dumps were |
| 57 // successful). |
| 58 void RequestGlobalDump(MemoryDumpType dump_type, |
| 59 const MemoryDumpCallback& callback); |
| 60 |
| 61 // Same as above (still asynchronous), but without callback. |
| 62 void RequestGlobalDump(MemoryDumpType dump_type); |
47 | 63 |
48 // TraceLog::EnabledStateObserver implementation. | 64 // TraceLog::EnabledStateObserver implementation. |
49 void OnTraceLogEnabled() override; | 65 void OnTraceLogEnabled() override; |
50 void OnTraceLogDisabled() override; | 66 void OnTraceLogDisabled() override; |
51 | 67 |
52 // Returns the MemoryDumpProvider which is currently being dumping into a | 68 // Returns the MemoryDumpProvider which is currently being dumping into a |
53 // ProcessMemoryDump via DumpInto(...) if any, nullptr otherwise. | 69 // ProcessMemoryDump via DumpInto(...) if any, nullptr otherwise. |
54 MemoryDumpProvider* dump_provider_currently_active() const { | 70 MemoryDumpProvider* dump_provider_currently_active() const { |
55 return dump_provider_currently_active_; | 71 return dump_provider_currently_active_; |
56 } | 72 } |
57 | 73 |
| 74 // Returns the MemoryDumpSessionState object, which is shared by all the |
| 75 // ProcessMemoryDump and MemoryAllocatorDump instances through all the tracing |
| 76 // session lifetime. |
| 77 const scoped_refptr<MemoryDumpSessionState>& session_state() const { |
| 78 return session_state_; |
| 79 } |
| 80 |
58 private: | 81 private: |
59 friend struct DefaultDeleter<MemoryDumpManager>; // For the testing instance. | 82 friend struct DefaultDeleter<MemoryDumpManager>; // For the testing instance. |
60 friend struct DefaultSingletonTraits<MemoryDumpManager>; | 83 friend struct DefaultSingletonTraits<MemoryDumpManager>; |
| 84 friend class MemoryDumpManagerDelegate; |
61 friend class MemoryDumpManagerTest; | 85 friend class MemoryDumpManagerTest; |
62 | 86 |
63 static const char kTraceCategory[]; | |
64 | |
65 static void SetInstanceForTesting(MemoryDumpManager* instance); | 87 static void SetInstanceForTesting(MemoryDumpManager* instance); |
66 | 88 |
67 MemoryDumpManager(); | 89 MemoryDumpManager(); |
68 virtual ~MemoryDumpManager(); | 90 virtual ~MemoryDumpManager(); |
69 | 91 |
70 // Broadcasts the dump requests to the other processes. | 92 // Internal, used only by MemoryDumpManagerDelegate. |
71 void BroadcastDumpRequest(); | 93 // Creates a memory dump for the current process and appends it to the trace. |
| 94 // |callback| will be invoked asynchronously upon completion on the same |
| 95 // thread on which CreateProcessDump() was called. |
| 96 void CreateProcessDump(const MemoryDumpRequestArgs& args, |
| 97 const MemoryDumpCallback& callback); |
72 | 98 |
73 // Creates a dump point for the current process and appends it to the trace. | 99 bool InvokeDumpProviderLocked(MemoryDumpProvider* mdp, |
74 void CreateLocalDumpPoint(DumpPointType dump_point_type, uint64 guid); | 100 ProcessMemoryDump* pmd); |
| 101 void ContinueAsyncProcessDump( |
| 102 MemoryDumpProvider* mdp, |
| 103 scoped_refptr<ProcessMemoryDumpHolder> pmd_holder); |
75 | 104 |
76 std::vector<MemoryDumpProvider*> dump_providers_registered_; // Not owned. | 105 hash_set<MemoryDumpProvider*> dump_providers_registered_; // Not owned. |
77 std::vector<MemoryDumpProvider*> dump_providers_enabled_; // Not owned. | 106 hash_set<MemoryDumpProvider*> dump_providers_enabled_; // Not owned. |
78 | 107 |
79 // TODO(primiano): this is required only until crbug.com/466121 gets fixed. | 108 // TODO(primiano): this is required only until crbug.com/466121 gets fixed. |
80 MemoryDumpProvider* dump_provider_currently_active_; // Now owned. | 109 MemoryDumpProvider* dump_provider_currently_active_; // Not owned. |
81 | 110 |
82 // Protects from concurrent accesses to the |dump_providers_*|, e.g., tearing | 111 // Shared among all the PMDs to keep state scoped to the tracing session. |
83 // down logging while creating a dump point on another thread. | 112 scoped_refptr<MemoryDumpSessionState> session_state_; |
| 113 |
| 114 MemoryDumpManagerDelegate* delegate_; // Not owned. |
| 115 |
| 116 // Protects from concurrent accesses to the |dump_providers_*| and |delegate_| |
| 117 // to guard against disabling logging while dumping on another thread. |
84 Lock lock_; | 118 Lock lock_; |
85 | 119 |
86 // Optimization to avoid attempting any dump point (i.e. to not walk an empty | 120 // Optimization to avoid attempting any memory dump (i.e. to not walk an empty |
87 // dump_providers_enabled_ list) when tracing is not enabled. | 121 // dump_providers_enabled_ list) when tracing is not enabled. |
88 subtle::AtomicWord memory_tracing_enabled_; | 122 subtle::AtomicWord memory_tracing_enabled_; |
89 | 123 |
| 124 // For time-triggered periodic dumps. |
| 125 RepeatingTimer<MemoryDumpManager> periodic_dump_timer_; |
| 126 |
| 127 // Skips the auto-registration of the core dumpers during Initialize(). |
| 128 bool skip_core_dumpers_auto_registration_for_testing_; |
| 129 |
90 DISALLOW_COPY_AND_ASSIGN(MemoryDumpManager); | 130 DISALLOW_COPY_AND_ASSIGN(MemoryDumpManager); |
91 }; | 131 }; |
92 | 132 |
| 133 // The delegate is supposed to be long lived (read: a Singleton) and thread |
| 134 // safe (i.e. should expect calls from any thread and handle thread hopping). |
| 135 class BASE_EXPORT MemoryDumpManagerDelegate { |
| 136 public: |
| 137 virtual void RequestGlobalMemoryDump(const MemoryDumpRequestArgs& args, |
| 138 const MemoryDumpCallback& callback) = 0; |
| 139 |
| 140 // Determines whether the MemoryDumpManager instance should be the master |
| 141 // (the ones which initiates and coordinates the multiprocess dumps) or not. |
| 142 virtual bool IsCoordinatorProcess() const = 0; |
| 143 |
| 144 protected: |
| 145 MemoryDumpManagerDelegate() {} |
| 146 virtual ~MemoryDumpManagerDelegate() {} |
| 147 |
| 148 void CreateProcessDump(const MemoryDumpRequestArgs& args, |
| 149 const MemoryDumpCallback& callback) { |
| 150 MemoryDumpManager::GetInstance()->CreateProcessDump(args, callback); |
| 151 } |
| 152 |
| 153 private: |
| 154 DISALLOW_COPY_AND_ASSIGN(MemoryDumpManagerDelegate); |
| 155 }; |
| 156 |
93 } // namespace trace_event | 157 } // namespace trace_event |
94 } // namespace base | 158 } // namespace base |
95 | 159 |
96 #endif // BASE_TRACE_EVENT_MEMORY_DUMP_MANAGER_H_ | 160 #endif // BASE_TRACE_EVENT_MEMORY_DUMP_MANAGER_H_ |
OLD | NEW |