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" |
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" |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 17 | #include "build/build_config.h" |
| 18 | #include "chrome/profiling/backtrace_storage.h" |
Erik Chen | e266c73 | 2017-08-12 02:27:19 | [diff] [blame] | 19 | #include "services/resource_coordinator/public/interfaces/memory_instrumentation/memory_instrumentation.mojom.h" |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 20 | |
| 21 | namespace base { |
Erik Chen | e266c73 | 2017-08-12 02:27:19 | [diff] [blame] | 22 | |
Albert J. Wong | 3432f46 | 2017-08-02 02:47:24 | [diff] [blame] | 23 | class SequencedTaskRunner; |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 24 | class SingleThreadTaskRunner; |
Erik Chen | e266c73 | 2017-08-12 02:27:19 | [diff] [blame] | 25 | |
| 26 | } // namespace base |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 27 | |
| 28 | namespace profiling { |
| 29 | |
| 30 | // Manages all connections and logging for each process. Pipes are supplied by |
| 31 | // 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] | 32 | // |
| 33 | // Note |backtrace_storage| must outlive MemlogConnectionManager. |
| 34 | // |
| 35 | // This object is constructed on the UI thread, but the rest of the usage |
| 36 | // (including deletion) is on the IO thread. |
| 37 | class MemlogConnectionManager { |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 38 | public: |
erikchen | 68532cc | 2017-08-16 19:38:36 | [diff] [blame^] | 39 | explicit MemlogConnectionManager( |
| 40 | scoped_refptr<base::SequencedTaskRunner> io_runner); |
Albert J. Wong | 3432f46 | 2017-08-02 02:47:24 | [diff] [blame] | 41 | ~MemlogConnectionManager(); |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 42 | |
Erik Chen | e266c73 | 2017-08-12 02:27:19 | [diff] [blame] | 43 | // Dumps the memory log for the given process into |output_file|. This must |
| 44 | // be provided the memory map for the given process since that is not tracked |
| 45 | // as part of the normal allocation process. |
| 46 | void DumpProcess( |
| 47 | base::ProcessId pid, |
| 48 | const std::vector<memory_instrumentation::mojom::VmRegionPtr>& maps, |
| 49 | base::File output_file); |
erikchen | 66af016 | 2017-08-02 19:53:19 | [diff] [blame] | 50 | |
Albert J. Wong | 59d85acb | 2017-08-10 00:50:57 | [diff] [blame] | 51 | void OnNewConnection(base::ScopedPlatformFile file, base::ProcessId pid); |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 52 | |
| 53 | private: |
| 54 | struct Connection; |
| 55 | |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 56 | // Notification that a connection is complete. Unlike OnNewConnection which |
| 57 | // is signaled by the pipe server, this is signaled by the allocation tracker |
| 58 | // to ensure that the pipeline for this process has been flushed of all |
| 59 | // messages. |
Albert J. Wong | 59d85acb | 2017-08-10 00:50:57 | [diff] [blame] | 60 | void OnConnectionComplete(base::ProcessId pid); |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 61 | |
| 62 | void OnConnectionCompleteThunk( |
| 63 | scoped_refptr<base::SingleThreadTaskRunner> main_loop, |
Albert J. Wong | 59d85acb | 2017-08-10 00:50:57 | [diff] [blame] | 64 | base::ProcessId process_id); |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 65 | |
Albert J. Wong | 3432f46 | 2017-08-02 02:47:24 | [diff] [blame] | 66 | scoped_refptr<base::SequencedTaskRunner> io_runner_; |
erikchen | 68532cc | 2017-08-16 19:38:36 | [diff] [blame^] | 67 | BacktraceStorage backtrace_storage_; |
Albert J. Wong | 3432f46 | 2017-08-02 02:47:24 | [diff] [blame] | 68 | |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 69 | // Maps process ID to the connection information for it. |
Albert J. Wong | 59d85acb | 2017-08-10 00:50:57 | [diff] [blame] | 70 | base::flat_map<base::ProcessId, std::unique_ptr<Connection>> connections_; |
erikchen | 66af016 | 2017-08-02 19:53:19 | [diff] [blame] | 71 | base::Lock connections_lock_; |
brettw | bd8214bf | 2017-06-20 03:47:03 | [diff] [blame] | 72 | |
| 73 | DISALLOW_COPY_AND_ASSIGN(MemlogConnectionManager); |
| 74 | }; |
| 75 | |
| 76 | } // namespace profiling |
| 77 | |
| 78 | #endif // CHROME_PROFILING_MEMLOG_CONNECTION_MANAGER_H_ |