Skip to content
This repository has been archived by the owner on Oct 3, 2020. It is now read-only.

Commit

Permalink
Bug 588507: Skip caching if Content-Length is greater than eviction s…
Browse files Browse the repository at this point in the history
…ize. r=michal, a=betaN+
  • Loading branch information
Byron Milligan committed Sep 7, 2010
1 parent ca321bb commit 3c2cca6
Show file tree
Hide file tree
Showing 11 changed files with 156 additions and 43 deletions.
4 changes: 4 additions & 0 deletions netwerk/cache/nsCacheEntry.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ class nsCacheEntry : public PRCList
nsISupports *Data() { return mData; }
void SetData( nsISupports * data);

PRInt64 PredictedDataSize() { return mPredictedDataSize; }
void SetPredictedDataSize(PRInt64 size) { mPredictedDataSize = size; }

PRUint32 DataSize() { return mDataSize; }
void SetDataSize( PRUint32 size) { mDataSize = size; }

Expand Down Expand Up @@ -239,6 +242,7 @@ class nsCacheEntry : public PRCList
PRUint32 mLastValidated; // 4
PRUint32 mExpirationTime; // 4
PRUint32 mFlags; // 4
PRInt64 mPredictedDataSize; // Size given by ContentLength.
PRUint32 mDataSize; // 4
nsCacheDevice * mCacheDevice; // 4
nsCOMPtr<nsISupports> mSecurityInfo; //
Expand Down
19 changes: 19 additions & 0 deletions netwerk/cache/nsCacheEntryDescriptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,25 @@ NS_IMETHODIMP nsCacheEntryDescriptor::IsStreamBased(PRBool *result)
return NS_OK;
}

NS_IMETHODIMP nsCacheEntryDescriptor::GetPredictedDataSize(PRInt64 *result)
{
NS_ENSURE_ARG_POINTER(result);
nsCacheServiceAutoLock lock;
if (!mCacheEntry) return NS_ERROR_NOT_AVAILABLE;

*result = mCacheEntry->PredictedDataSize();
return NS_OK;
}

NS_IMETHODIMP nsCacheEntryDescriptor::SetPredictedDataSize(PRInt64
predictedSize)
{
nsCacheServiceAutoLock lock;
if (!mCacheEntry) return NS_ERROR_NOT_AVAILABLE;

mCacheEntry->SetPredictedDataSize(predictedSize);
return NS_OK;
}

NS_IMETHODIMP nsCacheEntryDescriptor::GetDataSize(PRUint32 *result)
{
Expand Down
19 changes: 18 additions & 1 deletion netwerk/cache/nsCacheService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1481,6 +1481,7 @@ nsCacheService::EnsureEntryHasDevice(nsCacheEntry * entry)
nsCacheDevice * device = entry->CacheDevice();
if (device) return device;

PRInt64 predictedDataSize = entry->PredictedDataSize();
#ifdef NECKO_DISK_CACHE
if (entry->IsStreamData() && entry->IsAllowedOnDisk() && mEnableDiskDevice) {
// this is the default
Expand All @@ -1489,6 +1490,14 @@ nsCacheService::EnsureEntryHasDevice(nsCacheEntry * entry)
}

if (mDiskDevice) {
// Bypass the cache if Content-Length says the entry will be too big
if (predictedDataSize != -1 &&
mDiskDevice->EntryIsTooBig(predictedDataSize)) {
nsresult rv = nsCacheService::DoomEntry(entry);
NS_ASSERTION(NS_SUCCEEDED(rv),"DoomEntry() failed.");
return nsnull;
}

entry->MarkBinding(); // enter state of binding
nsresult rv = mDiskDevice->BindEntry(entry);
entry->ClearBinding(); // exit state of binding
Expand All @@ -1497,13 +1506,21 @@ nsCacheService::EnsureEntryHasDevice(nsCacheEntry * entry)
}
}
#endif // !NECKO_DISK_CACHE

// if we can't use mDiskDevice, try mMemoryDevice
if (!device && mEnableMemoryDevice && entry->IsAllowedInMemory()) {
if (!mMemoryDevice) {
(void)CreateMemoryDevice(); // ignore the error (check for mMemoryDevice instead)
}
if (mMemoryDevice) {
// Bypass the cache if Content-Length says entry will be too big
if (predictedDataSize != -1 &&
mMemoryDevice->EntryIsTooBig(predictedDataSize)) {
nsresult rv = nsCacheService::DoomEntry(entry);
NS_ASSERTION(NS_SUCCEEDED(rv),"DoomEntry() failed.");
return nsnull;
}

entry->MarkBinding(); // enter state of binding
nsresult rv = mMemoryDevice->BindEntry(entry);
entry->ClearBinding(); // exit state of binding
Expand Down
9 changes: 8 additions & 1 deletion netwerk/cache/nsDiskCacheDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -776,7 +776,7 @@ nsDiskCacheDevice::OnDataSizeChange(nsCacheEntry * entry, PRInt32 deltaSize)

// If the new size is larger than max. file size or larger than
// 1/8 the cache capacity (which is in KiB's), doom the entry and abort
if ((newSize > kMaxDataFileSize) || (newSizeK > mCacheCapacity/8)) {
if (EntryIsTooBig(newSize)) {
#ifdef DEBUG
nsresult rv =
#endif
Expand Down Expand Up @@ -859,6 +859,13 @@ nsDiskCacheDevice::Visit(nsICacheVisitor * visitor)
return NS_OK;
}

// Max allowed size for an entry is currently MIN(5MB, 1/8 CacheCapacity)
bool
nsDiskCacheDevice::EntryIsTooBig(PRInt64 entrySize)
{
return entrySize > kMaxDataFileSize
|| entrySize > (static_cast<PRInt64>(mCacheCapacity) * 1024 / 8);
}

nsresult
nsDiskCacheDevice::EvictEntries(const char * clientID)
Expand Down
2 changes: 2 additions & 0 deletions netwerk/cache/nsDiskCacheDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ class nsDiskCacheDevice : public nsCacheDevice {

virtual nsresult EvictEntries(const char * clientID);

bool EntryIsTooBig(PRInt64 entrySize);

/**
* Preference accessors
*/
Expand Down
1 change: 0 additions & 1 deletion netwerk/cache/nsDiskCacheDeviceSQL.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ class nsOfflineCacheDevice : public nsCacheDevice

virtual nsresult EvictEntries(const char * clientID);


/* Entry ownership */
nsresult GetOwnerDomains(const char * clientID,
PRUint32 * count,
Expand Down
10 changes: 9 additions & 1 deletion netwerk/cache/nsICacheEntryDescriptor.idl
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,15 @@ interface nsICacheEntryDescriptor : nsICacheEntryInfo
* object. The object will be released when the cache entry is destroyed.
*/
attribute nsISupports cacheElement;


/**
* Stores the Content-Length specified in the HTTP header for this
* entry. Checked before we write to the cache entry, to prevent ever
* taking up space in the cache for an entry that we know up front
* is going to have to be evicted anyway. See bug 588507.
*/
attribute PRInt64 predictedDataSize;

/**
* Get the access granted to this descriptor. See nsICache.idl for the
* definitions of the access modes and a thorough description of their
Expand Down
8 changes: 7 additions & 1 deletion netwerk/cache/nsMemoryCacheDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -294,14 +294,20 @@ nsMemoryCacheDevice::GetFileForEntry( nsCacheEntry * entry,
return NS_ERROR_NOT_IMPLEMENTED;
}
bool
nsMemoryCacheDevice::EntryIsTooBig(PRInt64 entrySize)
{
return entrySize > mSoftLimit;
}
nsresult
nsMemoryCacheDevice::OnDataSizeChange( nsCacheEntry * entry, PRInt32 deltaSize)
{
if (entry->IsStreamData()) {
// we have the right to refuse or pre-evict
PRUint32 newSize = entry->DataSize() + deltaSize;
if ((PRInt32) newSize > mSoftLimit) {
if (EntryIsTooBig(newSize)) {
#ifdef DEBUG
nsresult rv =
#endif
Expand Down
3 changes: 2 additions & 1 deletion netwerk/cache/nsMemoryCacheDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ class nsMemoryCacheDevice : public nsCacheDevice
virtual nsresult EvictEntries(const char * clientID);

void SetCapacity(PRInt32 capacity);


bool EntryIsTooBig(PRInt64 entrySize);
private:
friend class nsMemoryCacheDeviceInfo;
enum { DELETE_ENTRY = PR_TRUE,
Expand Down
7 changes: 7 additions & 0 deletions netwerk/protocol/http/nsHttpChannel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2798,6 +2798,13 @@ nsHttpChannel::InitCacheEntry()
rv = AddCacheEntryHeaders(mCacheEntry);
if (NS_FAILED(rv)) return rv;

// set predicted size of entry so we do not store anything that will exceed
// the max entry size limit
PRInt64 predictedDataSize;
GetContentLength(&predictedDataSize);
rv = mCacheEntry->SetPredictedDataSize(predictedDataSize);
if (NS_FAILED(rv)) return rv;

mInitedCacheEntry = PR_TRUE;
return NS_OK;
}
Expand Down
117 changes: 80 additions & 37 deletions xpcom/io/nsInputStreamTee.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
#include "nsAutoPtr.h"
#include "nsIEventTarget.h"
#include "nsThreadUtils.h"
#include <prlock.h>
#include "nsAutoLock.h"

#ifdef PR_LOGGING
static PRLogModuleInfo* gInputStreamTeeLog = PR_NewLogModule("nsInputStreamTee");
Expand All @@ -62,12 +64,14 @@ class nsInputStreamTee : public nsIInputStreamTee
NS_DECL_NSIINPUTSTREAMTEE

nsInputStreamTee();
bool SinkIsValid();
void InvalidateSink();

private:
~nsInputStreamTee() {}
~nsInputStreamTee() { if (mLock) PR_DestroyLock(mLock); }

nsresult TeeSegment(const char *buf, PRUint32 count);

static NS_METHOD WriteSegmentFun(nsIInputStream *, void *, const char *,
PRUint32, PRUint32, PRUint32 *);

Expand All @@ -76,13 +80,17 @@ class nsInputStreamTee : public nsIInputStreamTee
nsCOMPtr<nsIOutputStream> mSink;
nsCOMPtr<nsIEventTarget> mEventTarget;
nsWriteSegmentFun mWriter; // for implementing ReadSegments
void *mClosure; // for implementing ReadSegments
void *mClosure; // for implementing ReadSegments
PRLock *mLock; // synchronize access to mSinkIsValid
bool mSinkIsValid; // False if TeeWriteEvent fails
};

class nsInputStreamTeeWriteEvent : public nsRunnable {
public:
// aTee's lock is held across construction of this object
nsInputStreamTeeWriteEvent(const char *aBuf, PRUint32 aCount,
nsIOutputStream *aSink)
nsIOutputStream *aSink,
nsInputStreamTee *aTee)
{
// copy the buffer - will be free'd by dtor
mBuf = (char *)malloc(aCount);
Expand All @@ -92,20 +100,27 @@ class nsInputStreamTeeWriteEvent : public nsRunnable {
PRBool isNonBlocking;
mSink->IsNonBlocking(&isNonBlocking);
NS_ASSERTION(isNonBlocking == PR_FALSE, "mSink is nonblocking");
mTee = aTee;
}

NS_IMETHOD Run()
{
if (!mBuf) {
NS_WARNING("nsInputStreamTeeWriteEvent::Run() "
"memory not allocated\n");
"memory not allocated\n");
return NS_OK;
}
NS_ABORT_IF_FALSE(mSink, "mSink is null!");

// The output stream could have been invalidated between when
// this event was dispatched and now, so check before writing.
if (!mTee->SinkIsValid()) {
return NS_OK;
}

LOG(("nsInputStreamTeeWriteEvent::Run() [%p]"
"will write %u bytes to %p\n",
this, mCount, mSink.get()));
"will write %u bytes to %p\n",
this, mCount, mSink.get()));

PRUint32 totalBytesWritten = 0;
while (mCount) {
Expand All @@ -115,6 +130,7 @@ class nsInputStreamTeeWriteEvent : public nsRunnable {
if (NS_FAILED(rv)) {
LOG(("nsInputStreamTeeWriteEvent::Run[%p] error %x in writing",
this,rv));
mTee->InvalidateSink();
break;
}
totalBytesWritten += bytesWritten;
Expand All @@ -135,46 +151,66 @@ class nsInputStreamTeeWriteEvent : public nsRunnable {
char *mBuf;
PRUint32 mCount;
nsCOMPtr<nsIOutputStream> mSink;
// back pointer to the tee that created this runnable
nsRefPtr<nsInputStreamTee> mTee;
};

nsInputStreamTee::nsInputStreamTee()
nsInputStreamTee::nsInputStreamTee(): mLock(nsnull)
, mSinkIsValid(true)
{
}

bool
nsInputStreamTee::SinkIsValid()
{
nsAutoLock lock(mLock);
return mSinkIsValid;
}

void
nsInputStreamTee::InvalidateSink()
{
nsAutoLock lock(mLock);
mSinkIsValid = false;
}

nsresult
nsInputStreamTee::TeeSegment(const char *buf, PRUint32 count)
{
if (!mSink)
return NS_OK; // nothing to do

if (mEventTarget) {
if (!mSink) return NS_OK; // nothing to do
if (mLock) { // asynchronous case
NS_ASSERTION(mEventTarget, "mEventTarget is null, mLock is not null.");
if (!SinkIsValid()) {
return NS_OK; // nothing to do
}
nsRefPtr<nsIRunnable> event =
new nsInputStreamTeeWriteEvent(buf, count, mSink);
new nsInputStreamTeeWriteEvent(buf, count, mSink, this);
NS_ENSURE_TRUE(event, NS_ERROR_OUT_OF_MEMORY);
LOG(("nsInputStreamTee::TeeSegment [%p] dispatching write %u bytes\n",
this, count));
this, count));
return mEventTarget->Dispatch(event, NS_DISPATCH_NORMAL);
}

nsresult rv;
PRUint32 totalBytesWritten = 0;
while (count) {
PRUint32 bytesWritten = 0;
rv = mSink->Write(buf + totalBytesWritten, count, &bytesWritten);
if (NS_FAILED(rv)) {
// ok, this is not a fatal error... just drop our reference to mSink
// and continue on as if nothing happened.
NS_WARNING("Write failed (non-fatal)");
// catch possible misuse of the input stream tee
NS_ASSERTION(rv != NS_BASE_STREAM_WOULD_BLOCK, "sink must be a blocking stream");
mSink = 0;
break;
} else { // synchronous case
NS_ASSERTION(!mEventTarget, "mEventTarget is not null, mLock is null.");
nsresult rv;
PRUint32 totalBytesWritten = 0;
while (count) {
PRUint32 bytesWritten = 0;
rv = mSink->Write(buf + totalBytesWritten, count, &bytesWritten);
if (NS_FAILED(rv)) {
// ok, this is not a fatal error... just drop our reference to mSink
// and continue on as if nothing happened.
NS_WARNING("Write failed (non-fatal)");
// catch possible misuse of the input stream tee
NS_ASSERTION(rv != NS_BASE_STREAM_WOULD_BLOCK, "sink must be a blocking stream");
mSink = 0;
break;
}
totalBytesWritten += bytesWritten;
NS_ASSERTION(bytesWritten <= count, "wrote too much");
count -= bytesWritten;
}
totalBytesWritten += bytesWritten;
NS_ASSERTION(bytesWritten <= count, "wrote too much");
count -= bytesWritten;
return NS_OK;
}
return NS_OK;
}

NS_METHOD
Expand All @@ -193,10 +229,9 @@ nsInputStreamTee::WriteSegmentFun(nsIInputStream *in, void *closure, const char
return tee->TeeSegment(fromSegment, *writeCount);
}

NS_IMPL_ISUPPORTS2(nsInputStreamTee,
nsIInputStreamTee,
nsIInputStream)

NS_IMPL_THREADSAFE_ISUPPORTS2(nsInputStreamTee,
nsIInputStreamTee,
nsIInputStream)
NS_IMETHODIMP
nsInputStreamTee::Close()
{
Expand Down Expand Up @@ -287,6 +322,14 @@ NS_IMETHODIMP
nsInputStreamTee::SetEventTarget(nsIEventTarget *anEventTarget)
{
mEventTarget = anEventTarget;
if (mEventTarget) {
// Only need synchronization if this is an async tee
mLock = PR_NewLock();
if (!mLock) {
NS_ERROR("Failed to allocate lock for nsInputStreamTee");
return NS_ERROR_OUT_OF_MEMORY;
}
}
return NS_OK;
}

Expand Down

0 comments on commit 3c2cca6

Please sign in to comment.