OLD | NEW |
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 "chrome/browser/renderer_host/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/memory/singleton.h" | 11 #include "base/memory/singleton.h" |
12 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
13 #include "base/metrics/histogram.h" | 13 #include "base/metrics/histogram.h" |
14 #include "base/prefs/pref_registry_simple.h" | 14 #include "base/prefs/pref_registry_simple.h" |
15 #include "base/prefs/pref_service.h" | 15 #include "base/prefs/pref_service.h" |
16 #include "base/sys_info.h" | 16 #include "base/sys_info.h" |
17 #include "base/time/time.h" | 17 #include "base/time/time.h" |
18 #include "chrome/browser/browser_process.h" | 18 #include "components/web_cache/common/web_cache_messages.h" |
19 #include "chrome/browser/chrome_notification_types.h" | |
20 #include "chrome/common/chrome_constants.h" | |
21 #include "chrome/common/pref_names.h" | |
22 #include "chrome/common/render_messages.h" | |
23 #include "content/public/browser/notification_service.h" | 19 #include "content/public/browser/notification_service.h" |
| 20 #include "content/public/browser/notification_types.h" |
24 #include "content/public/browser/render_process_host.h" | 21 #include "content/public/browser/render_process_host.h" |
25 | 22 |
26 using base::Time; | 23 using base::Time; |
27 using base::TimeDelta; | 24 using base::TimeDelta; |
28 using blink::WebCache; | 25 using blink::WebCache; |
29 | 26 |
| 27 namespace web_cache { |
| 28 |
30 static const int kReviseAllocationDelayMS = 200; | 29 static const int kReviseAllocationDelayMS = 200; |
31 | 30 |
32 // The default size limit of the in-memory cache is 8 MB | 31 // The default size limit of the in-memory cache is 8 MB |
33 static const int kDefaultMemoryCacheSize = 8 * 1024 * 1024; | 32 static const int kDefaultMemoryCacheSize = 8 * 1024 * 1024; |
34 | 33 |
35 namespace { | 34 namespace { |
36 | 35 |
37 int GetDefaultCacheSize() { | 36 int GetDefaultCacheSize() { |
38 // Start off with a modest default | 37 // Start off with a modest default |
39 int default_cache_size = kDefaultMemoryCacheSize; | 38 int default_cache_size = kDefaultMemoryCacheSize; |
40 | 39 |
41 // Check how much physical memory the OS has | 40 // Check how much physical memory the OS has |
42 int mem_size_mb = base::SysInfo::AmountOfPhysicalMemoryMB(); | 41 int mem_size_mb = base::SysInfo::AmountOfPhysicalMemoryMB(); |
43 if (mem_size_mb >= 1000) // If we have a GB of memory, set a larger default. | 42 if (mem_size_mb >= 1000) // If we have a GB of memory, set a larger default. |
44 default_cache_size *= 4; | 43 default_cache_size *= 4; |
45 else if (mem_size_mb >= 512) // With 512 MB, set a slightly larger default. | 44 else if (mem_size_mb >= 512) // With 512 MB, set a slightly larger default. |
46 default_cache_size *= 2; | 45 default_cache_size *= 2; |
47 | 46 |
48 UMA_HISTOGRAM_MEMORY_MB("Cache.MaxCacheSizeMB", | 47 UMA_HISTOGRAM_MEMORY_MB("Cache.MaxCacheSizeMB", |
49 default_cache_size / 1024 / 1024); | 48 default_cache_size / 1024 / 1024); |
50 | 49 |
51 return default_cache_size; | 50 return default_cache_size; |
52 } | 51 } |
53 | 52 |
54 } // anonymous namespace | 53 } // anonymous namespace |
55 | 54 |
56 // static | 55 // static |
57 void WebCacheManager::RegisterPrefs(PrefRegistrySimple* registry) { | |
58 registry->RegisterIntegerPref(prefs::kMemoryCacheSize, GetDefaultCacheSize()); | |
59 } | |
60 | |
61 // static | |
62 WebCacheManager* WebCacheManager::GetInstance() { | 56 WebCacheManager* WebCacheManager::GetInstance() { |
63 return Singleton<WebCacheManager>::get(); | 57 return Singleton<WebCacheManager>::get(); |
64 } | 58 } |
65 | 59 |
66 WebCacheManager::WebCacheManager() | 60 WebCacheManager::WebCacheManager() |
67 : global_size_limit_(GetDefaultGlobalSizeLimit()), | 61 : global_size_limit_(GetDefaultGlobalSizeLimit()), |
68 weak_factory_(this) { | 62 weak_factory_(this) { |
69 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CREATED, | 63 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CREATED, |
70 content::NotificationService::AllBrowserContextsAndSources()); | 64 content::NotificationService::AllBrowserContextsAndSources()); |
71 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, | 65 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
176 break; | 170 break; |
177 } | 171 } |
178 default: | 172 default: |
179 NOTREACHED(); | 173 NOTREACHED(); |
180 break; | 174 break; |
181 } | 175 } |
182 } | 176 } |
183 | 177 |
184 // static | 178 // static |
185 size_t WebCacheManager::GetDefaultGlobalSizeLimit() { | 179 size_t WebCacheManager::GetDefaultGlobalSizeLimit() { |
186 PrefService* perf_service = g_browser_process->local_state(); | |
187 if (perf_service) | |
188 return perf_service->GetInteger(prefs::kMemoryCacheSize); | |
189 | |
190 return GetDefaultCacheSize(); | 180 return GetDefaultCacheSize(); |
191 } | 181 } |
192 | 182 |
193 void WebCacheManager::GatherStats(const std::set<int>& renderers, | 183 void WebCacheManager::GatherStats(const std::set<int>& renderers, |
194 WebCache::UsageStats* stats) { | 184 WebCache::UsageStats* stats) { |
195 DCHECK(stats); | 185 DCHECK(stats); |
196 | 186 |
197 memset(stats, 0, sizeof(WebCache::UsageStats)); | 187 memset(stats, 0, sizeof(WebCache::UsageStats)); |
198 | 188 |
199 std::set<int>::const_iterator iter = renderers.begin(); | 189 std::set<int>::const_iterator iter = renderers.begin(); |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
319 // prefer to keep live objects around. There is probably some performance | 309 // prefer to keep live objects around. There is probably some performance |
320 // tuning to be done here. | 310 // tuning to be done here. |
321 size_t min_dead_capacity = 0; | 311 size_t min_dead_capacity = 0; |
322 | 312 |
323 // We allow the dead objects to consume up to half of the cache capacity. | 313 // We allow the dead objects to consume up to half of the cache capacity. |
324 size_t max_dead_capacity = capacity / 2; | 314 size_t max_dead_capacity = capacity / 2; |
325 if (base::SysInfo::IsLowEndDevice()) { | 315 if (base::SysInfo::IsLowEndDevice()) { |
326 max_dead_capacity = std::min(static_cast<size_t>(512 * 1024), | 316 max_dead_capacity = std::min(static_cast<size_t>(512 * 1024), |
327 max_dead_capacity); | 317 max_dead_capacity); |
328 } | 318 } |
329 host->Send(new ChromeViewMsg_SetCacheCapacities(min_dead_capacity, | 319 host->Send(new WebCacheMsg_SetCacheCapacities(min_dead_capacity, |
330 max_dead_capacity, | 320 max_dead_capacity, |
331 capacity)); | 321 capacity)); |
332 } | 322 } |
333 ++allocation; | 323 ++allocation; |
334 } | 324 } |
335 } | 325 } |
336 | 326 |
337 void WebCacheManager::ClearRendererCache( | 327 void WebCacheManager::ClearRendererCache( |
338 const std::set<int>& renderers, | 328 const std::set<int>& renderers, |
339 WebCacheManager::ClearCacheOccasion occasion) { | 329 WebCacheManager::ClearCacheOccasion occasion) { |
340 std::set<int>::const_iterator iter = renderers.begin(); | 330 std::set<int>::const_iterator iter = renderers.begin(); |
341 for (; iter != renderers.end(); ++iter) { | 331 for (; iter != renderers.end(); ++iter) { |
342 content::RenderProcessHost* host = | 332 content::RenderProcessHost* host = |
343 content::RenderProcessHost::FromID(*iter); | 333 content::RenderProcessHost::FromID(*iter); |
344 if (host) | 334 if (host) |
345 host->Send(new ChromeViewMsg_ClearCache(occasion == ON_NAVIGATION)); | 335 host->Send(new WebCacheMsg_ClearCache(occasion == ON_NAVIGATION)); |
346 } | 336 } |
347 } | 337 } |
348 | 338 |
349 void WebCacheManager::ReviseAllocationStrategy() { | 339 void WebCacheManager::ReviseAllocationStrategy() { |
350 DCHECK(stats_.size() <= | 340 DCHECK(stats_.size() <= |
351 active_renderers_.size() + inactive_renderers_.size()); | 341 active_renderers_.size() + inactive_renderers_.size()); |
352 | 342 |
353 // Check if renderers have gone inactive. | 343 // Check if renderers have gone inactive. |
354 FindInactiveRenderers(); | 344 FindInactiveRenderers(); |
355 | 345 |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
435 if (idle >= TimeDelta::FromMinutes(kRendererInactiveThresholdMinutes)) { | 425 if (idle >= TimeDelta::FromMinutes(kRendererInactiveThresholdMinutes)) { |
436 // Moved to inactive status. This invalidates our iterator. | 426 // Moved to inactive status. This invalidates our iterator. |
437 inactive_renderers_.insert(*iter); | 427 inactive_renderers_.insert(*iter); |
438 active_renderers_.erase(*iter); | 428 active_renderers_.erase(*iter); |
439 iter = active_renderers_.begin(); | 429 iter = active_renderers_.begin(); |
440 continue; | 430 continue; |
441 } | 431 } |
442 ++iter; | 432 ++iter; |
443 } | 433 } |
444 } | 434 } |
| 435 |
| 436 } // namespace web_cache |
OLD | NEW |