blob: 81e95ec644295932d13cfe3104d2ff0a19537ce2 [file] [log] [blame]
brettwbd8214bf2017-06-20 03:47:031// 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
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"
Erik Chene2d064472017-10-07 03:34:0714#include "base/memory/ref_counted.h"
erikchena4d19192017-08-23 17:33:1615#include "base/memory/weak_ptr.h"
erikchen66af0162017-08-02 19:53:1916#include "base/synchronization/lock.h"
erikchena6f3feb2018-02-14 16:21:5617#include "base/threading/thread.h"
Sebastien Marchandefda77e532019-01-25 22:53:5218#include "base/timer/timer.h"
brettwbd8214bf2017-06-20 03:47:0319#include "build/build_config.h"
Alexei Filippov0a194542019-04-22 22:45:4620#include "components/services/heap_profiling/allocation.h"
erikchen0d4a6012018-04-03 16:06:3321#include "components/services/heap_profiling/public/mojom/heap_profiling_service.mojom.h"
Ken Rockotced31272019-08-02 21:12:1822#include "mojo/public/cpp/bindings/pending_remote.h"
Ken Rockot95c888a42018-02-11 05:54:1123#include "services/resource_coordinator/public/mojom/memory_instrumentation/memory_instrumentation.mojom.h"
brettwbd8214bf2017-06-20 03:47:0324
erikchen102fe212018-04-06 13:02:1025namespace heap_profiling {
brettwbd8214bf2017-06-20 03:47:0326
Alexei Filippovda9fb732019-04-25 22:40:3227struct ExportParams;
28
erikchen53cddfe62018-02-14 23:31:2929using VmRegions =
Ken Rockot089427352018-04-24 14:51:3730 base::flat_map<base::ProcessId,
31 std::vector<memory_instrumentation::mojom::VmRegionPtr>>;
erikchen53cddfe62018-02-14 23:31:2932
brettwbd8214bf2017-06-20 03:47:0333// Manages all connections and logging for each process. Pipes are supplied by
34// the pipe server and this class will connect them to a parser and logger.
Albert J. Wong3432f462017-08-02 02:47:2435//
erikchenfa983faa2018-04-05 18:56:4236// Note |backtrace_storage| must outlive ConnectionManager.
Albert J. Wong3432f462017-08-02 02:47:2437//
38// This object is constructed on the UI thread, but the rest of the usage
39// (including deletion) is on the IO thread.
erikchenfa983faa2018-04-05 18:56:4240class ConnectionManager {
Alexei Filippov1b7b8802019-04-17 22:01:4341 using AddressToStringMap = std::unordered_map<uint64_t, std::string>;
42 using CompleteCallback = base::OnceClosure;
43 using ContextMap = std::map<std::string, int>;
erikchen67fff822018-02-21 18:53:4344 using DumpProcessesForTracingCallback = memory_instrumentation::mojom::
45 HeapProfiler::DumpProcessesForTracingCallback;
46
brettwbd8214bf2017-06-20 03:47:0347 public:
erikchenfa983faa2018-04-05 18:56:4248 ConnectionManager();
Peter Boström09c01822021-09-20 22:43:2749
50 ConnectionManager(const ConnectionManager&) = delete;
51 ConnectionManager& operator=(const ConnectionManager&) = delete;
52
erikchenfa983faa2018-04-05 18:56:4253 ~ConnectionManager();
brettwbd8214bf2017-06-20 03:47:0354
Erik Chene2d064472017-10-07 03:34:0755 // Dumping is asynchronous so will not be complete when this function
56 // returns. The dump is complete when the callback provided in the args is
57 // fired.
Alexei Filippove0245182019-03-08 20:17:0558 void DumpProcessesForTracing(bool strip_path_from_mapped_files,
ssid035cbfb2021-07-24 20:35:5759 bool write_proto,
erikchen67fff822018-02-21 18:53:4360 DumpProcessesForTracingCallback callback,
61 VmRegions vm_regions);
erikchen66af0162017-08-02 19:53:1962
Erik Chene2d064472017-10-07 03:34:0763 void OnNewConnection(base::ProcessId pid,
Ken Rockotced31272019-08-02 21:12:1864 mojo::PendingRemote<mojom::ProfilingClient> client,
Erik Chen3303fd0232018-01-11 20:29:0565 mojom::ProcessType process_type,
Erik Chen8a2fb2e72018-02-09 20:34:2966 mojom::ProfilingParamsPtr params);
brettwbd8214bf2017-06-20 03:47:0367
Erik Chen7e4cccd62019-12-05 23:58:5868 // Returns pids of clients that have started profiling.
Erik Chenfe6fbee2017-12-06 07:40:0969 std::vector<base::ProcessId> GetConnectionPids();
Erik Chen7e4cccd62019-12-05 23:58:5870
71 // Returns pids of all connected clients that need vm regions, regardless of
72 // whether they've started profiling.
erikchen53cddfe62018-02-14 23:31:2973 std::vector<base::ProcessId> GetConnectionPidsThatNeedVmRegions();
Erik Chenfe6fbee2017-12-06 07:40:0974
brettwbd8214bf2017-06-20 03:47:0375 private:
76 struct Connection;
Erik Chene2d064472017-10-07 03:34:0777 struct DumpProcessesForTracingTracking;
brettwbd8214bf2017-06-20 03:47:0378
Alexei Filippove48985e2019-02-01 00:27:4179 void HeapProfileRetrieved(
80 scoped_refptr<DumpProcessesForTracingTracking> tracking,
81 base::ProcessId pid,
82 mojom::ProcessType process_type,
Alexei Filippove48985e2019-02-01 00:27:4183 bool strip_path_from_mapped_files,
84 uint32_t sampling_rate,
85 mojom::HeapProfilePtr profile);
86
Alexei Filippovda9fb732019-04-25 22:40:3287 bool ConvertProfileToExportParams(mojom::HeapProfilePtr profile,
88 uint32_t sampling_rate,
89 ExportParams* out_params);
erikchen708c31d2017-09-19 00:28:5390
Erik Chen7e4cccd62019-12-05 23:58:5891 // Notification that the client has disconnected. Unlike OnNewConnection which
brettwbd8214bf2017-06-20 03:47:0392 // is signaled by the pipe server, this is signaled by the allocation tracker
93 // to ensure that the pipeline for this process has been flushed of all
94 // messages.
Albert J. Wong59d85acb2017-08-10 00:50:5795 void OnConnectionComplete(base::ProcessId pid);
brettwbd8214bf2017-06-20 03:47:0396
Erik Chen7e4cccd62019-12-05 23:58:5897 // Indicates that the client has enabled profiling. Necessary for tests to
98 // know when initialization is complete.
99 void OnProfilingStarted(base::ProcessId pid);
100
erikchene382a102017-10-20 00:30:13101 // Reports the ProcessTypes of the processes being profiled.
102 void ReportMetrics();
103
erikchen535ef8e2018-02-01 18:04:54104 // The next ID to use when exporting a heap dump.
105 size_t next_id_ = 1;
106
brettwbd8214bf2017-06-20 03:47:03107 // Maps process ID to the connection information for it.
Albert J. Wong59d85acb2017-08-10 00:50:57108 base::flat_map<base::ProcessId, std::unique_ptr<Connection>> connections_;
erikchen66af0162017-08-02 19:53:19109 base::Lock connections_lock_;
brettwbd8214bf2017-06-20 03:47:03110
erikchene382a102017-10-20 00:30:13111 // Every 24-hours, reports the types of profiled processes.
112 base::RepeatingTimer metrics_timer_;
113
Alexei Filippov631529ee2019-04-18 16:39:05114 // Must be the last.
Alexei Filippov1b7b8802019-04-17 22:01:43115 base::WeakPtrFactory<ConnectionManager> weak_factory_{this};
brettwbd8214bf2017-06-20 03:47:03116};
117
erikchen102fe212018-04-06 13:02:10118} // namespace heap_profiling
brettwbd8214bf2017-06-20 03:47:03119
erikchenfa983faa2018-04-05 18:56:42120#endif // COMPONENTS_SERVICES_HEAP_PROFILING_CONNECTION_MANAGER_H_