blob: 6f4b002e477c1f9ff53fcc6e1cad00748b88e19b [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"
14#include "base/macros.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();
50 ~ConnectionManager();
brettwbd8214bf2017-06-20 03:47:0351
Erik Chene2d064472017-10-07 03:34:0752 // Dumping is asynchronous so will not be complete when this function
53 // returns. The dump is complete when the callback provided in the args is
54 // fired.
Alexei Filippove0245182019-03-08 20:17:0555 void DumpProcessesForTracing(bool strip_path_from_mapped_files,
ssid035cbfb2021-07-24 20:35:5756 bool write_proto,
erikchen67fff822018-02-21 18:53:4357 DumpProcessesForTracingCallback callback,
58 VmRegions vm_regions);
erikchen66af0162017-08-02 19:53:1959
Erik Chene2d064472017-10-07 03:34:0760 void OnNewConnection(base::ProcessId pid,
Ken Rockotced31272019-08-02 21:12:1861 mojo::PendingRemote<mojom::ProfilingClient> client,
Erik Chen3303fd0232018-01-11 20:29:0562 mojom::ProcessType process_type,
Erik Chen8a2fb2e72018-02-09 20:34:2963 mojom::ProfilingParamsPtr params);
brettwbd8214bf2017-06-20 03:47:0364
Erik Chen7e4cccd62019-12-05 23:58:5865 // Returns pids of clients that have started profiling.
Erik Chenfe6fbee2017-12-06 07:40:0966 std::vector<base::ProcessId> GetConnectionPids();
Erik Chen7e4cccd62019-12-05 23:58:5867
68 // Returns pids of all connected clients that need vm regions, regardless of
69 // whether they've started profiling.
erikchen53cddfe62018-02-14 23:31:2970 std::vector<base::ProcessId> GetConnectionPidsThatNeedVmRegions();
Erik Chenfe6fbee2017-12-06 07:40:0971
brettwbd8214bf2017-06-20 03:47:0372 private:
73 struct Connection;
Erik Chene2d064472017-10-07 03:34:0774 struct DumpProcessesForTracingTracking;
brettwbd8214bf2017-06-20 03:47:0375
Alexei Filippove48985e2019-02-01 00:27:4176 void HeapProfileRetrieved(
77 scoped_refptr<DumpProcessesForTracingTracking> tracking,
78 base::ProcessId pid,
79 mojom::ProcessType process_type,
Alexei Filippove48985e2019-02-01 00:27:4180 bool strip_path_from_mapped_files,
81 uint32_t sampling_rate,
82 mojom::HeapProfilePtr profile);
83
Alexei Filippovda9fb732019-04-25 22:40:3284 bool ConvertProfileToExportParams(mojom::HeapProfilePtr profile,
85 uint32_t sampling_rate,
86 ExportParams* out_params);
erikchen708c31d2017-09-19 00:28:5387
Erik Chen7e4cccd62019-12-05 23:58:5888 // Notification that the client has disconnected. Unlike OnNewConnection which
brettwbd8214bf2017-06-20 03:47:0389 // is signaled by the pipe server, this is signaled by the allocation tracker
90 // to ensure that the pipeline for this process has been flushed of all
91 // messages.
Albert J. Wong59d85acb2017-08-10 00:50:5792 void OnConnectionComplete(base::ProcessId pid);
brettwbd8214bf2017-06-20 03:47:0393
Erik Chen7e4cccd62019-12-05 23:58:5894 // Indicates that the client has enabled profiling. Necessary for tests to
95 // know when initialization is complete.
96 void OnProfilingStarted(base::ProcessId pid);
97
erikchene382a102017-10-20 00:30:1398 // Reports the ProcessTypes of the processes being profiled.
99 void ReportMetrics();
100
erikchen535ef8e2018-02-01 18:04:54101 // The next ID to use when exporting a heap dump.
102 size_t next_id_ = 1;
103
brettwbd8214bf2017-06-20 03:47:03104 // Maps process ID to the connection information for it.
Albert J. Wong59d85acb2017-08-10 00:50:57105 base::flat_map<base::ProcessId, std::unique_ptr<Connection>> connections_;
erikchen66af0162017-08-02 19:53:19106 base::Lock connections_lock_;
brettwbd8214bf2017-06-20 03:47:03107
erikchene382a102017-10-20 00:30:13108 // Every 24-hours, reports the types of profiled processes.
109 base::RepeatingTimer metrics_timer_;
110
Alexei Filippov631529ee2019-04-18 16:39:05111 // Must be the last.
Alexei Filippov1b7b8802019-04-17 22:01:43112 base::WeakPtrFactory<ConnectionManager> weak_factory_{this};
erikchena4d19192017-08-23 17:33:16113
erikchenfa983faa2018-04-05 18:56:42114 DISALLOW_COPY_AND_ASSIGN(ConnectionManager);
brettwbd8214bf2017-06-20 03:47:03115};
116
erikchen102fe212018-04-06 13:02:10117} // namespace heap_profiling
brettwbd8214bf2017-06-20 03:47:03118
erikchenfa983faa2018-04-05 18:56:42119#endif // COMPONENTS_SERVICES_HEAP_PROFILING_CONNECTION_MANAGER_H_