blob: 580ccc7504c538db7486502421c00f4d3f49119d [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
8#include <string>
Erik Chene266c732017-08-12 02:27:199#include <vector>
brettwbd8214bf2017-06-20 03:47:0310
11#include "base/containers/flat_map.h"
12#include "base/macros.h"
Erik Chene2d064472017-10-07 03:34:0713#include "base/memory/ref_counted.h"
erikchena4d19192017-08-23 17:33:1614#include "base/memory/weak_ptr.h"
Albert J. Wong59d85acb2017-08-10 00:50:5715#include "base/process/process_handle.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"
erikchen21a2cab2017-08-21 20:50:4419#include "base/values.h"
brettwbd8214bf2017-06-20 03:47:0320#include "build/build_config.h"
erikchenfa983faa2018-04-05 18:56:4221#include "components/services/heap_profiling/allocation_event.h"
22#include "components/services/heap_profiling/allocation_tracker.h"
23#include "components/services/heap_profiling/backtrace_storage.h"
erikchen0d4a6012018-04-03 16:06:3324#include "components/services/heap_profiling/public/mojom/heap_profiling_service.mojom.h"
Ken Rockot95c888a42018-02-11 05:54:1125#include "services/resource_coordinator/public/mojom/memory_instrumentation/memory_instrumentation.mojom.h"
brettwbd8214bf2017-06-20 03:47:0326
27namespace base {
Erik Chene266c732017-08-12 02:27:1928
Albert J. Wong3432f462017-08-02 02:47:2429class SequencedTaskRunner;
Erik Chene266c732017-08-12 02:27:1930
31} // namespace base
brettwbd8214bf2017-06-20 03:47:0332
erikchen102fe212018-04-06 13:02:1033namespace heap_profiling {
brettwbd8214bf2017-06-20 03:47:0334
erikchen53cddfe62018-02-14 23:31:2935using VmRegions =
Ken Rockot089427352018-04-24 14:51:3736 base::flat_map<base::ProcessId,
37 std::vector<memory_instrumentation::mojom::VmRegionPtr>>;
erikchen53cddfe62018-02-14 23:31:2938
brettwbd8214bf2017-06-20 03:47:0339// Manages all connections and logging for each process. Pipes are supplied by
40// the pipe server and this class will connect them to a parser and logger.
Albert J. Wong3432f462017-08-02 02:47:2441//
erikchenfa983faa2018-04-05 18:56:4242// Note |backtrace_storage| must outlive ConnectionManager.
Albert J. Wong3432f462017-08-02 02:47:2443//
44// This object is constructed on the UI thread, but the rest of the usage
45// (including deletion) is on the IO thread.
erikchenfa983faa2018-04-05 18:56:4246class ConnectionManager {
erikchen67fff822018-02-21 18:53:4347 using DumpProcessesForTracingCallback = memory_instrumentation::mojom::
48 HeapProfiler::DumpProcessesForTracingCallback;
49
brettwbd8214bf2017-06-20 03:47:0350 public:
erikchenfa983faa2018-04-05 18:56:4251 ConnectionManager();
52 ~ConnectionManager();
brettwbd8214bf2017-06-20 03:47:0353
Erik Chene2d064472017-10-07 03:34:0754 // Shared types for the dump-type-specific args structures.
55 struct DumpArgs {
56 DumpArgs();
57 DumpArgs(DumpArgs&&) noexcept;
58 ~DumpArgs();
Erik Chen22f66c6d2017-10-06 23:48:5059
60 private:
erikchenfa983faa2018-04-05 18:56:4261 friend ConnectionManager;
Erik Chene2d064472017-10-07 03:34:0762
63 // This lock keeps the backtrace atoms alive throughout the dumping
64 // process. It will be initialized by DumpProcess.
65 BacktraceStorage::Lock backtrace_storage_lock;
66
67 DISALLOW_COPY_AND_ASSIGN(DumpArgs);
erikchen1ca0e5f2017-10-06 22:06:1468 };
69
Erik Chene2d064472017-10-07 03:34:0770 // Dumping is asynchronous so will not be complete when this function
71 // returns. The dump is complete when the callback provided in the args is
72 // fired.
Alexei Filippove0245182019-03-08 20:17:0573 void DumpProcessesForTracing(bool strip_path_from_mapped_files,
erikchen67fff822018-02-21 18:53:4374 DumpProcessesForTracingCallback callback,
75 VmRegions vm_regions);
erikchen66af0162017-08-02 19:53:1976
Erik Chene2d064472017-10-07 03:34:0777 void OnNewConnection(base::ProcessId pid,
Brett Wilson40a6bb502017-10-10 20:36:1578 mojom::ProfilingClientPtr client,
erikchene382a102017-10-20 00:30:1379 mojo::ScopedHandle receiver_pipe_end,
Erik Chen3303fd0232018-01-11 20:29:0580 mojom::ProcessType process_type,
Erik Chen8a2fb2e72018-02-09 20:34:2981 mojom::ProfilingParamsPtr params);
brettwbd8214bf2017-06-20 03:47:0382
Erik Chenfe6fbee2017-12-06 07:40:0983 std::vector<base::ProcessId> GetConnectionPids();
erikchen53cddfe62018-02-14 23:31:2984 std::vector<base::ProcessId> GetConnectionPidsThatNeedVmRegions();
Erik Chenfe6fbee2017-12-06 07:40:0985
brettwbd8214bf2017-06-20 03:47:0386 private:
87 struct Connection;
Erik Chene2d064472017-10-07 03:34:0788 struct DumpProcessesForTracingTracking;
brettwbd8214bf2017-06-20 03:47:0389
Alexei Filippove48985e2019-02-01 00:27:4190 void HeapProfileRetrieved(
91 scoped_refptr<DumpProcessesForTracingTracking> tracking,
92 base::ProcessId pid,
93 mojom::ProcessType process_type,
Alexei Filippove48985e2019-02-01 00:27:4194 bool strip_path_from_mapped_files,
95 uint32_t sampling_rate,
96 mojom::HeapProfilePtr profile);
97
Erik Chene2d064472017-10-07 03:34:0798 void DoDumpOneProcessForTracing(
99 scoped_refptr<DumpProcessesForTracingTracking> tracking,
100 base::ProcessId pid,
Erik Chena610d552017-10-20 22:29:49101 mojom::ProcessType process_type,
erikchend1b8bc52017-12-21 18:12:42102 bool strip_path_from_mapped_files,
erikchen8bc20d82018-02-14 03:21:51103 uint32_t sampling_rate,
Erik Chene2d064472017-10-07 03:34:07104 bool success,
105 AllocationCountMap counts,
Erik Chen3303fd0232018-01-11 20:29:05106 AllocationTracker::ContextMap context,
107 AllocationTracker::AddressToStringMap mapped_strings);
erikchen708c31d2017-09-19 00:28:53108
brettwbd8214bf2017-06-20 03:47:03109 // Notification that a connection is complete. Unlike OnNewConnection which
110 // is signaled by the pipe server, this is signaled by the allocation tracker
111 // to ensure that the pipeline for this process has been flushed of all
112 // messages.
Albert J. Wong59d85acb2017-08-10 00:50:57113 void OnConnectionComplete(base::ProcessId pid);
brettwbd8214bf2017-06-20 03:47:03114
erikchene382a102017-10-20 00:30:13115 // Reports the ProcessTypes of the processes being profiled.
116 void ReportMetrics();
117
Erik Chene2d064472017-10-07 03:34:07118 // These thunks post the request back to the given thread.
119 static void OnConnectionCompleteThunk(
erikchena4d19192017-08-23 17:33:16120 scoped_refptr<base::SequencedTaskRunner> main_loop,
erikchenfa983faa2018-04-05 18:56:42121 base::WeakPtr<ConnectionManager> connection_manager,
Albert J. Wong59d85acb2017-08-10 00:50:57122 base::ProcessId process_id);
brettwbd8214bf2017-06-20 03:47:03123
erikchen68532cc2017-08-16 19:38:36124 BacktraceStorage backtrace_storage_;
Albert J. Wong3432f462017-08-02 02:47:24125
Erik Chene2d064472017-10-07 03:34:07126 // Next ID to use for a barrier request. This is incremented for each use
127 // to ensure barrier IDs are unique.
128 uint32_t next_barrier_id_ = 1;
129
erikchen535ef8e2018-02-01 18:04:54130 // The next ID to use when exporting a heap dump.
131 size_t next_id_ = 1;
132
brettwbd8214bf2017-06-20 03:47:03133 // Maps process ID to the connection information for it.
Albert J. Wong59d85acb2017-08-10 00:50:57134 base::flat_map<base::ProcessId, std::unique_ptr<Connection>> connections_;
erikchen66af0162017-08-02 19:53:19135 base::Lock connections_lock_;
brettwbd8214bf2017-06-20 03:47:03136
erikchene382a102017-10-20 00:30:13137 // Every 24-hours, reports the types of profiled processes.
138 base::RepeatingTimer metrics_timer_;
139
erikchena6f3feb2018-02-14 16:21:56140 // To avoid deadlock, synchronous calls to the browser are made on a dedicated
141 // thread that does nothing else. Both the IO thread and connection-specific
142 // threads could potentially be processing messages from the browser process,
erikchenfa983faa2018-04-05 18:56:42143 // which in turn could be blocked on sending more messages over the pipe.
erikchena6f3feb2018-02-14 16:21:56144 base::Thread blocking_thread_;
145
erikchena4d19192017-08-23 17:33:16146 // Must be last.
erikchenfa983faa2018-04-05 18:56:42147 base::WeakPtrFactory<ConnectionManager> weak_factory_;
erikchena4d19192017-08-23 17:33:16148
erikchenfa983faa2018-04-05 18:56:42149 DISALLOW_COPY_AND_ASSIGN(ConnectionManager);
brettwbd8214bf2017-06-20 03:47:03150};
151
erikchen102fe212018-04-06 13:02:10152} // namespace heap_profiling
brettwbd8214bf2017-06-20 03:47:03153
erikchenfa983faa2018-04-05 18:56:42154#endif // COMPONENTS_SERVICES_HEAP_PROFILING_CONNECTION_MANAGER_H_