memory_dump_manager.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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.
  4. #ifndef BASE_TRACE_EVENT_MEMORY_DUMP_MANAGER_H_
  5. #define BASE_TRACE_EVENT_MEMORY_DUMP_MANAGER_H_
  6. #include <stdint.h>
  7. #include <map>
  8. #include <memory>
  9. #include <unordered_set>
  10. #include <vector>
  11. #include "base/atomicops.h"
  12. #include "base/macros.h"
  13. #include "base/memory/ref_counted.h"
  14. #include "base/memory/singleton.h"
  15. #include "base/synchronization/lock.h"
  16. #include "base/trace_event/memory_allocator_dump.h"
  17. #include "base/trace_event/memory_dump_provider_info.h"
  18. #include "base/trace_event/memory_dump_request_args.h"
  19. #include "base/trace_event/process_memory_dump.h"
  20. #include "base/trace_event/trace_event.h"
  21. namespace base {
  22. class SequencedTaskRunner;
  23. class SingleThreadTaskRunner;
  24. class Thread;
  25. namespace trace_event {
  26. class MemoryDumpProvider;
  27. // This is the interface exposed to the rest of the codebase to deal with
  28. // memory tracing. The main entry point for clients is represented by
  29. // RequestDumpPoint(). The extension by Un(RegisterDumpProvider).
  30. class BASE_EXPORT MemoryDumpManager {
  31. public:
  32. using RequestGlobalDumpFunction =
  33. RepeatingCallback<void(MemoryDumpType, MemoryDumpLevelOfDetail)>;
  34. static constexpr const char* const kTraceCategory =
  35. TRACE_DISABLED_BY_DEFAULT("memory-infra");
  36. // This value is returned as the tracing id of the child processes by
  37. // GetTracingProcessId() when tracing is not enabled.
  38. static const uint64_t kInvalidTracingProcessId;
  39. static MemoryDumpManager* GetInstance();
  40. static std::unique_ptr<MemoryDumpManager> CreateInstanceForTesting();
  41. // Invoked once per process to listen to trace begin / end events.
  42. // Initialization can happen after (Un)RegisterMemoryDumpProvider() calls
  43. // and the MemoryDumpManager guarantees to support this.
  44. // On the other side, the MemoryDumpManager will not be fully operational
  45. // (any CreateProcessDump() will return a failure) until initialized.
  46. // Arguments:
  47. // is_coordinator: True when current process coordinates the periodic dump
  48. // triggering.
  49. // request_dump_function: Function to invoke a global dump. Global dump
  50. // involves embedder-specific behaviors like multiprocess handshaking.
  51. // TODO(primiano): this is only required to trigger global dumps from
  52. // the scheduler. Should be removed once they are both moved out of base.
  53. void Initialize(RequestGlobalDumpFunction request_dump_function,
  54. bool is_coordinator);
  55. // (Un)Registers a MemoryDumpProvider instance.
  56. // Args:
  57. // - mdp: the MemoryDumpProvider instance to be registered. MemoryDumpManager
  58. // does NOT take memory ownership of |mdp|, which is expected to either
  59. // be a singleton or unregister itself.
  60. // - name: a friendly name (duplicates allowed). Used for debugging and
  61. // run-time profiling of memory-infra internals. Must be a long-lived
  62. // C string.
  63. // - task_runner: either a SingleThreadTaskRunner or SequencedTaskRunner. All
  64. // the calls to |mdp| will be run on the given |task_runner|. If passed
  65. // null |mdp| should be able to handle calls on arbitrary threads.
  66. // - options: extra optional arguments. See memory_dump_provider.h.
  67. void RegisterDumpProvider(MemoryDumpProvider* mdp,
  68. const char* name,
  69. scoped_refptr<SingleThreadTaskRunner> task_runner);
  70. void RegisterDumpProvider(MemoryDumpProvider* mdp,
  71. const char* name,
  72. scoped_refptr<SingleThreadTaskRunner> task_runner,
  73. MemoryDumpProvider::Options options);
  74. void RegisterDumpProviderWithSequencedTaskRunner(
  75. MemoryDumpProvider* mdp,
  76. const char* name,
  77. scoped_refptr<SequencedTaskRunner> task_runner,
  78. MemoryDumpProvider::Options options);
  79. void UnregisterDumpProvider(MemoryDumpProvider* mdp);
  80. // Unregisters an unbound dump provider and takes care about its deletion
  81. // asynchronously. Can be used only for for dump providers with no
  82. // task-runner affinity.
  83. // This method takes ownership of the dump provider and guarantees that:
  84. // - The |mdp| will be deleted at some point in the near future.
  85. // - Its deletion will not happen concurrently with the OnMemoryDump() call.
  86. // Note that OnMemoryDump() calls can still happen after this method returns.
  87. void UnregisterAndDeleteDumpProviderSoon(
  88. std::unique_ptr<MemoryDumpProvider> mdp);
  89. // Prepare MemoryDumpManager for CreateProcessDump() calls for tracing-related
  90. // modes (i.e. |level_of_detail| != SUMMARY_ONLY).
  91. // Also initializes the scheduler with the given config.
  92. void SetupForTracing(const TraceConfig::MemoryDumpConfig&);
  93. // Tear-down tracing related state.
  94. // Non-tracing modes (e.g. SUMMARY_ONLY) will continue to work.
  95. void TeardownForTracing();
  96. // Creates a memory dump for the current process and appends it to the trace.
  97. // |callback| will be invoked asynchronously upon completion on the same
  98. // thread on which CreateProcessDump() was called. This method should only be
  99. // used by the memory-infra service while creating a global memory dump.
  100. void CreateProcessDump(const MemoryDumpRequestArgs& args,
  101. ProcessMemoryDumpCallback callback);
  102. // Lets tests see if a dump provider is registered.
  103. bool IsDumpProviderRegisteredForTesting(MemoryDumpProvider*);
  104. // Returns a unique id for identifying the processes. The id can be
  105. // retrieved by child processes only when tracing is enabled. This is
  106. // intended to express cross-process sharing of memory dumps on the
  107. // child-process side, without having to know its own child process id.
  108. uint64_t GetTracingProcessId() const { return tracing_process_id_; }
  109. void set_tracing_process_id(uint64_t tracing_process_id) {
  110. tracing_process_id_ = tracing_process_id;
  111. }
  112. // Returns the name for a the allocated_objects dump. Use this to declare
  113. // suballocator dumps from other dump providers.
  114. // It will return nullptr if there is no dump provider for the system
  115. // allocator registered (which is currently the case for Mac OS).
  116. const char* system_allocator_pool_name() const {
  117. return kSystemAllocatorPoolName;
  118. }
  119. // When set to true, calling |RegisterMemoryDumpProvider| is a no-op.
  120. void set_dumper_registrations_ignored_for_testing(bool ignored) {
  121. dumper_registrations_ignored_for_testing_ = ignored;
  122. }
  123. scoped_refptr<SequencedTaskRunner> GetDumpThreadTaskRunner();
  124. private:
  125. friend std::default_delete<MemoryDumpManager>; // For the testing instance.
  126. friend struct DefaultSingletonTraits<MemoryDumpManager>;
  127. friend class MemoryDumpManagerTest;
  128. FRIEND_TEST_ALL_PREFIXES(MemoryDumpManagerTest,
  129. NoStackOverflowWithTooManyMDPs);
  130. // Holds the state of a process memory dump that needs to be carried over
  131. // across task runners in order to fulfill an asynchronous CreateProcessDump()
  132. // request. At any time exactly one task runner owns a
  133. // ProcessMemoryDumpAsyncState.
  134. struct ProcessMemoryDumpAsyncState {
  135. ProcessMemoryDumpAsyncState(
  136. MemoryDumpRequestArgs req_args,
  137. const MemoryDumpProviderInfo::OrderedSet& dump_providers,
  138. ProcessMemoryDumpCallback callback,
  139. scoped_refptr<SequencedTaskRunner> dump_thread_task_runner);
  140. ~ProcessMemoryDumpAsyncState();
  141. // A ProcessMemoryDump to collect data from MemoryDumpProviders.
  142. std::unique_ptr<ProcessMemoryDump> process_memory_dump;
  143. // The arguments passed to the initial CreateProcessDump() request.
  144. const MemoryDumpRequestArgs req_args;
  145. // An ordered sequence of dump providers that have to be invoked to complete
  146. // the dump. This is a copy of |dump_providers_| at the beginning of a dump
  147. // and becomes empty at the end, when all dump providers have been invoked.
  148. std::vector<scoped_refptr<MemoryDumpProviderInfo>> pending_dump_providers;
  149. // Callback passed to the initial call to CreateProcessDump().
  150. ProcessMemoryDumpCallback callback;
  151. // The thread on which FinishAsyncProcessDump() (and hence |callback|)
  152. // should be invoked. This is the thread on which the initial
  153. // CreateProcessDump() request was called.
  154. const scoped_refptr<SingleThreadTaskRunner> callback_task_runner;
  155. // The thread on which unbound dump providers should be invoked.
  156. // This is essentially |dump_thread_|.task_runner() but needs to be kept
  157. // as a separate variable as it needs to be accessed by arbitrary dumpers'
  158. // threads outside of the lock_ to avoid races when disabling tracing.
  159. // It is immutable for all the duration of a tracing session.
  160. const scoped_refptr<SequencedTaskRunner> dump_thread_task_runner;
  161. private:
  162. DISALLOW_COPY_AND_ASSIGN(ProcessMemoryDumpAsyncState);
  163. };
  164. static const int kMaxConsecutiveFailuresCount;
  165. static const char* const kSystemAllocatorPoolName;
  166. MemoryDumpManager();
  167. virtual ~MemoryDumpManager();
  168. static void SetInstanceForTesting(MemoryDumpManager* instance);
  169. // Lazily initializes dump_thread_ and returns its TaskRunner.
  170. scoped_refptr<base::SequencedTaskRunner> GetOrCreateBgTaskRunnerLocked()
  171. EXCLUSIVE_LOCKS_REQUIRED(lock_);
  172. // Calls InvokeOnMemoryDump() for the each MDP that belongs to the current
  173. // task runner and switches to the task runner of the next MDP. Handles
  174. // failures in MDP and thread hops, and always calls FinishAsyncProcessDump()
  175. // at the end.
  176. void ContinueAsyncProcessDump(
  177. ProcessMemoryDumpAsyncState* owned_pmd_async_state);
  178. // Invokes OnMemoryDump() of the given MDP. Should be called on the MDP task
  179. // runner.
  180. void InvokeOnMemoryDump(MemoryDumpProviderInfo* mdpinfo,
  181. ProcessMemoryDump* pmd);
  182. void FinishAsyncProcessDump(
  183. std::unique_ptr<ProcessMemoryDumpAsyncState> pmd_async_state);
  184. // Helper for RegierDumpProvider* functions.
  185. void RegisterDumpProviderInternal(
  186. MemoryDumpProvider* mdp,
  187. const char* name,
  188. scoped_refptr<SequencedTaskRunner> task_runner,
  189. const MemoryDumpProvider::Options& options);
  190. // Helper for the public UnregisterDumpProvider* functions.
  191. void UnregisterDumpProviderInternal(MemoryDumpProvider* mdp,
  192. bool take_mdp_ownership_and_delete_async);
  193. bool can_request_global_dumps() const {
  194. return !request_dump_function_.is_null();
  195. }
  196. // An ordered set of registered MemoryDumpProviderInfo(s), sorted by task
  197. // runner affinity (MDPs belonging to the same task runners are adjacent).
  198. MemoryDumpProviderInfo::OrderedSet dump_providers_ GUARDED_BY(lock_);
  199. // Function provided by the embedder to handle global dump requests.
  200. RequestGlobalDumpFunction request_dump_function_;
  201. // True when current process coordinates the periodic dump triggering.
  202. bool is_coordinator_ GUARDED_BY(lock_);
  203. // Protects from concurrent accesses to the local state, eg: to guard against
  204. // disabling logging while dumping on another thread.
  205. Lock lock_;
  206. // Thread used for MemoryDumpProviders which don't specify a task runner
  207. // affinity.
  208. std::unique_ptr<Thread> dump_thread_ GUARDED_BY(lock_);
  209. // The unique id of the child process. This is created only for tracing and is
  210. // expected to be valid only when tracing is enabled.
  211. uint64_t tracing_process_id_;
  212. // When true, calling |RegisterMemoryDumpProvider| is a no-op.
  213. bool dumper_registrations_ignored_for_testing_;
  214. DISALLOW_COPY_AND_ASSIGN(MemoryDumpManager);
  215. };
  216. } // namespace trace_event
  217. } // namespace base
  218. #endif // BASE_TRACE_EVENT_MEMORY_DUMP_MANAGER_H_