amistry | faa231a4 | 2015-05-20 01:49:12 | [diff] [blame] | 1 | // Copyright 2015 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_BROWSER_PROCESS_RESOURCE_USAGE_H_ |
| 6 | #define CHROME_BROWSER_PROCESS_RESOURCE_USAGE_H_ |
| 7 | |
avi | b896c71 | 2015-12-26 02:10:43 | [diff] [blame] | 8 | #include <stddef.h> |
| 9 | |
amistry | 77099c2 | 2015-05-20 03:48:55 | [diff] [blame] | 10 | #include "base/callback.h" |
Brett Wilson | 275a137 | 2017-09-01 20:27:54 | [diff] [blame] | 11 | #include "base/containers/circular_deque.h" |
avi | b896c71 | 2015-12-26 02:10:43 | [diff] [blame] | 12 | #include "base/macros.h" |
amistry | faa231a4 | 2015-05-20 01:49:12 | [diff] [blame] | 13 | #include "base/threading/thread_checker.h" |
Ben Goodger | f180ce1 | 2018-02-09 22:54:01 | [diff] [blame] | 14 | #include "content/public/common/resource_usage_reporter.mojom.h" |
Julie Jeongeun Kim | 3930c63 | 2019-09-12 02:26:47 | [diff] [blame] | 15 | #include "mojo/public/cpp/bindings/pending_remote.h" |
| 16 | #include "mojo/public/cpp/bindings/remote.h" |
Sergio Villar Senin | ce85bcc | 2019-10-01 12:13:52 | [diff] [blame] | 17 | #include "third_party/blink/public/common/web_cache/web_cache_resource_type_stats.h" |
amistry | faa231a4 | 2015-05-20 01:49:12 | [diff] [blame] | 18 | |
| 19 | // Provides resource usage information about a child process. |
| 20 | // |
Ben Goodger | f180ce1 | 2018-02-09 22:54:01 | [diff] [blame] | 21 | // This is a wrapper around the content::mojom::ResourceUsageReporter Mojo |
nigeltao | 48a8603 | 2016-11-25 00:04:16 | [diff] [blame] | 22 | // service that exposes |
amistry | faa231a4 | 2015-05-20 01:49:12 | [diff] [blame] | 23 | // information about resources used by a child process. Currently, this is only |
amistry | 9e2a8b4 | 2015-06-13 01:11:27 | [diff] [blame] | 24 | // V8 memory and Blink resource cache usage, but could be expanded to include |
| 25 | // other resources. This is intended for status viewers such as the task |
asvitkine | 2c4b4d1a | 2016-03-19 14:18:07 | [diff] [blame] | 26 | // manager. |
amistry | faa231a4 | 2015-05-20 01:49:12 | [diff] [blame] | 27 | // |
| 28 | // To create: |
Julie Jeongeun Kim | 3930c63 | 2019-09-12 02:26:47 | [diff] [blame] | 29 | // 1. Create a mojo::PendingRemote<content::mojom::ResourceUsageReporter> and |
| 30 | // obtain a mojo::PendingReceiver<> using InitWithNewPipeAndPassReceiver(). |
amistry | 56de0e5 | 2015-05-21 04:31:42 | [diff] [blame] | 31 | // 2. Use the child process's service registry to connect to the service using |
Julie Jeongeun Kim | 3930c63 | 2019-09-12 02:26:47 | [diff] [blame] | 32 | // the mojo::PendingReceiver<>. Note, ServiceRegistry is thread hostile and |
| 33 | // must always be accessed from the same thread. However, PendingReceiver<> |
amistry | 56de0e5 | 2015-05-21 04:31:42 | [diff] [blame] | 34 | // can be passed safely between threads, and therefore a task can be posted |
| 35 | // to the ServiceRegistry thread to connect to the remote service. |
Julie Jeongeun Kim | 3930c63 | 2019-09-12 02:26:47 | [diff] [blame] | 36 | // 3. Pass the mojo::PendingRemote<content::mojom::ResourceUsageReporter> to the |
| 37 | // constructor. |
amistry | 56de0e5 | 2015-05-21 04:31:42 | [diff] [blame] | 38 | // |
| 39 | // Example: |
| 40 | // void Foo::ConnectToService( |
Julie Jeongeun Kim | 3930c63 | 2019-09-12 02:26:47 | [diff] [blame] | 41 | // mojo::PendingReceiver<content::mojom::ResourceUsageReporter> |
| 42 | // receiver) { |
amistry | 56de0e5 | 2015-05-21 04:31:42 | [diff] [blame] | 43 | // content::ServiceRegistry* registry = host_->GetServiceRegistry(); |
dcheng | 7061e5f | 2016-03-04 01:21:47 | [diff] [blame] | 44 | // registry->ConnectToRemoteService(std::move(req)); |
amistry | 56de0e5 | 2015-05-21 04:31:42 | [diff] [blame] | 45 | // } |
| 46 | // |
| 47 | // ... |
Julie Jeongeun Kim | 3930c63 | 2019-09-12 02:26:47 | [diff] [blame] | 48 | // mojo::PendingRemote<content::mojom::ResourceUsageReporter> service; |
| 49 | // mojo::PendingReceiver<content::mojom::ResourceUsageReporter> receiver = |
| 50 | // service.InitWithNewPipeAndPassReceiver(); |
Gabriel Charette | c40dd9c | 2021-03-30 11:00:11 | [diff] [blame] | 51 | // content::GetIOThreadTaskRunner({})->PostTask( |
| 52 | // FROM_HERE, |
Alexander Cooper | 6b447b2 | 2020-07-22 00:47:18 | [diff] [blame] | 53 | // base::BindOnce(&Foo::ConnectToService, this, |
| 54 | // base::Passed(&receiver))); |
dcheng | 7061e5f | 2016-03-04 01:21:47 | [diff] [blame] | 55 | // resource_usage_.reset(new ProcessResourceUsage(std::move(service))); |
amistry | 56de0e5 | 2015-05-21 04:31:42 | [diff] [blame] | 56 | // ... |
amistry | faa231a4 | 2015-05-20 01:49:12 | [diff] [blame] | 57 | // |
| 58 | // Note: ProcessResourceUsage is thread-hostile and must live on a single |
| 59 | // thread. |
| 60 | class ProcessResourceUsage { |
| 61 | public: |
| 62 | // Must be called from the same thread that created |service|. |
nigeltao | 48a8603 | 2016-11-25 00:04:16 | [diff] [blame] | 63 | explicit ProcessResourceUsage( |
Julie Jeongeun Kim | 3930c63 | 2019-09-12 02:26:47 | [diff] [blame] | 64 | mojo::PendingRemote<content::mojom::ResourceUsageReporter> service); |
amistry | faa231a4 | 2015-05-20 01:49:12 | [diff] [blame] | 65 | ~ProcessResourceUsage(); |
| 66 | |
amistry | 77099c2 | 2015-05-20 03:48:55 | [diff] [blame] | 67 | // Refresh the resource usage information. |callback| is invoked when the |
| 68 | // usage data is updated, or when the IPC connection is lost. |
Alexander Cooper | bc87af3 | 2020-07-15 15:59:45 | [diff] [blame] | 69 | void Refresh(base::OnceClosure callback); |
amistry | faa231a4 | 2015-05-20 01:49:12 | [diff] [blame] | 70 | |
| 71 | // Get V8 memory usage information. |
| 72 | bool ReportsV8MemoryStats() const; |
| 73 | size_t GetV8MemoryAllocated() const; |
| 74 | size_t GetV8MemoryUsed() const; |
| 75 | |
amistry | 9e2a8b4 | 2015-06-13 01:11:27 | [diff] [blame] | 76 | // Get Blink resource cache information. |
Sergio Villar Senin | ce85bcc | 2019-10-01 12:13:52 | [diff] [blame] | 77 | blink::WebCacheResourceTypeStats GetBlinkMemoryCacheStats() const; |
amistry | 9e2a8b4 | 2015-06-13 01:11:27 | [diff] [blame] | 78 | |
amistry | faa231a4 | 2015-05-20 01:49:12 | [diff] [blame] | 79 | private: |
| 80 | // Mojo IPC callback. |
Ben Goodger | f180ce1 | 2018-02-09 22:54:01 | [diff] [blame] | 81 | void OnRefreshDone(content::mojom::ResourceUsageDataPtr data); |
amistry | faa231a4 | 2015-05-20 01:49:12 | [diff] [blame] | 82 | |
amistry | 77099c2 | 2015-05-20 03:48:55 | [diff] [blame] | 83 | void RunPendingRefreshCallbacks(); |
| 84 | |
Julie Jeongeun Kim | 3930c63 | 2019-09-12 02:26:47 | [diff] [blame] | 85 | mojo::Remote<content::mojom::ResourceUsageReporter> service_; |
amistry | faa231a4 | 2015-05-20 01:49:12 | [diff] [blame] | 86 | bool update_in_progress_; |
Alexander Cooper | bc87af3 | 2020-07-15 15:59:45 | [diff] [blame] | 87 | base::circular_deque<base::OnceClosure> refresh_callbacks_; |
amistry | faa231a4 | 2015-05-20 01:49:12 | [diff] [blame] | 88 | |
Ben Goodger | f180ce1 | 2018-02-09 22:54:01 | [diff] [blame] | 89 | content::mojom::ResourceUsageDataPtr stats_; |
amistry | faa231a4 | 2015-05-20 01:49:12 | [diff] [blame] | 90 | |
amistry | faa231a4 | 2015-05-20 01:49:12 | [diff] [blame] | 91 | base::ThreadChecker thread_checker_; |
| 92 | |
| 93 | DISALLOW_COPY_AND_ASSIGN(ProcessResourceUsage); |
| 94 | }; |
| 95 | |
| 96 | #endif // CHROME_BROWSER_PROCESS_RESOURCE_USAGE_H_ |