From 0ee9052aba96c80adbfa97eb7b7870681c53299a Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Mon, 24 Oct 2011 17:50:47 -0700 Subject: [PATCH] Bug 696690 - Memory reporter for the startup cache. r=tglek. --- modules/libjar/nsZipArchive.cpp | 13 +++++++++++++ modules/libjar/nsZipArchive.h | 8 ++++++++ startupcache/StartupCache.cpp | 32 +++++++++++++++++++++++++++++++- startupcache/StartupCache.h | 6 ++++++ 4 files changed, 58 insertions(+), 1 deletion(-) diff --git a/modules/libjar/nsZipArchive.cpp b/modules/libjar/nsZipArchive.cpp index 68d362ca73da..3c39efd6b5a2 100644 --- a/modules/libjar/nsZipArchive.cpp +++ b/modules/libjar/nsZipArchive.cpp @@ -234,6 +234,11 @@ nsresult nsZipHandle::Init(nsZipArchive *zip, const char *entry, return NS_OK; } +PRInt64 nsZipHandle::SizeOfMapping() +{ + return mLen; +} + nsZipHandle::~nsZipHandle() { if (mMap) { @@ -775,6 +780,14 @@ MOZ_WIN_MEM_TRY_BEGIN MOZ_WIN_MEM_TRY_CATCH(return nsnull) } +//--------------------------------------------- +// nsZipArchive::SizeOfMapping +//--------------------------------------------- +PRInt64 nsZipArchive::SizeOfMapping() +{ + return mFd ? mFd->SizeOfMapping() : 0; +} + //------------------------------------------ // nsZipArchive constructor and destructor //------------------------------------------ diff --git a/modules/libjar/nsZipArchive.h b/modules/libjar/nsZipArchive.h index 871811a0dedd..6c273dc22b5d 100644 --- a/modules/libjar/nsZipArchive.h +++ b/modules/libjar/nsZipArchive.h @@ -221,6 +221,12 @@ public: */ const PRUint8* GetData(nsZipItem* aItem); + /** + * Gets the amount of memory taken up by the archive's mapping. + * @return the size + */ + PRInt64 SizeOfMapping(); + private: //--- private members --- @@ -382,6 +388,8 @@ public: NS_METHOD_(nsrefcnt) AddRef(void); NS_METHOD_(nsrefcnt) Release(void); + PRInt64 SizeOfMapping(); + protected: const PRUint8 * mFileData; /* pointer to mmaped file */ PRUint32 mLen; /* length of file and memory mapped area */ diff --git a/startupcache/StartupCache.cpp b/startupcache/StartupCache.cpp index f36bdd778309..7c6f392bbde8 100644 --- a/startupcache/StartupCache.cpp +++ b/startupcache/StartupCache.cpp @@ -49,6 +49,7 @@ #include "nsIClassInfo.h" #include "nsIFile.h" #include "nsILocalFile.h" +#include "nsIMemoryReporter.h" #include "nsIObserver.h" #include "nsIObserverService.h" #include "nsIOutputStream.h" @@ -81,6 +82,22 @@ #define SC_WORDSIZE "8" #endif +static PRInt64 +GetStartupCacheSize() +{ + mozilla::scache::StartupCache* sc = mozilla::scache::StartupCache::GetSingleton(); + return sc ? sc->SizeOfMapping() : 0; +} + +NS_MEMORY_REPORTER_IMPLEMENT(StartupCache, + "explicit/startup-cache", + KIND_NONHEAP, + nsIMemoryReporter::UNITS_BYTES, + GetStartupCacheSize, + "Memory used to hold the startup cache. This " + "memory is backed by a file and is likely to be " + "swapped out shortly after start-up."); + namespace mozilla { namespace scache { @@ -120,7 +137,8 @@ StartupCache* StartupCache::gStartupCache; bool StartupCache::gShutdownInitiated; StartupCache::StartupCache() - : mArchive(NULL), mStartupWriteInitiated(false), mWriteThread(NULL) {} + : mArchive(NULL), mStartupWriteInitiated(false), mWriteThread(NULL), + mMemoryReporter(nsnull) { } StartupCache::~StartupCache() { @@ -134,6 +152,8 @@ StartupCache::~StartupCache() WaitOnWriteThread(); WriteToDisk(); gStartupCache = nsnull; + (void)::NS_UnregisterMemoryReporter(mMemoryReporter); + mMemoryReporter = nsnull; } nsresult @@ -206,6 +226,10 @@ StartupCache::Init() NS_WARNING("Failed to load startupcache file correctly, removing!"); InvalidateCache(); } + + mMemoryReporter = new NS_MEMORY_REPORTER_NAME(StartupCache); + (void)::NS_RegisterMemoryReporter(mMemoryReporter); + return NS_OK; } @@ -308,6 +332,12 @@ StartupCache::PutBuffer(const char* id, const char* inbuf, PRUint32 len) return ResetStartupWriteTimer(); } +PRInt64 +StartupCache::SizeOfMapping() +{ + return mArchive ? mArchive->SizeOfMapping() : 0; +} + struct CacheWriteHolder { nsCOMPtr writer; diff --git a/startupcache/StartupCache.h b/startupcache/StartupCache.h index 5f16c85546ef..d737d43c1b45 100644 --- a/startupcache/StartupCache.h +++ b/startupcache/StartupCache.h @@ -95,6 +95,8 @@ * provide some convenience in writing out data. */ +class nsIMemoryReporter; + namespace mozilla { namespace scache { @@ -148,6 +150,8 @@ public: static StartupCache* GetSingleton(); static void DeleteSingleton(); + PRInt64 SizeOfMapping(); + private: StartupCache(); ~StartupCache(); @@ -178,6 +182,8 @@ private: #ifdef DEBUG nsTHashtable mWriteObjectMap; #endif + + nsIMemoryReporter* mMemoryReporter; }; // This debug outputstream attempts to detect if clients are writing multiple