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" |
erikchen | 66af016 | 2017-08-02 19:53:19 | [diff] [blame] | 12 | #include "base/files/file.h" |
Albert J. Wong | 3432f46 | 2017-08-02 02:47:24 | [diff] [blame] | 13 | #include "base/files/platform_file.h" |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 14 | #include "base/macros.h" |
erikchen | a4d1919 | 2017-08-23 17:33:16 | [diff] [blame] | 15 | #include "base/memory/weak_ptr.h" |
Albert J. Wong | 59d85acb | 2017-08-10 00:50:57 | [diff] [blame] | 16 | #include "base/process/process_handle.h" |
erikchen | 66af016 | 2017-08-02 19:53:19 | [diff] [blame] | 17 | #include "base/synchronization/lock.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" |
Erik Chen | 5eaed0e | 2017-08-26 22:16:49 | [diff] [blame] | 20 | #include "chrome/common/profiling/memlog.mojom.h" |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 21 | #include "chrome/profiling/backtrace_storage.h" |
Erik Chen | e266c73 | 2017-08-12 02:27:19 | [diff] [blame] | 22 | #include "services/resource_coordinator/public/interfaces/memory_instrumentation/memory_instrumentation.mojom.h" |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 23 | |
| 24 | namespace base { |
Erik Chen | e266c73 | 2017-08-12 02:27:19 | [diff] [blame] | 25 | |
Albert J. Wong | 3432f46 | 2017-08-02 02:47:24 | [diff] [blame] | 26 | class SequencedTaskRunner; |
Erik Chen | e266c73 | 2017-08-12 02:27:19 | [diff] [blame] | 27 | |
| 28 | } // namespace base |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 29 | |
| 30 | namespace profiling { |
| 31 | |
| 32 | // Manages all connections and logging for each process. Pipes are supplied by |
| 33 | // 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] | 34 | // |
| 35 | // Note |backtrace_storage| must outlive MemlogConnectionManager. |
| 36 | // |
| 37 | // This object is constructed on the UI thread, but the rest of the usage |
| 38 | // (including deletion) is on the IO thread. |
| 39 | class MemlogConnectionManager { |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 40 | public: |
erikchen | a4d1919 | 2017-08-23 17:33:16 | [diff] [blame] | 41 | MemlogConnectionManager(); |
Albert J. Wong | 3432f46 | 2017-08-02 02:47:24 | [diff] [blame] | 42 | ~MemlogConnectionManager(); |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 43 | |
erikchen | 708c31d | 2017-09-19 00:28:53 | [diff] [blame] | 44 | // Parameters to DumpProcess(). |
| 45 | struct DumpProcessArgs { |
| 46 | DumpProcessArgs(); |
| 47 | ~DumpProcessArgs(); |
| 48 | DumpProcessArgs(DumpProcessArgs&&); |
| 49 | base::ProcessId pid; |
| 50 | std::unique_ptr<base::DictionaryValue> metadata; |
| 51 | std::vector<memory_instrumentation::mojom::VmRegionPtr> maps; |
| 52 | base::File file; |
| 53 | mojom::Memlog::DumpProcessCallback callback; |
| 54 | |
| 55 | private: |
| 56 | DISALLOW_COPY_AND_ASSIGN(DumpProcessArgs); |
| 57 | }; |
| 58 | |
| 59 | // Dumps the memory log for the given process into |args.output_file|. This |
| 60 | // must be provided the memory map for the given process since that is not |
| 61 | // tracked as part of the normal allocation process. When |
| 62 | // |hop_to_connection_thread| is |true|, the task is posted onto the |
| 63 | // connection thread's task runner, and then back onto the current task |
| 64 | // runner. This ensures that all queued tasks on the connection's thread are |
| 65 | // flushed before the dump is executed. |
| 66 | void DumpProcess(DumpProcessArgs args, bool hop_to_connection_thread); |
| 67 | |
Erik Chen | ecc1021 | 2017-10-02 23:08:45 | [diff] [blame^] | 68 | void DumpProcessesForTracing( |
| 69 | mojom::Memlog::DumpProcessesForTracingCallback callback, |
| 70 | memory_instrumentation::mojom::GlobalMemoryDumpPtr dump); |
erikchen | 66af016 | 2017-08-02 19:53:19 | [diff] [blame] | 71 | |
Albert J. Wong | 59d85acb | 2017-08-10 00:50:57 | [diff] [blame] | 72 | void OnNewConnection(base::ScopedPlatformFile file, base::ProcessId pid); |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 73 | |
| 74 | private: |
| 75 | struct Connection; |
| 76 | |
erikchen | 708c31d | 2017-09-19 00:28:53 | [diff] [blame] | 77 | // This method is posted on the connection's thread, and immediately reposts |
| 78 | // DumpProcess back to |task_runner|. This ensures that all queued tasks on |
| 79 | // the connection's thread are flushed before the dump is executed. |
| 80 | static void HopToConnectionThread( |
| 81 | base::WeakPtr<MemlogConnectionManager> manager, |
| 82 | DumpProcessArgs args, |
| 83 | scoped_refptr<base::SequencedTaskRunner> task_runner); |
| 84 | |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 85 | // Notification that a connection is complete. Unlike OnNewConnection which |
| 86 | // is signaled by the pipe server, this is signaled by the allocation tracker |
| 87 | // to ensure that the pipeline for this process has been flushed of all |
| 88 | // messages. |
Albert J. Wong | 59d85acb | 2017-08-10 00:50:57 | [diff] [blame] | 89 | void OnConnectionComplete(base::ProcessId pid); |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 90 | |
| 91 | void OnConnectionCompleteThunk( |
erikchen | a4d1919 | 2017-08-23 17:33:16 | [diff] [blame] | 92 | scoped_refptr<base::SequencedTaskRunner> main_loop, |
Albert J. Wong | 59d85acb | 2017-08-10 00:50:57 | [diff] [blame] | 93 | base::ProcessId process_id); |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 94 | |
erikchen | 68532cc | 2017-08-16 19:38:36 | [diff] [blame] | 95 | BacktraceStorage backtrace_storage_; |
Albert J. Wong | 3432f46 | 2017-08-02 02:47:24 | [diff] [blame] | 96 | |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 97 | // Maps process ID to the connection information for it. |
Albert J. Wong | 59d85acb | 2017-08-10 00:50:57 | [diff] [blame] | 98 | base::flat_map<base::ProcessId, std::unique_ptr<Connection>> connections_; |
erikchen | 66af016 | 2017-08-02 19:53:19 | [diff] [blame] | 99 | base::Lock connections_lock_; |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 100 | |
erikchen | a4d1919 | 2017-08-23 17:33:16 | [diff] [blame] | 101 | // Must be last. |
| 102 | base::WeakPtrFactory<MemlogConnectionManager> weak_factory_; |
| 103 | |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 104 | DISALLOW_COPY_AND_ASSIGN(MemlogConnectionManager); |
| 105 | }; |
| 106 | |
| 107 | } // namespace profiling |
| 108 | |
| 109 | #endif // CHROME_PROFILING_MEMLOG_CONNECTION_MANAGER_H_ |