1 // Copyright 2015 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_PROCESS_RESOURCE_USAGE_H_
6 #define CHROME_BROWSER_PROCESS_RESOURCE_USAGE_H_
10 #include "base/containers/circular_deque.h"
11 #include "base/functional/callback.h"
12 #include "base/threading/thread_checker.h"
13 #include "content/public/common/resource_usage_reporter.mojom.h"
14 #include "mojo/public/cpp/bindings/pending_remote.h"
15 #include "mojo/public/cpp/bindings/remote.h"
16 #include "third_party/blink/public/common/web_cache/web_cache_resource_type_stats.h"
18 // Provides resource usage information about a child process.
20 // This is a wrapper around the content::mojom::ResourceUsageReporter Mojo
21 // service that exposes
22 // information about resources used by a child process. Currently, this is only
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
28 // 1. Create a mojo::PendingRemote<content::mojom::ResourceUsageReporter> and
29 // obtain a mojo::PendingReceiver<> using InitWithNewPipeAndPassReceiver().
30 // 2. Use the child process's service registry to connect to the service using
31 // the mojo::PendingReceiver<>. Note, ServiceRegistry is thread hostile and
32 // must always be accessed from the same thread. However, PendingReceiver<>
33 // can be passed safely between threads, and therefore a task can be posted
34 // to the ServiceRegistry thread to connect to the remote service.
35 // 3. Pass the mojo::PendingRemote<content::mojom::ResourceUsageReporter> to the
39 // void Foo::ConnectToService(
40 // mojo::PendingReceiver<content::mojom::ResourceUsageReporter>
42 // content::ServiceRegistry* registry = host_->GetServiceRegistry();
43 // registry->ConnectToRemoteService(std::move(req));
47 // mojo::PendingRemote<content::mojom::ResourceUsageReporter> service;
48 // mojo::PendingReceiver<content::mojom::ResourceUsageReporter> receiver =
49 // service.InitWithNewPipeAndPassReceiver();
50 // content::GetIOThreadTaskRunner({})->PostTask(
52 // base::BindOnce(&Foo::ConnectToService, this,
53 // base::Passed(&receiver)));
54 // resource_usage_.reset(new ProcessResourceUsage(std::move(service)));
57 // Note: ProcessResourceUsage is thread-hostile and must live on a single
59 class ProcessResourceUsage {
61 // Must be called from the same thread that created |service|.
62 explicit ProcessResourceUsage(
63 mojo::PendingRemote<content::mojom::ResourceUsageReporter> service);
65 ProcessResourceUsage(const ProcessResourceUsage&) = delete;
66 ProcessResourceUsage& operator=(const ProcessResourceUsage&) = delete;
68 ~ProcessResourceUsage();
70 // Refresh the resource usage information. |callback| is invoked when the
71 // usage data is updated, or when the IPC connection is lost.
72 void Refresh(base::OnceClosure callback);
74 // Get V8 memory usage information.
75 bool ReportsV8MemoryStats() const;
76 size_t GetV8MemoryAllocated() const;
77 size_t GetV8MemoryUsed() const;
79 // Get Blink resource cache information.
80 blink::WebCacheResourceTypeStats GetBlinkMemoryCacheStats() const;
84 void OnRefreshDone(content::mojom::ResourceUsageDataPtr data);
86 void RunPendingRefreshCallbacks();
88 mojo::Remote<content::mojom::ResourceUsageReporter> service_;
89 bool update_in_progress_;
90 base::circular_deque<base::OnceClosure> refresh_callbacks_;
92 content::mojom::ResourceUsageDataPtr stats_;
94 base::ThreadChecker thread_checker_;
97 #endif // CHROME_BROWSER_PROCESS_RESOURCE_USAGE_H_