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 | |
erikchen | fa983faa | 2018-04-05 18:56:42 | [diff] [blame] | 5 | #ifndef COMPONENTS_SERVICES_HEAP_PROFILING_CONNECTION_MANAGER_H_ |
| 6 | #define COMPONENTS_SERVICES_HEAP_PROFILING_CONNECTION_MANAGER_H_ |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 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 | a6f3feb | 2018-02-14 16:21:56 | [diff] [blame] | 17 | #include "base/threading/thread.h" |
erikchen | 21a2cab | 2017-08-21 20:50:44 | [diff] [blame] | 18 | #include "base/values.h" |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 19 | #include "build/build_config.h" |
erikchen | fa983faa | 2018-04-05 18:56:42 | [diff] [blame] | 20 | #include "components/services/heap_profiling/allocation_event.h" |
| 21 | #include "components/services/heap_profiling/allocation_tracker.h" |
| 22 | #include "components/services/heap_profiling/backtrace_storage.h" |
erikchen | 0d4a601 | 2018-04-03 16:06:33 | [diff] [blame] | 23 | #include "components/services/heap_profiling/public/mojom/heap_profiling_service.mojom.h" |
Erik Chen | e2d06447 | 2017-10-07 03:34:07 | [diff] [blame] | 24 | #include "mojo/edk/embedder/scoped_platform_handle.h" |
Ken Rockot | 95c888a4 | 2018-02-11 05:54:11 | [diff] [blame] | 25 | #include "services/resource_coordinator/public/mojom/memory_instrumentation/memory_instrumentation.mojom.h" |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 26 | |
| 27 | namespace base { |
Erik Chen | e266c73 | 2017-08-12 02:27:19 | [diff] [blame] | 28 | |
Albert J. Wong | 3432f46 | 2017-08-02 02:47:24 | [diff] [blame] | 29 | class SequencedTaskRunner; |
Erik Chen | e266c73 | 2017-08-12 02:27:19 | [diff] [blame] | 30 | |
| 31 | } // namespace base |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 32 | |
erikchen | 102fe21 | 2018-04-06 13:02:10 | [diff] [blame] | 33 | namespace heap_profiling { |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 34 | |
erikchen | 53cddfe6 | 2018-02-14 23:31:29 | [diff] [blame] | 35 | using VmRegions = |
Ken Rockot | 08942735 | 2018-04-24 14:51:37 | [diff] [blame^] | 36 | base::flat_map<base::ProcessId, |
| 37 | std::vector<memory_instrumentation::mojom::VmRegionPtr>>; |
erikchen | 53cddfe6 | 2018-02-14 23:31:29 | [diff] [blame] | 38 | |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 39 | // 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. Wong | 3432f46 | 2017-08-02 02:47:24 | [diff] [blame] | 41 | // |
erikchen | fa983faa | 2018-04-05 18:56:42 | [diff] [blame] | 42 | // Note |backtrace_storage| must outlive ConnectionManager. |
Albert J. Wong | 3432f46 | 2017-08-02 02:47:24 | [diff] [blame] | 43 | // |
| 44 | // This object is constructed on the UI thread, but the rest of the usage |
| 45 | // (including deletion) is on the IO thread. |
erikchen | fa983faa | 2018-04-05 18:56:42 | [diff] [blame] | 46 | class ConnectionManager { |
erikchen | 67fff82 | 2018-02-21 18:53:43 | [diff] [blame] | 47 | using DumpProcessesForTracingCallback = memory_instrumentation::mojom:: |
| 48 | HeapProfiler::DumpProcessesForTracingCallback; |
| 49 | |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 50 | public: |
erikchen | fa983faa | 2018-04-05 18:56:42 | [diff] [blame] | 51 | ConnectionManager(); |
| 52 | ~ConnectionManager(); |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 53 | |
Erik Chen | e2d06447 | 2017-10-07 03:34:07 | [diff] [blame] | 54 | // Shared types for the dump-type-specific args structures. |
| 55 | struct DumpArgs { |
| 56 | DumpArgs(); |
| 57 | DumpArgs(DumpArgs&&) noexcept; |
| 58 | ~DumpArgs(); |
Erik Chen | 22f66c6d | 2017-10-06 23:48:50 | [diff] [blame] | 59 | |
| 60 | private: |
erikchen | fa983faa | 2018-04-05 18:56:42 | [diff] [blame] | 61 | friend ConnectionManager; |
Erik Chen | e2d06447 | 2017-10-07 03:34:07 | [diff] [blame] | 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); |
erikchen | 1ca0e5f | 2017-10-06 22:06:14 | [diff] [blame] | 68 | }; |
| 69 | |
Erik Chen | e2d06447 | 2017-10-07 03:34:07 | [diff] [blame] | 70 | // 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. |
erikchen | 67fff82 | 2018-02-21 18:53:43 | [diff] [blame] | 73 | void DumpProcessesForTracing(bool keep_small_allocations, |
| 74 | bool strip_path_from_mapped_files, |
| 75 | DumpProcessesForTracingCallback callback, |
| 76 | VmRegions vm_regions); |
erikchen | 66af016 | 2017-08-02 19:53:19 | [diff] [blame] | 77 | |
Erik Chen | e2d06447 | 2017-10-07 03:34:07 | [diff] [blame] | 78 | void OnNewConnection(base::ProcessId pid, |
Brett Wilson | 40a6bb50 | 2017-10-10 20:36:15 | [diff] [
|