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 <deque> |
| 11 | |
amistry | 77099c2 | 2015-05-20 03:48:55 | [diff] [blame] | 12 | #include "base/callback.h" |
avi | b896c71 | 2015-12-26 02:10:43 | [diff] [blame] | 13 | #include "base/macros.h" |
amistry | faa231a4 | 2015-05-20 01:49:12 | [diff] [blame] | 14 | #include "base/threading/thread_checker.h" |
| 15 | #include "chrome/common/resource_usage_reporter.mojom.h" |
amistry | 9e2a8b4 | 2015-06-13 01:11:27 | [diff] [blame] | 16 | #include "third_party/WebKit/public/web/WebCache.h" |
amistry | faa231a4 | 2015-05-20 01:49:12 | [diff] [blame] | 17 | |
| 18 | // Provides resource usage information about a child process. |
| 19 | // |
leon.han | b4c77076 | 2016-04-06 05:52:04 | [diff] [blame^] | 20 | // This is a wrapper around the mojom::ResourceUsageReporter Mojo service that |
| 21 | // exposes |
amistry | faa231a4 | 2015-05-20 01:49:12 | [diff] [blame] | 22 | // information about resources used by a child process. Currently, this is only |
amistry | 9e2a8b4 | 2015-06-13 01:11:27 | [diff] [blame] | 23 | // V8 memory and Blink resource cache usage, but could be expanded to include |
| 24 | // other resources. This is intended for status viewers such as the task |
asvitkine | 2c4b4d1a | 2016-03-19 14:18:07 | [diff] [blame] | 25 | // manager. |
amistry | faa231a4 | 2015-05-20 01:49:12 | [diff] [blame] | 26 | // |
| 27 | // To create: |
leon.han | b4c77076 | 2016-04-06 05:52:04 | [diff] [blame^] | 28 | // 1. Create a mojom::ResourceUsageReporterPtr and obtain an InterfaceRequest<> |
| 29 | // using |
amistry | 56de0e5 | 2015-05-21 04:31:42 | [diff] [blame] | 30 | // mojo::GetProxy. |
| 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. |
leon.han | b4c77076 | 2016-04-06 05:52:04 | [diff] [blame^] | 36 | // 3. Pass the mojom::ResourceUsageReporterPtr to the constructor. |
amistry | 56de0e5 | 2015-05-21 04:31:42 | [diff] [blame] | 37 | // |
| 38 | // Example: |
| 39 | // void Foo::ConnectToService( |
leon.han | b4c77076 | 2016-04-06 05:52:04 | [diff] [blame^] | 40 | // mojo::InterfaceRequest<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 | // ... |
leon.han | b4c77076 | 2016-04-06 05:52:04 | [diff] [blame^] | 46 | // mojom::ResourceUsageReporterPtr service; |
| 47 | // mojo::InterfaceRequest<mojom::ResourceUsageReporter> request = |
amistry | 56de0e5 | 2015-05-21 04:31:42 | [diff] [blame] | 48 | // mojo::GetProxy(&service); |
| 49 | // content::BrowserThread::PostTask( |
| 50 | // content::BrowserThread::IO, FROM_HERE, |
| 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|. |
leon.han | b4c77076 | 2016-04-06 05:52:04 | [diff] [blame^] | 60 | explicit ProcessResourceUsage(mojom::ResourceUsageReporterPtr service); |
amistry | faa231a4 | 2015-05-20 01:49:12 | [diff] [blame] | 61 | ~ProcessResourceUsage(); |
| 62 | |
amistry | 77099c2 | 2015-05-20 03:48:55 | [diff] [blame] | 63 | // Refresh the resource usage information. |callback| is invoked when the |
| 64 | // usage data is updated, or when the IPC connection is lost. |
| 65 | void Refresh(const base::Closure& callback); |
amistry | faa231a4 | 2015-05-20 01:49:12 | [diff] [blame] | 66 | |
| 67 | // Get V8 memory usage information. |
| 68 | bool ReportsV8MemoryStats() const; |
| 69 | size_t GetV8MemoryAllocated() const; |
| 70 | size_t GetV8MemoryUsed() const; |
| 71 | |
amistry | 9e2a8b4 | 2015-06-13 01:11:27 | [diff] [blame] | 72 | // Get Blink resource cache information. |
| 73 | blink::WebCache::ResourceTypeStats GetWebCoreCacheStats() const; |
| 74 | |
amistry | faa231a4 | 2015-05-20 01:49:12 | [diff] [blame] | 75 | private: |
| 76 | // Mojo IPC callback. |
leon.han | b4c77076 | 2016-04-06 05:52:04 | [diff] [blame^] | 77 | void OnRefreshDone(mojom::ResourceUsageDataPtr data); |
amistry | faa231a4 | 2015-05-20 01:49:12 | [diff] [blame] | 78 | |
amistry | 77099c2 | 2015-05-20 03:48:55 | [diff] [blame] | 79 | void RunPendingRefreshCallbacks(); |
| 80 | |
leon.han | b4c77076 | 2016-04-06 05:52:04 | [diff] [blame^] | 81 | mojom::ResourceUsageReporterPtr service_; |
amistry | faa231a4 | 2015-05-20 01:49:12 | [diff] [blame] | 82 | bool update_in_progress_; |
amistry | 77099c2 | 2015-05-20 03:48:55 | [diff] [blame] | 83 | std::deque<base::Closure> refresh_callbacks_; |
amistry | faa231a4 | 2015-05-20 01:49:12 | [diff] [blame] | 84 | |
leon.han | b4c77076 | 2016-04-06 05:52:04 | [diff] [blame^] | 85 | mojom::ResourceUsageDataPtr stats_; |
amistry | faa231a4 | 2015-05-20 01:49:12 | [diff] [blame] | 86 | |
amistry | faa231a4 | 2015-05-20 01:49:12 | [diff] [blame] | 87 | base::ThreadChecker thread_checker_; |
| 88 | |
| 89 | DISALLOW_COPY_AND_ASSIGN(ProcessResourceUsage); |
| 90 | }; |
| 91 | |
| 92 | #endif // CHROME_BROWSER_PROCESS_RESOURCE_USAGE_H_ |