2013-01-15 00:26:47 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/* vim: set ts=8 sts=4 et sw=4 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
2012-05-21 11:12:37 +00:00
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2001-02-23 13:18:01 +00:00
|
|
|
|
2012-11-22 01:14:00 +00:00
|
|
|
#include "nsCache.h"
|
2001-02-23 13:18:01 +00:00
|
|
|
#include "nsMemoryCacheDevice.h"
|
2001-03-20 22:42:03 +00:00
|
|
|
#include "nsCacheService.h"
|
2001-02-23 13:18:01 +00:00
|
|
|
#include "nsICacheService.h"
|
2001-03-13 15:43:17 +00:00
|
|
|
#include "nsICacheVisitor.h"
|
2013-01-15 00:26:47 +00:00
|
|
|
#include "nsIStorageStream.h"
|
2002-05-15 18:55:21 +00:00
|
|
|
#include "nsCRT.h"
|
2003-04-23 05:15:48 +00:00
|
|
|
#include "nsReadableUtils.h"
|
2013-11-13 00:22:38 +00:00
|
|
|
#include "mozilla/MathAlgorithms.h"
|
2011-10-30 14:39:53 +00:00
|
|
|
#include "mozilla/Telemetry.h"
|
2013-01-15 12:22:03 +00:00
|
|
|
#include <algorithm>
|
2001-03-07 09:27:36 +00:00
|
|
|
|
2011-10-06 00:42:02 +00:00
|
|
|
// The memory cache implements the "LRU-SP" caching algorithm
|
2003-03-25 07:05:33 +00:00
|
|
|
// described in "LRU-SP: A Size-Adjusted and Popularity-Aware LRU Replacement
|
|
|
|
// Algorithm for Web Caching" by Kai Cheng and Yahiko Kambayashi.
|
2003-03-17 23:22:29 +00:00
|
|
|
|
2003-03-25 07:05:33 +00:00
|
|
|
// We keep kQueueCount LRU queues, which should be about ceil(log2(mHardLimit))
|
|
|
|
// The queues hold exponentially increasing ranges of floor(log2((size/nref)))
|
|
|
|
// values for entries.
|
|
|
|
// Entries larger than 2^(kQueueCount-1) go in the last queue.
|
|
|
|
// Entries with no expiration go in the first queue.
|
2001-02-23 13:18:01 +00:00
|
|
|
|
2001-03-20 22:42:03 +00:00
|
|
|
const char *gMemoryDeviceID = "memory";
|
2001-03-07 09:27:36 +00:00
|
|
|
|
2013-01-15 00:26:47 +00:00
|
|
|
|
2001-02-23 13:18:01 +00:00
|
|
|
nsMemoryCacheDevice::nsMemoryCacheDevice()
|
2011-10-17 14:59:28 +00:00
|
|
|
: mInitialized(false),
|
2003-03-25 07:05:33 +00:00
|
|
|
mHardLimit(4 * 1024 * 1024), // default, if no pref
|
|
|
|
mSoftLimit((mHardLimit * 9) / 10), // default, if no pref
|
2001-03-07 09:27:36 +00:00
|
|
|
mTotalSize(0),
|
|
|
|
mInactiveSize(0),
|
|
|
|
mEntryCount(0),
|
2011-06-27 12:21:15 +00:00
|
|
|
mMaxEntryCount(0),
|
2013-11-26 01:04:01 +00:00
|
|
|
mMaxEntrySize(-1) // -1 means "no limit"
|
2001-02-23 13:18:01 +00:00
|
|
|
{
|
2003-03-25 07:05:33 +00:00
|
|
|
for (int i=0; i<kQueueCount; ++i)
|
|
|
|
PR_INIT_CLIST(&mEvictionList[i]);
|
2001-02-23 13:18:01 +00:00
|
|
|
}
|
|
|
|
|
2001-05-03 02:35:26 +00:00
|
|
|
|
2001-02-23 13:18:01 +00:00
|
|
|
nsMemoryCacheDevice::~nsMemoryCacheDevice()
|
2013-01-15 00:26:47 +00:00
|
|
|
{
|
2001-05-03 02:35:26 +00:00
|
|
|
Shutdown();
|
2001-03-07 09:27:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-02-23 13:18:01 +00:00
|
|
|
nsresult
|
|
|
|
nsMemoryCacheDevice::Init()
|
|
|
|
{
|
2001-08-17 00:01:01 +00:00
|
|
|
if (mInitialized) return NS_ERROR_ALREADY_INITIALIZED;
|
2001-02-23 13:18:01 +00:00
|
|
|
|
2001-08-17 00:01:01 +00:00
|
|
|
nsresult rv = mMemCacheEntries.Init();
|
|
|
|
mInitialized = NS_SUCCEEDED(rv);
|
2001-02-23 13:18:01 +00:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2001-08-17 00:01:01 +00:00
|
|
|
|
2001-03-29 05:54:58 +00:00
|
|
|
nsresult
|
|
|
|
nsMemoryCacheDevice::Shutdown()
|
|
|
|
{
|
2003-03-25 07:05:33 +00:00
|
|
|
NS_ASSERTION(mInitialized, "### attempting shutdown while not initialized");
|
2001-08-17 00:01:01 +00:00
|
|
|
NS_ENSURE_TRUE(mInitialized, NS_ERROR_NOT_INITIALIZED);
|
|
|
|
|
2001-05-03 02:35:26 +00:00
|
|
|
mMemCacheEntries.Shutdown();
|
|
|
|
|
|
|
|
// evict all entries
|
|
|
|
nsCacheEntry * entry, * next;
|
|
|
|
|
2003-03-25 07:05:33 +00:00
|
|
|
for (int i = kQueueCount - 1; i >= 0; --i) {
|
2001-05-08 00:04:50 +00:00
|
|
|
entry = (nsCacheEntry *)PR_LIST_HEAD(&mEvictionList[i]);
|
|
|
|
while (entry != &mEvictionList[i]) {
|
2010-02-07 15:52:43 +00:00
|
|
|
NS_ASSERTION(!entry->IsInUse(), "### shutting down with active entries");
|
2001-05-08 00:04:50 +00:00
|
|
|
next = (nsCacheEntry *)PR_NEXT_LINK(entry);
|
|
|
|
PR_REMOVE_AND_INIT_LINK(entry);
|
|
|
|
|
|
|
|
// update statistics
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t memoryRecovered = (int32_t)entry->DataSize();
|
2001-05-08 00:04:50 +00:00
|
|
|
mTotalSize -= memoryRecovered;
|
|
|
|
mInactiveSize -= memoryRecovered;
|
|
|
|
--mEntryCount;
|
|
|
|
|
|
|
|
delete entry;
|
|
|
|
entry = next;
|
|
|
|
}
|
2001-05-03 02:35:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* we're not factoring in changes to meta data yet...
|
2008-11-19 23:15:35 +00:00
|
|
|
* NS_ASSERTION(mTotalSize == 0, "### mem cache leaking entries?");
|
2001-05-03 02:35:26 +00:00
|
|
|
*/
|
2008-11-19 23:15:35 +00:00
|
|
|
NS_ASSERTION(mInactiveSize == 0, "### mem cache leaking entries?");
|
|
|
|
NS_ASSERTION(mEntryCount == 0, "### mem cache leaking entries?");
|
2001-05-03 02:35:26 +00:00
|
|
|
|
2011-10-17 14:59:28 +00:00
|
|
|
mInitialized = false;
|
2003-03-17 23:22:29 +00:00
|
|
|
|
2001-03-29 05:54:58 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2001-02-23 13:18:01 +00:00
|
|
|
|
2001-08-17 00:01:01 +00:00
|
|
|
|
2001-02-24 01:02:37 +00:00
|
|
|
const char *
|
|
|
|
nsMemoryCacheDevice::GetDeviceID()
|
|
|
|
{
|
2001-03-20 22:42:03 +00:00
|
|
|
return gMemoryDeviceID;
|
2001-02-24 01:02:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-02-27 05:35:53 +00:00
|
|
|
nsCacheEntry *
|
2011-09-29 06:19:26 +00:00
|
|
|
nsMemoryCacheDevice::FindEntry(nsCString * key, bool *collision)
|
2001-02-23 13:18:01 +00:00
|
|
|
{
|
2012-08-23 17:23:03 +00:00
|
|
|
mozilla::Telemetry::AutoTimer<mozilla::Telemetry::CACHE_MEMORY_SEARCH_2> timer;
|
2001-03-02 01:51:41 +00:00
|
|
|
nsCacheEntry * entry = mMemCacheEntries.GetEntry(key);
|
2012-07-30 14:20:58 +00:00
|
|
|
if (!entry) return nullptr;
|
2001-02-23 13:18:01 +00:00
|
|
|
|
2001-05-08 00:04:50 +00:00
|
|
|
// move entry to the tail of an eviction list
|
2001-03-05 07:17:58 +00:00
|
|
|
PR_REMOVE_AND_INIT_LINK(entry);
|
2001-05-08 00:04:50 +00:00
|
|
|
PR_APPEND_LINK(entry, &mEvictionList[EvictionList(entry, 0)]);
|
2001-02-23 13:18:01 +00:00
|
|
|
|
2012-04-24 23:11:39 +00:00
|
|
|
mInactiveSize -= entry->DataSize();
|
2001-03-13 15:43:17 +00:00
|
|
|
|
|
|
|
return entry;
|
2001-02-23 13:18:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsMemoryCacheDevice::DeactivateEntry(nsCacheEntry * entry)
|
|
|
|
{
|
2008-09-12 11:44:19 +00:00
|
|
|
CACHE_LOG_DEBUG(("nsMemoryCacheDevice::DeactivateEntry for entry 0x%p\n",
|
|
|
|
entry));
|
2001-03-02 01:51:41 +00:00
|
|
|
if (entry->IsDoomed()) {
|
2003-03-25 07:05:33 +00:00
|
|
|
#ifdef DEBUG
|
2001-03-04 00:11:30 +00:00
|
|
|
// XXX verify we've removed it from mMemCacheEntries & eviction list
|
2001-03-02 01:51:41 +00:00
|
|
|
#endif
|
|
|
|
delete entry;
|
2008-09-12 11:44:19 +00:00
|
|
|
CACHE_LOG_DEBUG(("deleted doomed entry 0x%p\n", entry));
|
2001-03-02 01:51:41 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2001-02-23 13:18:01 +00:00
|
|
|
|
2003-03-25 07:05:33 +00:00
|
|
|
#ifdef DEBUG
|
2001-03-02 01:51:41 +00:00
|
|
|
nsCacheEntry * ourEntry = mMemCacheEntries.GetEntry(entry->Key());
|
2001-02-23 13:18:01 +00:00
|
|
|
NS_ASSERTION(ourEntry, "DeactivateEntry called for an entry we don't have!");
|
2001-03-02 01:51:41 +00:00
|
|
|
NS_ASSERTION(entry == ourEntry, "entry doesn't match ourEntry");
|
|
|
|
if (ourEntry != entry)
|
2001-02-23 13:18:01 +00:00
|
|
|
return NS_ERROR_INVALID_POINTER;
|
2003-03-25 07:05:33 +00:00
|
|
|
#endif
|
2001-02-23 13:18:01 +00:00
|
|
|
|
2012-04-24 23:11:39 +00:00
|
|
|
mInactiveSize += entry->DataSize();
|
2001-03-13 15:43:17 +00:00
|
|
|
EvictEntriesIfNecessary();
|
|
|
|
|
2001-03-02 01:51:41 +00:00
|
|
|
return NS_OK;
|
2001-02-23 13:18:01 +00:00
|
|
|
}
|
|
|
|
|
2001-02-27 05:35:53 +00:00
|
|
|
|
2001-02-23 13:18:01 +00:00
|
|
|
nsresult
|
|
|
|
nsMemoryCacheDevice::BindEntry(nsCacheEntry * entry)
|
|
|
|
{
|
2010-05-24 02:15:18 +00:00
|
|
|
if (!entry->IsDoomed()) {
|
|
|
|
NS_ASSERTION(PR_CLIST_IS_EMPTY(entry),"entry is already on a list!");
|
|
|
|
|
|
|
|
// append entry to the eviction list
|
2001-05-08 00:04:50 +00:00
|
|
|
PR_APPEND_LINK(entry, &mEvictionList[EvictionList(entry, 0)]);
|
2001-03-02 01:51:41 +00:00
|
|
|
|
2001-04-04 03:30:45 +00:00
|
|
|
// add entry to hashtable of mem cache entries
|
|
|
|
nsresult rv = mMemCacheEntries.AddEntry(entry);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
PR_REMOVE_AND_INIT_LINK(entry);
|
|
|
|
return rv;
|
|
|
|
}
|
2001-02-23 13:18:01 +00:00
|
|
|
|
2010-05-24 02:15:18 +00:00
|
|
|
// add size of entry to memory totals
|
|
|
|
++mEntryCount;
|
|
|
|
if (mMaxEntryCount < mEntryCount) mMaxEntryCount = mEntryCount;
|
|
|
|
|
2012-04-24 23:11:39 +00:00
|
|
|
mTotalSize += entry->DataSize();
|
2010-05-24 02:15:18 +00:00
|
|
|
EvictEntriesIfNecessary();
|
|
|
|
}
|
2001-03-13 15:43:17 +00:00
|
|
|
|
2001-02-28 04:36:51 +00:00
|
|
|
return NS_OK;
|
2001-02-23 13:18:01 +00:00
|
|
|
}
|
|
|
|
|
2001-02-27 05:35:53 +00:00
|
|
|
|
2001-03-08 05:37:00 +00:00
|
|
|
void
|
2001-02-28 03:42:33 +00:00
|
|
|
nsMemoryCacheDevice::DoomEntry(nsCacheEntry * entry)
|
|
|
|
{
|
2003-03-25 07:05:33 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
// debug code to verify we have entry
|
|
|
|
nsCacheEntry * hashEntry = mMemCacheEntries.GetEntry(entry->Key());
|
|
|
|
if (!hashEntry) NS_WARNING("no entry for key");
|
|
|
|
else if (entry != hashEntry) NS_WARNING("entry != hashEntry");
|
|
|
|
#endif
|
2008-09-12 11:44:19 +00:00
|
|
|
CACHE_LOG_DEBUG(("Dooming entry 0x%p in memory cache\n", entry));
|
2003-03-25 07:05:33 +00:00
|
|
|
EvictEntry(entry, DO_NOT_DELETE_ENTRY);
|
2001-02-28 03:42:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-02-24 01:02:37 +00:00
|
|
|
nsresult
|
2003-01-18 02:15:14 +00:00
|
|
|
nsMemoryCacheDevice::OpenInputStreamForEntry( nsCacheEntry * entry,
|
|
|
|
nsCacheAccessMode mode,
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t offset,
|
2003-01-18 02:15:14 +00:00
|
|
|
nsIInputStream ** result)
|
2001-02-24 01:02:37 +00:00
|
|
|
{
|
2001-02-28 01:41:02 +00:00
|
|
|
NS_ENSURE_ARG_POINTER(entry);
|
2003-01-18 02:15:14 +00:00
|
|
|
NS_ENSURE_ARG_POINTER(result);
|
2001-02-28 01:41:02 +00:00
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
nsCOMPtr<nsIStorageStream> storage;
|
2004-10-20 05:00:23 +00:00
|
|
|
nsresult rv;
|
2001-02-28 01:41:02 +00:00
|
|
|
|
2004-10-20 05:00:23 +00:00
|
|
|
nsISupports *data = entry->Data();
|
2003-01-18 02:15:14 +00:00
|
|
|
if (data) {
|
|
|
|
storage = do_QueryInterface(data, &rv);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
}
|
2001-02-28 01:41:02 +00:00
|
|
|
else {
|
2012-08-22 15:56:38 +00:00
|
|
|
rv = NS_NewStorageStream(4096, uint32_t(-1), getter_AddRefs(storage));
|
2003-01-18 02:15:14 +00:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
entry->SetData(storage);
|
|
|
|
}
|
|
|
|
|
|
|
|
return storage->NewInputStream(offset, result);
|
|
|
|
}
|
|
|
|
|
2003-03-25 07:05:33 +00:00
|
|
|
|
2003-01-18 02:15:14 +00:00
|
|
|
nsresult
|
|
|
|
nsMemoryCacheDevice::OpenOutputStreamForEntry( nsCacheEntry * entry,
|
|
|
|
nsCacheAccessMode mode,
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t offset,
|
2003-01-18 02:15:14 +00:00
|
|
|
nsIOutputStream ** result)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(entry);
|
|
|
|
NS_ENSURE_ARG_POINTER(result);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIStorageStream> storage;
|
2004-10-20 05:00:23 +00:00
|
|
|
nsresult rv;
|
2003-01-18 02:15:14 +00:00
|
|
|
|
2004-10-20 05:00:23 +00:00
|
|
|
nsISupports *data = entry->Data();
|
2003-01-18 02:15:14 +00:00
|
|
|
if (data) {
|
|
|
|
storage = do_QueryInterface(data, &rv);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
else {
|
2012-08-22 15:56:38 +00:00
|
|
|
rv = NS_NewStorageStream(4096, uint32_t(-1), getter_AddRefs(storage));
|
2003-01-18 02:15:14 +00:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
entry->SetData(storage);
|
2001-02-28 01:41:02 +00:00
|
|
|
}
|
2003-01-18 02:15:14 +00:00
|
|
|
|
|
|
|
return storage->GetOutputStream(offset, result);
|
2001-02-24 01:02:37 +00:00
|
|
|
}
|
|
|
|
|
2001-03-01 05:01:43 +00:00
|
|
|
|
2001-03-08 21:22:18 +00:00
|
|
|
nsresult
|
|
|
|
nsMemoryCacheDevice::GetFileForEntry( nsCacheEntry * entry,
|
|
|
|
nsIFile ** result )
|
|
|
|
{
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2010-09-07 22:39:28 +00:00
|
|
|
bool
|
2012-08-22 15:56:38 +00:00
|
|
|
nsMemoryCacheDevice::EntryIsTooBig(int64_t entrySize)
|
2010-09-07 22:39:28 +00:00
|
|
|
{
|
2011-06-27 12:21:15 +00:00
|
|
|
CACHE_LOG_DEBUG(("nsMemoryCacheDevice::EntryIsTooBig "
|
|
|
|
"[size=%d max=%d soft=%d]\n",
|
|
|
|
entrySize, mMaxEntrySize, mSoftLimit));
|
|
|
|
if (mMaxEntrySize == -1)
|
|
|
|
return entrySize > mSoftLimit;
|
|
|
|
else
|
|
|
|
return (entrySize > mSoftLimit || entrySize > mMaxEntrySize);
|
2010-09-07 22:39:28 +00:00
|
|
|
}
|
|
|
|
|
2011-07-19 01:22:36 +00:00
|
|
|
size_t
|
|
|
|
nsMemoryCacheDevice::TotalSize()
|
|
|
|
{
|
|
|
|
return mTotalSize;
|
|
|
|
}
|
2001-03-08 21:22:18 +00:00
|
|
|
|
2001-02-24 01:02:37 +00:00
|
|
|
nsresult
|
2012-08-22 15:56:38 +00:00
|
|
|
nsMemoryCacheDevice::OnDataSizeChange( nsCacheEntry * entry, int32_t deltaSize)
|
2001-02-24 01:02:37 +00:00
|
|
|
{
|
2001-03-13 15:43:17 +00:00
|
|
|
if (entry->IsStreamData()) {
|
|
|
|
// we have the right to refuse or pre-evict
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t newSize = entry->DataSize() + deltaSize;
|
2010-09-07 22:39:28 +00:00
|
|
|
if (EntryIsTooBig(newSize)) {
|
2008-09-15 13:34:46 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
nsresult rv =
|
|
|
|
#endif
|
|
|
|
nsCacheService::DoomEntry(entry);
|
2002-08-07 01:13:29 +00:00
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv),"DoomEntry() failed.");
|
2002-05-10 23:44:53 +00:00
|
|
|
return NS_ERROR_ABORT;
|
|
|
|
}
|
2001-03-13 15:43:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// adjust our totals
|
|
|
|
mTotalSize += deltaSize;
|
2001-05-08 00:04:50 +00:00
|
|
|
|
|
|
|
if (!entry->IsDoomed()) {
|
|
|
|
// move entry to the tail of the appropriate eviction list
|
|
|
|
PR_REMOVE_AND_INIT_LINK(entry);
|
|
|
|
PR_APPEND_LINK(entry, &mEvictionList[EvictionList(entry, deltaSize)]);
|
|
|
|
}
|
2001-03-13 15:43:17 +00:00
|
|
|
|
|
|
|
EvictEntriesIfNecessary();
|
2001-03-01 05:01:43 +00:00
|
|
|
return NS_OK;
|
2001-02-24 01:02:37 +00:00
|
|
|
}
|
2001-02-23 13:18:01 +00:00
|
|
|
|
2001-03-07 09:27:36 +00:00
|
|
|
|
2001-03-13 15:43:17 +00:00
|
|
|
void
|
2012-08-22 15:56:38 +00:00
|
|
|
nsMemoryCacheDevice::AdjustMemoryLimits(int32_t softLimit, int32_t hardLimit)
|
2001-03-07 09:27:36 +00:00
|
|
|
{
|
|
|
|
mSoftLimit = softLimit;
|
|
|
|
mHardLimit = hardLimit;
|
2003-03-17 23:22:29 +00:00
|
|
|
|
|
|
|
// First, evict entries that won't fit into the new cache size.
|
2001-03-13 15:43:17 +00:00
|
|
|
EvictEntriesIfNecessary();
|
2001-03-07 09:27:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-04-04 03:30:45 +00:00
|
|
|
void
|
2011-09-29 06:19:26 +00:00
|
|
|
nsMemoryCacheDevice::EvictEntry(nsCacheEntry * entry, bool deleteEntry)
|
2001-04-04 03:30:45 +00:00
|
|
|
{
|
2008-09-12 11:44:19 +00:00
|
|
|
CACHE_LOG_DEBUG(("Evicting entry 0x%p from memory cache, deleting: %d\n",
|
|
|
|
entry, deleteEntry));
|
2001-04-04 03:30:45 +00:00
|
|
|
// remove entry from our hashtable
|
|
|
|
mMemCacheEntries.RemoveEntry(entry);
|
|
|
|
|
|
|
|
// remove entry from the eviction list
|
|
|
|
PR_REMOVE_AND_INIT_LINK(entry);
|
|
|
|
|
|
|
|
// update statistics
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t memoryRecovered = (int32_t)entry->DataSize();
|
2001-04-04 03:30:45 +00:00
|
|
|
mTotalSize -= memoryRecovered;
|
2003-03-26 05:14:27 +00:00
|
|
|
if (!entry->IsDoomed())
|
|
|
|
mInactiveSize -= memoryRecovered;
|
2001-04-04 03:30:45 +00:00
|
|
|
--mEntryCount;
|
2003-03-25 07:05:33 +00:00
|
|
|
|
|
|
|
if (deleteEntry) delete entry;
|
2001-04-04 03:30:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-03-13 15:43:17 +00:00
|
|
|
void
|
|
|
|
nsMemoryCacheDevice::EvictEntriesIfNecessary(void)
|
2001-03-07 09:27:36 +00:00
|
|
|
{
|
2011-10-06 00:42:02 +00:00
|
|
|
nsCacheEntry * entry;
|
|
|
|
nsCacheEntry * maxEntry;
|
2008-09-12 11:44:19 +00:00
|
|
|
CACHE_LOG_DEBUG(("EvictEntriesIfNecessary. mTotalSize: %d, mHardLimit: %d,"
|
|
|
|
"mInactiveSize: %d, mSoftLimit: %d\n",
|
|
|
|
mTotalSize, mHardLimit, mInactiveSize, mSoftLimit));
|
|
|
|
|
2001-03-13 15:43:17 +00:00
|
|
|
if ((mTotalSize < mHardLimit) && (mInactiveSize < mSoftLimit))
|
|
|
|
return;
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t now = SecondsFromPRTime(PR_Now());
|
|
|
|
uint64_t entryCost = 0;
|
|
|
|
uint64_t maxCost = 0;
|
2011-10-06 00:42:02 +00:00
|
|
|
do {
|
|
|
|
// LRU-SP eviction selection: Check the head of each segment (each
|
|
|
|
// eviction list, kept in LRU order) and select the maximal-cost
|
|
|
|
// entry for eviction. Cost is time-since-accessed * size / nref.
|
|
|
|
maxEntry = 0;
|
|
|
|
for (int i = kQueueCount - 1; i >= 0; --i) {
|
|
|
|
entry = (nsCacheEntry *)PR_LIST_HEAD(&mEvictionList[i]);
|
|
|
|
|
|
|
|
// If the head of a list is in use, check the next available entry
|
|
|
|
while ((entry != &mEvictionList[i]) &&
|
|
|
|
(entry->IsInUse())) {
|
2001-05-08 00:04:50 +00:00
|
|
|
entry = (nsCacheEntry *)PR_NEXT_LINK(entry);
|
|
|
|
}
|
2001-03-07 09:27:36 +00:00
|
|
|
|
2011-10-06 00:42:02 +00:00
|
|
|
if (entry != &mEvictionList[i]) {
|
2012-08-22 15:56:38 +00:00
|
|
|
entryCost = (uint64_t)
|
2012-04-24 23:11:39 +00:00
|
|
|
(now - entry->LastFetched()) * entry->DataSize() /
|
2013-01-15 12:22:03 +00:00
|
|
|
std::max(1, entry->FetchCount());
|
2011-10-06 00:42:02 +00:00
|
|
|
if (!maxEntry || (entryCost > maxCost)) {
|
|
|
|
maxEntry = entry;
|
|
|
|
maxCost = entryCost;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (maxEntry) {
|
|
|
|
EvictEntry(maxEntry, DELETE_ENTRY);
|
|
|
|
} else {
|
|
|
|
break;
|
2001-03-07 09:27:36 +00:00
|
|
|
}
|
2001-05-08 00:04:50 +00:00
|
|
|
}
|
2011-10-06 00:42:02 +00:00
|
|
|
while ((mTotalSize >= mHardLimit) || (mInactiveSize >= mSoftLimit));
|
2001-05-08 00:04:50 +00:00
|
|
|
}
|
2001-03-07 09:27:36 +00:00
|
|
|
|
|
|
|
|
2001-05-08 00:04:50 +00:00
|
|
|
int
|
2012-08-22 15:56:38 +00:00
|
|
|
nsMemoryCacheDevice::EvictionList(nsCacheEntry * entry, int32_t deltaSize)
|
2001-05-08 00:04:50 +00:00
|
|
|
{
|
2003-03-17 23:22:29 +00:00
|
|
|
// favor items which never expire by putting them in the lowest-index queue
|
2010-06-24 06:55:19 +00:00
|
|
|
if (entry->ExpirationTime() == nsICache::NO_EXPIRATION_TIME)
|
2003-03-17 23:22:29 +00:00
|
|
|
return 0;
|
|
|
|
|
2003-03-25 07:05:33 +00:00
|
|
|
// compute which eviction queue this entry should go into,
|
|
|
|
// based on floor(log2(size/nref))
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t size = deltaSize + (int32_t)entry->DataSize();
|
2013-01-15 12:22:03 +00:00
|
|
|
int32_t fetchCount = std::max(1, entry->FetchCount());
|
2003-03-25 07:05:33 +00:00
|
|
|
|
2013-11-13 00:22:38 +00:00
|
|
|
return std::min((int)mozilla::FloorLog2(size / fetchCount), kQueueCount - 1);
|
2001-03-13 15:43:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-03-20 22:42:03 +00:00
|
|
|
nsresult
|
|
|
|
nsMemoryCacheDevice::Visit(nsICacheVisitor * visitor)
|
|
|
|
{
|
|
|
|
nsMemoryCacheDeviceInfo * deviceInfo = new nsMemoryCacheDeviceInfo(this);
|
|
|
|
nsCOMPtr<nsICacheDeviceInfo> deviceRef(deviceInfo);
|
|
|
|
if (!deviceInfo) return NS_ERROR_OUT_OF_MEMORY;
|
2001-03-07 09:27:36 +00:00
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool keepGoing;
|
2001-03-20 22:42:03 +00:00
|
|
|
nsresult rv = visitor->VisitDevice(gMemoryDeviceID, deviceInfo, &keepGoing);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
if (!keepGoing)
|
|
|
|
return NS_OK;
|
2001-03-13 15:43:17 +00:00
|
|
|
|
2001-03-20 22:42:03 +00:00
|
|
|
nsCacheEntry * entry;
|
|
|
|
nsCOMPtr<nsICacheEntryInfo> entryRef;
|
|
|
|
|
2003-03-25 07:05:33 +00:00
|
|
|
for (int i = kQueueCount - 1; i >= 0; --i) {
|
2001-05-08 00:04:50 +00:00
|
|
|
entry = (nsCacheEntry *)PR_LIST_HEAD(&mEvictionList[i]);
|
|
|
|
while (entry != &mEvictionList[i]) {
|
|
|
|
nsCacheEntryInfo * entryInfo = new nsCacheEntryInfo(entry);
|
|
|
|
if (!entryInfo) return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
entryRef = entryInfo;
|
2001-03-20 22:42:03 +00:00
|
|
|
|
2001-05-08 00:04:50 +00:00
|
|
|
rv = visitor->VisitEntry(gMemoryDeviceID, entryInfo, &keepGoing);
|
|
|
|
entryInfo->DetachEntry();
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
if (!keepGoing) break;
|
2001-03-20 22:42:03 +00:00
|
|
|
|
2001-05-08 00:04:50 +00:00
|
|
|
entry = (nsCacheEntry *)PR_NEXT_LINK(entry);
|
|
|
|
}
|
2001-03-13 15:43:17 +00:00
|
|
|
}
|
2001-03-20 22:42:03 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-05-24 15:31:54 +00:00
|
|
|
static bool
|
|
|
|
IsEntryPrivate(nsCacheEntry* entry, void* args)
|
|
|
|
{
|
|
|
|
return entry->IsPrivate();
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ClientIDArgs {
|
|
|
|
const char* clientID;
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t prefixLength;
|
2012-05-24 15:31:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static bool
|
|
|
|
EntryMatchesClientID(nsCacheEntry* entry, void* args)
|
|
|
|
{
|
|
|
|
const char * clientID = static_cast<ClientIDArgs*>(args)->clientID;
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t prefixLength = static_cast<ClientIDArgs*>(args)->prefixLength;
|
2012-05-24 15:31:54 +00:00
|
|
|
const char * key = entry->Key()->get();
|
|
|
|
return !clientID || nsCRT::strncmp(clientID, key, prefixLength) == 0;
|
|
|
|
}
|
|
|
|
|
2001-03-29 05:54:58 +00:00
|
|
|
nsresult
|
2012-05-24 15:31:54 +00:00
|
|
|
nsMemoryCacheDevice::DoEvictEntries(bool (*matchFn)(nsCacheEntry* entry, void* args), void* args)
|
2001-03-29 05:54:58 +00:00
|
|
|
{
|
2001-04-04 03:30:45 +00:00
|
|
|
nsCacheEntry * entry;
|
|
|
|
|
2003-03-25 07:05:33 +00:00
|
|
|
for (int i = kQueueCount - 1; i >= 0; --i) {
|
2001-05-08 00:04:50 +00:00
|
|
|
PRCList * elem = PR_LIST_HEAD(&mEvictionList[i]);
|
|
|
|
while (elem != &mEvictionList[i]) {
|
|
|
|
entry = (nsCacheEntry *)elem;
|
|
|
|
elem = PR_NEXT_LINK(elem);
|
2012-05-24 15:31:54 +00:00
|
|
|
|
|
|
|
if (!matchFn(entry, args))
|
2001-05-08 00:04:50 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (entry->IsInUse()) {
|
2002-08-07 01:13:29 +00:00
|
|
|
nsresult rv = nsCacheService::DoomEntry(entry);
|
2003-03-25 07:05:33 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
2012-05-24 15:31:54 +00:00
|
|
|
CACHE_LOG_WARNING(("memCache->DoEvictEntries() aborted: rv =%x", rv));
|
2003-03-25 07:05:33 +00:00
|
|
|
return rv;
|
|
|
|
}
|
2001-05-08 00:04:50 +00:00
|
|
|
} else {
|
2003-03-25 07:05:33 +00:00
|
|
|
EvictEntry(entry, DELETE_ENTRY);
|
2001-05-08 00:04:50 +00:00
|
|
|
}
|
2001-04-04 03:30:45 +00:00
|
|
|
}
|
|
|
|
}
|
2003-03-25 07:05:33 +00:00
|
|
|
|
2001-04-04 03:30:45 +00:00
|
|
|
return NS_OK;
|
2001-03-29 05:54:58 +00:00
|
|
|
}
|
|
|
|
|
2012-05-24 15:31:54 +00:00
|
|
|
nsresult
|
|
|
|
nsMemoryCacheDevice::EvictEntries(const char * clientID)
|
|
|
|
{
|
2012-08-22 15:56:38 +00:00
|
|
|
ClientIDArgs args = {clientID, clientID ? uint32_t(strlen(clientID)) : 0};
|
2012-05-24 15:31:54 +00:00
|
|
|
return DoEvictEntries(&EntryMatchesClientID, &args);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsMemoryCacheDevice::EvictPrivateEntries()
|
|
|
|
{
|
2013-09-19 19:28:26 +00:00
|
|
|
return DoEvictEntries(&IsEntryPrivate, nullptr);
|
2012-05-24 15:31:54 +00:00
|
|
|
}
|
|
|
|
|
2001-03-29 05:54:58 +00:00
|
|
|
|
2003-03-25 07:05:33 +00:00
|
|
|
// WARNING: SetCapacity can get called before Init()
|
2001-06-28 01:30:26 +00:00
|
|
|
void
|
2012-08-22 15:56:38 +00:00
|
|
|
nsMemoryCacheDevice::SetCapacity(int32_t capacity)
|
2001-06-28 01:30:26 +00:00
|
|
|
{
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t hardLimit = capacity * 1024; // convert k into bytes
|
|
|
|
int32_t softLimit = (hardLimit * 9) / 10;
|
2001-06-28 01:30:26 +00:00
|
|
|
AdjustMemoryLimits(softLimit, hardLimit);
|
|
|
|
}
|
|
|
|
|
2011-06-27 12:21:15 +00:00
|
|
|
void
|
2012-08-22 15:56:38 +00:00
|
|
|
nsMemoryCacheDevice::SetMaxEntrySize(int32_t maxSizeInKilobytes)
|
2011-06-27 12:21:15 +00:00
|
|
|
{
|
|
|
|
// Internal unit is bytes. Changing this only takes effect *after* the
|
|
|
|
// change and has no consequences for existing cache-entries
|
|
|
|
if (maxSizeInKilobytes >= 0)
|
|
|
|
mMaxEntrySize = maxSizeInKilobytes * 1024;
|
|
|
|
else
|
|
|
|
mMaxEntrySize = -1;
|
|
|
|
}
|
2003-03-25 07:05:33 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
2008-10-10 15:04:34 +00:00
|
|
|
static PLDHashOperator
|
2012-08-22 15:56:38 +00:00
|
|
|
CountEntry(PLDHashTable * table, PLDHashEntryHdr * hdr, uint32_t number, void * arg)
|
2003-03-25 07:05:33 +00:00
|
|
|
{
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t *entryCount = (int32_t *)arg;
|
2008-03-11 08:08:05 +00:00
|
|
|
++(*entryCount);
|
|
|
|
return PL_DHASH_NEXT;
|
2003-03-25 07:05:33 +00:00
|
|
|
}
|
|
|
|
|
2003-03-17 23:22:29 +00:00
|
|
|
void
|
2003-03-25 07:05:33 +00:00
|
|
|
nsMemoryCacheDevice::CheckEntryCount()
|
2003-03-17 23:22:29 +00:00
|
|
|
{
|
2003-03-25 07:05:33 +00:00
|
|
|
if (!mInitialized) return;
|
2003-03-17 23:22:29 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t evictionListCount = 0;
|
2003-03-25 07:05:33 +00:00
|
|
|
for (int i=0; i<kQueueCount; ++i) {
|
|
|
|
PRCList * elem = PR_LIST_HEAD(&mEvictionList[i]);
|
|
|
|
while (elem != &mEvictionList[i]) {
|
|
|
|
elem = PR_NEXT_LINK(elem);
|
|
|
|
++evictionListCount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
NS_ASSERTION(mEntryCount == evictionListCount, "### mem cache badness");
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t entryCount = 0;
|
2008-03-11 08:08:05 +00:00
|
|
|
mMemCacheEntries.VisitEntries(CountEntry, &entryCount);
|
|
|
|
NS_ASSERTION(mEntryCount == entryCount, "### mem cache badness");
|
2003-03-17 23:22:29 +00:00
|
|
|
}
|
2003-03-25 07:05:33 +00:00
|
|
|
#endif
|
2001-06-28 01:30:26 +00:00
|
|
|
|
2001-03-20 22:42:03 +00:00
|
|
|
/******************************************************************************
|
|
|
|
* nsMemoryCacheDeviceInfo - for implementing about:cache
|
|
|
|
*****************************************************************************/
|
2001-03-13 15:43:17 +00:00
|
|
|
|
|
|
|
|
2003-09-07 23:12:15 +00:00
|
|
|
NS_IMPL_ISUPPORTS1(nsMemoryCacheDeviceInfo, nsICacheDeviceInfo)
|
2001-03-13 15:43:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsMemoryCacheDeviceInfo::GetDescription(char ** result)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(result);
|
2007-05-14 20:09:20 +00:00
|
|
|
*result = NS_strdup("Memory cache device");
|
2001-03-13 15:43:17 +00:00
|
|
|
if (!*result) return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsMemoryCacheDeviceInfo::GetUsageReport(char ** result)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(result);
|
2003-04-23 05:15:48 +00:00
|
|
|
nsCString buffer;
|
|
|
|
|
2010-07-02 20:56:09 +00:00
|
|
|
buffer.AssignLiteral(" <tr>\n"
|
|
|
|
" <th>Inactive storage:</th>\n"
|
|
|
|
" <td>");
|
2003-04-23 05:15:48 +00:00
|
|
|
buffer.AppendInt(mDevice->mInactiveSize / 1024);
|
2010-07-02 20:56:09 +00:00
|
|
|
buffer.AppendLiteral(" KiB</td>\n"
|
|
|
|
" </tr>\n");
|
|
|
|
|
2003-04-23 05:15:48 +00:00
|
|
|
*result = ToNewCString(buffer);
|
2001-03-13 15:43:17 +00:00
|
|
|
if (!*result) return NS_ERROR_OUT_OF_MEMORY;
|
2001-03-07 09:27:36 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-03-13 15:43:17 +00:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 15:56:38 +00:00
|
|
|
nsMemoryCacheDeviceInfo::GetEntryCount(uint32_t * result)
|
2001-03-13 15:43:17 +00:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(result);
|
|
|
|
// XXX compare calculated count vs. mEntryCount
|
2012-08-22 15:56:38 +00:00
|
|
|
*result = (uint32_t)mDevice->mEntryCount;
|
2001-03-20 22:42:03 +00:00
|
|
|
return NS_OK;
|
2001-03-13 15:43:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 15:56:38 +00:00
|
|
|
nsMemoryCacheDeviceInfo::GetTotalSize(uint32_t * result)
|
2001-03-13 15:43:17 +00:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(result);
|
2012-08-22 15:56:38 +00:00
|
|
|
*result = (uint32_t)mDevice->mTotalSize;
|
2001-03-13 15:43:17 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 15:56:38 +00:00
|
|
|
nsMemoryCacheDeviceInfo::GetMaximumSize(uint32_t * result)
|
2001-03-13 15:43:17 +00:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(result);
|
2012-08-22 15:56:38 +00:00
|
|
|
*result = (uint32_t)mDevice->mHardLimit;
|
2001-03-13 15:43:17 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|