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