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" |
Ken Rockot | 95c888a4 | 2018-02-11 05:54:11 | [diff] [blame^] | 24 | #include "services/resource_coordinator/public/mojom/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 | // 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 Chen | ecc1021 | 2017-10-02 23:08:45 | [diff] [blame] | 66 | void DumpProcessesForTracing( |
Erik Chen | 8bb76b5 | 2017-12-06 19:06:25 | [diff] [blame] | 67 | bool keep_small_allocations, |
erikchen | d1b8bc5 | 2017-12-21 18:12:42 | [diff] [blame] | 68 | bool strip_path_from_mapped_files, |
Brett Wilson | 40a6bb50 | 2017-10-10 20:36:15 | [diff] [blame] | 69 | mojom::ProfilingService::DumpProcessesForTracingCallback callback, |
Erik Chen | ecc1021 | 2017-10-02 23:08:45 | [diff] [blame] | 70 | memory_instrumentation::mojom::GlobalMemoryDumpPtr dump); |
erikchen | 66af016 | 2017-08-02 19:53:19 | [diff] [blame] | 71 | |
Erik Chen | e2d06447 | 2017-10-07 03:34:07 | [diff] [blame] | 72 | void OnNewConnection(base::ProcessId pid, |
Brett Wilson | 40a6bb50 | 2017-10-10 20:36:15 | [diff] [blame] | 73 | mojom::ProfilingClientPtr client, |
erikchen | e382a10 | 2017-10-20 00:30:13 | [diff] [blame] | 74 | mojo::ScopedHandle receiver_pipe_end, |
Erik Chen | 3303fd023 | 2018-01-11 20:29:05 | [diff] [blame] | 75 | mojom::ProcessType process_type, |
Erik Chen | 8a2fb2e7 | 2018-02-09 20:34:29 | [diff] [blame] | 76 | mojom::ProfilingParamsPtr params); |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 77 | |
Erik Chen | fe6fbee | 2017-12-06 07:40:09 | [diff] [blame] | 78 | std::vector<base::ProcessId> GetConnectionPids(); |
| 79 | |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 80 | private: |
| 81 | struct Connection; |
Erik Chen | e2d06447 | 2017-10-07 03:34:07 | [diff] [blame] | 82 | struct DumpProcessesForTracingTracking; |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 83 | |
Erik Chen | e2d06447 | 2017-10-07 03:34:07 | [diff] [blame] | 84 | void DoDumpOneProcessForTracing( |
| 85 | scoped_refptr<DumpProcessesForTracingTracking> tracking, |
| 86 | base::ProcessId pid, |
Erik Chen | a610d55 | 2017-10-20 22:29:49 | [diff] [blame] | 87 | mojom::ProcessType process_type, |
Erik Chen | 8bb76b5 | 2017-12-06 19:06:25 | [diff] [blame] | 88 | bool keep_small_allocations, |
erikchen | d1b8bc5 | 2017-12-21 18:12:42 | [diff] [blame] | 89 | bool strip_path_from_mapped_files, |
Erik Chen | e2d06447 | 2017-10-07 03:34:07 | [diff] [blame] | 90 | bool success, |
| 91 | AllocationCountMap counts, |
Erik Chen | 3303fd023 | 2018-01-11 20:29:05 | [diff] [blame] | 92 | AllocationTracker::ContextMap context, |
| 93 | AllocationTracker::AddressToStringMap mapped_strings); |
erikchen | 708c31d | 2017-09-19 00:28:53 | [diff] [blame] | 94 | |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 95 | // 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. Wong | 59d85acb | 2017-08-10 00:50:57 | [diff] [blame] | 99 | void OnConnectionComplete(base::ProcessId pid); |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 100 | |
erikchen | e382a10 | 2017-10-20 00:30:13 | [diff] [blame] | 101 | // Reports the ProcessTypes of the processes being profiled. |
| 102 | void ReportMetrics(); |
| 103 | |
Erik Chen | e2d06447 | 2017-10-07 03:34:07 | [diff] [blame] | 104 | // These thunks post the request back to the given thread. |
| 105 | static void OnConnectionCompleteThunk( |
erikchen | a4d1919 | 2017-08-23 17:33:16 | [diff] [blame] | 106 | scoped_refptr<base::SequencedTaskRunner> main_loop, |
Erik Chen | e2d06447 | 2017-10-07 03:34:07 | [diff] [blame] | 107 | base::WeakPtr<MemlogConnectionManager> connection_manager, |
Albert J. Wong | 59d85acb | 2017-08-10 00:50:57 | [diff] [blame] | 108 | base::ProcessId process_id); |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 109 | |
erikchen | 68532cc | 2017-08-16 19:38:36 | [diff] [blame] | 110 | BacktraceStorage backtrace_storage_; |
Albert J. Wong | 3432f46 | 2017-08-02 02:47:24 | [diff] [blame] | 111 | |
Erik Chen | e2d06447 | 2017-10-07 03:34:07 | [diff] [blame] | 112 | // 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 | |
erikchen | 535ef8e | 2018-02-01 18:04:54 | [diff] [blame] | 116 | // The next ID to use when exporting a heap dump. |
| 117 | size_t next_id_ = 1; |
| 118 | |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 119 | // Maps process ID to the connection information for it. |
Albert J. Wong | 59d85acb | 2017-08-10 00:50:57 | [diff] [blame] | 120 | base::flat_map<base::ProcessId, std::unique_ptr<Connection>> connections_; |
erikchen | 66af016 | 2017-08-02 19:53:19 | [diff] [blame] | 121 | base::Lock connections_lock_; |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 122 | |
erikchen | e382a10 | 2017-10-20 00:30:13 | [diff] [blame] | 123 | // Every 24-hours, reports the types of profiled processes. |
| 124 | base::RepeatingTimer metrics_timer_; |
| 125 | |
erikchen | a4d1919 | 2017-08-23 17:33:16 | [diff] [blame] | 126 | // Must be last. |
| 127 | base::WeakPtrFactory<MemlogConnectionManager> weak_factory_; |
| 128 | |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 129 | DISALLOW_COPY_AND_ASSIGN(MemlogConnectionManager); |
| 130 | }; |
| 131 | |
| 132 | } // namespace profiling |
| 133 | |
| 134 | #endif // CHROME_PROFILING_MEMLOG_CONNECTION_MANAGER_H_ |