blob: 7a995027a9b004e98121ef71f3a72733240a19bb [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 <deque>
11
amistry77099c22015-05-20 03:48:5512#include "base/callback.h"
avib896c712015-12-26 02:10:4313#include "base/macros.h"
amistryfaa231a42015-05-20 01:49:1214#include "base/threading/thread_checker.h"
15#include "chrome/common/resource_usage_reporter.mojom.h"
amistry9e2a8b42015-06-13 01:11:2716#include "third_party/WebKit/public/web/WebCache.h"
amistryfaa231a42015-05-20 01:49:1217
18// Provides resource usage information about a child process.
19//
leon.hanb4c770762016-04-06 05:52:0420// This is a wrapper around the mojom::ResourceUsageReporter Mojo service that
21// exposes
amistryfaa231a42015-05-20 01:49:1222// information about resources used by a child process. Currently, this is only
amistry9e2a8b42015-06-13 01:11:2723// 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
asvitkine2c4b4d1a2016-03-19 14:18:0725// manager.
amistryfaa231a42015-05-20 01:49:1226//
27// To create:
leon.hanb4c770762016-04-06 05:52:0428// 1. Create a mojom::ResourceUsageReporterPtr and obtain an InterfaceRequest<>
29// using
amistry56de0e52015-05-21 04:31:4230// 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.hanb4c770762016-04-06 05:52:0436// 3. Pass the mojom::ResourceUsageReporterPtr to the constructor.
amistry56de0e52015-05-21 04:31:4237//
38// Example:
39// void Foo::ConnectToService(
leon.hanb4c770762016-04-06 05:52:0440// mojo::InterfaceRequest<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// ...
leon.hanb4c770762016-04-06 05:52:0446// mojom::ResourceUsageReporterPtr service;
47// mojo::InterfaceRequest<mojom::ResourceUsageReporter> request =
amistry56de0e52015-05-21 04:31:4248// mojo::GetProxy(&service);
49// content::BrowserThread::PostTask(
50// content::BrowserThread::IO, FROM_HERE,
51// 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|.
leon.hanb4c770762016-04-06 05:52:0460 explicit ProcessResourceUsage(mojom::ResourceUsageReporterPtr service);
amistryfaa231a42015-05-20 01:49:1261 ~ProcessResourceUsage();
62
amistry77099c22015-05-20 03:48:5563 // 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);
amistryfaa231a42015-05-20 01:49:1266
67 // Get V8 memory usage information.
68 bool ReportsV8MemoryStats() const;
69 size_t GetV8MemoryAllocated() const;
70 size_t GetV8MemoryUsed() const;
71
amistry9e2a8b42015-06-13 01:11:2772 // Get Blink resource cache information.
73 blink::WebCache::ResourceTypeStats GetWebCoreCacheStats() const;
74
amistryfaa231a42015-05-20 01:49:1275 private:
76 // Mojo IPC callback.
leon.hanb4c770762016-04-06 05:52:0477 void OnRefreshDone(mojom::ResourceUsageDataPtr data);
amistryfaa231a42015-05-20 01:49:1278
amistry77099c22015-05-20 03:48:5579 void RunPendingRefreshCallbacks();
80
leon.hanb4c770762016-04-06 05:52:0481 mojom::ResourceUsageReporterPtr service_;
amistryfaa231a42015-05-20 01:49:1282 bool update_in_progress_;
amistry77099c22015-05-20 03:48:5583 std::deque<base::Closure> refresh_callbacks_;
amistryfaa231a42015-05-20 01:49:1284
leon.hanb4c770762016-04-06 05:52:0485 mojom::ResourceUsageDataPtr stats_;
amistryfaa231a42015-05-20 01:49:1286
amistryfaa231a42015-05-20 01:49:1287 base::ThreadChecker thread_checker_;
88
89 DISALLOW_COPY_AND_ASSIGN(ProcessResourceUsage);
90};
91
92#endif // CHROME_BROWSER_PROCESS_RESOURCE_USAGE_H_