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 CHROME_BROWSER_RENDERER_HOST_WEB_CACHE_MANAGER_H_
9 #define CHROME_BROWSER_RENDERER_HOST_WEB_CACHE_MANAGER_H_
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.h"
20 #include "content/public/browser/notification_observer.h"
21 #include "content/public/browser/notification_registrar.h"
22 #include "third_party/WebKit/Source/WebKit/chromium/public/WebCache.h"
24 template<typename Type
>
25 struct DefaultSingletonTraits
;
26 class PrefRegistrySimple
;
28 class WebCacheManager
: public content::NotificationObserver
{
29 friend class WebCacheManagerTest
;
30 FRIEND_TEST_ALL_PREFIXES(WebCacheManagerBrowserTest
, CrashOnceOnly
);
33 static void RegisterPrefs(PrefRegistrySimple
* registry
);
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();
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
);
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
);
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
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
);
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.
61 int renderer_id
, const WebKit::WebCache::UsageStats
& stats
);
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_
; }
66 // Sets the global size limit, forcing a recalculation of cache allocations.
67 void SetGlobalSizeLimit(size_t bytes
);
69 // Clears all in-memory caches.
72 // Clears all in-memory caches when a tab is reloaded or the user navigates
73 // to a different website.
74 void ClearCacheOnNavigation();
76 // content::NotificationObserver implementation:
77 virtual void Observe(int type
,
78 const content::NotificationSource
& source
,
79 const content::NotificationDetails
& details
) OVERRIDE
;
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();
86 // The amount of idle time before we consider a tab to be "inactive"
87 static const int kRendererInactiveThresholdMinutes
= 5;
89 // Keep track of some renderer information.
90 struct RendererInfo
: WebKit::WebCache::UsageStats
{
91 // The access time for this renderer.
95 typedef std::map
<int, RendererInfo
> StatsMap
;
97 // An allocation is the number of bytes a specific renderer should use for
99 typedef std::pair
<int,size_t> Allocation
;
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
;
105 // This class is a singleton. Do not instantiate directly.
107 friend struct DefaultSingletonTraits
<WebCacheManager
>;
109 virtual ~WebCacheManager();
111 // Recomputes the allocation of cache resources among the renderers. Also
112 // informs the renderers of their new allocation.
113 void ReviseAllocationStrategy();
115 // Schedules a call to ReviseAllocationStrategy after a short delay.
116 void ReviseAllocationStrategyLater();
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.
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
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
,
135 // Allow each renderer to keep its current set of cached resources.
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
,
142 // Allow each renderer to keep cache resources it believes are currently
143 // being used, but instruct the renderer to discard all other data.
147 // Helper functions for devising an allocation strategy
149 // Add up all the stats from the given set of renderers and place the result
151 void GatherStats(const std::set
<int>& renderers
,
152 WebKit::WebCache::UsageStats
* stats
);
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 WebKit::WebCache::UsageStats
& stats
);
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.
165 // Returns |true| on success and |false| on failure. Does not modify
166 // |strategy| on failure.
167 bool AttemptTactic(AllocationTactic active_tactic
,
168 const WebKit::WebCache::UsageStats
& active_stats
,
169 AllocationTactic inactive_tactic
,
170 const WebKit::WebCache::UsageStats
& inactive_stats
,
171 AllocationStrategy
* strategy
);
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
);
181 // Enact an allocation strategy by informing the renderers of their
182 // allocations according to |strategy|.
183 void EnactStrategy(const AllocationStrategy
& strategy
);
185 enum ClearCacheOccasion
{
186 // Instructs to clear the cache instantly.
188 // Instructs to clear the cache when a navigation takes place (this
189 // includes reloading a tab).
193 // Inform all |renderers| to clear their cache.
194 void ClearRendererCache(const std::set
<int>& renderers
,
195 ClearCacheOccasion occation
);
197 // Check to see if any active renderers have fallen inactive.
198 void FindInactiveRenderers();
200 // The global size limit for all in-memory caches.
201 size_t global_size_limit_
;
203 // Maps every renderer_id our most recent copy of its statistics.
206 // Every renderer we think is still around is in one of these two sets.
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_
;
215 base::WeakPtrFactory
<WebCacheManager
> weak_factory_
;
217 content::NotificationRegistrar registrar_
;
219 DISALLOW_COPY_AND_ASSIGN(WebCacheManager
);
222 #endif // CHROME_BROWSER_RENDERER_HOST_WEB_CACHE_MANAGER_H_