blob: 09c241fd63f68637f0ea20ce20a63de02994258f [file] [log] [blame]
Avi Drissman8ba1bad2022-09-13 19:22:361// Copyright 2017 The Chromium Authors
brettwbd8214bf2017-06-20 03:47:032// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
erikchenfa983faa2018-04-05 18:56:425#ifndef COMPONENTS_SERVICES_HEAP_PROFILING_CONNECTION_MANAGER_H_
6#define COMPONENTS_SERVICES_HEAP_PROFILING_CONNECTION_MANAGER_H_
brettwbd8214bf2017-06-20 03:47:037
Alexei Filippov1b7b8802019-04-17 22:01:438#include <map>
brettwbd8214bf2017-06-20 03:47:039#include <string>
Alexei Filippov1b7b8802019-04-17 22:01:4310#include <unordered_map>
Erik Chene266c732017-08-12 02:27:1911#include <vector>
brettwbd8214bf2017-06-20 03:47:0312
13#include "base/containers/flat_map.h"
Joe Masoncf9e0bc2023-06-06 18:30:4714#include "base/functional/callback_forward.h"
Erik Chene2d064472017-10-07 03:34:0715#include "base/memory/ref_counted.h"
erikchena4d19192017-08-23 17:33:1616#include "base/memory/weak_ptr.h"
erikchen66af0162017-08-02 19:53:1917#include "base/synchronization/lock.h"
erikchena6f3feb2018-02-14 16:21:5618#include "base/threading/thread.h"
Sebastien Marchandefda77e532019-01-25 22:53:5219#include "base/timer/timer.h"
brettwbd8214bf2017-06-20 03:47:0320#include "build/build_config.h"
Alexei Filippov0a194542019-04-22 22:45:4621#include "components/services/heap_profiling/allocation.h"
erikchen0d4a6012018-04-03 16:06:3322#include "components/services/heap_profiling/public/mojom/heap_profiling_service.mojom.h"
Ken Rockotced31272019-08-02 21:12:1823#include "mojo/public/cpp/bindings/pending_remote.h"
Ken Rockot95c888a42018-02-11 05:54:1124#include "services/resource_coordinator/public/mojom/memory_instrumentation/memory_instrumentation.mojom.h"
brettwbd8214bf2017-06-20 03:47:0325
erikchen102fe212018-04-06 13:02:1026namespace heap_profiling {
brettwbd8214bf2017-06-20 03:47:0327
Alexei Filippovda9fb732019-04-25 22:40:3228struct ExportParams;
29
erikchen53cddfe62018-02-14 23:31:2930using VmRegions =
Ken Rockot089427352018-04-24 14:51:3731 base::flat_map<base::ProcessId,
32 std::vector<memory_instrumentation::mojom::VmRegionPtr>>;
erikchen53cddfe62018-02-14 23:31:2933
brettwbd8214bf2017-06-20 03:47:0334// 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. Wong3432f462017-08-02 02:47:2436//
erikchenfa983faa2018-04-05 18:56:4237// Note |backtrace_storage| must outlive ConnectionManager.
Albert J. Wong3432f462017-08-02 02:47:2438//
39// This object is constructed on the UI thread, but the rest of the usage
40// (including deletion) is on the IO thread.
erikchenfa983faa2018-04-05 18:56:4241class ConnectionManager {
Alexei Filippov1b7b8802019-04-17 22:01:4342 using AddressToStringMap = std::unordered_map<uint64_t, std::string>;
43 using CompleteCallback = base::OnceClosure;
44 using ContextMap = std::map<std::string, int>;
erikchen67fff822018-02-21 18:53:4345 using DumpProcessesForTracingCallback = memory_instrumentation::mojom::
46 HeapProfiler::DumpProcessesForTracingCallback;
47
brettwbd8214bf2017-06-20 03:47:0348 public:
erikchenfa983faa2018-04-05 18:56:4249 ConnectionManager();
Peter Boström09c01822021-09-20 22:43:2750
51 ConnectionManager(const ConnectionManager&) = delete;
52 ConnectionManager& operator=(const ConnectionManager&) = delete;
53
erikchenfa983faa2018-04-05 18:56:4254 ~ConnectionManager();
brettwbd8214bf2017-06-20 03:47:0355
Erik Chene2d064472017-10-07 03:34:0756 // 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 Filippove0245182019-03-08 20:17:0559 void DumpProcessesForTracing(bool strip_path_from_mapped_files,
ssid035cbfb2021-07-24 20:35:5760 bool write_proto,
erikchen67fff822018-02-21 18:53:4361 DumpProcessesForTracingCallback callback,
62 VmRegions vm_regions);
erikchen66af0162017-08-02 19:53:1963
Erik Chene2d064472017-10-07 03:34:0764 void OnNewConnection(base::ProcessId pid,
Ken Rockotced31272019-08-02 21:12:1865 mojo::PendingRemote<mojom::ProfilingClient> client,
Erik Chen3303fd0232018-01-11 20:29:0566 mojom::ProcessType process_type,
Joe Masoncf9e0bc2023-06-06 18:30:4767 mojom::ProfilingParamsPtr params,
Takashi Sakamoto83d6a182024-04-26 06:45:0868 mojom::ProfilingService::AddProfilingClientCallback
69 started_profiling_closure);
brettwbd8214bf2017-06-20 03:47:0370
Erik Chen7e4cccd62019-12-05 23:58:5871 // Returns pids of clients that have started profiling.
Erik Chenfe6fbee2017-12-06 07:40:0972 std::vector<base::ProcessId> GetConnectionPids();
Erik Chen7e4cccd62019-12-05 23:58:5873
74 // Returns pids of all connected clients that need vm regions, regardless of
75 // whether they've started profiling.
erikchen53cddfe62018-02-14 23:31:2976 std::vector<base::ProcessId> GetConnectionPidsThatNeedVmRegions();
Erik Chenfe6fbee2017-12-06 07:40:0977
brettwbd8214bf2017-06-20 03:47:0378 private:
79 struct Connection;
Erik Chene2d064472017-10-07 03:34:0780 struct DumpProcessesForTracingTracking;
brettwbd8214bf2017-06-20 03:47:0381
Alexei Filippove48985e2019-02-01 00:27:4182 void HeapProfileRetrieved(
83 scoped_refptr<DumpProcessesForTracingTracking> tracking,
84 base::ProcessId pid,
85 mojom::ProcessType process_type,
Alexei Filippove48985e2019-02-01 00:27:4186 bool strip_path_from_mapped_files,
87 uint32_t sampling_rate,
88 mojom::HeapProfilePtr profile);
89
Alexei Filippovda9fb732019-04-25 22:40:3290 bool ConvertProfileToExportParams(mojom::HeapProfilePtr profile,
91 uint32_t sampling_rate,
92 ExportParams* out_params);
erikchen708c31d2017-09-19 00:28:5393
Erik Chen7e4cccd62019-12-05 23:58:5894 // Notification that the client has disconnected. Unlike OnNewConnection which
brettwbd8214bf2017-06-20 03:47:0395 // 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. Wong59d85acb2017-08-10 00:50:5798 void OnConnectionComplete(base::ProcessId pid);
brettwbd8214bf2017-06-20 03:47:0399
Erik Chen7e4cccd62019-12-05 23:58:58100 // Indicates that the client has enabled profiling. Necessary for tests to
101 // know when initialization is complete.
102 void OnProfilingStarted(base::ProcessId pid);
103
erikchene382a102017-10-20 00:30:13104 // Reports the ProcessTypes of the processes being profiled.
105 void ReportMetrics();
106
erikchen535ef8e2018-02-01 18:04:54107 // The next ID to use when exporting a heap dump.
108 size_t next_id_ = 1;
109
brettwbd8214bf2017-06-20 03:47:03110 // Maps process ID to the connection information for it.
Albert J. Wong59d85acb2017-08-10 00:50:57111 base::flat_map<base::ProcessId, std::unique_ptr<Connection>> connections_;
erikchen66af0162017-08-02 19:53:19112 base::Lock connections_lock_;
brettwbd8214bf2017-06-20 03:47:03113
erikchene382a102017-10-20 00:30:13114 // Every 24-hours, reports the types of profiled processes.
115 base::RepeatingTimer metrics_timer_;
116
Alexei Filippov631529ee2019-04-18 16:39:05117 // Must be the last.
Alexei Filippov1b7b8802019-04-17 22:01:43118 base::WeakPtrFactory<ConnectionManager> weak_factory_{this};
brettwbd8214bf2017-06-20 03:47:03119};
120
erikchen102fe212018-04-06 13:02:10121} // namespace heap_profiling
brettwbd8214bf2017-06-20 03:47:03122
erikchenfa983faa2018-04-05 18:56:42123#endif // COMPONENTS_SERVICES_HEAP_PROFILING_CONNECTION_MANAGER_H_