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