Bug 559942: Set cache size based on disk space available. Synchronous. r=jduell, a=beta6+

This commit is contained in:
Byron Milligan 2010-09-08 17:49:17 -07:00
parent 92cc84ffa8
commit 567cc5b7df
2 changed files with 202 additions and 56 deletions

View File

@ -217,65 +217,64 @@
<!-- Network -->
<tabpanel id="networkPanel" orient="vertical">
<!-- Connection -->
<groupbox id="connectionGroup">
<caption label="&connection.label;"/>
<!-- Connection -->
<groupbox id="connectionGroup">
<caption label="&connection.label;"/>
<hbox align="center">
<description flex="1" control="connectionSettings">&connectionDesc.label;</description>
<button id="connectionSettings" icon="network" label="&connectionSettings.label;"
accesskey="&connectionSettings.accesskey;"
oncommand="gAdvancedPane.showConnections();"/>
</hbox>
</groupbox>
<hbox align="center">
<description flex="1" control="connectionSettings">&connectionDesc.label;</description>
<button id="connectionSettings" icon="network" label="&connectionSettings.label;"
accesskey="&connectionSettings.accesskey;"
oncommand="gAdvancedPane.showConnections();"/>
</hbox>
</groupbox>
<!-- Cache/Offline apps -->
<groupbox id="offlineGroup">
<caption label="&offlineStorage.label;"/>
<!-- Cache/Offline apps -->
<groupbox id="offlineGroup">
<caption label="&offlineStorage.label;"/>
<hbox align="center">
<label id="useCacheBefore" control="cacheSize"
accesskey="&useCacheBefore.accesskey;" value="&useCacheBefore.label;"/>
<textbox id="cacheSize" type="number" size="2"
preference="browser.cache.disk.capacity"
onsyncfrompreference="return gAdvancedPane.readCacheSize();"
onsynctopreference="return gAdvancedPane.writeCacheSize();"
aria-labelledby="useCacheBefore cacheSize useCacheAfter"/>
<label id="useCacheAfter" flex="1">&useCacheAfter.label;</label>
<button id="clearCacheButton" icon="clear"
label="&clearCacheNow.label;" accesskey="&clearCacheNow.accesskey;"
oncommand="gAdvancedPane.clearCache();"/>
</hbox>
<hbox align="center">
<checkbox id="offlineNotify" flex="1"
label="&offlineNotify.label;" accesskey="&offlineNotify.accesskey;"
preference="browser.offline-apps.notify"
onsyncfrompreference="return gAdvancedPane.readOfflineNotify();"/>
<button id="offlineNotifyExceptions"
label="&offlineNotifyExceptions.label;"
accesskey="&offlineNotifyExceptions.accesskey;"
oncommand="gAdvancedPane.showOfflineExceptions();"/>
</hbox>
<hbox>
<vbox flex="1">
<label id="offlineAppsListLabel">&offlineAppsList.label;</label>
<listbox id="offlineAppsList"
style="height: &offlineAppsList.height;;"
flex="1"
aria-labelledby="offlineAppsListLabel"
onselect="gAdvancedPane.offlineAppSelected(event);">
</listbox>
</vbox>
<vbox pack="end">
<button id="offlineAppsListRemove"
disabled="true"
label="&offlineAppsListRemove.label;"
accesskey="&offlineAppsListRemove.accesskey;"
oncommand="gAdvancedPane.removeOfflineApp();"/>
</vbox>
</hbox>
</groupbox>
<hbox align="center">
<label id="useCacheBefore" control="cacheSize"
accesskey="&useCacheBefore.accesskey;" value="&useCacheBefore.label;"/>
<textbox id="cacheSize" type="number" size="2"
preference="browser.cache.disk.capacity"
onsyncfrompreference="return gAdvancedPane.readCacheSize();"
onsynctopreference="return gAdvancedPane.writeCacheSize();"
aria-labelledby="useCacheBefore cacheSize useCacheAfter"/>
<label id="useCacheAfter" flex="1">&useCacheAfter.label;</label>
<button id="clearCacheButton" icon="clear"
label="&clearCacheNow.label;" accesskey="&clearCacheNow.accesskey;"
oncommand="gAdvancedPane.clearCache();"/>
</hbox>
<hbox align="center">
<checkbox id="offlineNotify" flex="1"
label="&offlineNotify.label;" accesskey="&offlineNotify.accesskey;"
preference="browser.offline-apps.notify"
onsyncfrompreference="return gAdvancedPane.readOfflineNotify();"/>
<button id="offlineNotifyExceptions"
label="&offlineNotifyExceptions.label;"
accesskey="&offlineNotifyExceptions.accesskey;"
oncommand="gAdvancedPane.showOfflineExceptions();"/>
</hbox>
<hbox>
<vbox flex="1">
<label id="offlineAppsListLabel">&offlineAppsList.label;</label>
<listbox id="offlineAppsList"
style="height: &offlineAppsList.height;;"
flex="1"
aria-labelledby="offlineAppsListLabel"
onselect="gAdvancedPane.offlineAppSelected(event);">
</listbox>
</vbox>
<vbox pack="end">
<button id="offlineAppsListRemove"
disabled="true"
label="&offlineAppsListRemove.label;"
accesskey="&offlineAppsListRemove.accesskey;"
oncommand="gAdvancedPane.removeOfflineApp();"/>
</vbox>
</hbox>
</groupbox>
</tabpanel>
<!-- Update -->

View File

@ -84,6 +84,8 @@
#define DISK_CACHE_ENABLE_PREF "browser.cache.disk.enable"
#define DISK_CACHE_DIR_PREF "browser.cache.disk.parent_directory"
#define DISK_CACHE_SMART_SIZE_FIRST_RUN_PREF "browser.cache.disk.smart_size.first_run"
#define DISK_CACHE_SMART_SIZE_ENABLED_PREF "browser.cache.disk.smart_size.enabled"
#define DISK_CACHE_CAPACITY_PREF "browser.cache.disk.capacity"
#define DISK_CACHE_MAX_ENTRY_SIZE_PREF "browser.cache.disk.max_entry_size"
#define DISK_CACHE_CAPACITY 256000
@ -106,6 +108,7 @@ static const char * observerList[] = {
static const char * prefList[] = {
#ifdef NECKO_DISK_CACHE
DISK_CACHE_ENABLE_PREF,
DISK_CACHE_SMART_SIZE_ENABLED_PREF,
DISK_CACHE_CAPACITY_PREF,
DISK_CACHE_DIR_PREF,
#endif
@ -153,6 +156,10 @@ public:
PRBool MemoryCacheEnabled();
PRInt32 MemoryCacheCapacity();
private:
bool PermittedToSmartSize(nsIPrefBranch*, PRBool firstRun);
PRUint32 GetSmartCacheSize(void);
private:
PRBool mHaveProfile;
@ -302,6 +309,27 @@ nsCacheProfilePrefObserver::Observe(nsISupports * subject,
if (NS_FAILED(rv)) return rv;
mDiskCacheCapacity = PR_MAX(0, capacity);
nsCacheService::SetDiskCacheCapacity(mDiskCacheCapacity);
// Update the cache capacity when smart sizing is turned on/off
} else if (!strcmp(DISK_CACHE_SMART_SIZE_ENABLED_PREF, data.get())) {
// Is the update because smartsizing was turned on, or off?
PRBool smartSizeEnabled;
rv = branch->GetBoolPref(DISK_CACHE_SMART_SIZE_ENABLED_PREF,
&smartSizeEnabled);
if (NS_FAILED(rv)) return rv;
PRInt32 newCapacity = 0;
if (smartSizeEnabled) {
// Smart sizing switched on: recalculate the capacity.
newCapacity = GetSmartCacheSize() / 1024;
} else {
// Smart sizing switched off: use user specified size
rv = branch->GetIntPref(DISK_CACHE_CAPACITY_PREF, &newCapacity);
if (NS_FAILED(rv)) return rv;
}
mDiskCacheCapacity = PR_MAX(0, newCapacity);
nsCacheService::SetDiskCacheCapacity(mDiskCacheCapacity);
#if 0
} else if (!strcmp(DISK_CACHE_DIR_PREF, data.get())) {
// XXX We probaby don't want to respond to this pref except after
@ -399,6 +427,104 @@ nsCacheProfilePrefObserver::Observe(nsISupports * subject,
}
/* Computes our best guess for the default size of the user's disk cache,
* based on the amount of space they have free on their hard drive.
* We use a tiered scheme: the more space available,
* the larger the disk cache will be. However, we do not want
* to enable the disk cache to grow to an unbounded size, so the larger the
* user's available space is, the smaller of a percentage we take. We set a
* 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 bytes.
*/
PRUint32
nsCacheProfilePrefObserver::GetSmartCacheSize(void) {
// Let our base line be 250MB.
const PRInt32 BASE_LINE = 250 * 1024 * 1024;
const PRInt32 MIN_SIZE = 50 * 1024 * 1024;
const PRInt32 MAX_SIZE = 1024 * 1024 * 1024;
// Get a handle to disk where cache lives, so we can check for free space
nsresult rv;
nsCOMPtr<nsIFile> profileDirectory;
rv = NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR,
getter_AddRefs(profileDirectory));
if (NS_FAILED(rv)) {
return BASE_LINE;
}
nsCOMPtr<nsILocalFile> diskHandle = do_QueryInterface(profileDirectory);
PRInt64 bytesAvailable;
diskHandle->GetDiskSpaceAvailable(&bytesAvailable);
/* 0MB <= Available < 500MB
* Use between 50MB and 200MB
*/
if (bytesAvailable < BASE_LINE * 2) {
return PR_MAX(MIN_SIZE, bytesAvailable * 4 / 10);
}
/* 500MB <= Available < 2500MB
* Use 250MB
*/
if (bytesAvailable < static_cast<PRInt64>(BASE_LINE) * 10) {
return BASE_LINE;
}
/* 2500MB <= Available < 5000MB
* Use between 250MB and 500MB
*/
if (bytesAvailable < static_cast<PRInt64>(BASE_LINE) * 20) {
return bytesAvailable / 10;
}
/* 5000MB <= Available < 50000MB
* Use 625MB
*/
if (bytesAvailable < static_cast<PRInt64>(BASE_LINE) * 200 ) {
return BASE_LINE * 5 / 2;
}
/* 50000MB <= Available < 75000MB
* Use 800MB
*/
if (bytesAvailable < static_cast<PRInt64>(BASE_LINE) * 300) {
return BASE_LINE / 5 * 16;
}
/* We have come within range of the ceiling
* Use 1GB
*/
return MAX_SIZE;
}
/* 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, PRBool
firstRun)
{
nsresult rv;
// If user has explicitly set cache size, do not use smart-sizing by default.
if (firstRun) {
// check if user has set cache size in the past
PRBool userSet;
rv = branch->PrefHasUserValue(DISK_CACHE_CAPACITY_PREF, &userSet);
if (NS_FAILED(rv)) userSet = PR_TRUE;
if (userSet) {
branch->SetBoolPref(DISK_CACHE_SMART_SIZE_ENABLED_PREF, PR_FALSE);
return false;
}
}
PRBool smartSizeEnabled;
rv = branch->GetBoolPref(DISK_CACHE_SMART_SIZE_ENABLED_PREF,
&smartSizeEnabled);
if (NS_FAILED(rv)) return false;
return !!smartSizeEnabled;
}
nsresult
nsCacheProfilePrefObserver::ReadPrefs(nsIPrefBranch* branch)
{
@ -456,6 +582,27 @@ nsCacheProfilePrefObserver::ReadPrefs(nsIPrefBranch* branch)
if (directory)
mDiskCacheParentDirectory = do_QueryInterface(directory, &rv);
}
if (mDiskCacheParentDirectory) {
PRBool firstSmartSizeRun;
rv = branch->GetBoolPref(DISK_CACHE_SMART_SIZE_FIRST_RUN_PREF, &firstSmartSizeRun);
if (NS_FAILED(rv)) firstSmartSizeRun = PR_FALSE;
if (PermittedToSmartSize(branch, firstSmartSizeRun)) {
// This change will be reflected in UI and about:cache
mDiskCacheCapacity = nsCacheProfilePrefObserver::GetSmartCacheSize() /
1024; // convert to KB.
if (firstSmartSizeRun) {
// It is no longer our first run
rv = branch->SetBoolPref(DISK_CACHE_SMART_SIZE_FIRST_RUN_PREF, PR_FALSE);
if (NS_FAILED(rv))
NS_WARNING("Failed setting first_run pref in ReadPrefs.");
// As a starting point, set disk.capacity to equal smart size
rv = branch->SetIntPref(DISK_CACHE_CAPACITY_PREF, mDiskCacheCapacity);
if (NS_FAILED(rv))
NS_WARNING("Failed setting disk.capacity pref in ReadPrefs.");
}
}
}
#endif // !NECKO_DISK_CACHE
#ifdef NECKO_OFFLINE_CACHE