brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 1 | // Copyright 2017 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 | |
| 5 | #ifndef CHROME_PROFILING_MEMLOG_CONNECTION_MANAGER_H_ |
| 6 | #define CHROME_PROFILING_MEMLOG_CONNECTION_MANAGER_H_ |
| 7 | |
| 8 | #include <string> |
Erik Chen | e266c73 | 2017-08-12 02:27:19 | [diff] [blame] | 9 | #include <vector> |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 10 | |
| 11 | #include "base/containers/flat_map.h" |
| 12 | #include "base/macros.h" |
Erik Chen | e2d06447 | 2017-10-07 03:34:07 | [diff] [blame] | 13 | #include "base/memory/ref_counted.h" |
erikchen | a4d1919 | 2017-08-23 17:33:16 | [diff] [blame] | 14 | #include "base/memory/weak_ptr.h" |
Albert J. Wong | 59d85acb | 2017-08-10 00:50:57 | [diff] [blame] | 15 | #include "base/process/process_handle.h" |
erikchen | 66af016 | 2017-08-02 19:53:19 | [diff] [blame] | 16 | #include "base/synchronization/lock.h" |
erikchen | a6f3feb | 2018-02-14 16:21:56 | [diff] [blame] | 17 | #include "base/threading/thread.h" |
erikchen | 21a2cab | 2017-08-21 20:50:44 | [diff] [blame] | 18 | #include "base/values.h" |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 19 | #include "build/build_config.h" |
Erik Chen | e2d06447 | 2017-10-07 03:34:07 | [diff] [blame] | 20 | #include "chrome/profiling/allocation_event.h" |
| 21 | #include "chrome/profiling/allocation_tracker.h" |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 22 | #include "chrome/profiling/backtrace_storage.h" |
erikchen | 0d4a601 | 2018-04-03 16:06:33 | [diff] [blame^] | 23 | #include "components/services/heap_profiling/public/mojom/heap_profiling_service.mojom.h" |
Erik Chen | e2d06447 | 2017-10-07 03:34:07 | [diff] [blame] | 24 | #include "mojo/edk/embedder/scoped_platform_handle.h" |
Ken Rockot | 95c888a4 | 2018-02-11 05:54:11 | [diff] [blame] | 25 | #include "services/resource_coordinator/public/mojom/memory_instrumentation/memory_instrumentation.mojom.h" |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 26 | |
| 27 | namespace base { |
Erik Chen | e266c73 | 2017-08-12 02:27:19 | [diff] [blame] | 28 | |
Albert J. Wong | 3432f46 | 2017-08-02 02:47:24 | [diff] [blame] | 29 | class SequencedTaskRunner; |
Erik Chen | e266c73 | 2017-08-12 02:27:19 | [diff] [blame] | 30 | |
| 31 | } // namespace base |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 32 | |
| 33 | namespace profiling { |
| 34 | |
erikchen | 53cddfe6 | 2018-02-14 23:31:29 | [diff] [blame] | 35 | using VmRegions = |
| 36 | std::unordered_map<base::ProcessId, |
| 37 | std::vector<memory_instrumentation::mojom::VmRegionPtr>>; |
| 38 | |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 39 | // Manages all connections and logging for each process. Pipes are supplied by |
| 40 | // the pipe server and this class will connect them to a parser and logger. |
Albert J. Wong | 3432f46 | 2017-08-02 02:47:24 | [diff] [blame] | 41 | // |
| 42 | // Note |backtrace_storage| must outlive MemlogConnectionManager. |
| 43 | // |
| 44 | // This object is constructed on the UI thread, but the rest of the usage |
| 45 | // (including deletion) is on the IO thread. |
| 46 | class MemlogConnectionManager { |
erikchen | 67fff82 | 2018-02-21 18:53:43 | [diff] [blame] | 47 | using DumpProcessesForTracingCallback = memory_instrumentation::mojom:: |
| 48 | HeapProfiler::DumpProcessesForTracingCallback; |
| 49 | |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 50 | public: |
erikchen | a4d1919 | 2017-08-23 17:33:16 | [diff] [blame] | 51 | MemlogConnectionManager(); |
Albert J. Wong | 3432f46 | 2017-08-02 02:47:24 | [diff] [blame] | 52 | ~MemlogConnectionManager(); |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 53 | |
Erik Chen | e2d06447 | 2017-10-07 03:34:07 | [diff] [blame] | 54 | // Shared types for the dump-type-specific args structures. |
| 55 | struct DumpArgs { |
| 56 | DumpArgs(); |
| 57 | DumpArgs(DumpArgs&&) noexcept; |
| 58 | ~DumpArgs(); |
Erik Chen | 22f66c6d | 2017-10-06 23:48:50 | [diff] [blame] | 59 | |
| 60 | private: |
Erik Chen | e2d06447 | 2017-10-07 03:34:07 | [diff] [blame] | 61 | friend MemlogConnectionManager; |
| 62 | |
| 63 | // This lock keeps the backtrace atoms alive throughout the dumping |
| 64 | // process. It will be initialized by DumpProcess. |
| 65 | BacktraceStorage::Lock backtrace_storage_lock; |
| 66 | |
| 67 | DISALLOW_COPY_AND_ASSIGN(DumpArgs); |
erikchen | 1ca0e5f | 2017-10-06 22:06:14 | [diff] [blame] | 68 | }; |
| 69 | |
Erik Chen | e2d06447 | 2017-10-07 03:34:07 | [diff] [blame] | 70 | // Dumping is asynchronous so will not be complete when this function |
| 71 | // returns. The dump is complete when the callback provided in the args is |
| 72 | // fired. |
erikchen | 67fff82 | 2018-02-21 18:53:43 | [diff] [blame] | 73 | void DumpProcessesForTracing(bool keep_small_allocations, |
| 74 | bool strip_path_from_mapped_files, |
| 75 | DumpProcessesForTracingCallback callback, |
| 76 | VmRegions vm_regions); |
erikchen | 66af016 | 2017-08-02 19:53:19 | [diff] [blame] | 77 | |
Erik Chen | e2d06447 | 2017-10-07 03:34:07 | [diff] [blame] | 78 | void OnNewConnection(base::ProcessId pid, |
Brett Wilson | 40a6bb50 | 2017-10-10 20:36:15 | [diff] [blame] | 79 | mojom::ProfilingClientPtr client, |
erikchen | e382a10 | 2017-10-20 00:30:13 | [diff] [blame] | 80 | mojo::ScopedHandle receiver_pipe_end, |
Erik Chen | 3303fd023 | 2018-01-11 20:29:05 | [diff] [blame] | 81 | mojom::ProcessType process_type, |
Erik Chen | 8a2fb2e7 | 2018-02-09 20:34:29 | [diff] [blame] | 82 | mojom::ProfilingParamsPtr params); |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 83 | |
Erik Chen | fe6fbee | 2017-12-06 07:40:09 | [diff] [blame] | 84 | std::vector<base::ProcessId> GetConnectionPids(); |
erikchen | 53cddfe6 | 2018-02-14 23:31:29 | [diff] [blame] | 85 | std::vector<base::ProcessId> GetConnectionPidsThatNeedVmRegions(); |
Erik Chen | fe6fbee | 2017-12-06 07:40:09 | [diff] [blame] | 86 | |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 87 | private: |
| 88 | struct Connection; |
Erik Chen | e2d06447 | 2017-10-07 03:34:07 | [diff] [blame] | 89 | struct DumpProcessesForTracingTracking; |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 90 | |
Erik Chen | e2d06447 | 2017-10-07 03:34:07 | [diff] [blame] | 91 | void DoDumpOneProcessForTracing( |
| 92 | scoped_refptr<DumpProcessesForTracingTracking> tracking, |
| 93 | base::ProcessId pid, |
Erik Chen | a610d55 | 2017-10-20 22:29:49 | [diff] [blame] | 94 | mojom::ProcessType process_type, |
Erik Chen | 8bb76b5 | 2017-12-06 19:06:25 | [diff] [blame] | 95 | bool keep_small_allocations, |
erikchen | d1b8bc5 | 2017-12-21 18:12:42 | [diff] [blame] | 96 | bool strip_path_from_mapped_files, |
erikchen | 8bc20d8 | 2018-02-14 03:21:51 | [diff] [blame] | 97 | uint32_t sampling_rate, |
Erik Chen | e2d06447 | 2017-10-07 03:34:07 | [diff] [blame] | 98 | bool success, |
| 99 | AllocationCountMap counts, |
Erik Chen | 3303fd023 | 2018-01-11 20:29:05 | [diff] [blame] | 100 | AllocationTracker::ContextMap context, |
| 101 | AllocationTracker::AddressToStringMap mapped_strings); |
erikchen | 708c31d | 2017-09-19 00:28:53 | [diff] [blame] | 102 | |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 103 | // Notification that a connection is complete. Unlike OnNewConnection which |
| 104 | // is signaled by the pipe server, this is signaled by the allocation tracker |
| 105 | // to ensure that the pipeline for this process has been flushed of all |
| 106 | // messages. |
Albert J. Wong | 59d85acb | 2017-08-10 00:50:57 | [diff] [blame] | 107 | void OnConnectionComplete(base::ProcessId pid); |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 108 | |
erikchen | e382a10 | 2017-10-20 00:30:13 | [diff] [blame] | 109 | // Reports the ProcessTypes of the processes being profiled. |
| 110 | void ReportMetrics(); |
| 111 | |
Erik Chen | e2d06447 | 2017-10-07 03:34:07 | [diff] [blame] | 112 | // These thunks post the request back to the given thread. |
| 113 | static void OnConnectionCompleteThunk( |
erikchen | a4d1919 | 2017-08-23 17:33:16 | [diff] [blame] | 114 | scoped_refptr<base::SequencedTaskRunner> main_loop, |
Erik Chen | e2d06447 | 2017-10-07 03:34:07 | [diff] [blame] | 115 | base::WeakPtr<MemlogConnectionManager> connection_manager, |
Albert J. Wong | 59d85acb | 2017-08-10 00:50:57 | [diff] [blame] | 116 | base::ProcessId process_id); |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 117 | |
erikchen | 68532cc | 2017-08-16 19:38:36 | [diff] [blame] | 118 | BacktraceStorage backtrace_storage_; |
Albert J. Wong | 3432f46 | 2017-08-02 02:47:24 | [diff] [blame] | 119 | |
Erik Chen | e2d06447 | 2017-10-07 03:34:07 | [diff] [blame] | 120 | // Next ID to use for a barrier request. This is incremented for each use |
| 121 | // to ensure barrier IDs are unique. |
| 122 | uint32_t next_barrier_id_ = 1; |
| 123 | |
erikchen | 535ef8e | 2018-02-01 18:04:54 | [diff] [blame] | 124 | // The next ID to use when exporting a heap dump. |
| 125 | size_t next_id_ = 1; |
| 126 | |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 127 | // Maps process ID to the connection information for it. |
Albert J. Wong | 59d85acb | 2017-08-10 00:50:57 | [diff] [blame] | 128 | base::flat_map<base::ProcessId, std::unique_ptr<Connection>> connections_; |
erikchen | 66af016 | 2017-08-02 19:53:19 | [diff] [blame] | 129 | base::Lock connections_lock_; |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 130 | |
erikchen | e382a10 | 2017-10-20 00:30:13 | [diff] [blame] | 131 | // Every 24-hours, reports the types of profiled processes. |
| 132 | base::RepeatingTimer metrics_timer_; |
| 133 | |
erikchen | a6f3feb | 2018-02-14 16:21:56 | [diff] [blame] | 134 | // To avoid deadlock, synchronous calls to the browser are made on a dedicated |
| 135 | // thread that does nothing else. Both the IO thread and connection-specific |
| 136 | // threads could potentially be processing messages from the browser process, |
| 137 | // which in turn could be blocked on sending more messages over the memlog |
| 138 | // pipe. |
| 139 | base::Thread blocking_thread_; |
| 140 | |
erikchen | a4d1919 | 2017-08-23 17:33:16 | [diff] [blame] | 141 | // Must be last. |
| 142 | base::WeakPtrFactory<MemlogConnectionManager> weak_factory_; |
| 143 | |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 144 | DISALLOW_COPY_AND_ASSIGN(MemlogConnectionManager); |
| 145 | }; |
| 146 | |
| 147 | } // namespace profiling |
| 148 | |
| 149 | #endif // CHROME_PROFILING_MEMLOG_CONNECTION_MANAGER_H_ |