blob: 40931b05aeb577653ab7aff4f9527ec94052bb49 [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
5#ifndef CHROME_PROFILING_MEMLOG_CONNECTION_MANAGER_H_
6#define CHROME_PROFILING_MEMLOG_CONNECTION_MANAGER_H_
7
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"
Erik Chene2d064472017-10-07 03:34:0720#include "chrome/profiling/allocation_event.h"
21#include "chrome/profiling/allocation_tracker.h"
brettwbd8214bf2017-06-20 03:47:0322#include "chrome/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
33namespace profiling {
34
erikchen53cddfe62018-02-14 23:31:2935using VmRegions =
36 std::unordered_map<base::ProcessId,
37 std::vector<memory_instrumentation::mojom::VmRegionPtr>>;
38
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//
42// Note |backtrace_storage| must outlive MemlogConnectionManager.
43//
44// This object is constructed on the UI thread, but the rest of the usage
45// (including deletion) is on the IO thread.
46class MemlogConnectionManager {
erikchen67fff822018-02-21 18:53:4347 using DumpProcessesForTracingCallback = memory_instrumentation::mojom::
48 HeapProfiler::DumpProcessesForTracingCallback;
49
brettwbd8214bf2017-06-20 03:47:0350 public:
erikchena4d19192017-08-23 17:33:1651 MemlogConnectionManager();
Albert J. Wong3432f462017-08-02 02:47:2452 ~MemlogConnectionManager();
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:
Erik Chene2d064472017-10-07 03:34:0761 friend MemlogConnectionManager;
62
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
Erik Chene2d064472017-10-07 03:34:0791 void DoDumpOneProcessForTracing(
92 scoped_refptr<DumpProcessesForTracingTracking> tracking,
93 base::ProcessId pid,
Erik Chena610d552017-10-20 22:29:4994 mojom::ProcessType process_type,
Erik Chen8bb76b52017-12-06 19:06:2595 bool keep_small_allocations,
erikchend1b8bc52017-12-21 18:12:4296 bool strip_path_from_mapped_files,
erikchen8bc20d82018-02-14 03:21:5197 uint32_t sampling_rate,
Erik Chene2d064472017-10-07 03:34:0798 bool success,
99 AllocationCountMap counts,
Erik Chen3303fd0232018-01-11 20:29:05100 AllocationTracker::ContextMap context,
101 AllocationTracker::AddressToStringMap mapped_strings);
erikchen708c31d2017-09-19 00:28:53102
brettwbd8214bf2017-06-20 03:47:03103 // Notification that a connection is complete. Unlike OnNewConnection which
104 // is signaled by the pipe server, this is signaled by the allocation tracker
105 // to ensure that the pipeline for this process has been flushed of all
106 // messages.
Albert J. Wong59d85acb2017-08-10 00:50:57107 void OnConnectionComplete(base::ProcessId pid);
brettwbd8214bf2017-06-20 03:47:03108
erikchene382a102017-10-20 00:30:13109 // Reports the ProcessTypes of the processes being profiled.
110 void ReportMetrics();
111
Erik Chene2d064472017-10-07 03:34:07112 // These thunks post the request back to the given thread.
113 static void OnConnectionCompleteThunk(
erikchena4d19192017-08-23 17:33:16114 scoped_refptr<base::SequencedTaskRunner> main_loop,
Erik Chene2d064472017-10-07 03:34:07115 base::WeakPtr<MemlogConnectionManager> connection_manager,
Albert J. Wong59d85acb2017-08-10 00:50:57116 base::ProcessId process_id);
brettwbd8214bf2017-06-20 03:47:03117
erikchen68532cc2017-08-16 19:38:36118 BacktraceStorage backtrace_storage_;
Albert J. Wong3432f462017-08-02 02:47:24119
Erik Chene2d064472017-10-07 03:34:07120 // Next ID to use for a barrier request. This is incremented for each use
121 // to ensure barrier IDs are unique.
122 uint32_t next_barrier_id_ = 1;
123
erikchen535ef8e2018-02-01 18:04:54124 // The next ID to use when exporting a heap dump.
125 size_t next_id_ = 1;
126
brettwbd8214bf2017-06-20 03:47:03127 // Maps process ID to the connection information for it.
Albert J. Wong59d85acb2017-08-10 00:50:57128 base::flat_map<base::ProcessId, std::unique_ptr<Connection>> connections_;
erikchen66af0162017-08-02 19:53:19129 base::Lock connections_lock_;
brettwbd8214bf2017-06-20 03:47:03130
erikchene382a102017-10-20 00:30:13131 // Every 24-hours, reports the types of profiled processes.
132 base::RepeatingTimer metrics_timer_;
133
erikchena6f3feb2018-02-14 16:21:56134 // To avoid deadlock, synchronous calls to the browser are made on a dedicated
135 // thread that does nothing else. Both the IO thread and connection-specific
136 // threads could potentially be processing messages from the browser process,
137 // which in turn could be blocked on sending more messages over the memlog
138 // pipe.
139 base::Thread blocking_thread_;
140
erikchena4d19192017-08-23 17:33:16141 // Must be last.
142 base::WeakPtrFactory<MemlogConnectionManager> weak_factory_;
143
brettwbd8214bf2017-06-20 03:47:03144 DISALLOW_COPY_AND_ASSIGN(MemlogConnectionManager);
145};
146
147} // namespace profiling
148
149#endif // CHROME_PROFILING_MEMLOG_CONNECTION_MANAGER_H_