Avi Drissman | 8ba1bad | 2022-09-13 19:22:36 | [diff] [blame] | 1 | // Copyright 2017 The Chromium Authors |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 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 | |
Alexei Filippov | 1b7b880 | 2019-04-17 22:01:43 | [diff] [blame] | 8 | #include <map> |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 9 | #include <string> |
Alexei Filippov | 1b7b880 | 2019-04-17 22:01:43 | [diff] [blame] | 10 | #include <unordered_map> |
Erik Chen | e266c73 | 2017-08-12 02:27:19 | [diff] [blame] | 11 | #include <vector> |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 12 | |
| 13 | #include "base/containers/flat_map.h" |
Joe Mason | cf9e0bc | 2023-06-06 18:30:47 | [diff] [blame] | 14 | #include "base/functional/callback_forward.h" |
Erik Chen | e2d06447 | 2017-10-07 03:34:07 | [diff] [blame] | 15 | #include "base/memory/ref_counted.h" |
erikchen | a4d1919 | 2017-08-23 17:33:16 | [diff] [blame] | 16 | #include "base/memory/weak_ptr.h" |
erikchen | 66af016 | 2017-08-02 19:53:19 | [diff] [blame] | 17 | #include "base/synchronization/lock.h" |
erikchen | a6f3feb | 2018-02-14 16:21:56 | [diff] [blame] | 18 | #include "base/threading/thread.h" |
Sebastien Marchand | efda77e53 | 2019-01-25 22:53:52 | [diff] [blame] | 19 | #include "base/timer/timer.h" |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 20 | #include "build/build_config.h" |
Alexei Filippov | 0a19454 | 2019-04-22 22:45:46 | [diff] [blame] | 21 | #include "components/services/heap_profiling/allocation.h" |
erikchen | 0d4a601 | 2018-04-03 16:06:33 | [diff] [blame] | 22 | #include "components/services/heap_profiling/public/mojom/heap_profiling_service.mojom.h" |
Ken Rockot | ced3127 | 2019-08-02 21:12:18 | [diff] [blame] | 23 | #include "mojo/public/cpp/bindings/pending_remote.h" |
Ken Rockot | 95c888a4 | 2018-02-11 05:54:11 | [diff] [blame] | 24 | #include "services/resource_coordinator/public/mojom/memory_instrumentation/memory_instrumentation.mojom.h" |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 25 | |
erikchen | 102fe21 | 2018-04-06 13:02:10 | [diff] [blame] | 26 | namespace heap_profiling { |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 27 | |
Alexei Filippov | da9fb73 | 2019-04-25 22:40:32 | [diff] [blame] | 28 | struct ExportParams; |
| 29 | |
erikchen | 53cddfe6 | 2018-02-14 23:31:29 | [diff] [blame] | 30 | using VmRegions = |
Ken Rockot | 08942735 | 2018-04-24 14:51:37 | [diff] [blame] | 31 | base::flat_map<base::ProcessId, |
| 32 | std::vector<memory_instrumentation::mojom::VmRegionPtr>>; |
erikchen | 53cddfe6 | 2018-02-14 23:31:29 | [diff] [blame] | 33 | |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 34 | // Manages all connections and logging for each process. Pipes are supplied by |
| 35 | // 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] | 36 | // |
erikchen | fa983faa | 2018-04-05 18:56:42 | [diff] [blame] | 37 | // Note |backtrace_storage| must outlive ConnectionManager. |
Albert J. Wong | 3432f46 | 2017-08-02 02:47:24 | [diff] [blame] | 38 | // |
| 39 | // This object is constructed on the UI thread, but the rest of the usage |
| 40 | // (including deletion) is on the IO thread. |
erikchen | fa983faa | 2018-04-05 18:56:42 | [diff] [blame] | 41 | class ConnectionManager { |
Alexei Filippov | 1b7b880 | 2019-04-17 22:01:43 | [diff] [blame] | 42 | using AddressToStringMap = std::unordered_map<uint64_t, std::string>; |
| 43 | using CompleteCallback = base::OnceClosure; |
| 44 | using ContextMap = std::map<std::string, int>; |
erikchen | 67fff82 | 2018-02-21 18:53:43 | [diff] [blame] | 45 | using DumpProcessesForTracingCallback = memory_instrumentation::mojom:: |
| 46 | HeapProfiler::DumpProcessesForTracingCallback; |
| 47 | |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 48 | public: |
erikchen | fa983faa | 2018-04-05 18:56:42 | [diff] [blame] | 49 | ConnectionManager(); |
Peter Boström | 09c0182 | 2021-09-20 22:43:27 | [diff] [blame] | 50 | |
| 51 | ConnectionManager(const ConnectionManager&) = delete; |
| 52 | ConnectionManager& operator=(const ConnectionManager&) = delete; |
| 53 | |
erikchen | fa983faa | 2018-04-05 18:56:42 | [diff] [blame] | 54 | ~ConnectionManager(); |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 55 | |
Erik Chen | e2d06447 | 2017-10-07 03:34:07 | [diff] [blame] | 56 | // Dumping is asynchronous so will not be complete when this function |
| 57 | // returns. The dump is complete when the callback provided in the args is |
| 58 | // fired. |
Alexei Filippov | e024518 | 2019-03-08 20:17:05 | [diff] [blame] | 59 | void DumpProcessesForTracing(bool strip_path_from_mapped_files, |
ssid | 035cbfb | 2021-07-24 20:35:57 | [diff] [blame] | 60 | bool write_proto, |
erikchen | 67fff82 | 2018-02-21 18:53:43 | [diff] [blame] | 61 | DumpProcessesForTracingCallback callback, |
| 62 | VmRegions vm_regions); |
erikchen | 66af016 | 2017-08-02 19:53:19 | [diff] [blame] | 63 | |
Erik Chen | e2d06447 | 2017-10-07 03:34:07 | [diff] [blame] | 64 | void OnNewConnection(base::ProcessId pid, |
Ken Rockot | ced3127 | 2019-08-02 21:12:18 | [diff] [blame] | 65 | mojo::PendingRemote<mojom::ProfilingClient> client, |
Erik Chen | 3303fd023 | 2018-01-11 20:29:05 | [diff] [blame] | 66 | mojom::ProcessType process_type, |
Joe Mason | cf9e0bc | 2023-06-06 18:30:47 | [diff] [blame] | 67 | mojom::ProfilingParamsPtr params, |
Takashi Sakamoto | 83d6a18 | 2024-04-26 06:45:08 | [diff] [blame] | 68 | mojom::ProfilingService::AddProfilingClientCallback |
| 69 | started_profiling_closure); |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 70 | |
Erik Chen | 7e4cccd6 | 2019-12-05 23:58:58 | [diff] [blame] | 71 | // Returns pids of clients that have started profiling. |
Erik Chen | fe6fbee | 2017-12-06 07:40:09 | [diff] [blame] | 72 | std::vector<base::ProcessId> GetConnectionPids(); |
Erik Chen | 7e4cccd6 | 2019-12-05 23:58:58 | [diff] [blame] | 73 | |
| 74 | // Returns pids of all connected clients that need vm regions, regardless of |
| 75 | // whether they've started profiling. |
erikchen | 53cddfe6 | 2018-02-14 23:31:29 | [diff] [blame] | 76 | std::vector<base::ProcessId> GetConnectionPidsThatNeedVmRegions(); |
Erik Chen | fe6fbee | 2017-12-06 07:40:09 | [diff] [blame] | 77 | |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 78 | private: |
| 79 | struct Connection; |
Erik Chen | e2d06447 | 2017-10-07 03:34:07 | [diff] [blame] | 80 | struct DumpProcessesForTracingTracking; |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 81 | |
Alexei Filippov | e48985e | 2019-02-01 00:27:41 | [diff] [blame] | 82 | void HeapProfileRetrieved( |
| 83 | scoped_refptr<DumpProcessesForTracingTracking> tracking, |
| 84 | base::ProcessId pid, |
| 85 | mojom::ProcessType process_type, |
Alexei Filippov | e48985e | 2019-02-01 00:27:41 | [diff] [blame] | 86 | bool strip_path_from_mapped_files, |
| 87 | uint32_t sampling_rate, |
| 88 | mojom::HeapProfilePtr profile); |
| 89 | |
Alexei Filippov | da9fb73 | 2019-04-25 22:40:32 | [diff] [blame] | 90 | bool ConvertProfileToExportParams(mojom::HeapProfilePtr profile, |
| 91 | uint32_t sampling_rate, |
| 92 | ExportParams* out_params); |
erikchen | 708c31d | 2017-09-19 00:28:53 | [diff] [blame] | 93 | |
Erik Chen | 7e4cccd6 | 2019-12-05 23:58:58 | [diff] [blame] | 94 | // Notification that the client has disconnected. Unlike OnNewConnection which |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 95 | // is signaled by the pipe server, this is signaled by the allocation tracker |
| 96 | // to ensure that the pipeline for this process has been flushed of all |
| 97 | // messages. |
Albert J. Wong | 59d85acb | 2017-08-10 00:50:57 | [diff] [blame] | 98 | void OnConnectionComplete(base::ProcessId pid); |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 99 | |
Erik Chen | 7e4cccd6 | 2019-12-05 23:58:58 | [diff] [blame] | 100 | // Indicates that the client has enabled profiling. Necessary for tests to |
| 101 | // know when initialization is complete. |
| 102 | void OnProfilingStarted(base::ProcessId pid); |
| 103 | |
erikchen | e382a10 | 2017-10-20 00:30:13 | [diff] [blame] | 104 | // Reports the ProcessTypes of the processes being profiled. |
| 105 | void ReportMetrics(); |
| 106 | |
erikchen | 535ef8e | 2018-02-01 18:04:54 | [diff] [blame] | 107 | // The next ID to use when exporting a heap dump. |
| 108 | size_t next_id_ = 1; |
| 109 | |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 110 | // Maps process ID to the connection information for it. |
Albert J. Wong | 59d85acb | 2017-08-10 00:50:57 | [diff] [blame] | 111 | base::flat_map<base::ProcessId, std::unique_ptr<Connection>> connections_; |
erikchen | 66af016 | 2017-08-02 19:53:19 | [diff] [blame] | 112 | base::Lock connections_lock_; |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 113 | |
erikchen | e382a10 | 2017-10-20 00:30:13 | [diff] [blame] | 114 | // Every 24-hours, reports the types of profiled processes. |
| 115 | base::RepeatingTimer metrics_timer_; |
| 116 | |
Alexei Filippov | 631529ee | 2019-04-18 16:39:05 | [diff] [blame] | 117 | // Must be the last. |
Alexei Filippov | 1b7b880 | 2019-04-17 22:01:43 | [diff] [blame] | 118 | base::WeakPtrFactory<ConnectionManager> weak_factory_{this}; |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 119 | }; |
| 120 | |
erikchen | 102fe21 | 2018-04-06 13:02:10 | [diff] [blame] | 121 | } // namespace heap_profiling |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 122 | |
erikchen | fa983faa | 2018-04-05 18:56:42 | [diff] [blame] | 123 | #endif // COMPONENTS_SERVICES_HEAP_PROFILING_CONNECTION_MANAGER_H_ |