blob: c2c74dfd71bb4d6ac8c1a943de60507ce15f4a04 [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"
erikchen21a2cab2017-08-21 20:50:4418#include "base/values.h"
brettwbd8214bf2017-06-20 03:47:0319#include "build/build_config.h"
erikchenfa983faa2018-04-05 18:56:4220#include "components/services/heap_profiling/allocation_event.h"
21#include "components/services/heap_profiling/allocation_tracker.h"
22#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"
Erik Chene2d064472017-10-07 03:34:0724#include "mojo/edk/embedder/scoped_platform_handle.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:15