blob: 6571f9f565ac9ffcd420125956f744757bf36745 [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.
erikchen67fff822018-02-21 18:53:4373 void DumpProcessesForTracing(bool keep_small_allocations,
74 bool strip_path_from_mapped_files,
75 DumpProcessesForTracingCallback callback,
76 VmRegions vm_regions);
erikchen66af0162017-08-02 19:53:1977
Erik Chene2d064472017-10-07 03:34:0778 void OnNewConnection(base::ProcessId pid,
Brett Wilson40a6bb502017-10-10 20:36:1579 mojom::ProfilingClientPtr client,
erikchene382a102017-10-20 00:30:1380 mojo::ScopedHandle receiver_pipe_end,
Erik Chen3303fd0232018-01-11 20:29:0581 mojom::ProcessType process_type,
Erik Chen8a2fb2e72018-02-09 20:34:2982 mojom::ProfilingParamsPtr params);
brettwbd8214bf2017-06-20 03:47:0383
Erik Chenfe6fbee2017-12-06 07:40:0984 std::vector<base::ProcessId> GetConnectionPids();
erikchen53cddfe62018-02-14 23:31:2985 std::vector<base::ProcessId> GetConnectionPidsThatNeedVmRegions();
Erik Chenfe6fbee2017-12-06 07:40:0986
brettwbd8214bf2017-06-20 03:47:0387 private:
88 struct Connection;
Erik Chene2d064472017-10-07 03:34:0789 struct DumpProcessesForTracingTracking;
brettwbd8214bf2017-06-20 03:47:0390
Alexei Filippove48985e2019-02-01 00:27:4191 void HeapProfileRetrieved(
92 scoped_refptr<DumpProcessesForTracingTracking> tracking,
93 base::ProcessId pid,
94 mojom::ProcessType process_type,
95 bool keep_small_allocations,
96 bool strip_path_from_mapped_files,
97 uint32_t sampling_rate,
98 mojom::HeapProfilePtr profile);
99
Erik Chene2d064472017-10-07 03:34:07100 void DoDumpOneProcessForTracing(
101 scoped_refptr<DumpProcessesForTracingTracking> tracking,
102 base::ProcessId pid,
Erik Chena610d552017-10-20 22:29:49103 mojom::ProcessType process_type,
Erik Chen8bb76b52017-12-06 19:06:25104 bool keep_small_allocations,
erikchend1b8bc52017-12-21 18:12:42105 bool strip_path_from_mapped_files,
erikchen8bc20d82018-02-14 03:21:51106 uint32_t sampling_rate,
Erik Chene2d064472017-10-07 03:34:07107 bool success,
108 AllocationCountMap counts,
Erik Chen3303fd0232018-01-11 20:29:05109 AllocationTracker::ContextMap context,
110 AllocationTracker::AddressToStringMap mapped_strings);
erikchen708c31d2017-09-19 00:28:53111
brettwbd8214bf2017-06-20 03:47:03112 // Notification that a connection is complete. Unlike OnNewConnection which
113 // is signaled by the pipe server, this is signaled by the allocation tracker
114 // to ensure that the pipeline for this process has been flushed of all
115 // messages.
Albert J. Wong59d85acb2017-08-10 00:50:57116 void OnConnectionComplete(base::ProcessId pid);
brettwbd8214bf2017-06-20 03:47:03117
erikchene382a102017-10-20 00:30:13118 // Reports the ProcessTypes of the processes being profiled.
119 void ReportMetrics();
120
Erik Chene2d064472017-10-07 03:34:07121 // These thunks post the request back to the given thread.
122 static void OnConnectionCompleteThunk(
erikchena4d19192017-08-23 17:33:16123 scoped_refptr<base::SequencedTaskRunner> main_loop,
erikchenfa983faa2018-04-05 18:56:42124 base::WeakPtr<ConnectionManager> connection_manager,
Albert J. Wong59d85acb2017-08-10 00:50:57125 base::ProcessId process_id);
brettwbd8214bf2017-06-20 03:47:03126
erikchen68532cc2017-08-16 19:38:36127 BacktraceStorage backtrace_storage_;
Albert J. Wong3432f462017-08-02 02:47:24128
Erik Chene2d064472017-10-07 03:34:07129 // Next ID to use for a barrier request. This is incremented for each use
130 // to ensure barrier IDs are unique.
131 uint32_t next_barrier_id_ = 1;
132
erikchen535ef8e2018-02-01 18:04:54133 // The next ID to use when exporting a heap dump.
134 size_t next_id_ = 1;
135
brettwbd8214bf2017-06-20 03:47:03136 // Maps process ID to the connection information for it.
Albert J. Wong59d85acb2017-08-10 00:50:57137 base::flat_map<base::ProcessId, std::unique_ptr<Connection>> connections_;
erikchen66af0162017-08-02 19:53:19138 base::Lock connections_lock_;
brettwbd8214bf2017-06-20 03:47:03139
erikchene382a102017-10-20 00:30:13140 // Every 24-hours, reports the types of profiled processes.
141 base::RepeatingTimer metrics_timer_;
142
erikchena6f3feb2018-02-14 16:21:56143 // To avoid deadlock, synchronous calls to the browser are made on a dedicated
144 // thread that does nothing else. Both the IO thread and connection-specific
145 // threads could potentially be processing messages from the browser process,
erikchenfa983faa2018-04-05 18:56:42146 // which in turn could be blocked on sending more messages over the pipe.
erikchena6f3feb2018-02-14 16:21:56147 base::Thread blocking_thread_;
148
erikchena4d19192017-08-23 17:33:16149 // Must be last.
erikchenfa983faa2018-04-05 18:56:42150 base::WeakPtrFactory<ConnectionManager> weak_factory_;
erikchena4d19192017-08-23 17:33:16151
erikchenfa983faa2018-04-05 18:56:42152 DISALLOW_COPY_AND_ASSIGN(ConnectionManager);
brettwbd8214bf2017-06-20 03:47:03153};
154
erikchen102fe212018-04-06 13:02:10155} // namespace heap_profiling
brettwbd8214bf2017-06-20 03:47:03156
erikchenfa983faa2018-04-05 18:56:42157#endif // COMPONENTS_SERVICES_HEAP_PROFILING_CONNECTION_MANAGER_H_