1 // Copyright (c) 2011 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. 4 5 // This is the browser side of the cache manager, it tracks the activity of the 6 // render processes and allocates available memory cache resources. 7 8 #ifndef CHROME_BROWSER_RENDERER_HOST_WEB_CACHE_MANAGER_H_ 9 #define CHROME_BROWSER_RENDERER_HOST_WEB_CACHE_MANAGER_H_ 10 11 #include <list> 12 #include <map> 13 #include <set> 14 15 #include "base/basictypes.h" 16 #include "base/compiler_specific.h" 17 #include "base/gtest_prod_util.h" 18 #include "base/memory/weak_ptr.h" 19 #include "base/time/time.h" 20 #include "content/public/browser/notification_observer.h" 21 #include "content/public/browser/notification_registrar.h" 22 #include "third_party/WebKit/public/web/WebCache.h" 23 24 template<typename Type> 25 struct DefaultSingletonTraits; 26 class PrefRegistrySimple; 27 28 class WebCacheManager : public content::NotificationObserver { 29 friend class WebCacheManagerTest; 30 FRIEND_TEST_ALL_PREFIXES(WebCacheManagerBrowserTest, CrashOnceOnly); 31 32 public: 33 static void RegisterPrefs(PrefRegistrySimple* registry); 34 35 // Gets the singleton WebCacheManager object. The first time this method 36 // is called, a WebCacheManager object is constructed and returned. 37 // Subsequent calls will return the same object. 38 static WebCacheManager* GetInstance(); 39 40 // When a render process is created, it registers itself with the cache 41 // manager host, causing the renderer to be allocated cache resources. 42 void Add(int renderer_id); 43 44 // When a render process ends, it removes itself from the cache manager host, 45 // freeing the manager to assign its cache resources to other renderers. 46 void Remove(int renderer_id); 47 48 // The cache manager assigns more cache resources to active renderer. When a 49 // renderer is active, it should inform the cache manager to receive more 50 // cache resources. 51 // 52 // When a renderer moves from being inactive to being active, the cache 53 // manager may decide to adjust its resource allocation, but it will delay 54 // the recalculation, allowing ObserveActivity to return quickly. 55 void ObserveActivity(int renderer_id); 56 57 // Periodically, renderers should inform the cache manager of their current 58 // statistics. The more up-to-date the cache manager's statistics, the 59 // better it can allocate cache resources. 60 void ObserveStats( 61 int renderer_id, const blink::WebCache::UsageStats& stats); 62 63 // The global limit on the number of bytes in all the in-memory caches. 64 size_t global_size_limit() const { return global_size_limit_; } 65 66 // Sets the global size limit, forcing a recalculation of cache allocations. 67 void SetGlobalSizeLimit(size_t bytes); 68 69 // Clears all in-memory caches. 70 void ClearCache(); 71 72 // Clears all in-memory caches when a tab is reloaded or the user navigates 73 // to a different website. 74 void ClearCacheOnNavigation(); 75 76 // content::NotificationObserver implementation: 77 virtual void Observe(int type, 78 const content::NotificationSource& source, 79 const content::NotificationDetails& details) OVERRIDE; 80 81 // Gets the default global size limit. This interrogates system metrics to 82 // tune the default size to the current system. 83 static size_t GetDefaultGlobalSizeLimit(); 84 85 protected: 86 // The amount of idle time before we consider a tab to be "inactive" 87 static const int kRendererInactiveThresholdMinutes = 5; 88 89 // Keep track of some renderer information. 90 struct RendererInfo : blink::WebCache::UsageStats { 91 // The access time for this renderer. 92 base::Time access; 93 }; 94 95 typedef std::map<int, RendererInfo> StatsMap; 96 97 // An allocation is the number of bytes a specific renderer should use for 98 // its cache. 99 typedef std::pair<int,size_t> Allocation; 100 101 // An allocation strategy is a list of allocations specifying the resources 102 // each renderer is permitted to consume for its cache. 103 typedef std::list<Allocation> AllocationStrategy; 104 105 // This class is a singleton. Do not instantiate directly. 106 WebCacheManager(); 107 friend struct DefaultSingletonTraits<WebCacheManager>; 108 109 virtual ~WebCacheManager(); 110 111 // Recomputes the allocation of cache resources among the renderers. Also 112 // informs the renderers of their new allocation. 113 void ReviseAllocationStrategy(); 114 115 // Schedules a call to ReviseAllocationStrategy after a short delay. 116 void ReviseAllocationStrategyLater(); 117 118 // The various tactics used as part of an allocation strategy. To decide 119 // how many resources a given renderer should be allocated, we consider its 120 // usage statistics. Each tactic specifies the function that maps usage 121 // statistics to resource allocations. 122 // 123 // Determining a resource allocation strategy amounts to picking a tactic 124 // for each renderer and checking that the total memory required fits within 125 // our |global_size_limit_|. 126 enum AllocationTactic { 127 // Ignore cache statistics and divide resources equally among the given 128 // set of caches. 129 DIVIDE_EVENLY, 130 131 // Allow each renderer to keep its current set of cached resources, with 132 // some extra allocation to store new objects. 133 KEEP_CURRENT_WITH_HEADROOM, 134 135 // Allow each renderer to keep its current set of cached resources. 136 KEEP_CURRENT, 137 138 // Allow each renderer to keep cache resources it believes are currently 139 // being used, with some extra allocation to store new objects. 140 KEEP_LIVE_WITH_HEADROOM, 141 142 // Allow each renderer to keep cache resources it believes are currently 143 // being used, but instruct the renderer to discard all other data. 144 KEEP_LIVE, 145 }; 146 147 // Helper functions for devising an allocation strategy 148 149 // Add up all the stats from the given set of renderers and place the result 150 // in |stats|. 151 void GatherStats(const std::set<int>& renderers, 152 blink::WebCache::UsageStats* stats); 153 154 // Get the amount of memory that would be required to implement |tactic| 155 // using the specified allocation tactic. This function defines the 156 // semantics for each of the tactics. 157 static size_t GetSize(AllocationTactic tactic, 158 const blink::WebCache::UsageStats& stats); 159 160 // Attempt to use the specified tactics to compute an allocation strategy 161 // and place the result in |strategy|. |active_stats| and |inactive_stats| 162 // are the aggregate statistics for |active_renderers_| and 163 // |inactive_renderers_|, respectively. 164 // 165 // Returns |true| on success and |false| on failure. Does not modify 166 // |strategy| on failure. 167 bool AttemptTactic(AllocationTactic active_tactic, 168 const blink::WebCache::UsageStats& active_stats, 169 AllocationTactic inactive_tactic, 170 const blink::WebCache::UsageStats& inactive_stats, 171 AllocationStrategy* strategy); 172 173 // For each renderer in |renderers|, computes its allocation according to 174 // |tactic| and add the result to |strategy|. Any |extra_bytes_to_allocate| 175 // is divided evenly among the renderers. 176 void AddToStrategy(const std::set<int>& renderers, 177 AllocationTactic tactic, 178 size_t extra_bytes_to_allocate, 179 AllocationStrategy* strategy); 180 181 // Enact an allocation strategy by informing the renderers of their 182 // allocations according to |strategy|. 183 void EnactStrategy(const AllocationStrategy& strategy); 184 185 enum ClearCacheOccasion { 186 // Instructs to clear the cache instantly. 187 INSTANTLY, 188 // Instructs to clear the cache when a navigation takes place (this 189 // includes reloading a tab). 190 ON_NAVIGATION 191 }; 192 193 // Inform all |renderers| to clear their cache. 194 void ClearRendererCache(const std::set<int>& renderers, 195 ClearCacheOccasion occation); 196 197 // Check to see if any active renderers have fallen inactive. 198 void FindInactiveRenderers(); 199 200 // The global size limit for all in-memory caches. 201 size_t global_size_limit_; 202 203 // Maps every renderer_id our most recent copy of its statistics. 204 StatsMap stats_; 205 206 // Every renderer we think is still around is in one of these two sets. 207 // 208 // Active renderers are those renderers that have been active more recently 209 // than they have been inactive. 210 std::set<int> active_renderers_; 211 // Inactive renderers are those renderers that have been inactive more 212 // recently than they have been active. 213 std::set<int> inactive_renderers_; 214 215 base::WeakPtrFactory<WebCacheManager> weak_factory_; 216 217 content::NotificationRegistrar registrar_; 218 219 DISALLOW_COPY_AND_ASSIGN(WebCacheManager); 220 }; 221 222 #endif // CHROME_BROWSER_RENDERER_HOST_WEB_CACHE_MANAGER_H_ 223