blob: c4baebe85efdb7bf0fabd50b4df0ef87f17517f1 [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"
erikchenfa983faa2018-04-05 18:56:4221#include "components/services/heap_profiling/allocation_event.h"
erikchenfa983faa2018-04-05 18:56:4222#include "components/services/heap_profiling/backtrace_storage.h"
erikchen0d4a6012018-04-03 16:06:3323#include "components/services/heap_profiling/public/mojom/heap_profiling_service.mojom.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
erikchen53cddfe62018-02-14 23:31:2928using VmRegions =
Ken Rockot089427352018-04-24 14:51:3729 base::flat_map<base::ProcessId,
30 std::vector<memory_instrumentation::mojom::VmRegionPtr>>;
erikchen53cddfe62018-02-14 23:31:2931
brettwbd8214bf2017-06-20 03:47:0332// Manages all connections and logging for each process. Pipes are supplied by
33// the pipe server and this class will connect them to a parser and logger.
Albert J. Wong3432f462017-08-02 02:47:2434//
erikchenfa983faa2018-04-05 18:56:4235// Note |backtrace_storage| must outlive ConnectionManager.
Albert J. Wong3432f462017-08-02 02:47:2436//
37// This object is constructed on the UI thread, but the rest of the usage
38// (including deletion) is on the IO thread.
erikchenfa983faa2018-04-05 18:56:4239class ConnectionManager {
Alexei Filippov1b7b8802019-04-17 22:01:4340 using AddressToStringMap = std::unordered_map<uint64_t, std::string>;
41 using CompleteCallback = base::OnceClosure;
42 using ContextMap = std::map<std::string, int>;
erikchen67fff822018-02-21 18:53:4343 using DumpProcessesForTracingCallback = memory_instrumentation::mojom::
44 HeapProfiler::DumpProcessesForTracingCallback;
45
brettwbd8214bf2017-06-20 03:47:0346 public:
erikchenfa983faa2018-04-05 18:56:4247 ConnectionManager();
48 ~ConnectionManager();
brettwbd8214bf2017-06-20 03:47:0349
Erik Chene2d064472017-10-07 03:34:0750 // Shared types for the dump-type-specific args structures.
51 struct DumpArgs {
52 DumpArgs();
53 DumpArgs(DumpArgs&&) noexcept;
54 ~DumpArgs();
Erik Chen22f66c6d2017-10-06 23:48:5055
56 private:
erikchenfa983faa2018-04-05 18:56:4257 friend ConnectionManager;
Erik Chene2d064472017-10-07 03:34:0758
59 // This lock keeps the backtrace atoms alive throughout the dumping
60 // process. It will be initialized by DumpProcess.
61 BacktraceStorage::Lock backtrace_storage_lock;
62
63 DISALLOW_COPY_AND_ASSIGN(DumpArgs);
erikchen1ca0e5f2017-10-06 22:06:1464 };
65
Erik Chene2d064472017-10-07 03:34:0766 // Dumping is asynchronous so will not be complete when this function
67 // returns. The dump is complete when the callback provided in the args is
68 // fired.
Alexei Filippove0245182019-03-08 20:17:0569 void DumpProcessesForTracing(bool strip_path_from_mapped_files,
erikchen67fff822018-02-21 18:53:4370 DumpProcessesForTracingCallback callback,
71 VmRegions vm_regions);
erikchen66af0162017-08-02 19:53:1972
Erik Chene2d064472017-10-07 03:34:0773 void OnNewConnection(base::ProcessId pid,
Brett Wilson40a6bb502017-10-10 20:36:1574 mojom::ProfilingClientPtr client,
Erik Chen3303fd0232018-01-11 20:29:0575 mojom::ProcessType process_type,
Erik Chen8a2fb2e72018-02-09 20:34:2976 mojom::ProfilingParamsPtr params);
brettwbd8214bf2017-06-20 03:47:0377
Erik Chenfe6fbee2017-12-06 07:40:0978 std::vector<base::ProcessId> GetConnectionPids();
erikchen53cddfe62018-02-14 23:31:2979 std::vector<base::ProcessId> GetConnectionPidsThatNeedVmRegions();
Erik Chenfe6fbee2017-12-06 07:40:0980
brettwbd8214bf2017-06-20 03:47:0381 private:
82 struct Connection;
Erik Chene2d064472017-10-07 03:34:0783 struct DumpProcessesForTracingTracking;
brettwbd8214bf2017-06-20 03:47:0384
Alexei Filippove48985e2019-02-01 00:27:4185 void HeapProfileRetrieved(
86 scoped_refptr<DumpProcessesForTracingTracking> tracking,
87 base::ProcessId pid,
88 mojom::ProcessType process_type,
Alexei Filippove48985e2019-02-01 00:27:4189 bool strip_path_from_mapped_files,
90 uint32_t sampling_rate,
91 mojom::HeapProfilePtr profile);
92
Erik Chene2d064472017-10-07 03:34:0793 void DoDumpOneProcessForTracing(
94 scoped_refptr<DumpProcessesForTracingTracking> tracking,
95 base::ProcessId pid,
Erik Chena610d552017-10-20 22:29:4996 mojom::ProcessType process_type,
erikchend1b8bc52017-12-21 18:12:4297 bool strip_path_from_mapped_files,
erikchen8bc20d82018-02-14 03:21:5198 uint32_t sampling_rate,
Erik Chene2d064472017-10-07 03:34:0799 bool success,
100 AllocationCountMap counts,
Alexei Filippov1b7b8802019-04-17 22:01:43101 ContextMap context,
102 AddressToStringMap mapped_strings);
erikchen708c31d2017-09-19 00:28:53103
brettwbd8214bf2017-06-20 03:47:03104 // Notification that a connection is complete. Unlike OnNewConnection which
105 // is signaled by the pipe server, this is signaled by the allocation tracker
106 // to ensure that the pipeline for this process has been flushed of all
107 // messages.
Albert J. Wong59d85acb2017-08-10 00:50:57108 void OnConnectionComplete(base::ProcessId pid);
brettwbd8214bf2017-06-20 03:47:03109
erikchene382a102017-10-20 00:30:13110 // Reports the ProcessTypes of the processes being profiled.
111 void ReportMetrics();
112
erikchen68532cc2017-08-16 19:38:36113 BacktraceStorage backtrace_storage_;
Albert J. Wong3432f462017-08-02 02:47:24114
erikchen535ef8e2018-02-01 18:04:54115 // The next ID to use when exporting a heap dump.
116 size_t next_id_ = 1;
117
brettwbd8214bf2017-06-20 03:47:03118 // Maps process ID to the connection information for it.
Albert J. Wong59d85acb2017-08-10 00:50:57119 base::flat_map<base::ProcessId, std::unique_ptr<Connection>> connections_;
erikchen66af0162017-08-02 19:53:19120 base::Lock connections_lock_;
brettwbd8214bf2017-06-20 03:47:03121
erikchene382a102017-10-20 00:30:13122 // Every 24-hours, reports the types of profiled processes.
123 base::RepeatingTimer metrics_timer_;
124
erikchena6f3feb2018-02-14 16:21:56125 // To avoid deadlock, synchronous calls to the browser are made on a dedicated
126 // thread that does nothing else. Both the IO thread and connection-specific
127 // threads could potentially be processing messages from the browser process,
erikchenfa983faa2018-04-05 18:56:42128 // which in turn could be blocked on sending more messages over the pipe.
erikchena6f3feb2018-02-14 16:21:56129 base::Thread blocking_thread_;
130
erikchena4d19192017-08-23 17:33:16131 // Must be last.
Alexei Filippov1b7b8802019-04-17 22:01:43132 base::WeakPtrFactory<ConnectionManager> weak_factory_{this};
erikchena4d19192017-08-23 17:33:16133
erikchenfa983faa2018-04-05 18:56:42134 DISALLOW_COPY_AND_ASSIGN(ConnectionManager);
brettwbd8214bf2017-06-20 03:47:03135};
136
erikchen102fe212018-04-06 13:02:10137} // namespace heap_profiling
brettwbd8214bf2017-06-20 03:47:03138
erikchenfa983faa2018-04-05 18:56:42139#endif // COMPONENTS_SERVICES_HEAP_PROFILING_CONNECTION_MANAGER_H_