bug 783755 - Make the new default cache size stick. r=michal
authorNick Hurley <hurley@todesschaf.org>
Tue, 21 Aug 2012 14:52:44 -0700
changeset 105609 28c1e4960596c9fe35e1b79fdea3a72fc7c39fd8
parent 105608 c47ec3f2e7771182cd7d34d9501047d64d9e3f30
child 105610 d6aeeb24e99ef5a940b6fdcef573021589ad88ff
push id55
push usershu@rfrn.org
push dateThu, 30 Aug 2012 01:33:09 +0000
reviewersmichal
bugs783755
milestone17.0a1
bug 783755 - Make the new default cache size stick. r=michal
netwerk/cache/nsCacheService.cpp
--- a/netwerk/cache/nsCacheService.cpp
+++ b/netwerk/cache/nsCacheService.cpp
@@ -119,17 +119,17 @@ public:
     NS_DECL_NSIOBSERVER
 
     nsCacheProfilePrefObserver()
         : mHaveProfile(false)
         , mDiskCacheEnabled(false)
         , mDiskCacheCapacity(0)
         , mDiskCacheMaxEntrySize(-1) // -1 means "no limit"
         , mSmartSizeEnabled(false)
-        , mUseOldMaxSmartSize(false)
+        , mShouldUseOldMaxSmartSize(false)
         , mOfflineCacheEnabled(false)
         , mOfflineCacheCapacity(0)
         , mMemoryCacheEnabled(true)
         , mMemoryCacheCapacity(-1)
         , mMemoryCacheMaxEntrySize(-1) // -1 means "no limit"
         , mCacheCompressionLevel(CACHE_COMPRESSION_LEVEL)
         , mSanitizeOnShutdown(false)
         , mClearCacheOnShutdown(false)
@@ -144,46 +144,46 @@ public:
     
     bool            DiskCacheEnabled();
     int32_t         DiskCacheCapacity()         { return mDiskCacheCapacity; }
     void            SetDiskCacheCapacity(int32_t);
     int32_t         DiskCacheMaxEntrySize()     { return mDiskCacheMaxEntrySize; }
     nsIFile *       DiskCacheParentDirectory()  { return mDiskCacheParentDirectory; }
     bool            SmartSizeEnabled()          { return mSmartSizeEnabled; }
 
-    bool            UseOldMaxSmartSize()        { return mUseOldMaxSmartSize; }
-    void            SetUseNewMaxSmartSize()     { mUseOldMaxSmartSize = false; }
+    bool            ShouldUseOldMaxSmartSize()        { return mShouldUseOldMaxSmartSize; }
+    void            SetUseNewMaxSmartSize(bool useNew)     { mShouldUseOldMaxSmartSize = !useNew; }
 
     bool            OfflineCacheEnabled();
     int32_t         OfflineCacheCapacity()         { return mOfflineCacheCapacity; }
     nsIFile *       OfflineCacheParentDirectory()  { return mOfflineCacheParentDirectory; }
     
     bool            MemoryCacheEnabled();
     int32_t         MemoryCacheCapacity();
     int32_t         MemoryCacheMaxEntrySize()     { return mMemoryCacheMaxEntrySize; }
 
     int32_t         CacheCompressionLevel();
 
     bool            SanitizeAtShutdown() { return mSanitizeOnShutdown && mClearCacheOnShutdown; }
 
     static uint32_t GetSmartCacheSize(const nsAString& cachePath,
                                       uint32_t currentSize,
-                                      bool useOldMaxSmartSize);
+                                      bool shouldUseOldMaxSmartSize);
 
 private:
     bool                    PermittedToSmartSize(nsIPrefBranch*, bool firstRun);
     bool                    mHaveProfile;
     
     bool                    mDiskCacheEnabled;
     int32_t                 mDiskCacheCapacity; // in kilobytes
     int32_t                 mDiskCacheMaxEntrySize; // in kilobytes
     nsCOMPtr<nsIFile>       mDiskCacheParentDirectory;
     bool                    mSmartSizeEnabled;
 
-    bool                    mUseOldMaxSmartSize;
+    bool                    mShouldUseOldMaxSmartSize;
 
     bool                    mOfflineCacheEnabled;
     int32_t                 mOfflineCacheCapacity; // in kilobytes
     nsCOMPtr<nsIFile>       mOfflineCacheParentDirectory;
     
     bool                    mMemoryCacheEnabled;
     int32_t                 mMemoryCacheCapacity; // in kilobytes
     int32_t                 mMemoryCacheMaxEntrySize; // in kilobytes
@@ -251,38 +251,38 @@ private:
 };
 
 
 // Runnable sent from main thread to cacheIO thread
 class nsGetSmartSizeEvent: public nsRunnable
 {
 public:
     nsGetSmartSizeEvent(const nsAString& cachePath, uint32_t currentSize,
-                        bool useOldMaxSmartSize)
+                        bool shouldUseOldMaxSmartSize)
       : mCachePath(cachePath)
       , mCurrentSize(currentSize)
-      , mUseOldMaxSmartSize(useOldMaxSmartSize)
+      , mShouldUseOldMaxSmartSize(shouldUseOldMaxSmartSize)
     {}
    
     // Calculates user's disk space available on a background thread and
     // dispatches this value back to the main thread.
     NS_IMETHOD Run()
     {
         uint32_t size;
         size = nsCacheProfilePrefObserver::GetSmartCacheSize(mCachePath,
                                                              mCurrentSize,
-                                                             mUseOldMaxSmartSize);
+                                                             mShouldUseOldMaxSmartSize);
         NS_DispatchToMainThread(new nsSetSmartSizeEvent(size));
         return NS_OK;
     }
 
 private:
     nsString mCachePath;
     uint32_t mCurrentSize;
-    bool     mUseOldMaxSmartSize;
+    bool     mShouldUseOldMaxSmartSize;
 };
 
 class nsBlockOnCacheThreadEvent : public nsRunnable {
 public:
     nsBlockOnCacheThreadEvent()
     {
     }
     NS_IMETHOD Run()
@@ -443,17 +443,17 @@ nsCacheProfilePrefObserver::Observe(nsIS
                 rv = branch->GetIntPref(DISK_CACHE_CAPACITY_PREF, &newCapacity);
                 if (NS_FAILED(rv)) 
                     return rv;
                 mDiskCacheCapacity = NS_MAX(0, newCapacity);
                 nsCacheService::SetDiskCacheCapacity(mDiskCacheCapacity);
             }
         } else if (!strcmp(DISK_CACHE_USE_OLD_MAX_SMART_SIZE_PREF, data.get())) {
             rv = branch->GetBoolPref(DISK_CACHE_USE_OLD_MAX_SMART_SIZE_PREF,
-                                     &mUseOldMaxSmartSize);
+                                     &mShouldUseOldMaxSmartSize);
             if (NS_FAILED(rv))
                 return rv;
         } else if (!strcmp(DISK_CACHE_MAX_ENTRY_SIZE_PREF, data.get())) {
             int32_t newMaxSize;
             rv = branch->GetIntPref(DISK_CACHE_MAX_ENTRY_SIZE_PREF,
                                     &newMaxSize);
             if (NS_FAILED(rv)) 
                 return rv;
@@ -541,19 +541,19 @@ nsCacheProfilePrefObserver::Observe(nsIS
     }
 
     return NS_OK;
 }
 
 // Returns default ("smart") size (in KB) of cache, given available disk space
 // (also in KB)
 static uint32_t
-SmartCacheSize(const uint32_t availKB, bool useOldMaxSmartSize)
+SmartCacheSize(const uint32_t availKB, bool shouldUseOldMaxSmartSize)
 {
-    uint32_t maxSize = useOldMaxSmartSize ? OLD_MAX_CACHE_SIZE : MAX_CACHE_SIZE;
+    uint32_t maxSize = shouldUseOldMaxSmartSize ? OLD_MAX_CACHE_SIZE : MAX_CACHE_SIZE;
 
     if (availKB > 100 * 1024 * 1024)
         return maxSize;  // skip computing if we're over 100 GB
 
     // Grow/shrink in 10 MB units, deliberately, so that in the common case we
     // don't shrink cache and evict items every time we startup (it's important
     // that we don't slow down startup benchmarks).
     uint32_t sz10MBs = 0;
@@ -599,34 +599,34 @@ SmartCacheSize(const uint32_t availKB, b
   * lower bound of 50MB and an upper bound of 1GB.  
   *
   *@param:  None.
   *@return: The size that the user's disk cache should default to, in kBytes.
   */
 uint32_t
 nsCacheProfilePrefObserver::GetSmartCacheSize(const nsAString& cachePath,
                                               uint32_t currentSize,
-                                              bool useOldMaxSmartSize)
+                                              bool shouldUseOldMaxSmartSize)
 {
     // Check for free space on device where cache directory lives
     nsresult rv;
     nsCOMPtr<nsIFile> 
         cacheDirectory (do_CreateInstance(NS_LOCAL_FILE_CONTRACTID, &rv));
     if (NS_FAILED(rv) || !cacheDirectory)
         return DEFAULT_CACHE_SIZE;
     rv = cacheDirectory->InitWithPath(cachePath);
     if (NS_FAILED(rv))
         return DEFAULT_CACHE_SIZE;
     int64_t bytesAvailable;
     rv = cacheDirectory->GetDiskSpaceAvailable(&bytesAvailable);
     if (NS_FAILED(rv))
         return DEFAULT_CACHE_SIZE;
 
     return SmartCacheSize((bytesAvailable / 1024) + currentSize,
-                          useOldMaxSmartSize);
+                          shouldUseOldMaxSmartSize);
 }
 
 /* Determine if we are permitted to dynamically size the user's disk cache based
  * on their disk space available. We may do this so long as the pref 
  * smart_size.enabled is true.
  */
 bool
 nsCacheProfilePrefObserver::PermittedToSmartSize(nsIPrefBranch* branch, bool
@@ -647,17 +647,17 @@ nsCacheProfilePrefObserver::PermittedToS
                 mSmartSizeEnabled = false;
                 branch->SetBoolPref(DISK_CACHE_SMART_SIZE_ENABLED_PREF,
                                     mSmartSizeEnabled);
                 return mSmartSizeEnabled;
             }
         }
         // Set manual setting to MAX cache size as starting val for any
         // adjustment by user: (bug 559942 comment 65)
-        int32_t maxSize = mUseOldMaxSmartSize ? OLD_MAX_CACHE_SIZE : MAX_CACHE_SIZE;
+        int32_t maxSize = mShouldUseOldMaxSmartSize ? OLD_MAX_CACHE_SIZE : MAX_CACHE_SIZE;
         branch->SetIntPref(DISK_CACHE_CAPACITY_PREF, maxSize);
     }
 
     rv = branch->GetBoolPref(DISK_CACHE_SMART_SIZE_ENABLED_PREF,
                              &mSmartSizeEnabled);
     if (NS_FAILED(rv))
         mSmartSizeEnabled = false;
     return mSmartSizeEnabled;
@@ -681,17 +681,17 @@ nsCacheProfilePrefObserver::ReadPrefs(ns
                               &mDiskCacheMaxEntrySize);
     mDiskCacheMaxEntrySize = NS_MAX(-1, mDiskCacheMaxEntrySize);
     
     (void) branch->GetComplexValue(DISK_CACHE_DIR_PREF,     // ignore error
                                    NS_GET_IID(nsIFile),
                                    getter_AddRefs(mDiskCacheParentDirectory));
 
     (void) branch->GetBoolPref(DISK_CACHE_USE_OLD_MAX_SMART_SIZE_PREF,
-                               &mUseOldMaxSmartSize);
+                               &mShouldUseOldMaxSmartSize);
     
     if (!mDiskCacheParentDirectory) {
         nsCOMPtr<nsIFile>  directory;
 
         // try to get the disk cache parent directory
         rv = NS_GetSpecialDirectory(NS_APP_CACHE_PARENT_DIR,
                                     getter_AddRefs(directory));
         if (NS_FAILED(rv)) {
@@ -1564,17 +1564,17 @@ nsCacheService::CreateDiskDevice()
 #endif        
         mEnableDiskDevice = false;
         delete mDiskDevice;
         mDiskDevice = nullptr;
         return rv;
     }
 
     Telemetry::Accumulate(Telemetry::DISK_CACHE_SMART_SIZE_USING_OLD_MAX,
-                          mObserver->UseOldMaxSmartSize());
+                          mObserver->ShouldUseOldMaxSmartSize());
 
     NS_ASSERTION(!mSmartSizeTimer, "Smartsize timer was already fired!");
 
     // Disk device is usually created during the startup. Delay smart size
     // calculation to avoid possible massive IO caused by eviction of entries
     // in case the new smart size is smaller than current cache usage.
     mSmartSizeTimer = do_CreateInstance("@mozilla.org/timer;1", &rv);
     if (NS_SUCCEEDED(rv)) {
@@ -1607,49 +1607,44 @@ public:
             return NS_ERROR_NOT_AVAILABLE;
 
         nsDisableOldMaxSmartSizePrefEvent::DisableOldMaxSmartSizePref(true);
         return NS_OK;
     }
 
     static void DisableOldMaxSmartSizePref(bool async)
     {
-        nsCOMPtr<nsIPrefService> prefService = do_GetService(NS_PREFSERVICE_CONTRACTID);
-        if (!prefService) {
+        nsCOMPtr<nsIPrefBranch> branch = do_GetService(NS_PREFSERVICE_CONTRACTID);
+        if (!branch) {
             return;
         }
 
-        nsCOMPtr<nsIPrefBranch> branch;
-        nsresult rv = prefService->GetDefaultBranch(nullptr, getter_AddRefs(branch));
+        nsresult rv = branch->SetBoolPref(DISK_CACHE_USE_OLD_MAX_SMART_SIZE_PREF, false);
         if (NS_FAILED(rv)) {
-            return;
-        }
-
-        rv = branch->SetBoolPref(DISK_CACHE_USE_OLD_MAX_SMART_SIZE_PREF, false);
-        if (NS_FAILED(rv)) {
+            NS_WARNING("Failed to disable old max smart size");
             return;
         }
 
         if (async) {
             nsCacheService::SetDiskSmartSize();
         } else {
             nsCacheService::gService->SetDiskSmartSize_Locked();
         }
     }
 };
 
 void
 nsCacheService::MarkStartingFresh()
 {
-    if (!gService->mObserver->UseOldMaxSmartSize()) {
+    if (!gService->mObserver->ShouldUseOldMaxSmartSize()) {
         // Already using new max, nothing to do here
         return;
     }
 
-    gService->mObserver->SetUseNewMaxSmartSize();
+    gService->mObserver->SetUseNewMaxSmartSize(true);
 
     if (NS_IsMainThread()) {
         nsDisableOldMaxSmartSizePrefEvent::DisableOldMaxSmartSizePref(false);
     } else {
         NS_DispatchToMainThread(new nsDisableOldMaxSmartSizePrefEvent());
     }
 }
 
@@ -2987,17 +2982,17 @@ nsCacheService::SetDiskSmartSize_Locked(
     if (!mObserver->SmartSizeEnabled())
         return NS_ERROR_NOT_AVAILABLE;
 
     nsAutoString cachePath;
     rv = mObserver->DiskCacheParentDirectory()->GetPath(cachePath);
     if (NS_SUCCEEDED(rv)) {
         nsCOMPtr<nsIRunnable> event =
             new nsGetSmartSizeEvent(cachePath, mDiskDevice->getCacheSize(),
-                                    mObserver->UseOldMaxSmartSize());
+                                    mObserver->ShouldUseOldMaxSmartSize());
         DispatchToCacheIOThread(event);
     } else {
         return NS_ERROR_FAILURE;
     }
 
     return NS_OK;
 }