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