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.
5 #ifndef CHROME_BROWSER_PROCESS_RESOURCE_USAGE_H_
6 #define CHROME_BROWSER_PROCESS_RESOURCE_USAGE_H_
10 #include "base/basictypes.h"
11 #include "base/callback.h"
12 #include "base/threading/thread_checker.h"
13 #include "chrome/common/resource_usage_reporter.mojom.h"
14 #include "third_party/WebKit/public/web/WebCache.h"
16 // Provides resource usage information about a child process.
18 // This is a wrapper around the ResourceUsageReporter Mojo service that exposes
19 // information about resources used by a child process. Currently, this is only
20 // V8 memory and Blink resource cache usage, but could be expanded to include
21 // other resources. This is intended for status viewers such as the task
22 // manager and about://memory-internals.
25 // 1. Create a ResourceUsageReporterPtr and obtain an InterfaceRequest<> using
27 // 2. Use the child process's service registry to connect to the service using
28 // the InterfaceRequest<>. Note, ServiceRegistry is thread hostile and
29 // must always be accessed from the same thread. However, InterfaceRequest<>
30 // can be passed safely between threads, and therefore a task can be posted
31 // to the ServiceRegistry thread to connect to the remote service.
32 // 3. Pass the ResourceUsageReporterPtr to the constructor.
35 // void Foo::ConnectToService(
36 // mojo::InterfaceRequest<ResourceUsageReporter> req) {
37 // content::ServiceRegistry* registry = host_->GetServiceRegistry();
38 // registry->ConnectToRemoteService(req.Pass());
42 // ResourceUsageReporterPtr service;
43 // mojo::InterfaceRequest<ResourceUsageReporter> request =
44 // mojo::GetProxy(&service);
45 // content::BrowserThread::PostTask(
46 // content::BrowserThread::IO, FROM_HERE,
47 // base::Bind(&Foo::ConnectToService, this, base::Passed(&request)));
48 // resource_usage_.reset(new ProcessResourceUsage(service.Pass()));
51 // Note: ProcessResourceUsage is thread-hostile and must live on a single
53 class ProcessResourceUsage
{
55 // Must be called from the same thread that created |service|.
56 explicit ProcessResourceUsage(ResourceUsageReporterPtr service
);
57 ~ProcessResourceUsage();
59 // Refresh the resource usage information. |callback| is invoked when the
60 // usage data is updated, or when the IPC connection is lost.
61 void Refresh(const base::Closure
& callback
);
63 // Get V8 memory usage information.
64 bool ReportsV8MemoryStats() const;
65 size_t GetV8MemoryAllocated() const;
66 size_t GetV8MemoryUsed() const;
68 // Get Blink resource cache information.
69 blink::WebCache::ResourceTypeStats
GetWebCoreCacheStats() const;
73 void OnRefreshDone(ResourceUsageDataPtr data
);
75 void RunPendingRefreshCallbacks();
77 ResourceUsageReporterPtr service_
;
78 bool update_in_progress_
;
79 std::deque
<base::Closure
> refresh_callbacks_
;
81 ResourceUsageDataPtr stats_
;
83 base::ThreadChecker thread_checker_
;
85 DISALLOW_COPY_AND_ASSIGN(ProcessResourceUsage
);
88 #endif // CHROME_BROWSER_PROCESS_RESOURCE_USAGE_H_