Convert all sizes in cache smart size logic to use KB. r=jst, a=beta7

This commit is contained in:
Jason Duell 2010-09-29 16:41:03 -07:00
parent 515d69dabc
commit 2097d18eac

View File

@ -129,12 +129,12 @@ static const char * prefList[] = {
MEMORY_CACHE_CAPACITY_PREF
};
// 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;
// Cache sizes, in KB
const PRInt32 DEFAULT_CACHE_SIZE = 250 * 1024; // 250 MB
const PRInt32 MIN_CACHE_SIZE = 50 * 1024; // 50 MB
const PRInt32 MAX_CACHE_SIZE = 1024 * 1024; // 1 GB
// Default cache size was 50 MB for many years until FF 4:
const PRInt32 PRE_FF4_DEFAULT_CACHE_SIZE = 50 * 1024 * 1024;
const PRInt32 PRE_FF4_DEFAULT_CACHE_SIZE = 50 * 1024;
class nsCacheProfilePrefObserver : public nsIObserver
{
@ -250,7 +250,7 @@ public:
// dispatches this value back to the main thread.
NS_IMETHOD Run()
{
mSmartSize = nsCacheProfilePrefObserver::GetSmartCacheSize() / 1024;
mSmartSize = nsCacheProfilePrefObserver::GetSmartCacheSize();
nsCOMPtr<nsIRunnable> event = new nsSetSmartSizeEvent(mFirstRun,
mSmartSize);
NS_DispatchToMainThread(event);
@ -375,10 +375,12 @@ nsCacheProfilePrefObserver::Observe(nsISupports * subject,
} else if (!strcmp(NS_PREFBRANCH_PREFCHANGE_TOPIC_ID, topic)) {
// ignore pref changes until we're done switch profiles
if (!mHaveProfile) return NS_OK;
if (!mHaveProfile)
return NS_OK;
nsCOMPtr<nsIPrefBranch> branch = do_QueryInterface(subject, &rv);
if (NS_FAILED(rv)) return rv;
if (NS_FAILED(rv))
return rv;
#ifdef NECKO_DISK_CACHE
// which preference changed?
@ -387,7 +389,8 @@ nsCacheProfilePrefObserver::Observe(nsISupports * subject,
if (!mInPrivateBrowsing) {
rv = branch->GetBoolPref(DISK_CACHE_ENABLE_PREF,
&mDiskCacheEnabled);
if (NS_FAILED(rv)) return rv;
if (NS_FAILED(rv))
return rv;
nsCacheService::SetDiskCacheEnabled(DiskCacheEnabled());
}
@ -395,7 +398,8 @@ nsCacheProfilePrefObserver::Observe(nsISupports * subject,
PRInt32 capacity = 0;
rv = branch->GetIntPref(DISK_CACHE_CAPACITY_PREF, &capacity);
if (NS_FAILED(rv)) return rv;
if (NS_FAILED(rv))
return rv;
mDiskCacheCapacity = PR_MAX(0, capacity);
nsCacheService::SetDiskCacheCapacity(mDiskCacheCapacity);
@ -405,18 +409,21 @@ nsCacheProfilePrefObserver::Observe(nsISupports * subject,
PRBool smartSizeEnabled;
rv = branch->GetBoolPref(DISK_CACHE_SMART_SIZE_ENABLED_PREF,
&smartSizeEnabled);
if (NS_FAILED(rv)) return rv;
if (NS_FAILED(rv))
return rv;
PRInt32 newCapacity = 0;
if (smartSizeEnabled) {
// Smart sizing switched on: recalculate the capacity.
nsCOMPtr<nsIRunnable> event = new nsGetSmartSizeEvent(false);
rv = nsCacheService::DispatchToCacheIOThread(event);
// If the dispatch failed, just use our base line for the size
if (NS_FAILED(rv)) mDiskCacheCapacity = BASE_LINE;
if (NS_FAILED(rv))
mDiskCacheCapacity = DEFAULT_CACHE_SIZE;
} else {
// Smart sizing switched off: use user specified size
rv = branch->GetIntPref(DISK_CACHE_CAPACITY_PREF, &newCapacity);
if (NS_FAILED(rv)) return rv;
if (NS_FAILED(rv))
return rv;
mDiskCacheCapacity = PR_MAX(0, newCapacity);
nsCacheService::SetDiskCacheCapacity(mDiskCacheCapacity);
}
@ -464,7 +471,8 @@ nsCacheProfilePrefObserver::Observe(nsISupports * subject,
rv = branch->GetBoolPref(MEMORY_CACHE_ENABLE_PREF,
&mMemoryCacheEnabled);
if (NS_FAILED(rv)) return rv;
if (NS_FAILED(rv))
return rv;
nsCacheService::SetMemoryCache();
} else if (!strcmp(MEMORY_CACHE_CAPACITY_PREF, data.get())) {
@ -496,7 +504,8 @@ nsCacheProfilePrefObserver::Observe(nsISupports * subject,
#if defined(NECKO_DISK_CACHE) || defined(NECKO_OFFLINE_CACHE)
nsCOMPtr<nsIPrefBranch> branch = do_GetService(NS_PREFSERVICE_CONTRACTID, &rv);
if (NS_FAILED(rv)) return rv;
if (NS_FAILED(rv))
return rv;
#endif // !NECKO_DISK_CACHE && !NECKO_OFFLINE_CACHE
#ifdef NECKO_DISK_CACHE
@ -529,7 +538,7 @@ nsCacheProfilePrefObserver::Observe(nsISupports * subject,
* 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.
*@return: The size that the user's disk cache should default to, in kBytes.
*/
PRUint32
nsCacheProfilePrefObserver::GetSmartCacheSize(void) {
@ -539,51 +548,37 @@ nsCacheProfilePrefObserver::GetSmartCacheSize(void) {
rv = NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR,
getter_AddRefs(profileDirectory));
if (NS_FAILED(rv)) {
return BASE_LINE;
return DEFAULT_CACHE_SIZE;
}
nsCOMPtr<nsILocalFile> diskHandle = do_QueryInterface(profileDirectory);
PRInt64 bytesAvailable;
diskHandle->GetDiskSpaceAvailable(&bytesAvailable);
rv = diskHandle->GetDiskSpaceAvailable(&bytesAvailable);
if (NS_FAILED(rv))
return DEFAULT_CACHE_SIZE;
PRInt64 kBytesAvail = bytesAvailable / 1024;
/* 0MB <= Available < 500MB
* Use between 50MB and 200MB
*/
if (bytesAvailable < BASE_LINE * 2) {
return PR_MAX(MIN_SIZE, bytesAvailable * 4 / 10);
}
// 0 MB <= Available < 500 MB: Use between 50MB and 200MB
if (kBytesAvail < DEFAULT_CACHE_SIZE * 2)
return PR_MAX(MIN_CACHE_SIZE, kBytesAvail * 4 / 10);
/* 500MB <= Available < 2500MB
* Use 250MB
*/
if (bytesAvailable < static_cast<PRInt64>(BASE_LINE) * 10) {
return BASE_LINE;
}
// 500MB <= Available < 2.5 GB: Use 250MB
if (kBytesAvail < static_cast<PRInt64>(DEFAULT_CACHE_SIZE) * 10)
return DEFAULT_CACHE_SIZE;
/* 2500MB <= Available < 5000MB
* Use between 250MB and 500MB
*/
if (bytesAvailable < static_cast<PRInt64>(BASE_LINE) * 20) {
return bytesAvailable / 10;
}
// 2.5 GB <= Available < 5 GB: Use between 250MB and 500MB
if (kBytesAvail < static_cast<PRInt64>(DEFAULT_CACHE_SIZE) * 20)
return kBytesAvail / 10;
/* 5000MB <= Available < 50000MB
* Use 625MB
*/
if (bytesAvailable < static_cast<PRInt64>(BASE_LINE) * 200 ) {
return BASE_LINE * 5 / 2;
}
// 5 GB <= Available < 50 GB: Use 625MB
if (kBytesAvail < static_cast<PRInt64>(DEFAULT_CACHE_SIZE) * 200 )
return DEFAULT_CACHE_SIZE * 5 / 2;
/* 50000MB <= Available < 75000MB
* Use 800MB
*/
if (bytesAvailable < static_cast<PRInt64>(BASE_LINE) * 300) {
return BASE_LINE / 5 * 16;
}
// 50 GB <= Available < 75 GB: Use 800MB
if (kBytesAvail < static_cast<PRInt64>(DEFAULT_CACHE_SIZE) * 300)
return DEFAULT_CACHE_SIZE / 5 * 16;
/* We have come within range of the ceiling
* Use 1GB
*/
return MAX_SIZE;
// Use 1 GB
return MAX_CACHE_SIZE;
}
/* Determine if we are permitted to dynamically size the user's disk cache based
@ -595,8 +590,6 @@ nsCacheProfilePrefObserver::PermittedToSmartSize(nsIPrefBranch* branch, PRBool
firstRun)
{
nsresult rv;
// If user has explicitly set cache size to be smaller than previous default
// of 50 MB, then keep user's value. Otherwise use smart sizing.
if (firstRun) {
// check if user has set cache size in the past
PRBool userSet;
@ -604,8 +597,10 @@ nsCacheProfilePrefObserver::PermittedToSmartSize(nsIPrefBranch* branch, PRBool
if (NS_FAILED(rv)) userSet = PR_TRUE;
if (userSet) {
PRInt32 oldCapacity;
// If user explicitly set cache size to be smaller than old default
// of 50 MB, then keep user's value. Otherwise use smart sizing.
rv = branch->GetIntPref(DISK_CACHE_CAPACITY_PREF, &oldCapacity);
if (oldCapacity < PRE_FF4_DEFAULT_CACHE_SIZE / 1024) {
if (oldCapacity < PRE_FF4_DEFAULT_CACHE_SIZE) {
branch->SetBoolPref(DISK_CACHE_SMART_SIZE_ENABLED_PREF,
PR_FALSE);
return false;
@ -615,7 +610,8 @@ nsCacheProfilePrefObserver::PermittedToSmartSize(nsIPrefBranch* branch, PRBool
PRBool smartSizeEnabled;
rv = branch->GetBoolPref(DISK_CACHE_SMART_SIZE_ENABLED_PREF,
&smartSizeEnabled);
if (NS_FAILED(rv)) return false;
if (NS_FAILED(rv))
return false;
return !!smartSizeEnabled;
}
@ -681,7 +677,8 @@ nsCacheProfilePrefObserver::ReadPrefs(nsIPrefBranch* branch)
PRBool firstSmartSizeRun;
rv = branch->GetBoolPref(DISK_CACHE_SMART_SIZE_FIRST_RUN_PREF,
&firstSmartSizeRun);
if (NS_FAILED(rv)) firstSmartSizeRun = PR_FALSE;
if (NS_FAILED(rv))
firstSmartSizeRun = PR_FALSE;
if (PermittedToSmartSize(branch, firstSmartSizeRun)) {
// Prevent unnecessary eviction before smart size event returns
if (!firstSmartSizeRun) {
@ -691,13 +688,15 @@ nsCacheProfilePrefObserver::ReadPrefs(nsIPrefBranch* branch)
mDiskCacheCapacity = oldSmartSize;
} else {
rv = branch->SetIntPref(DISK_CACHE_CAPACITY_PREF,
MAX_SIZE / 1024);
if (NS_FAILED(rv)) NS_WARNING("Failed setting capacity pref");
MAX_CACHE_SIZE);
if (NS_FAILED(rv))
NS_WARNING("Failed setting capacity pref");
}
nsCOMPtr<nsIRunnable> event =
new nsGetSmartSizeEvent(!!firstSmartSizeRun);
rv = nsCacheService::DispatchToCacheIOThread(event);
if (NS_FAILED(rv)) mDiskCacheCapacity = BASE_LINE;
if (NS_FAILED(rv))
mDiskCacheCapacity = DEFAULT_CACHE_SIZE;
}
if (firstSmartSizeRun) {