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

Side by Side Diff: components/web_cache/browser/web_cache_manager.cc

Issue 1283793004: [tracing] Add WebCache memory usage in Blink to chrome://tracing (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Nits. Created 5 years, 4 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 (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "components/web_cache/browser/web_cache_manager.h" 5 #include "components/web_cache/browser/web_cache_manager.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/compiler_specific.h" 10 #include "base/compiler_specific.h"
11 #include "base/format_macros.h"
11 #include "base/location.h" 12 #include "base/location.h"
12 #include "base/memory/singleton.h" 13 #include "base/memory/singleton.h"
13 #include "base/metrics/histogram_macros.h" 14 #include "base/metrics/histogram_macros.h"
14 #include "base/prefs/pref_registry_simple.h" 15 #include "base/prefs/pref_registry_simple.h"
15 #include "base/prefs/pref_service.h" 16 #include "base/prefs/pref_service.h"
16 #include "base/single_thread_task_runner.h" 17 #include "base/single_thread_task_runner.h"
18 #include "base/strings/stringprintf.h"
17 #include "base/sys_info.h" 19 #include "base/sys_info.h"
18 #include "base/thread_task_runner_handle.h" 20 #include "base/thread_task_runner_handle.h"
19 #include "base/time/time.h" 21 #include "base/time/time.h"
22 #include "base/trace_event/memory_allocator_dump.h"
23 #include "base/trace_event/memory_dump_manager.h"
24 #include "base/trace_event/process_memory_dump.h"
20 #include "components/web_cache/common/web_cache_messages.h" 25 #include "components/web_cache/common/web_cache_messages.h"
26 #include "content/common/child_process_host_impl.h"
21 #include "content/public/browser/notification_service.h" 27 #include "content/public/browser/notification_service.h"
22 #include "content/public/browser/notification_types.h" 28 #include "content/public/browser/notification_types.h"
23 #include "content/public/browser/render_process_host.h" 29 #include "content/public/browser/render_process_host.h"
24 30
25 using base::Time; 31 using base::Time;
26 using base::TimeDelta; 32 using base::TimeDelta;
27 using blink::WebCache; 33 using blink::WebCache;
28 34
29 namespace web_cache { 35 namespace web_cache {
30 36
(...skipping 28 matching lines...) Expand all
59 return Singleton<WebCacheManager>::get(); 65 return Singleton<WebCacheManager>::get();
60 } 66 }
61 67
62 WebCacheManager::WebCacheManager() 68 WebCacheManager::WebCacheManager()
63 : global_size_limit_(GetDefaultGlobalSizeLimit()), 69 : global_size_limit_(GetDefaultGlobalSizeLimit()),
64 weak_factory_(this) { 70 weak_factory_(this) {
65 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CREATED, 71 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CREATED,
66 content::NotificationService::AllBrowserContextsAndSources()); 72 content::NotificationService::AllBrowserContextsAndSources());
67 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, 73 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED,
68 content::NotificationService::AllBrowserContextsAndSources()); 74 content::NotificationService::AllBrowserContextsAndSources());
75 base::trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider(
76 this);
Primiano Tucci (use gerrit) 2015/08/12 10:04:45 don't you need thread runner affinity here?
69 } 77 }
70 78
71 WebCacheManager::~WebCacheManager() { 79 WebCacheManager::~WebCacheManager() {
80 base::trace_event::MemoryDumpManager::GetInstance()->UnregisterDumpProvider(
81 this);
72 } 82 }
73 83
74 void WebCacheManager::Add(int renderer_id) { 84 void WebCacheManager::Add(int renderer_id) {
75 DCHECK(inactive_renderers_.count(renderer_id) == 0); 85 DCHECK(inactive_renderers_.count(renderer_id) == 0);
76 86
77 // It is tempting to make the following DCHECK here, but it fails when a new 87 // It is tempting to make the following DCHECK here, but it fails when a new
78 // tab is created as we observe activity from that tab because the 88 // tab is created as we observe activity from that tab because the
79 // RenderProcessHost is recreated and adds itself. 89 // RenderProcessHost is recreated and adds itself.
80 // 90 //
81 // DCHECK(active_renderers_.count(renderer_id) == 0); 91 // DCHECK(active_renderers_.count(renderer_id) == 0);
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
170 content::Source<content::RenderProcessHost>(source).ptr(); 180 content::Source<content::RenderProcessHost>(source).ptr();
171 Remove(process->GetID()); 181 Remove(process->GetID());
172 break; 182 break;
173 } 183 }
174 default: 184 default:
175 NOTREACHED(); 185 NOTREACHED();
176 break; 186 break;
177 } 187 }
178 } 188 }
179 189
190 bool WebCacheManager::OnMemoryDump(
191 const base::trace_event::MemoryDumpArgs& args,
192 base::trace_event::ProcessMemoryDump* pmd) {
193 std::set<int>::const_iterator active_renderers_iter =
194 active_renderers_.begin();
195 while (active_renderers_iter != active_renderers_.end()) {
Primiano Tucci (use gerrit) 2015/08/12 10:04:45 I think lines 193-195 could be just: for (int rend
196 DumpUsageStats(*active_renderers_iter, pmd);
197 ++active_renderers_iter;
198 }
199
200 std::set<int>::const_iterator inactive_renderers_iter =
Primiano Tucci (use gerrit) 2015/08/12 10:04:45 same here
201 inactive_renderers_.begin();
202 while (inactive_renderers_iter != inactive_renderers_.end()) {
203 DumpUsageStats(*inactive_renderers_iter, pmd);
204 ++inactive_renderers_iter;
205 }
206
207 return true;
208 }
209
210 void WebCacheManager::DumpUsageStats(
211 int renderer_id,
212 base::trace_event::ProcessMemoryDump* pmd) {
213 StatsMap::iterator stats = stats_.find(renderer_id);
214 if (stats == stats_.end())
Primiano Tucci (use gerrit) 2015/08/12 10:04:45 Are you expecting this to happen genuinely for som
215 return;
216
217 uint64 child_tracing_id =
218 content::ChildProcessHostImpl::ChildProcessUniqueIdToTracingProcessId(
219 renderer_id);
220 std::string dump_name = base::StringPrintf(
221 "web_cache/renderer_%lu", static_cast<unsigned long>(child_tracing_id));
222 base::trace_event::MemoryAllocatorDump* allocator_dump =
223 pmd->CreateAllocatorDump(dump_name);
224
225 allocator_dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize,
226 base::trace_event::MemoryAllocatorDump::kUnitsBytes,
227 stats->second.liveSize + stats->second.deadSize);
228 allocator_dump->AddScalar("live_size",
229 base::trace_event::MemoryAllocatorDump::kUnitsBytes,
230 stats->second.liveSize);
231 allocator_dump->AddScalar("dead_size",
232 base::trace_event::MemoryAllocatorDump::kUnitsBytes,
233 stats->second.deadSize);
234
235 // Generate a global GUID used to share this allocation with renderer
236 // processes.
237 auto shared_guid = base::trace_event::MemoryAllocatorDumpGuid(
238 base::StringPrintf("web-cache-x-process/%" PRIx64, child_tracing_id));
239 pmd->CreateSharedGlobalAllocatorDump(shared_guid);
240 pmd->AddOwnershipEdge(allocator_dump->guid(), shared_guid);
241 }
242
180 // static 243 // static
181 size_t WebCacheManager::GetDefaultGlobalSizeLimit() { 244 size_t WebCacheManager::GetDefaultGlobalSizeLimit() {
182 return GetDefaultCacheSize(); 245 return GetDefaultCacheSize();
183 } 246 }
184 247
185 void WebCacheManager::GatherStats(const std::set<int>& renderers, 248 void WebCacheManager::GatherStats(const std::set<int>& renderers,
186 WebCache::UsageStats* stats) { 249 WebCache::UsageStats* stats) {
187 DCHECK(stats); 250 DCHECK(stats);
188 251
189 memset(stats, 0, sizeof(WebCache::UsageStats)); 252 memset(stats, 0, sizeof(WebCache::UsageStats));
(...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after
434 inactive_renderers_.insert(*iter); 497 inactive_renderers_.insert(*iter);
435 active_renderers_.erase(*iter); 498 active_renderers_.erase(*iter);
436 iter = active_renderers_.begin(); 499 iter = active_renderers_.begin();
437 continue; 500 continue;
438 } 501 }
439 ++iter; 502 ++iter;
440 } 503 }
441 } 504 }
442 505
443 } // namespace web_cache 506 } // namespace web_cache
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698