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" |
Blink Reformat | a30d423 | 2018-04-07 15:31:06 | [diff] [blame] | 15 | #include "third_party/blink/public/platform/web_cache.h" |
amistry | faa231a4 | 2015-05-20 01:49:12 | [diff] [blame] | 16 | |
| 17 | // Provides resource usage information about a child process. |
| 18 | // |
Ben Goodger | f180ce1 | 2018-02-09 22:54:01 | [diff] [blame] | 19 | // This is a wrapper around the content::mojom::ResourceUsageReporter Mojo |
nigeltao | 48a8603 | 2016-11-25 00:04:16 | [diff] [blame] | 20 | // service that exposes |
amistry | faa231a4 | 2015-05-20 01:49:12 | [diff] [blame] | 21 | // information about resources used by a child process. Currently, this is only |
amistry | 9e2a8b4 | 2015-06-13 01:11:27 | [diff] [blame] | 22 | // V8 memory and Blink resource cache usage, but could be expanded to include |
| 23 | // other resources. This is intended for status viewers such as the task |
asvitkine | 2c4b4d1a | 2016-03-19 14:18:07 | [diff] [blame] | 24 | // manager. |
amistry | faa231a4 | 2015-05-20 01:49:12 | [diff] [blame] | 25 | // |
| 26 | // To create: |
Ben Goodger | f180ce1 | 2018-02-09 22:54:01 | [diff] [blame] | 27 | // 1. Create a content::mojom::ResourceUsageReporterPtr and obtain an |
nigeltao | 48a8603 | 2016-11-25 00:04:16 | [diff] [blame] | 28 | // InterfaceRequest<> |
leon.han | b4c77076 | 2016-04-06 05:52:04 | [diff] [blame] | 29 | // using |
blundell | e0a9f158 | 2016-12-20 11:23:32 | [diff] [blame] | 30 | // mojo::MakeRequest. |
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 |
| 32 | // the InterfaceRequest<>. Note, ServiceRegistry is thread hostile and |
| 33 | // must always be accessed from the same thread. However, InterfaceRequest<> |
| 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. |
Ben Goodger | f180ce1 | 2018-02-09 22:54:01 | [diff] [blame] | 36 | // 3. Pass the content::mojom::ResourceUsageReporterPtr to the constructor. |
amistry | 56de0e5 | 2015-05-21 04:31:42 | [diff] [blame] | 37 | // |
| 38 | // Example: |
| 39 | // void Foo::ConnectToService( |
Ben Goodger | f180ce1 | 2018-02-09 22:54:01 | [diff] [blame] | 40 | // mojo::InterfaceRequest<content::mojom::ResourceUsageReporter> req) { |
amistry | 56de0e5 | 2015-05-21 04:31:42 | [diff] [blame] | 41 | // content::ServiceRegistry* registry = host_->GetServiceRegistry(); |
dcheng | 7061e5f | 2016-03-04 01:21:47 | [diff] [blame] | 42 | // registry->ConnectToRemoteService(std::move(req)); |
amistry | 56de0e5 | 2015-05-21 04:31:42 | [diff] [blame] | 43 | // } |
| 44 | // |
| 45 | // ... |
Ben Goodger | f180ce1 | 2018-02-09 22:54:01 | [diff] [blame] | 46 | // content::mojom::ResourceUsageReporterPtr service; |
| 47 | // mojo::InterfaceRequest<content::mojom::ResourceUsageReporter> request = |
blundell | e0a9f158 | 2016-12-20 11:23:32 | [diff] [blame] | 48 | // mojo::MakeRequest(&service); |
Eric Seckler | 8652dcd5 | 2018-09-20 10:42:28 | [diff] [blame] | 49 | // base::PostTaskWithTraits( |
| 50 | // FROM_HERE, {content::BrowserThread::IO}, |
amistry | 56de0e5 | 2015-05-21 04:31:42 | [diff] [blame] | 51 | // base::Bind(&Foo::ConnectToService, this, base::Passed(&request))); |
dcheng | 7061e5f | 2016-03-04 01:21:47 | [diff] [blame] | 52 | // resource_usage_.reset(new ProcessResourceUsage(std::move(service))); |
amistry | 56de0e5 | 2015-05-21 04:31:42 | [diff] [blame] | 53 | // ... |
amistry | faa231a4 | 2015-05-20 01:49:12 | [diff] [blame] | 54 | // |
| 55 | // Note: ProcessResourceUsage is thread-hostile and must live on a single |
| 56 | // thread. |
| 57 | class ProcessResourceUsage { |
| 58 | public: |
| 59 | // Must be called from the same thread that created |service|. |
nigeltao | 48a8603 | 2016-11-25 00:04:16 | [diff] [blame] | 60 | explicit ProcessResourceUsage( |
Ben Goodger | f180ce1 | 2018-02-09 22:54:01 | [diff] [blame] | 61 | content::mojom::ResourceUsageReporterPtr service); |
amistry | faa231a4 | 2015-05-20 01:49:12 | [diff] [blame] | 62 | ~ProcessResourceUsage(); |
| 63 | |
amistry | 77099c2 | 2015-05-20 03:48:55 | [diff] [blame] | 64 | // Refresh the resource usage information. |callback| is invoked when the |
| 65 | // usage data is updated, or when the IPC connection is lost. |
| 66 | void Refresh(const base::Closure& callback); |
amistry | faa231a4 | 2015-05-20 01:49:12 | [diff] [blame] | 67 | |
| 68 | // Get V8 memory usage information. |
| 69 | bool ReportsV8MemoryStats() const; |
| 70 | size_t GetV8MemoryAllocated() const; |
| 71 | size_t GetV8MemoryUsed() const; |
| 72 | |
amistry | 9e2a8b4 | 2015-06-13 01:11:27 | [diff] [blame] | 73 | // Get Blink resource cache information. |
| 74 | blink::WebCache::ResourceTypeStats GetWebCoreCacheStats() const; |
| 75 | |
amistry | faa231a4 | 2015-05-20 01:49:12 | [diff] [blame] | 76 | private: |
| 77 | // Mojo IPC callback. |
Ben Goodger | f180ce1 | 2018-02-09 22:54:01 | [diff] [blame] | 78 | void OnRefreshDone(content::mojom::ResourceUsageDataPtr data); |
amistry | faa231a4 | 2015-05-20 01:49:12 | [diff] [blame] | 79 | |
amistry | 77099c2 | 2015-05-20 03:48:55 | [diff] [blame] | 80 | void RunPendingRefreshCallbacks(); |
| 81 | |
Ben Goodger | f180ce1 | 2018-02-09 22:54:01 | [diff] [blame] | 82 | content::mojom::ResourceUsageReporterPtr service_; |
amistry | faa231a4 | 2015-05-20 01:49:12 | [diff] [blame] | 83 | bool update_in_progress_; |
Brett Wilson | 275a137 | 2017-09-01 20:27:54 | [diff] [blame] | 84 | base::circular_deque<base::Closure> refresh_callbacks_; |
amistry | faa231a4 | 2015-05-20 01:49:12 | [diff] [blame] | 85 | |
Ben Goodger | f180ce1 | 2018-02-09 22:54:01 | [diff] [blame] | 86 | content::mojom::ResourceUsageDataPtr stats_; |
amistry | faa231a4 | 2015-05-20 01:49:12 | [diff] [blame] | 87 | |
amistry | faa231a4 | 2015-05-20 01:49:12 | [diff] [blame] | 88 | base::ThreadChecker thread_checker_; |
| 89 | |
| 90 | DISALLOW_COPY_AND_ASSIGN(ProcessResourceUsage); |
| 91 | }; |
| 92 | |
| 93 | #endif // CHROME_BROWSER_PROCESS_RESOURCE_USAGE_H_ |