Fixing build: GetViewContainer changed name from under me. :)
[chromium-blink-merge.git] / chrome / browser / cache_manager_host.h
blobbff754e40b696032453944ad33fd5f68371cfccb
1 // Copyright (c) 2006-2008 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_CACHE_MANAGER_HOST_H__
9 #define CHROME_BROWSER_CACHE_MANAGER_HOST_H__
11 #include <map>
12 #include <list>
13 #include <set>
15 #include "base/basictypes.h"
16 #include "base/shared_memory.h"
17 #include "base/singleton.h"
18 #include "base/task.h"
19 #include "base/time.h"
20 #include "webkit/glue/cache_manager.h"
22 class PrefService;
23 class RenderProcessHost;
25 class CacheManagerHost {
26 // Unit tests are our friends.
27 friend class CacheManagerHostTest;
29 public:
30 static void RegisterPrefs(PrefService* prefs);
32 // Gets the singleton CacheManagerHost object. The first time this method
33 // is called, a CacheManagerHost object is constructed and returned.
34 // Subsequent calls will return the same object.
35 static CacheManagerHost* GetInstance();
37 // When a render process is created, it registers itself with the cache
38 // manager host, causing the renderer to be allocated cache resources.
39 void Add(int renderer_id);
41 // When a render process ends, it removes itself from the cache manager host,
42 // freeing the manager to assign its cache resources to other renderers.
43 void Remove(int renderer_id);
45 // The cache manager assigns more cache resources to active renderer. When a
46 // renderer is active, it should inform the cache manager to receive more
47 // cache resources.
49 // When a renderer moves from being inactive to being active, the cache
50 // manager may decide to adjust its resource allocation, but it will delay
51 // the recalculation, allowing ObserveActivity to return quickly.
52 void ObserveActivity(int renderer_id);
54 // Periodically, renderers should inform the cache manager of their current
55 // statistics. The more up-to-date the cache manager's statistics, the
56 // better it can allocate cache resources.
57 void ObserveStats(int renderer_id, const CacheManager::UsageStats& stats);
59 // The global limit on the number of bytes in all the in-memory caches.
60 size_t global_size_limit() const { return global_size_limit_; }
62 // Sets the global size limit, forcing a recalculation of cache allocations.
63 void SetGlobalSizeLimit(size_t bytes);
65 // Gets the default global size limit. This interrogates system metrics to
66 // tune the default size to the current system.
67 static size_t GetDefaultGlobalSizeLimit();
69 protected:
70 // The amount of idle time before we consider a tab to be "inactive"
71 static const TimeDelta kRendererInactiveThreshold;
73 // Keep track of some renderer information.
74 struct RendererInfo : CacheManager::UsageStats {
75 // The access time for this renderer.
76 Time access;
79 typedef std::map<int, RendererInfo> StatsMap;
81 // An allocation is the number of bytes a specific renderer should use for
82 // its cache.
83 typedef std::pair<int,size_t> Allocation;
85 // An allocation strategy is a list of allocations specifying the resources
86 // each renderer is permitted to consume for its cache.
87 typedef std::list<Allocation> AllocationStrategy;
89 // This class is a singleton. Do not instantiate directly.
90 CacheManagerHost();
91 friend DefaultSingletonTraits<CacheManagerHost>;
93 ~CacheManagerHost();
95 // Recomputes the allocation of cache resources among the renderers. Also
96 // informs the renderers of their new allocation.
97 void ReviseAllocationStrategy();
99 // Schedules a call to ReviseAllocationStrategy after a short delay.
100 void ReviseAllocationStrategyLater();
102 // The various tactics used as part of an allocation strategy. To decide
103 // how many resources a given renderer should be allocated, we consider its
104 // usage statistics. Each tactic specifies the function that maps usage
105 // statistics to resource allocations.
107 // Determining a resource allocation strategy amounts to picking a tactic
108 // for each renderer and checking that the total memory required fits within
109 // our |global_size_limit_|.
110 enum AllocationTactic {
111 // Ignore cache statistics and divide resources equally among the given
112 // set of caches.
113 DIVIDE_EVENLY,
115 // Allow each renderer to keep its current set of cached resources, with
116 // some extra allocation to store new objects.
117 KEEP_CURRENT_WITH_HEADROOM,
119 // Allow each renderer to keep its current set of cached resources.
120 KEEP_CURRENT,
122 // Allow each renderer to keep cache resources it believs are currently
123 // being used, with some extra allocation to store new objects.
124 KEEP_LIVE_WITH_HEADROOM,
126 // Allow each renderer to keep cache resources it believes are currently
127 // being used, but instruct the renderer to discard all other data.
128 KEEP_LIVE,
131 // Helper functions for devising an allocation strategy
133 // Add up all the stats from the given set of renderers and place the result
134 // in |stats|.
135 void GatherStats(const std::set<int>& renderers,
136 CacheManager::UsageStats* stats);
138 // Get the amount of memory that would be required to implement |tactic|
139 // using the specified allocation tactic. This function defines the
140 // semantics for each of the tactics.
141 static size_t CacheManagerHost::GetSize(AllocationTactic tactic,
142 const CacheManager::UsageStats& stats);
144 // Attempt to use the specified tactics to compute an allocation strategy
145 // and place the result in |strategy|. |active_stats| and |inactive_stats|
146 // are the aggregate statistics for |active_renderers_| and
147 // |inactive_renderers_|, respectively.
149 // Returns |true| on success and |false| on failure. Does not modify
150 // |strategy| on failure.
151 bool AttemptTactic(AllocationTactic active_tactic,
152 const CacheManager::UsageStats& active_stats,
153 AllocationTactic inactive_tactic,
154 const CacheManager::UsageStats& inactive_stats,
155 AllocationStrategy* strategy);
157 // For each renderer in |renderers|, computes its allocation according to
158 // |tactic| and add the result to |strategy|. Any |extra_bytes_to_allocate|
159 // is divided evenly among the renderers.
160 void AddToStrategy(std::set<int> renderers,
161 AllocationTactic tactic,
162 size_t extra_bytes_to_allocate,
163 AllocationStrategy* strategy);
165 // Enact an allocation strategy by informing the renderers of their
166 // allocations according to |strategy|.
167 void EnactStrategy(const AllocationStrategy& strategy);
169 // Check to see if any active renderers have fallen inactive.
170 void FindInactiveRenderers();
172 // The global size limit for all in-memory caches.
173 size_t global_size_limit_;
175 // Maps every renderer_id our most recent copy of its statistics.
176 StatsMap stats_;
178 // Every renderer we think is still around is in one of these two sets.
180 // Active renderers are those renderers that have been active more recently
181 // than they have been inactive.
182 std::set<int> active_renderers_;
183 // Inactive renderers are those renderers that have been inactive more
184 // recently than they have been active.
185 std::set<int> inactive_renderers_;
187 ScopedRunnableMethodFactory<CacheManagerHost> revise_allocation_factory_;
189 DISALLOW_EVIL_CONSTRUCTORS(CacheManagerHost);
192 #endif // CHROME_BROWSER_CACHE_MANAGER_HOST_H__