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 | |
erikchen | fa983faa | 2018-04-05 18:56:42 | [diff] [blame] | 5 | #ifndef COMPONENTS_SERVICES_HEAP_PROFILING_CONNECTION_MANAGER_H_ |
| 6 | #define COMPONENTS_SERVICES_HEAP_PROFILING_CONNECTION_MANAGER_H_ |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 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" |
Sebastien Marchand | efda77e53 | 2019-01-25 22:53:52 | [diff] [blame] | 18 | #include "base/timer/timer.h" |
erikchen | 21a2cab | 2017-08-21 20:50:44 | [diff] [blame] | 19 | #include "base/values.h" |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 20 | #include "build/build_config.h" |
erikchen | fa983faa | 2018-04-05 18:56:42 | [diff] [blame] | 21 | #include "components/services/heap_profiling/allocation_event.h" |
| 22 | #include "components/services/heap_profiling/allocation_tracker.h" |
| 23 | #include "components/services/heap_profiling/backtrace_storage.h" |
erikchen | 0d4a601 | 2018-04-03 16:06:33 | [diff] [blame] | 24 | #include "components/services/heap_profiling/public/mojom/heap_profiling_service.mojom.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 | |
erikchen | 102fe21 | 2018-04-06 13:02:10 | [diff] [blame] | 33 | namespace heap_profiling { |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 34 | |
erikchen | 53cddfe6 | 2018-02-14 23:31:29 | [diff] [blame] | 35 | using VmRegions = |
Ken Rockot | 08942735 | 2018-04-24 14:51:37 | [diff] [blame] | 36 | base::flat_map<base::ProcessId, |
| 37 | std::vector<memory_instrumentation::mojom::VmRegionPtr>>; |
erikchen | 53cddfe6 | 2018-02-14 23:31:29 | [diff] [blame] | 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 | // |
erikchen | fa983faa | 2018-04-05 18:56:42 | [diff] [blame] | 42 | // Note |backtrace_storage| must outlive ConnectionManager. |
Albert J. Wong | 3432f46 | 2017-08-02 02:47:24 | [diff] [blame] | 43 | // |
| 44 | // This object is constructed on the UI thread, but the rest of the usage |
| 45 | // (including deletion) is on the IO thread. |
erikchen | fa983faa | 2018-04-05 18:56:42 | [diff] [blame] | 46 | class ConnectionManager { |
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 | fa983faa | 2018-04-05 18:56:42 | [diff] [blame] | 51 | ConnectionManager(); |
| 52 | ~ConnectionManager(); |
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: |
erikchen | fa983faa | 2018-04-05 18:56:42 | [diff] [blame] | 61 | friend ConnectionManager; |
Erik Chen | e2d06447 | 2017-10-07 03:34:07 | [diff] [blame] | 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. |
Alexei Filippov | e024518 | 2019-03-08 20:17:05 | [diff] [blame] | 73 | void DumpProcessesForTracing(bool strip_path_from_mapped_files, |
erikchen | 67fff82 | 2018-02-21 18:53:43 | [diff] [blame] | 74 | DumpProcessesForTracingCallback callback, |
| 75 | VmRegions vm_regions); |
erikchen | 66af016 | 2017-08-02 19:53:19 | [diff] [blame] | 76 | |
Erik Chen | e2d06447 | 2017-10-07 03:34:07 | [diff] [blame] | 77 | void OnNewConnection(base::ProcessId pid, |
Brett Wilson | 40a6bb50 | 2017-10-10 20:36:15 | [diff] [blame] | 78 | mojom::ProfilingClientPtr client, |
erikchen | e382a10 | 2017-10-20 00:30:13 | [diff] [blame] | 79 | mojo::ScopedHandle receiver_pipe_end, |
Erik Chen | 3303fd023 | 2018-01-11 20:29:05 | [diff] [blame] | 80 | mojom::ProcessType process_type, |
Erik Chen | 8a2fb2e7 | 2018-02-09 20:34:29 | [diff] [blame] | 81 | mojom::ProfilingParamsPtr params); |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 82 | |
Erik Chen | fe6fbee | 2017-12-06 07:40:09 | [diff] [blame] | 83 | std::vector<base::ProcessId> GetConnectionPids(); |
erikchen | 53cddfe6 | 2018-02-14 23:31:29 | [diff] [blame] | 84 | std::vector<base::ProcessId> GetConnectionPidsThatNeedVmRegions(); |
Erik Chen | fe6fbee | 2017-12-06 07:40:09 | [diff] [blame] | 85 | |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 86 | private: |
| 87 | struct Connection; |
Erik Chen | e2d06447 | 2017-10-07 03:34:07 | [diff] [blame] | 88 | struct DumpProcessesForTracingTracking; |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 89 | |
Alexei Filippov | e48985e | 2019-02-01 00:27:41 | [diff] [blame] | 90 | void HeapProfileRetrieved( |
| 91 | scoped_refptr<DumpProcessesForTracingTracking> tracking, |
| 92 | base::ProcessId pid, |
| 93 | mojom::ProcessType process_type, |
Alexei Filippov | e48985e | 2019-02-01 00:27:41 | [diff] [blame] | 94 | bool strip_path_from_mapped_files, |
| 95 | uint32_t sampling_rate, |
| 96 | mojom::HeapProfilePtr profile); |
| 97 | |
Erik Chen | e2d06447 | 2017-10-07 03:34:07 | [diff] [blame] | 98 | void DoDumpOneProcessForTracing( |
| 99 | scoped_refptr<DumpProcessesForTracingTracking> tracking, |
| 100 | base::ProcessId pid, |
Erik Chen | a610d55 | 2017-10-20 22:29:49 | [diff] [blame] | 101 | mojom::ProcessType process_type, |
erikchen | d1b8bc5 | 2017-12-21 18:12:42 | [diff] [blame] | 102 | bool strip_path_from_mapped_files, |
erikchen | 8bc20d8 | 2018-02-14 03:21:51 | [diff] [blame] | 103 | uint32_t sampling_rate, |
Erik Chen | e2d06447 | 2017-10-07 03:34:07 | [diff] [blame] | 104 | bool success, |
| 105 | AllocationCountMap counts, |
Erik Chen | 3303fd023 | 2018-01-11 20:29:05 | [diff] [blame] | 106 | AllocationTracker::ContextMap context, |
| 107 | AllocationTracker::AddressToStringMap mapped_strings); |
erikchen | 708c31d | 2017-09-19 00:28:53 | [diff] [blame] | 108 | |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 109 | // Notification that a connection is complete. Unlike OnNewConnection which |
| 110 | // is signaled by the pipe server, this is signaled by the allocation tracker |
| 111 | // to ensure that the pipeline for this process has been flushed of all |
| 112 | // messages. |
Albert J. Wong | 59d85acb | 2017-08-10 00:50:57 | [diff] [blame] | 113 | void OnConnectionComplete(base::ProcessId pid); |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 114 | |
erikchen | e382a10 | 2017-10-20 00:30:13 | [diff] [blame] | 115 | // Reports the ProcessTypes of the processes being profiled. |
| 116 | void ReportMetrics(); |
| 117 | |
Erik Chen | e2d06447 | 2017-10-07 03:34:07 | [diff] [blame] | 118 | // These thunks post the request back to the given thread. |
| 119 | static void OnConnectionCompleteThunk( |
erikchen | a4d1919 | 2017-08-23 17:33:16 | [diff] [blame] | 120 | scoped_refptr<base::SequencedTaskRunner> main_loop, |
erikchen | fa983faa | 2018-04-05 18:56:42 | [diff] [blame] | 121 | base::WeakPtr<ConnectionManager> connection_manager, |
Albert J. Wong | 59d85acb | 2017-08-10 00:50:57 | [diff] [blame] | 122 | base::ProcessId process_id); |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 123 | |
erikchen | 68532cc | 2017-08-16 19:38:36 | [diff] [blame] | 124 | BacktraceStorage backtrace_storage_; |
Albert J. Wong | 3432f46 | 2017-08-02 02:47:24 | [diff] [blame] | 125 | |
Erik Chen | e2d06447 | 2017-10-07 03:34:07 | [diff] [blame] | 126 | // Next ID to use for a barrier request. This is incremented for each use |
| 127 | // to ensure barrier IDs are unique. |
| 128 | uint32_t next_barrier_id_ = 1; |
| 129 | |
erikchen | 535ef8e | 2018-02-01 18:04:54 | [diff] [blame] | 130 | // The next ID to use when exporting a heap dump. |
| 131 | size_t next_id_ = 1; |
| 132 | |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 133 | // Maps process ID to the connection information for it. |
Albert J. Wong | 59d85acb | 2017-08-10 00:50:57 | [diff] [blame] | 134 | base::flat_map<base::ProcessId, std::unique_ptr<Connection>> connections_; |
erikchen | 66af016 | 2017-08-02 19:53:19 | [diff] [blame] | 135 | base::Lock connections_lock_; |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 136 | |
erikchen | e382a10 | 2017-10-20 00:30:13 | [diff] [blame] | 137 | // Every 24-hours, reports the types of profiled processes. |
| 138 | base::RepeatingTimer metrics_timer_; |
| 139 | |
erikchen | a6f3feb | 2018-02-14 16:21:56 | [diff] [blame] | 140 | // To avoid deadlock, synchronous calls to the browser are made on a dedicated |
| 141 | // thread that does nothing else. Both the IO thread and connection-specific |
| 142 | // threads could potentially be processing messages from the browser process, |
erikchen | fa983faa | 2018-04-05 18:56:42 | [diff] [blame] | 143 | // which in turn could be blocked on sending more messages over the pipe. |
erikchen | a6f3feb | 2018-02-14 16:21:56 | [diff] [blame] | 144 | base::Thread blocking_thread_; |
| 145 | |
erikchen | a4d1919 | 2017-08-23 17:33:16 | [diff] [blame] | 146 | // Must be last. |
erikchen | fa983faa | 2018-04-05 18:56:42 | [diff] [blame] | 147 | base::WeakPtrFactory<ConnectionManager> weak_factory_; |
erikchen | a4d1919 | 2017-08-23 17:33:16 | [diff] [blame] | 148 | |
erikchen | fa983faa | 2018-04-05 18:56:42 | [diff] [blame] | 149 | DISALLOW_COPY_AND_ASSIGN(ConnectionManager); |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 150 | }; |
| 151 | |
erikchen | 102fe21 | 2018-04-06 13:02:10 | [diff] [blame] | 152 | } // namespace heap_profiling |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 153 | |
erikchen | fa983faa | 2018-04-05 18:56:42 | [diff] [blame] | 154 | #endif // COMPONENTS_SERVICES_HEAP_PROFILING_CONNECTION_MANAGER_H_ |