brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 1 | // 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 Chen | e266c73 | 2017-08-12 02:27:19 | [diff] [blame] | 9 | #include <vector> |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 10 | |
| 11 | #include "base/containers/flat_map.h" |
| 12 | #include "base/macros.h" |
Erik Chen | e2d06447 | 2017-10-07 03:34:07 | [diff] [blame] | 13 | #include "base/memory/ref_counted.h" |
erikchen | a4d1919 | 2017-08-23 17:33:16 | [diff] [blame] | 14 | #include "base/memory/weak_ptr.h" |
Albert J. Wong | 59d85acb | 2017-08-10 00:50:57 | [diff] [blame] | 15 | #include "base/process/process_handle.h" |
erikchen | 66af016 | 2017-08-02 19:53:19 | [diff] [blame] | 16 | #include "base/synchronization/lock.h" |
erikchen | 21a2cab | 2017-08-21 20:50:44 | [diff] [blame] | 17 | #include "base/values.h" |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 18 | #include "build/build_config.h" |
Brett Wilson | 40a6bb50 | 2017-10-10 20:36:15 | [diff] [blame^] | 19 | #include "chrome/common/profiling/profiling_service.mojom.h" |
Erik Chen | e2d06447 | 2017-10-07 03:34:07 | [diff] [blame] | 20 | #include "chrome/profiling/allocation_event.h" |
| 21 | #include "chrome/profiling/allocation_tracker.h" |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 22 | #include "chrome/profiling/backtrace_storage.h" |
Erik Chen | e2d06447 | 2017-10-07 03:34:07 | [diff] [blame] | 23 | #include "mojo/edk/embedder/scoped_platform_handle.h" |
Erik Chen | e266c73 | 2017-08-12 02:27:19 | [diff] [blame] | 24 | #include "services/resource_coordinator/public/interfaces/memory_instrumentation/memory_instrumentation.mojom.h" |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 25 | |
| 26 | namespace base { |
Erik Chen | e266c73 | 2017-08-12 02:27:19 | [diff] [blame] | 27 | |
Albert J. Wong | 3432f46 | 2017-08-02 02:47:24 | [diff] [blame] | 28 | class SequencedTaskRunner; |
Erik Chen | e266c73 | 2017-08-12 02:27:19 | [diff] [blame] | 29 | |
| 30 | } // namespace base |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 31 | |
| 32 | namespace 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. Wong | 3432f46 | 2017-08-02 02:47:24 | [diff] [blame] | 36 | // |
| 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. |
| 41 | class MemlogConnectionManager { |
Erik Chen | e2d06447 | 2017-10-07 03:34:07 | [diff] [blame] | 42 | private: |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 43 | public: |
erikchen | a4d1919 | 2017-08-23 17:33:16 | [diff] [blame] | 44 | MemlogConnectionManager(); |
Albert J. Wong | 3432f46 | 2017-08-02 02:47:24 | [diff] [blame] | 45 | ~MemlogConnectionManager(); |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 46 | |
Erik Chen | e2d06447 | 2017-10-07 03:34:07 | [diff] [blame] | 47 | // Shared types for the dump-type-specific args structures. |
| 48 | struct DumpArgs { |
| 49 | DumpArgs(); |
| 50 | DumpArgs(DumpArgs&&) noexcept; |
| 51 | ~DumpArgs(); |
Erik Chen | 22f66c6d | 2017-10-06 23:48:50 | [diff] [blame] | 52 | |
| 53 | private: |
Erik Chen | e2d06447 | 2017-10-07 03:34:07 | [diff] [blame] | 54 | 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); |
erikchen | 1ca0e5f | 2017-10-06 22:06:14 | [diff] [blame] | 61 | }; |
| 62 | |
Erik Chen | e2d06447 | 2017-10-07 03:34:07 | [diff] [blame] | 63 | // Parameters to DumpProcess(). |
| 64 | struct DumpProcessArgs : public DumpArgs { |
| 65 | DumpProcessArgs(); |
| 66 | DumpProcessArgs(DumpProcessArgs&&) noexcept; |
| 67 | ~DumpProcessArgs(); |
Erik Chen | 22f66c6d | 2017-10-06 23:48:50 | [diff] [blame] | 68 | |
Erik Chen | e2d06447 | 2017-10-07 03:34:07 | [diff] [blame] | 69 | // 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 Wilson | 40a6bb50 | 2017-10-10 20:36:15 | [diff] [blame^] | 81 | mojom::ProfilingService::DumpProcessCallback callback; |
Erik Chen | e2d06447 | 2017-10-07 03:34:07 | [diff] [blame] | 82 | }; |
| 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 Chen | ecc1021 | 2017-10-02 23:08:45 | [diff] [blame] | 88 | void DumpProcessesForTracing( |
Brett Wilson | 40a6bb50 | 2017-10-10 20:36:15 | [diff] [blame^] | 89 | mojom::ProfilingService::DumpProcessesForTracingCallback callback, |
Erik Chen | ecc1021 | 2017-10-02 23:08:45 | [diff] [blame] | 90 | memory_instrumentation::mojom::GlobalMemoryDumpPtr dump); |
erikchen | 66af016 | 2017-08-02 19:53:19 | [diff] [blame] | 91 | |
Erik Chen | e2d06447 | 2017-10-07 03:34:07 | [diff] [blame] | 92 | void OnNewConnection(base::ProcessId pid, |
Brett Wilson | 40a6bb50 | 2017-10-10 20:36:15 | [diff] [blame^] | 93 | mojom::ProfilingClientPtr client, |
| 94 | mojo::ScopedHandle sender_pipe_end, |
| 95 | mojo::ScopedHandle receiver_pipe_end); |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 96 | |
| 97 | private: |
| 98 | struct Connection; |
Erik Chen | e2d06447 | 2017-10-07 03:34:07 | [diff] [blame] | 99 | struct DumpProcessesForTracingTracking; |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 100 | |
Erik Chen | e2d06447 | 2017-10-07 03:34:07 | [diff] [blame] | 101 | // 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); |
erikchen | 708c31d | 2017-09-19 00:28:53 | [diff] [blame] | 118 | |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 119 | // 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. Wong | 59d85acb | 2017-08-10 00:50:57 | [diff] [blame] | 123 | void OnConnectionComplete(base::ProcessId pid); |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 124 | |
Erik Chen | e2d06447 | 2017-10-07 03:34:07 | [diff] [blame] | 125 | // These thunks post the request back to the given thread. |
| 126 | static void OnConnectionCompleteThunk( |
erikchen | a4d1919 | 2017-08-23 17:33:16 | [diff] [blame] | 127 | scoped_refptr<base::SequencedTaskRunner> main_loop, |
Erik Chen | e2d06447 | 2017-10-07 03:34:07 | [diff] [blame] | 128 | base::WeakPtr<MemlogConnectionManager> connection_manager, |
Albert J. Wong | 59d85acb | 2017-08-10 00:50:57 | [diff] [blame] | 129 | base::ProcessId process_id); |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 130 | |
erikchen | 68532cc | 2017-08-16 19:38:36 | [diff] [blame] | 131 | BacktraceStorage backtrace_storage_; |
Albert J. Wong | 3432f46 | 2017-08-02 02:47:24 | [diff] [blame] | 132 | |
Erik Chen | e2d06447 | 2017-10-07 03:34:07 | [diff] [blame] | 133 | // 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 | |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 137 | // Maps process ID to the connection information for it. |
Albert J. Wong | 59d85acb | 2017-08-10 00:50:57 | [diff] [blame] | 138 | base::flat_map<base::ProcessId, std::unique_ptr<Connection>> connections_; |
erikchen | 66af016 | 2017-08-02 19:53:19 | [diff] [blame] | 139 | base::Lock connections_lock_; |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 140 | |
erikchen | a4d1919 | 2017-08-23 17:33:16 | [diff] [blame] | 141 | // Must be last. |
| 142 | base::WeakPtrFactory<MemlogConnectionManager> weak_factory_; |
| 143 | |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 144 | DISALLOW_COPY_AND_ASSIGN(MemlogConnectionManager); |
| 145 | }; |
| 146 | |
| 147 | } // namespace profiling |
| 148 | |
| 149 | #endif // CHROME_PROFILING_MEMLOG_CONNECTION_MANAGER_H_ |