Avi Drissman | 8ba1bad | 2022-09-13 19:22:36 | [diff] [blame^] | 1 | // Copyright 2017 The Chromium Authors |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 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 | |
Alexei Filippov | 1b7b880 | 2019-04-17 22:01:43 | [diff] [blame] | 8 | #include <map> |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 9 | #include <string> |
Alexei Filippov | 1b7b880 | 2019-04-17 22:01:43 | [diff] [blame] | 10 | #include <unordered_map> |
Erik Chen | e266c73 | 2017-08-12 02:27:19 | [diff] [blame] | 11 | #include <vector> |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 12 | |
| 13 | #include "base/containers/flat_map.h" |
Erik Chen | e2d06447 | 2017-10-07 03:34:07 | [diff] [blame] | 14 | #include "base/memory/ref_counted.h" |
erikchen | a4d1919 | 2017-08-23 17:33:16 | [diff] [blame] | 15 | #include "base/memory/weak_ptr.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" |
Sebastien Marchand | efda77e53 | 2019-01-25 22:53:52 | [diff] [blame] | 18 | #include "base/timer/timer.h" |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 19 | #include "build/build_config.h" |
Alexei Filippov | 0a19454 | 2019-04-22 22:45:46 | [diff] [blame] | 20 | #include "components/services/heap_profiling/allocation.h" |
erikchen | 0d4a601 | 2018-04-03 16:06:33 | [diff] [blame] | 21 | #include "components/services/heap_profiling/public/mojom/heap_profiling_service.mojom.h" |
Ken Rockot | ced3127 | 2019-08-02 21:12:18 | [diff] [blame] | 22 | #include "mojo/public/cpp/bindings/pending_remote.h" |
Ken Rockot | 95c888a4 | 2018-02-11 05:54:11 | [diff] [blame] | 23 | #include "services/resource_coordinator/public/mojom/memory_instrumentation/memory_instrumentation.mojom.h" |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 24 | |
erikchen | 102fe21 | 2018-04-06 13:02:10 | [diff] [blame] | 25 | namespace heap_profiling { |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 26 | |
Alexei Filippov | da9fb73 | 2019-04-25 22:40:32 | [diff] [blame] | 27 | struct ExportParams; |
| 28 | |
erikchen | 53cddfe6 | 2018-02-14 23:31:29 | [diff] [blame] | 29 | using VmRegions = |
Ken Rockot | 08942735 | 2018-04-24 14:51:37 | [diff] [blame] | 30 | base::flat_map<base::ProcessId, |
| 31 | std::vector<memory_instrumentation::mojom::VmRegionPtr>>; |
erikchen | 53cddfe6 | 2018-02-14 23:31:29 | [diff] [blame] | 32 | |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 33 | // Manages all connections and logging for each process. Pipes are supplied by |
| 34 | // 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] | 35 | // |
erikchen | fa983faa | 2018-04-05 18:56:42 | [diff] [blame] | 36 | // Note |backtrace_storage| must outlive ConnectionManager. |
Albert J. Wong | 3432f46 | 2017-08-02 02:47:24 | [diff] [blame] | 37 | // |
| 38 | // This object is constructed on the UI thread, but the rest of the usage |
| 39 | // (including deletion) is on the IO thread. |
erikchen | fa983faa | 2018-04-05 18:56:42 | [diff] [blame] | 40 | class ConnectionManager { |
Alexei Filippov | 1b7b880 | 2019-04-17 22:01:43 | [diff] [blame] | 41 | using AddressToStringMap = std::unordered_map<uint64_t, std::string>; |
| 42 | using CompleteCallback = base::OnceClosure; |
| 43 | using ContextMap = std::map<std::string, int>; |
erikchen | 67fff82 | 2018-02-21 18:53:43 | [diff] [blame] | 44 | using DumpProcessesForTracingCallback = memory_instrumentation::mojom:: |
| 45 | HeapProfiler::DumpProcessesForTracingCallback; |
| 46 | |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 47 | public: |
erikchen | fa983faa | 2018-04-05 18:56:42 | [diff] [blame] | 48 | ConnectionManager(); |
Peter Boström | 09c0182 | 2021-09-20 22:43:27 | [diff] [blame] | 49 | |
| 50 | ConnectionManager(const ConnectionManager&) = delete; |
| 51 | ConnectionManager& operator=(const ConnectionManager&) = delete; |
| 52 | |
erikchen | fa983faa | 2018-04-05 18:56:42 | [diff] [blame] | 53 | ~ConnectionManager(); |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 54 | |
Erik Chen | e2d06447 | 2017-10-07 03:34:07 | [diff] [blame] | 55 | // Dumping is asynchronous so will not be complete when this function |
| 56 | // returns. The dump is complete when the callback provided in the args is |
| 57 | // fired. |
Alexei Filippov | e024518 | 2019-03-08 20:17:05 | [diff] [blame] | 58 | void DumpProcessesForTracing(bool strip_path_from_mapped_files, |
ssid | 035cbfb | 2021-07-24 20:35:57 | [diff] [blame] | 59 | bool write_proto, |
erikchen | 67fff82 | 2018-02-21 18:53:43 | [diff] [blame] | 60 | DumpProcessesForTracingCallback callback, |
| 61 | VmRegions vm_regions); |
erikchen | 66af016 | 2017-08-02 19:53:19 | [diff] [
|