Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(45)

Side by Side Diff: chrome/browser/ui/webui/memory_internals/memory_internals_proxy.cc

Issue 1081323003: Convert renderer JS memory usage reporting to use Mojo. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@utility-process-report-js-memory
Patch Set: Fix build files. Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/ui/webui/memory_internals/memory_internals_proxy.h" 5 #include "chrome/browser/ui/webui/memory_internals/memory_internals_proxy.h"
6 6
7 #include <map>
7 #include <set> 8 #include <set>
8 #include <string> 9 #include <string>
10 #include <utility>
9 #include <vector> 11 #include <vector>
10 12
11 #include "base/bind.h" 13 #include "base/bind.h"
14 #include "base/memory/linked_ptr.h"
15 #include "base/memory/weak_ptr.h"
12 #include "base/strings/string16.h" 16 #include "base/strings/string16.h"
13 #include "base/sys_info.h" 17 #include "base/sys_info.h"
14 #include "base/values.h" 18 #include "base/values.h"
15 #include "chrome/browser/browser_process.h" 19 #include "chrome/browser/browser_process.h"
16 #include "chrome/browser/chrome_notification_types.h" 20 #include "chrome/browser/chrome_notification_types.h"
17 #include "chrome/browser/memory_details.h" 21 #include "chrome/browser/memory_details.h"
18 #include "chrome/browser/prerender/prerender_manager.h" 22 #include "chrome/browser/prerender/prerender_manager.h"
19 #include "chrome/browser/prerender/prerender_manager_factory.h" 23 #include "chrome/browser/prerender/prerender_manager_factory.h"
24 #include "chrome/browser/process_resource_usage.h"
20 #include "chrome/browser/profiles/profile.h" 25 #include "chrome/browser/profiles/profile.h"
21 #include "chrome/browser/profiles/profile_manager.h" 26 #include "chrome/browser/profiles/profile_manager.h"
22 #include "chrome/browser/renderer_host/chrome_render_message_filter.h" 27 #include "chrome/browser/renderer_host/chrome_render_message_filter.h"
23 #include "chrome/browser/ui/android/tab_model/tab_model.h" 28 #include "chrome/browser/ui/android/tab_model/tab_model.h"
24 #include "chrome/browser/ui/android/tab_model/tab_model_list.h" 29 #include "chrome/browser/ui/android/tab_model/tab_model_list.h"
25 #include "chrome/browser/ui/browser.h" 30 #include "chrome/browser/ui/browser.h"
26 #include "chrome/browser/ui/browser_iterator.h" 31 #include "chrome/browser/ui/browser_iterator.h"
27 #include "chrome/browser/ui/tab_contents/tab_contents_iterator.h" 32 #include "chrome/browser/ui/tab_contents/tab_contents_iterator.h"
28 #include "chrome/browser/ui/webui/memory_internals/memory_internals_handler.h" 33 #include "chrome/browser/ui/webui/memory_internals/memory_internals_handler.h"
29 #include "chrome/common/render_messages.h" 34 #include "chrome/common/render_messages.h"
30 #include "content/public/browser/navigation_controller.h" 35 #include "content/public/browser/navigation_controller.h"
31 #include "content/public/browser/navigation_entry.h" 36 #include "content/public/browser/navigation_entry.h"
32 #include "content/public/browser/notification_observer.h"
33 #include "content/public/browser/notification_registrar.h"
34 #include "content/public/browser/notification_service.h"
35 #include "content/public/browser/render_process_host.h" 37 #include "content/public/browser/render_process_host.h"
36 #include "content/public/browser/render_view_host.h" 38 #include "content/public/browser/render_view_host.h"
37 #include "content/public/browser/web_contents.h" 39 #include "content/public/browser/web_contents.h"
38 #include "content/public/browser/web_ui.h" 40 #include "content/public/browser/web_ui.h"
41 #include "content/public/common/service_registry.h"
39 42
40 #if defined(ENABLE_PRINT_PREVIEW) 43 #if defined(ENABLE_PRINT_PREVIEW)
41 #include "chrome/browser/printing/background_printing_manager.h" 44 #include "chrome/browser/printing/background_printing_manager.h"
42 #endif 45 #endif
43 46
44 using content::BrowserThread; 47 using content::BrowserThread;
45 48
46 class Profile; 49 class Profile;
47 50
48 namespace { 51 namespace {
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
111 printing::BackgroundPrintingManager* printing_manager = 114 printing::BackgroundPrintingManager* printing_manager =
112 g_browser_process->background_printing_manager(); 115 g_browser_process->background_printing_manager();
113 std::set<content::WebContents*> printing_contents = 116 std::set<content::WebContents*> printing_contents =
114 printing_manager->CurrentContentSet(); 117 printing_manager->CurrentContentSet();
115 web_contents->insert(printing_contents.begin(), printing_contents.end()); 118 web_contents->insert(printing_contents.begin(), printing_contents.end());
116 #endif 119 #endif
117 } 120 }
118 121
119 } // namespace 122 } // namespace
120 123
121 class RendererDetails : public content::NotificationObserver { 124 class RendererDetails {
122 public: 125 public:
123 typedef base::Callback<void(const base::ProcessId pid, 126 typedef base::Callback<void(const base::ProcessId pid,
124 const size_t v8_allocated, 127 const size_t v8_allocated,
125 const size_t v8_used)> V8DataCallback; 128 const size_t v8_used)> V8DataCallback;
126 129
127 explicit RendererDetails(const V8DataCallback& callback) 130 explicit RendererDetails(const V8DataCallback& callback)
128 : callback_(callback) { 131 : callback_(callback), weak_factory_(this) {}
129 registrar_.Add(this, chrome::NOTIFICATION_RENDERER_V8_HEAP_STATS_COMPUTED, 132 ~RendererDetails() {}
130 content::NotificationService::AllSources());
131 }
132 ~RendererDetails() override {}
133 133
134 void Request() { 134 void Request() {
135 for (std::set<content::WebContents*>::iterator iter = web_contents_.begin(); 135 for (std::set<content::WebContents*>::iterator iter = web_contents_.begin();
136 iter != web_contents_.end(); ++iter) 136 iter != web_contents_.end(); ++iter) {
137 (*iter)->GetRenderViewHost()->Send(new ChromeViewMsg_GetV8HeapStats); 137 auto rph = (*iter)->GetRenderViewHost()->GetProcess();
138 auto resource_usage = resource_usage_reporters_[(*iter)];
139 DCHECK(resource_usage.get());
140 resource_usage->Refresh(base::Bind(
141 &RendererDetails::OnRefreshDone, weak_factory_.GetWeakPtr(),
142 *iter, base::GetProcId(rph->GetHandle())));
143 }
138 } 144 }
139 145
140 void AddWebContents(content::WebContents* content) { 146 void AddWebContents(content::WebContents* content) {
141 web_contents_.insert(content); 147 web_contents_.insert(content);
148
149 auto rph = content->GetRenderViewHost()->GetProcess();
150 content::ServiceRegistry* service_registry = rph->GetServiceRegistry();
151 ResourceUsageReporterPtr service;
152 if (service_registry)
153 service_registry->ConnectToRemoteService(&service);
154 resource_usage_reporters_.insert(std::make_pair(
155 content, make_linked_ptr(new ProcessResourceUsage(service.Pass()))));
156 DCHECK_EQ(web_contents_.size(), resource_usage_reporters_.size());
142 } 157 }
143 158
144 void Clear() { 159 void Clear() {
145 web_contents_.clear(); 160 web_contents_.clear();
161 resource_usage_reporters_.clear();
ncarter (slow) 2015/05/14 06:28:12 Invalidating the weak pointers here seems like a g
Anand Mistry (off Chromium) 2015/05/14 07:30:41 Done.
146 } 162 }
147 163
148 void RemoveWebContents(base::ProcessId) { 164 void RemoveWebContents(base::ProcessId) {
149 // We don't have to detect which content is the caller of this method. 165 // We don't have to detect which content is the caller of this method.
150 if (!web_contents_.empty()) 166 if (!web_contents_.empty())
151 web_contents_.erase(web_contents_.begin()); 167 web_contents_.erase(web_contents_.begin());
168 if (web_contents_.empty())
169 resource_usage_reporters_.clear();
152 } 170 }
153 171
154 int IsClean() { 172 int IsClean() {
155 return web_contents_.empty(); 173 return web_contents_.empty();
156 } 174 }
157 175
158 private: 176 private:
159 // NotificationObserver: 177 void OnRefreshDone(content::WebContents* content, const base::ProcessId pid) {
160 void Observe(int type, 178 auto iter = resource_usage_reporters_.find(content);
161 const content::NotificationSource& source, 179 DCHECK(iter != resource_usage_reporters_.end());
ncarter (slow) 2015/05/14 06:28:12 This code was pretty dicey to begin with, so we ca
Anand Mistry (off Chromium) 2015/05/14 07:30:41 I've considered this, which is why |content| is on
ncarter (slow) 2015/05/14 13:59:58 OK. I'll try to come back here myself and do somet
Anand Mistry (off Chromium) 2015/05/19 04:28:37 So, I've done some more testing and it turns out I
162 const content::NotificationDetails& details) override { 180 linked_ptr<ProcessResourceUsage> usage = iter->second;
163 const base::ProcessId* pid = 181 callback_.Run(pid, usage->GetV8MemoryAllocated(), usage->GetV8MemoryUsed());
164 content::Source<const base::ProcessId>(source).ptr();
165 const ChromeRenderMessageFilter::V8HeapStatsDetails* v8_heap =
166 content::Details<const ChromeRenderMessageFilter::V8HeapStatsDetails>(
167 details).ptr();
168 callback_.Run(*pid,
169 v8_heap->v8_memory_allocated(),
170 v8_heap->v8_memory_used());
171 } 182 }
172 183
173 V8DataCallback callback_; 184 V8DataCallback callback_;
174 content::NotificationRegistrar registrar_;
175 std::set<content::WebContents*> web_contents_; // This class does not own 185 std::set<content::WebContents*> web_contents_; // This class does not own
186 std::map<content::WebContents*, linked_ptr<ProcessResourceUsage>>
187 resource_usage_reporters_;
188
189 base::WeakPtrFactory<RendererDetails> weak_factory_;
176 190
177 DISALLOW_COPY_AND_ASSIGN(RendererDetails); 191 DISALLOW_COPY_AND_ASSIGN(RendererDetails);
178 }; 192 };
179 193
180 MemoryInternalsProxy::MemoryInternalsProxy() 194 MemoryInternalsProxy::MemoryInternalsProxy()
181 : information_(new base::DictionaryValue()), 195 : information_(new base::DictionaryValue()),
182 renderer_details_(new RendererDetails( 196 renderer_details_(new RendererDetails(
183 base::Bind(&MemoryInternalsProxy::OnRendererAvailable, this))) {} 197 base::Bind(&MemoryInternalsProxy::OnRendererAvailable, this))) {}
184 198
185 void MemoryInternalsProxy::Attach(MemoryInternalsHandler* handler) { 199 void MemoryInternalsProxy::Attach(MemoryInternalsHandler* handler) {
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after
344 const std::string& function, const base::Value& args) { 358 const std::string& function, const base::Value& args) {
345 DCHECK_CURRENTLY_ON(BrowserThread::UI); 359 DCHECK_CURRENTLY_ON(BrowserThread::UI);
346 360
347 std::vector<const base::Value*> args_vector(1, &args); 361 std::vector<const base::Value*> args_vector(1, &args);
348 base::string16 update = 362 base::string16 update =
349 content::WebUI::GetJavascriptCall(function, args_vector); 363 content::WebUI::GetJavascriptCall(function, args_vector);
350 // Don't forward updates to a destructed UI. 364 // Don't forward updates to a destructed UI.
351 if (handler_) 365 if (handler_)
352 handler_->OnUpdate(update); 366 handler_->OnUpdate(update);
353 } 367 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698