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_
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 template<typename Type
>
25 struct DefaultSingletonTraits
;
26 class PrefRegistrySimple
;
30 class WebCacheManager
: public content::NotificationObserver
{
31 friend class WebCacheManagerTest
;
32 FRIEND_TEST_ALL_PREFIXES(
34 CallRemoveRendererAndObserveActivityInAnyOrderShouldNotCrashTest_1
);
35 FRIEND_TEST_ALL_PREFIXES(
37 CallRemoveRendererAndObserveActivityInAnyOrderShouldNotCrashTest_2
);
38 FRIEND_TEST_ALL_PREFIXES(
40 CallRemoveRendererAndObserveActivityInAnyOrderShouldNotCrashTest_3
);
41 FRIEND_TEST_ALL_PREFIXES(
43 CallRemoveRendererAndObserveActivityInAnyOrderShouldNotCrashTest_4
);
44 FRIEND_TEST_ALL_PREFIXES(
46 CallRemoveRendererAndObserveActivityInAnyOrderShouldNotCrashTest_5
);
47 FRIEND_TEST_ALL_PREFIXES(
49 CallRemoveRendererAndObserveActivityInAnyOrderShouldNotCrashTest_6
);
52 // Gets the singleton WebCacheManager object. The first time this method
53 // is called, a WebCacheManager object is constructed and returned.
54 // Subsequent calls will return the same object.
55 static WebCacheManager
* GetInstance();
57 // When a render process is created, it registers itself with the cache
58 // manager host, causing the renderer to be allocated cache resources.
59 void Add(int renderer_id
);
61 // When a render process ends, it removes itself from the cache manager host,
62 // freeing the manager to assign its cache resources to other renderers.
63 void Remove(int renderer_id
);
65 // The cache manager assigns more cache resources to active renderer. When a
66 // renderer is active, it should inform the cache manager to receive more
69 // When a renderer moves from being inactive to being active, the cache
70 // manager may decide to adjust its resource allocation, but it will delay
71 // the recalculation, allowing ObserveActivity to return quickly.
72 void ObserveActivity(int renderer_id
);
74 // Periodically, renderers should inform the cache manager of their current
75 // statistics. The more up-to-date the cache manager's statistics, the
76 // better it can allocate cache resources.
78 int renderer_id
, const blink::WebCache::UsageStats
& stats
);
80 // The global limit on the number of bytes in all the in-memory caches.
81 size_t global_size_limit() const { return global_size_limit_
; }
83 // Sets the global size limit, forcing a recalculation of cache allocations.
84 void SetGlobalSizeLimit(size_t bytes
);
86 // Clears all in-memory caches.
89 // Instantly clears renderer cache for a process.
90 void ClearCacheForProcess(int process_id
);
92 // Clears all in-memory caches when a tab is reloaded or the user navigates
93 // to a different website.
94 void ClearCacheOnNavigation();
96 // content::NotificationObserver implementation:
97 void Observe(int type
,
98 const content::NotificationSource
& source
,
99 const content::NotificationDetails
& details
) override
;
101 // Gets the default global size limit. This interrogates system metrics to
102 // tune the default size to the current system.
103 static size_t GetDefaultGlobalSizeLimit();
106 // The amount of idle time before we consider a tab to be "inactive"
107 static const int kRendererInactiveThresholdMinutes
= 5;
109 // Keep track of some renderer information.
110 struct RendererInfo
: blink::WebCache::UsageStats
{
111 // The access time for this renderer.
115 typedef std::map
<int, RendererInfo
> StatsMap
;
117 // An allocation is the number of bytes a specific renderer should use for
119 typedef std::pair
<int,size_t> Allocation
;
121 // An allocation strategy is a list of allocations specifying the resources
122 // each renderer is permitted to consume for its cache.
123 typedef std::list
<Allocation
> AllocationStrategy
;
125 // This class is a singleton. Do not instantiate directly.
127 friend struct DefaultSingletonTraits
<WebCacheManager
>;
129 ~WebCacheManager() override
;
131 // Recomputes the allocation of cache resources among the renderers. Also
132 // informs the renderers of their new allocation.
133 void ReviseAllocationStrategy();
135 // Schedules a call to ReviseAllocationStrategy after a short delay.
136 void ReviseAllocationStrategyLater();
138 // The various tactics used as part of an allocation strategy. To decide
139 // how many resources a given renderer should be allocated, we consider its
140 // usage statistics. Each tactic specifies the function that maps usage
141 // statistics to resource allocations.
143 // Determining a resource allocation strategy amounts to picking a tactic
144 // for each renderer and checking that the total memory required fits within
145 // our |global_size_limit_|.
146 enum AllocationTactic
{
147 // Ignore cache statistics and divide resources equally among the given
151 // Allow each renderer to keep its current set of cached resources, with
152 // some extra allocation to store new objects.
153 KEEP_CURRENT_WITH_HEADROOM
,
155 // Allow each renderer to keep its current set of cached resources.
158 // Allow each renderer to keep cache resources it believes are currently
159 // being used, with some extra allocation to store new objects.
160 KEEP_LIVE_WITH_HEADROOM
,
162 // Allow each renderer to keep cache resources it believes are currently
163 // being used, but instruct the renderer to discard all other data.
167 // Helper functions for devising an allocation strategy
169 // Add up all the stats from the given set of renderers and place the result
171 void GatherStats(const std::set
<int>& renderers
,
172 blink::WebCache::UsageStats
* stats
);
174 // Get the amount of memory that would be required to implement |tactic|
175 // using the specified allocation tactic. This function defines the
176 // semantics for each of the tactics.
177 static size_t GetSize(AllocationTactic tactic
,
178 const blink::WebCache::UsageStats
& stats
);
180 // Attempt to use the specified tactics to compute an allocation strategy
181 // and place the result in |strategy|. |active_stats| and |inactive_stats|
182 // are the aggregate statistics for |active_renderers_| and
183 // |inactive_renderers_|, respectively.
185 // Returns |true| on success and |false| on failure. Does not modify
186 // |strategy| on failure.
187 bool AttemptTactic(AllocationTactic active_tactic
,
188 const blink::WebCache::UsageStats
& active_stats
,
189 AllocationTactic inactive_tactic
,
190 const blink::WebCache::UsageStats
& inactive_stats
,
191 AllocationStrategy
* strategy
);
193 // For each renderer in |renderers|, computes its allocation according to
194 // |tactic| and add the result to |strategy|. Any |extra_bytes_to_allocate|
195 // is divided evenly among the renderers.
196 void AddToStrategy(const std::set
<int>& renderers
,
197 AllocationTactic tactic
,
198 size_t extra_bytes_to_allocate
,
199 AllocationStrategy
* strategy
);
201 // Enact an allocation strategy by informing the renderers of their
202 // allocations according to |strategy|.
203 void EnactStrategy(const AllocationStrategy
& strategy
);
205 enum ClearCacheOccasion
{
206 // Instructs to clear the cache instantly.
208 // Instructs to clear the cache when a navigation takes place (this
209 // includes reloading a tab).
213 // Inform all |renderers| to clear their cache.
214 void ClearRendererCache(const std::set
<int>& renderers
,
215 ClearCacheOccasion occation
);
217 // Check to see if any active renderers have fallen inactive.
218 void FindInactiveRenderers();
220 // The global size limit for all in-memory caches.
221 size_t global_size_limit_
;
223 // Maps every renderer_id our most recent copy of its statistics.
226 // Every renderer we think is still around is in one of these two sets.
228 // Active renderers are those renderers that have been active more recently
229 // than they have been inactive.
230 std::set
<int> active_renderers_
;
231 // Inactive renderers are those renderers that have been inactive more
232 // recently than they have been active.
233 std::set
<int> inactive_renderers_
;
235 content::NotificationRegistrar registrar_
;
237 base::WeakPtrFactory
<WebCacheManager
> weak_factory_
;
239 DISALLOW_COPY_AND_ASSIGN(WebCacheManager
);
242 } // namespace web_cache
244 #endif // COMPONENTS_WEB_CACHE_BROWSER_WEB_CACHE_MANAGER_H_