From 9dc2c72bd772c63a450d0d8f63a612b1a6305986 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Sun, 18 Dec 2011 16:20:36 -0800 Subject: [PATCH] Bug 697335 - Another memory reporter for the startup cache. r=taras. --- startupcache/StartupCache.cpp | 65 +++++++++++++++++++++++++++-------- startupcache/StartupCache.h | 18 ++++++++-- xpcom/glue/nsBaseHashtable.h | 2 +- 3 files changed, 68 insertions(+), 17 deletions(-) diff --git a/startupcache/StartupCache.cpp b/startupcache/StartupCache.cpp index b66adbb00d9b..b74dfde17962 100644 --- a/startupcache/StartupCache.cpp +++ b/startupcache/StartupCache.cpp @@ -82,24 +82,41 @@ #define SC_WORDSIZE "8" #endif +namespace mozilla { +namespace scache { + static PRInt64 -GetStartupCacheSize() +GetStartupCacheMappingSize() { mozilla::scache::StartupCache* sc = mozilla::scache::StartupCache::GetSingleton(); return sc ? sc->SizeOfMapping() : 0; } -NS_MEMORY_REPORTER_IMPLEMENT(StartupCache, - "explicit/startup-cache", +NS_MEMORY_REPORTER_IMPLEMENT(StartupCacheMapping, + "explicit/startup-cache/mapping", 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 " + GetStartupCacheMappingSize, + "Memory used to hold the mapping of the startup " + "cache from file. This memory is likely to be " "swapped out shortly after start-up.") -namespace mozilla { -namespace scache { +NS_MEMORY_REPORTER_MALLOC_SIZEOF_FUN(StartupCacheDataMallocSizeOf, "startup-cache/data") + +static PRInt64 +GetStartupCacheDataSize() +{ + mozilla::scache::StartupCache* sc = mozilla::scache::StartupCache::GetSingleton(); + return sc ? sc->HeapSizeOfIncludingThis(StartupCacheDataMallocSizeOf) : 0; +} + +NS_MEMORY_REPORTER_IMPLEMENT(StartupCacheData, + "explicit/startup-cache/data", + KIND_HEAP, + nsIMemoryReporter::UNITS_BYTES, + GetStartupCacheDataSize, + "Memory used by the startup cache for things " + "other than the file mapping.") static const char sStartupCacheName[] = "startupCache." SC_WORDSIZE "." SC_ENDIAN; static NS_DEFINE_CID(kZipReaderCID, NS_ZIPREADER_CID); @@ -138,7 +155,7 @@ bool StartupCache::gShutdownInitiated; StartupCache::StartupCache() : mArchive(NULL), mStartupWriteInitiated(false), mWriteThread(NULL), - mMemoryReporter(nsnull) { } + mMappingMemoryReporter(nsnull), mDataMemoryReporter(nsnull) { } StartupCache::~StartupCache() { @@ -152,8 +169,10 @@ StartupCache::~StartupCache() WaitOnWriteThread(); WriteToDisk(); gStartupCache = nsnull; - (void)::NS_UnregisterMemoryReporter(mMemoryReporter); - mMemoryReporter = nsnull; + (void)::NS_UnregisterMemoryReporter(mMappingMemoryReporter); + (void)::NS_UnregisterMemoryReporter(mDataMemoryReporter); + mMappingMemoryReporter = nsnull; + mDataMemoryReporter = nsnull; } nsresult @@ -227,8 +246,10 @@ StartupCache::Init() InvalidateCache(); } - mMemoryReporter = new NS_MEMORY_REPORTER_NAME(StartupCache); - (void)::NS_RegisterMemoryReporter(mMemoryReporter); + mMappingMemoryReporter = new NS_MEMORY_REPORTER_NAME(StartupCacheMapping); + mDataMemoryReporter = new NS_MEMORY_REPORTER_NAME(StartupCacheData); + (void)::NS_RegisterMemoryReporter(mMappingMemoryReporter); + (void)::NS_RegisterMemoryReporter(mDataMemoryReporter); return NS_OK; } @@ -335,12 +356,28 @@ StartupCache::PutBuffer(const char* id, const char* inbuf, PRUint32 len) return ResetStartupWriteTimer(); } -PRInt64 +size_t StartupCache::SizeOfMapping() { return mArchive ? mArchive->SizeOfMapping() : 0; } +size_t +StartupCache::HeapSizeOfIncludingThis(nsMallocSizeOfFun aMallocSizeOf) +{ + // This function could measure more members, but they haven't been found by + // DMD to be significant. They can be added later if necessary. + return aMallocSizeOf(this, sizeof(StartupCache)) + + mTable.SizeOfExcludingThis(SizeOfEntryExcludingThis, aMallocSizeOf); +} + +/* static */ size_t +StartupCache::SizeOfEntryExcludingThis(const nsACString& key, const nsAutoPtr& data, + nsMallocSizeOfFun mallocSizeOf, void *) +{ + return data->SizeOfExcludingThis(mallocSizeOf); +} + struct CacheWriteHolder { nsCOMPtr writer; diff --git a/startupcache/StartupCache.h b/startupcache/StartupCache.h index 8d5a7a826426..56bf6eff11f2 100644 --- a/startupcache/StartupCache.h +++ b/startupcache/StartupCache.h @@ -113,6 +113,10 @@ struct CacheEntry ~CacheEntry() { } + + size_t SizeOfExcludingThis(nsMallocSizeOfFun mallocSizeOf) { + return mallocSizeOf(data, size); + } }; // We don't want to refcount StartupCache, and ObserverService wants to @@ -150,7 +154,11 @@ public: static StartupCache* GetSingleton(); static void DeleteSingleton(); - PRInt64 SizeOfMapping(); + // This measures all the heap memory used by the StartupCache, i.e. it + // excludes the mapping. + size_t HeapSizeOfIncludingThis(nsMallocSizeOfFun mallocSizeOf); + + size_t SizeOfMapping(); private: StartupCache(); @@ -166,6 +174,11 @@ private: static void WriteTimeout(nsITimer *aTimer, void *aClosure); static void ThreadedWrite(void *aClosure); + static size_t SizeOfEntryExcludingThis(const nsACString& key, + const nsAutoPtr& data, + nsMallocSizeOfFun mallocSizeOf, + void *); + nsClassHashtable mTable; nsRefPtr mArchive; nsCOMPtr mFile; @@ -183,7 +196,8 @@ private: nsTHashtable mWriteObjectMap; #endif - nsIMemoryReporter* mMemoryReporter; + nsIMemoryReporter* mMappingMemoryReporter; + nsIMemoryReporter* mDataMemoryReporter; }; // This debug outputstream attempts to detect if clients are writing multiple diff --git a/xpcom/glue/nsBaseHashtable.h b/xpcom/glue/nsBaseHashtable.h index 92542d3f352f..ff25768fc865 100644 --- a/xpcom/glue/nsBaseHashtable.h +++ b/xpcom/glue/nsBaseHashtable.h @@ -272,7 +272,7 @@ public: * @return the summed size of all the entries */ size_t SizeOfExcludingThis(SizeOfEntryExcludingThisFun sizeOfEntryExcludingThis, - nsMallocSizeOfFun mallocSizeOf, void *userArg) + nsMallocSizeOfFun mallocSizeOf, void *userArg = NULL) { if (IsInitialized()) { s_SizeOfArgs args = { sizeOfEntryExcludingThis, userArg };