blob: 65011a74edb44887383215561abc3fe36f102c42 [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"
Julie Jeongeun Kim3930c632019-09-12 02:26:4715#include "mojo/public/cpp/bindings/pending_remote.h"
16#include "mojo/public/cpp/bindings/remote.h"
Sergio Villar Senince85bcc2019-10-01 12:13:5217#include "third_party/blink/public/common/web_cache/web_cache_resource_type_stats.h"
amistryfaa231a42015-05-20 01:49:1218
19// Provides resource usage information about a child process.
20//
Ben Goodgerf180ce12018-02-09 22:54:0121// This is a wrapper around the content::mojom::ResourceUsageReporter Mojo
nigeltao48a86032016-11-25 00:04:1622// service that exposes
amistryfaa231a42015-05-20 01:49:1223// information about resources used by a child process. Currently, this is only
amistry9e2a8b42015-06-13 01:11:2724// 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
asvitkine2c4b4d1a2016-03-19 14:18:0726// manager.
amistryfaa231a42015-05-20 01:49:1227//
28// To create:
Julie Jeongeun Kim3930c632019-09-12 02:26:4729// 1. Create a mojo::PendingRemote<content::mojom::ResourceUsageReporter> and
30// obtain a mojo::PendingReceiver<> using InitWithNewPipeAndPassReceiver().
amistry56de0e52015-05-21 04:31:4231// 2. Use the child process's service registry to connect to the service using
Julie Jeongeun Kim3930c632019-09-12 02:26:4732// the mojo::PendingReceiver<>. Note, ServiceRegistry is thread hostile and
33// must always be accessed from the same thread. However, PendingReceiver<>
amistry56de0e52015-05-21 04:31:4234// 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 Kim3930c632019-09-12 02:26:4736// 3. Pass the mojo::PendingRemote<content::mojom::ResourceUsageReporter> to the
37// constructor.
amistry56de0e52015-05-21 04:31:4238//
39// Example:
40// void Foo::ConnectToService(
Julie Jeongeun Kim3930c632019-09-12 02:26:4741// mojo::PendingReceiver<content::mojom::ResourceUsageReporter>
42// receiver) {
amistry56de0e52015-05-21 04:31:4243// content::ServiceRegistry* registry = host_->GetServiceRegistry();
dcheng7061e5f2016-03-04 01:21:4744// registry->ConnectToRemoteService(std::move(req));
amistry56de0e52015-05-21 04:31:4245// }
46//
47// ...
Julie Jeongeun Kim3930c632019-09-12 02:26:4748// mojo::PendingRemote<content::mojom::ResourceUsageReporter> service;
49// mojo::PendingReceiver<content::mojom::ResourceUsageReporter> receiver =
50// service.InitWithNewPipeAndPassReceiver();
Gabriel Charettec40dd9c2021-03-30 11:00:1151// content::GetIOThreadTaskRunner({})->PostTask(
52// FROM_HERE,
Alexander Cooper6b447b22020-07-22 00:47:1853// base::BindOnce(&Foo::ConnectToService, this,
54// base::Passed(&receiver)));
dcheng7061e5f2016-03-04 01:21:4755// resource_usage_.reset(new ProcessResourceUsage(std::move(service)));
amistry56de0e52015-05-21 04:31:4256// ...
amistryfaa231a42015-05-20 01:49:1257//
58// Note: ProcessResourceUsage is thread-hostile and must live on a single
59// thread.
60class ProcessResourceUsage {
61 public:
62 // Must be called from the same thread that created |service|.
nigeltao48a86032016-11-25 00:04:1663 explicit ProcessResourceUsage(
Julie Jeongeun Kim3930c632019-09-12 02:26:4764 mojo::PendingRemote<content::mojom::ResourceUsageReporter> service);
amistryfaa231a42015-05-20 01:49:1265 ~ProcessResourceUsage();
66
amistry77099c22015-05-20 03:48:5567 // Refresh the resource usage information. |callback| is invoked when the
68 // usage data is updated, or when the IPC connection is lost.
Alexander Cooperbc87af32020-07-15 15:59:4569 void Refresh(base::OnceClosure callback);
amistryfaa231a42015-05-20 01:49:1270
71 // Get V8 memory usage information.
72 bool ReportsV8MemoryStats() const;
73 size_t GetV8MemoryAllocated() const;
74 size_t GetV8MemoryUsed() const;
75
amistry9e2a8b42015-06-13 01:11:2776 // Get Blink resource cache information.
Sergio Villar Senince85bcc2019-10-01 12:13:5277 blink::WebCacheResourceTypeStats GetBlinkMemoryCacheStats() const;
amistry9e2a8b42015-06-13 01:11:2778
amistryfaa231a42015-05-20 01:49:1279 private:
80 // Mojo IPC callback.
Ben Goodgerf180ce12018-02-09 22:54:0181 void OnRefreshDone(content::mojom::ResourceUsageDataPtr data);
amistryfaa231a42015-05-20 01:49:1282
amistry77099c22015-05-20 03:48:5583 void RunPendingRefreshCallbacks();
84
Julie Jeongeun Kim3930c632019-09-12 02:26:4785 mojo::Remote<content::mojom::ResourceUsageReporter> service_;
amistryfaa231a42015-05-20 01:49:1286 bool update_in_progress_;
Alexander Cooperbc87af32020-07-15 15:59:4587 base::circular_deque<base::OnceClosure> refresh_callbacks_;
amistryfaa231a42015-05-20 01:49:1288
Ben Goodgerf180ce12018-02-09 22:54:0189 content::mojom::ResourceUsageDataPtr stats_;
amistryfaa231a42015-05-20 01:49:1290
amistryfaa231a42015-05-20 01:49:1291 base::ThreadChecker thread_checker_;
92
93 DISALLOW_COPY_AND_ASSIGN(ProcessResourceUsage);
94};
95
96#endif // CHROME_BROWSER_PROCESS_RESOURCE_USAGE_H_