blob: 40d364e05badcc853867f4b1859ffbb09c2e18dc [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"
erikchen21a2cab2017-08-21 20:50:4417#include "base/values.h"
brettwbd8214bf2017-06-20 03:47:0318#include "build/build_config.h"
Brett Wilson40a6bb502017-10-10 20:36:1519#include "chrome/common/profiling/profiling_service.mojom.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"
Erik Chene2d064472017-10-07 03:34:0723#include "mojo/edk/embedder/scoped_platform_handle.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
26namespace base {
Erik Chene266c732017-08-12 02:27:1927
Albert J. Wong3432f462017-08-02 02:47:2428class SequencedTaskRunner;
Erik Chene266c732017-08-12 02:27:1929
30} // namespace base
brettwbd8214bf2017-06-20 03:47:0331
32namespace profiling {
33
34// Manages all connections and logging for each process. Pipes are supplied by
35// the pipe server and this class will connect them to a parser and logger.
Albert J. Wong3432f462017-08-02 02:47:2436//
37// Note |backtrace_storage| must outlive MemlogConnectionManager.
38//
39// This object is constructed on the UI thread, but the rest of the usage
40// (including deletion) is on the IO thread.
41class MemlogConnectionManager {
Erik Chene2d064472017-10-07 03:34:0742 private:
brettwbd8214bf2017-06-20 03:47:0343 public:
erikchena4d19192017-08-23 17:33:1644 MemlogConnectionManager();
Albert J. Wong3432f462017-08-02 02:47:2445 ~MemlogConnectionManager();
brettwbd8214bf2017-06-20 03:47:0346
Erik Chene2d064472017-10-07 03:34:0747 // Shared types for the dump-type-specific args structures.
48 struct DumpArgs {
49 DumpArgs();
50 DumpArgs(DumpArgs&&) noexcept;
51 ~DumpArgs();
Erik Chen22f66c6d2017-10-06 23:48:5052
53 private:
Erik Chene2d064472017-10-07 03:34:0754 friend MemlogConnectionManager;
55
56 // This lock keeps the backtrace atoms alive throughout the dumping
57 // process. It will be initialized by DumpProcess.
58 BacktraceStorage::Lock backtrace_storage_lock;
59
60 DISALLOW_COPY_AND_ASSIGN(DumpArgs);
erikchen1ca0e5f2017-10-06 22:06:1461 };
62
Erik Chene2d064472017-10-07 03:34:0763 // Dumping is asynchronous so will not be complete when this function
64 // returns. The dump is complete when the callback provided in the args is
65 // fired.
Erik Chenecc10212017-10-02 23:08:4566 void DumpProcessesForTracing(
Erik Chen8bb76b52017-12-06 19:06:2567 bool keep_small_allocations,
erikchend1b8bc52017-12-21 18:12:4268 bool strip_path_from_mapped_files,
Brett Wilson40a6bb502017-10-10 20:36:1569 mojom::ProfilingService::DumpProcessesForTracingCallback callback,
Erik Chenecc10212017-10-02 23:08:4570 memory_instrumentation::mojom::GlobalMemoryDumpPtr dump);
erikchen66af0162017-08-02 19:53:1971
Erik Chene2d064472017-10-07 03:34:0772 void OnNewConnection(base::ProcessId pid,
Brett Wilson40a6bb502017-10-10 20:36:1573 mojom::ProfilingClientPtr client,
erikchene382a102017-10-20 00:30:1374 mojo::ScopedHandle receiver_pipe_end,
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();
79
brettwbd8214bf2017-06-20 03:47:0380 private:
81 struct Connection;
Erik Chene2d064472017-10-07 03:34:0782 struct DumpProcessesForTracingTracking;
brettwbd8214bf2017-06-20 03:47:0383
Erik Chene2d064472017-10-07 03:34:0784 void DoDumpOneProcessForTracing(
85 scoped_refptr<DumpProcessesForTracingTracking> tracking,
86 base::ProcessId pid,
Erik Chena610d552017-10-20 22:29:4987 mojom::ProcessType process_type,
Erik Chen8bb76b52017-12-06 19:06:2588 bool keep_small_allocations,
erikchend1b8bc52017-12-21 18:12:4289 bool strip_path_from_mapped_files,
Erik Chene2d064472017-10-07 03:34:0790 bool success,
91 AllocationCountMap counts,
Erik Chen3303fd0232018-01-11 20:29:0592 AllocationTracker::ContextMap context,
93 AllocationTracker::AddressToStringMap mapped_strings);
erikchen708c31d2017-09-19 00:28:5394
brettwbd8214bf2017-06-20 03:47:0395 // Notification that a connection is complete. Unlike OnNewConnection which
96 // is signaled by the pipe server, this is signaled by the allocation tracker
97 // to ensure that the pipeline for this process has been flushed of all
98 // messages.
Albert J. Wong59d85acb2017-08-10 00:50:5799 void OnConnectionComplete(base::ProcessId pid);
brettwbd8214bf2017-06-20 03:47:03100
erikchene382a102017-10-20 00:30:13101 // Reports the ProcessTypes of the processes being profiled.
102 void ReportMetrics();
103
Erik Chene2d064472017-10-07 03:34:07104 // These thunks post the request back to the given thread.
105 static void OnConnectionCompleteThunk(
erikchena4d19192017-08-23 17:33:16106 scoped_refptr<base::SequencedTaskRunner> main_loop,
Erik Chene2d064472017-10-07 03:34:07107 base::WeakPtr<MemlogConnectionManager> connection_manager,
Albert J. Wong59d85acb2017-08-10 00:50:57108 base::ProcessId process_id);
brettwbd8214bf2017-06-20 03:47:03109
erikchen68532cc2017-08-16 19:38:36110 BacktraceStorage backtrace_storage_;
Albert J. Wong3432f462017-08-02 02:47:24111
Erik Chene2d064472017-10-07 03:34:07112 // Next ID to use for a barrier request. This is incremented for each use
113 // to ensure barrier IDs are unique.
114 uint32_t next_barrier_id_ = 1;
115
erikchen535ef8e2018-02-01 18:04:54116 // The next ID to use when exporting a heap dump.
117 size_t next_id_ = 1;
118
brettwbd8214bf2017-06-20 03:47:03119 // Maps process ID to the connection information for it.
Albert J. Wong59d85acb2017-08-10 00:50:57120 base::flat_map<base::ProcessId, std::unique_ptr<Connection>> connections_;
erikchen66af0162017-08-02 19:53:19121 base::Lock connections_lock_;
brettwbd8214bf2017-06-20 03:47:03122
erikchene382a102017-10-20 00:30:13123 // Every 24-hours, reports the types of profiled processes.
124 base::RepeatingTimer metrics_timer_;
125
erikchena4d19192017-08-23 17:33:16126 // Must be last.
127 base::WeakPtrFactory<MemlogConnectionManager> weak_factory_;
128
brettwbd8214bf2017-06-20 03:47:03129 DISALLOW_COPY_AND_ASSIGN(MemlogConnectionManager);
130};
131
132} // namespace profiling
133
134#endif // CHROME_PROFILING_MEMLOG_CONNECTION_MANAGER_H_