blob: 2412ac940cd58d50d97efa684a721e7fd178d4ae [file] [log] [blame]
amistryfaa231a42015-05-20 01:49:121// 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
avib896c712015-12-26 02:10:438#include <stddef.h>
9
amistry77099c22015-05-20 03:48:5510#include "base/callback.h"
Brett Wilson275a1372017-09-01 20:27:5411#include "base/containers/circular_deque.h"
avib896c712015-12-26 02:10:4312#include "base/macros.h"
amistryfaa231a42015-05-20 01:49:1213#include "base/threading/thread_checker.h"
Ben Goodgerf180ce12018-02-09 22:54:0114#include "content/public/common/resource_usage_reporter.mojom.h"
Blink Reformata30d4232018-04-07 15:31:0615#include "third_party/blink/public/platform/web_cache.h"
amistryfaa231a42015-05-20 01:49:1216
17// Provides resource usage information about a child process.
18//
Ben Goodgerf180ce12018-02-09 22:54:0119// This is a wrapper around the content::mojom::ResourceUsageReporter Mojo
nigeltao48a86032016-11-25 00:04:1620// service that exposes
amistryfaa231a42015-05-20 01:49:1221// information about resources used by a child process. Currently, this is only
amistry9e2a8b42015-06-13 01:11:2722// 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
asvitkine2c4b4d1a2016-03-19 14:18:0724// manager.
amistryfaa231a42015-05-20 01:49:1225//
26// To create:
Ben Goodgerf180ce12018-02-09 22:54:0127// 1. Create a content::mojom::ResourceUsageReporterPtr and obtain an
nigeltao48a86032016-11-25 00:04:1628// InterfaceRequest<>
leon.hanb4c770762016-04-06 05:52:0429// using
blundelle0a9f1582016-12-20 11:23:3230// mojo::MakeRequest.
amistry56de0e52015-05-21 04:31:4231// 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 Goodgerf180ce12018-02-09 22:54:0136// 3. Pass the content::mojom::ResourceUsageReporterPtr to the constructor.
amistry56de0e52015-05-21 04:31:4237//
38// Example:
39// void Foo::ConnectToService(
Ben Goodgerf180ce12018-02-09 22:54:0140// mojo::InterfaceRequest<content::mojom::ResourceUsageReporter> req) {
amistry56de0e52015-05-21 04:31:4241// content::ServiceRegistry* registry = host_->GetServiceRegistry();
dcheng7061e5f2016-03-04 01:21:4742// registry->ConnectToRemoteService(std::move(req));
amistry56de0e52015-05-21 04:31:4243// }
44//
45// ...
Ben Goodgerf180ce12018-02-09 22:54:0146// content::mojom::ResourceUsageReporterPtr service;
47// mojo::InterfaceRequest<content::mojom::ResourceUsageReporter> request =
blundelle0a9f1582016-12-20 11:23:3248// mojo::MakeRequest(&service);
Eric Seckler8652dcd52018-09-20 10:42:2849// base::PostTaskWithTraits(
50// FROM_HERE, {content::BrowserThread::IO},
amistry56de0e52015-05-21 04:31:4251// base::Bind(&Foo::ConnectToService, this, base::Passed(&request)));
dcheng7061e5f2016-03-04 01:21:4752// resource_usage_.reset(new ProcessResourceUsage(std::move(service)));
amistry56de0e52015-05-21 04:31:4253// ...
amistryfaa231a42015-05-20 01:49:1254//
55// Note: ProcessResourceUsage is thread-hostile and must live on a single
56// thread.
57class ProcessResourceUsage {
58 public:
59 // Must be called from the same thread that created |service|.
nigeltao48a86032016-11-25 00:04:1660 explicit ProcessResourceUsage(
Ben Goodgerf180ce12018-02-09 22:54:0161 content::mojom::ResourceUsageReporterPtr service);
amistryfaa231a42015-05-20 01:49:1262 ~ProcessResourceUsage();
63
amistry77099c22015-05-20 03:48:5564 // 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);
amistryfaa231a42015-05-20 01:49:1267
68 // Get V8 memory usage information.
69 bool ReportsV8MemoryStats() const;
70 size_t GetV8MemoryAllocated() const;
71 size_t GetV8MemoryUsed() const;
72
amistry9e2a8b42015-06-13 01:11:2773 // Get Blink resource cache information.
74 blink::WebCache::ResourceTypeStats GetWebCoreCacheStats() const;
75
amistryfaa231a42015-05-20 01:49:1276 private:
77 // Mojo IPC callback.
Ben Goodgerf180ce12018-02-09 22:54:0178 void OnRefreshDone(content::mojom::ResourceUsageDataPtr data);
amistryfaa231a42015-05-20 01:49:1279
amistry77099c22015-05-20 03:48:5580 void RunPendingRefreshCallbacks();
81
Ben Goodgerf180ce12018-02-09 22:54:0182 content::mojom::ResourceUsageReporterPtr service_;
amistryfaa231a42015-05-20 01:49:1283 bool update_in_progress_;
Brett Wilson275a1372017-09-01 20:27:5484 base::circular_deque<base::Closure> refresh_callbacks_;
amistryfaa231a42015-05-20 01:49:1285
Ben Goodgerf180ce12018-02-09 22:54:0186 content::mojom::ResourceUsageDataPtr stats_;
amistryfaa231a42015-05-20 01:49:1287
amistryfaa231a42015-05-20 01:49:1288 base::ThreadChecker thread_checker_;
89
90 DISALLOW_COPY_AND_ASSIGN(ProcessResourceUsage);
91};
92
93#endif // CHROME_BROWSER_PROCESS_RESOURCE_USAGE_H_