blob: 02d5f07a8162ae5d5ba082527ce6eb74662fc9c2 [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"
Erik Chene266c732017-08-12 02:27:1924#include "services/resource_coordinator/public/interfaces/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 // Parameters to DumpProcess().
64 struct DumpProcessArgs : public DumpArgs {
65 DumpProcessArgs();
66 DumpProcessArgs(DumpProcessArgs&&) noexcept;
67 ~DumpProcessArgs();
Erik Chen22f66c6d2017-10-06 23:48:5068
Erik Chene2d064472017-10-07 03:34:0769 // Process ID to dump.
70 base::ProcessId pid;
71
72 // The memory map for the given process for the dumped process must be
73 // provided here since that is not tracked as part of the normal allocation
74 // process.
75 std::vector<memory_instrumentation::mojom::VmRegionPtr> maps;
76
77 std::unique_ptr<base::DictionaryValue> metadata;
78
79 // File to dump the output to.
80 base::File file;
Brett Wilson40a6bb502017-10-10 20:36:1581 mojom::ProfilingService::DumpProcessCallback callback;
Erik Chene2d064472017-10-07 03:34:0782 };
83
84 // Dumping is asynchronous so will not be complete when this function
85 // returns. The dump is complete when the callback provided in the args is
86 // fired.
87 void DumpProcess(DumpProcessArgs args);
Erik Chenecc10212017-10-02 23:08:4588 void DumpProcessesForTracing(
Brett Wilson40a6bb502017-10-10 20:36:1589 mojom::ProfilingService::DumpProcessesForTracingCallback callback,
Erik Chenecc10212017-10-02 23:08:4590 memory_instrumentation::mojom::GlobalMemoryDumpPtr dump);
erikchen66af0162017-08-02 19:53:1991
Erik Chene2d064472017-10-07 03:34:0792 void OnNewConnection(base::ProcessId pid,
Brett Wilson40a6bb502017-10-10 20:36:1593 mojom::ProfilingClientPtr client,
94 mojo::ScopedHandle sender_pipe_end,
95 mojo::ScopedHandle receiver_pipe_end);
brettwbd8214bf2017-06-20 03:47:0396
97 private:
98 struct Connection;
Erik Chene2d064472017-10-07 03:34:0799 struct DumpProcessesForTracingTracking;
brettwbd8214bf2017-06-20 03:47:03100
Erik Chene2d064472017-10-07 03:34:07101 // Schedules the given callback to execute after the given process ID has
102 // been synchronized. If the process ID isn't found, the callback will be
103 // asynchronously run with "false" as the success parameter.
104 void SynchronizeOnPid(base::ProcessId process_id,
105 AllocationTracker::SnapshotCallback callback);
106
107 // Actually does the dump assuming the given process has been synchronized.
108 void DoDumpProcess(DumpProcessArgs args,
109 bool success,
110 AllocationCountMap counts,
111 AllocationTracker::ContextMap context);
112 void DoDumpOneProcessForTracing(
113 scoped_refptr<DumpProcessesForTracingTracking> tracking,
114 base::ProcessId pid,
115 bool success,
116 AllocationCountMap counts,
117 AllocationTracker::ContextMap context);
erikchen708c31d2017-09-19 00:28:53118
brettwbd8214bf2017-06-20 03:47:03119 // Notification that a connection is complete. Unlike OnNewConnection which
120 // is signaled by the pipe server, this is signaled by the allocation tracker
121 // to ensure that the pipeline for this process has been flushed of all
122 // messages.
Albert J. Wong59d85acb2017-08-10 00:50:57123 void OnConnectionComplete(base::ProcessId pid);
brettwbd8214bf2017-06-20 03:47:03124
Erik Chene2d064472017-10-07 03:34:07125 // These thunks post the request back to the given thread.
126 static void OnConnectionCompleteThunk(
erikchena4d19192017-08-23 17:33:16127 scoped_refptr<base::SequencedTaskRunner> main_loop,
Erik Chene2d064472017-10-07 03:34:07128 base::WeakPtr<MemlogConnectionManager> connection_manager,
Albert J. Wong59d85acb2017-08-10 00:50:57129 base::ProcessId process_id);
brettwbd8214bf2017-06-20 03:47:03130
erikchen68532cc2017-08-16 19:38:36131 BacktraceStorage backtrace_storage_;
Albert J. Wong3432f462017-08-02 02:47:24132
Erik Chene2d064472017-10-07 03:34:07133 // Next ID to use for a barrier request. This is incremented for each use
134 // to ensure barrier IDs are unique.
135 uint32_t next_barrier_id_ = 1;
136
brettwbd8214bf2017-06-20 03:47:03137 // Maps process ID to the connection information for it.
Albert J. Wong59d85acb2017-08-10 00:50:57138 base::flat_map<base::ProcessId, std::unique_ptr<Connection>> connections_;
erikchen66af0162017-08-02 19:53:19139 base::Lock connections_lock_;
brettwbd8214bf2017-06-20 03:47:03140
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_