From 3eae0dfea5802a6e0a551e5b6008f0eb0674dc75 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Wed, 6 Nov 2013 14:58:20 +1100 Subject: [PATCH] Bug 934321 - Add MemoryMultiReporter, a helper class that reduces some boilerplate, and convert all existing multi-reporters to use it. r=wchen. --HG-- extra : rebase_source : a6110c3558c571b7908b63e8cc7ca21103098d0d --- content/base/src/nsDOMFile.cpp | 17 ++---- content/base/src/nsFrameMessageManager.cpp | 19 +++---- content/canvas/src/WebGLContextReporter.cpp | 21 +++---- content/media/MediaDecoder.cpp | 14 ++--- dom/ipc/ContentParent.cpp | 31 ++++------- dom/workers/WorkerPrivate.cpp | 16 +----- gfx/thebes/gfxASurface.cpp | 14 +---- gfx/thebes/gfxFont.cpp | 9 --- gfx/thebes/gfxFont.h | 11 ++-- gfx/thebes/gfxPlatformFontList.cpp | 11 +--- gfx/thebes/gfxPlatformFontList.h | 8 +-- gfx/thebes/gfxWindowsPlatform.cpp | 62 +++++++++------------ image/src/imgLoader.cpp | 17 +----- js/xpconnect/src/XPCJSRuntime.cpp | 13 ++--- modules/libpref/src/Preferences.cpp | 36 +++++------- modules/libpref/src/nsPrefBranch.h | 6 +- storage/src/mozStorageService.cpp | 18 +----- xpcom/base/nsCycleCollector.cpp | 15 +---- xpcom/base/nsIMemoryReporter.idl | 52 +++++++++++++++-- xpcom/base/nsMemoryReporterManager.cpp | 14 +---- xpcom/ds/nsObserverService.cpp | 20 +++---- 21 files changed, 170 insertions(+), 254 deletions(-) diff --git a/content/base/src/nsDOMFile.cpp b/content/base/src/nsDOMFile.cpp index 7fdef4474c33..48f69f49f5d6 100644 --- a/content/base/src/nsDOMFile.cpp +++ b/content/base/src/nsDOMFile.cpp @@ -1,4 +1,5 @@ /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ /* This Source Code Form is subject to the terms of the Mozilla Public * 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/. */ @@ -645,15 +646,12 @@ nsDOMMemoryFile::DataOwner::sMemoryReporterRegistered; NS_MEMORY_REPORTER_MALLOC_SIZEOF_FUN(DOMMemoryFileDataOwnerMallocSizeOf) class nsDOMMemoryFileDataOwnerMemoryReporter MOZ_FINAL - : public nsIMemoryReporter + : public MemoryMultiReporter { - NS_DECL_THREADSAFE_ISUPPORTS - - NS_IMETHOD GetName(nsACString& aName) - { - aName.AssignASCII("dom-memory-file-data-owner"); - return NS_OK; - } +public: + nsDOMMemoryFileDataOwnerMemoryReporter() + : MemoryMultiReporter("dom-memory-file-data-owner") + {} NS_IMETHOD CollectReports(nsIMemoryReporterCallback *aCallback, nsISupports *aClosure) @@ -728,9 +726,6 @@ class nsDOMMemoryFileDataOwnerMemoryReporter MOZ_FINAL } }; -NS_IMPL_ISUPPORTS1(nsDOMMemoryFileDataOwnerMemoryReporter, - nsIMemoryReporter) - /* static */ void nsDOMMemoryFile::DataOwner::EnsureMemoryReporterRegistered() { diff --git a/content/base/src/nsFrameMessageManager.cpp b/content/base/src/nsFrameMessageManager.cpp index bc94b0d8e25f..a989affc923c 100644 --- a/content/base/src/nsFrameMessageManager.cpp +++ b/content/base/src/nsFrameMessageManager.cpp @@ -1080,11 +1080,15 @@ struct MessageManagerReferentCount namespace mozilla { namespace dom { -class MessageManagerReporter MOZ_FINAL : public nsIMemoryReporter +class MessageManagerReporter MOZ_FINAL : public MemoryMultiReporter { public: - NS_DECL_ISUPPORTS - NS_DECL_NSIMEMORYREPORTER + MessageManagerReporter() + : MemoryMultiReporter("message-manager") + {} + + NS_IMETHOD CollectReports(nsIMemoryReporterCallback* aCallback, + nsISupports* aData); static const size_t kSuspectReferentCount = 300; protected: @@ -1092,8 +1096,6 @@ protected: MessageManagerReferentCount* aReferentCount); }; -NS_IMPL_ISUPPORTS1(MessageManagerReporter, nsIMemoryReporter) - static PLDHashOperator CollectMessageListenerData(const nsAString& aKey, nsAutoTObserverArray* aListeners, @@ -1153,13 +1155,6 @@ MessageManagerReporter::CountReferents(nsFrameMessageManager* aMessageManager, } } -NS_IMETHODIMP -MessageManagerReporter::GetName(nsACString& aName) -{ - aName.AssignLiteral("message-manager"); - return NS_OK; -} - static nsresult ReportReferentCount(const char* aManagerType, const MessageManagerReferentCount& aReferentCount, diff --git a/content/canvas/src/WebGLContextReporter.cpp b/content/canvas/src/WebGLContextReporter.cpp index 8b42f8b475bc..f98153d45043 100644 --- a/content/canvas/src/WebGLContextReporter.cpp +++ b/content/canvas/src/WebGLContextReporter.cpp @@ -11,22 +11,17 @@ using namespace mozilla; NS_IMPL_ISUPPORTS1(WebGLMemoryPressureObserver, nsIObserver) -class WebGLMemoryReporter MOZ_FINAL : public nsIMemoryReporter +class WebGLMemoryReporter MOZ_FINAL : public MemoryMultiReporter { - public: - NS_DECL_ISUPPORTS - NS_DECL_NSIMEMORYREPORTER +public: + WebGLMemoryReporter() + : MemoryMultiReporter("webgl") + {} + + NS_IMETHOD CollectReports(nsIMemoryReporterCallback* aCb, + nsISupports* aClosure); }; -NS_IMPL_ISUPPORTS1(WebGLMemoryReporter, nsIMemoryReporter) - -NS_IMETHODIMP -WebGLMemoryReporter::GetName(nsACString &aName) -{ - aName.AssignLiteral("webgl"); - return NS_OK; -} - NS_IMETHODIMP WebGLMemoryReporter::CollectReports(nsIMemoryReporterCallback* aCb, nsISupports* aClosure) diff --git a/content/media/MediaDecoder.cpp b/content/media/MediaDecoder.cpp index c0de05d0050c..6dc305faadcc 100644 --- a/content/media/MediaDecoder.cpp +++ b/content/media/MediaDecoder.cpp @@ -1763,16 +1763,12 @@ MediaDecoder::IsAppleMP3Enabled() } #endif -class MediaReporter MOZ_FINAL : public nsIMemoryReporter +class MediaReporter MOZ_FINAL : public MemoryMultiReporter { public: - NS_DECL_ISUPPORTS - - NS_IMETHOD GetName(nsACString& aName) - { - aName.AssignLiteral("media"); - return NS_OK; - } + MediaReporter() + : MemoryMultiReporter("media") + {} NS_IMETHOD CollectReports(nsIMemoryReporterCallback* aCb, nsISupports* aClosure) @@ -1800,8 +1796,6 @@ public: } }; -NS_IMPL_ISUPPORTS1(MediaReporter, nsIMemoryReporter) - MediaDecoderOwner* MediaDecoder::GetOwner() { diff --git a/dom/ipc/ContentParent.cpp b/dom/ipc/ContentParent.cpp index 6c2877b8b590..c2a2366dd09f 100644 --- a/dom/ipc/ContentParent.cpp +++ b/dom/ipc/ContentParent.cpp @@ -196,29 +196,21 @@ MemoryReportRequestParent::~MemoryReportRequestParent() MOZ_COUNT_DTOR(MemoryReportRequestParent); } -/** - * A memory reporter for ContentParent objects themselves. - */ -class ContentParentMemoryReporter MOZ_FINAL : public nsIMemoryReporter +// A memory reporter for ContentParent objects themselves. +class ContentParentsMemoryReporter MOZ_FINAL : public MemoryMultiReporter { public: - NS_DECL_ISUPPORTS - NS_DECL_NSIMEMORYREPORTER - NS_MEMORY_REPORTER_MALLOC_SIZEOF_FUN(MallocSizeOf) + ContentParentsMemoryReporter() + : MemoryMultiReporter("content-parents") + {} + + NS_IMETHOD CollectReports(nsIMemoryReporterCallback* cb, + nsISupports* aClosure); }; -NS_IMPL_ISUPPORTS1(ContentParentMemoryReporter, nsIMemoryReporter) - NS_IMETHODIMP -ContentParentMemoryReporter::GetName(nsACString& aName) -{ - aName.AssignLiteral("ContentParents"); - return NS_OK; -} - -NS_IMETHODIMP -ContentParentMemoryReporter::CollectReports(nsIMemoryReporterCallback* cb, - nsISupports* aClosure) +ContentParentsMemoryReporter::CollectReports(nsIMemoryReporterCallback* cb, + nsISupports* aClosure) { nsAutoTArray cps; ContentParent::GetAllEvenIfDead(cps); @@ -468,8 +460,7 @@ ContentParent::StartUp() return; } - nsRefPtr mr = new ContentParentMemoryReporter(); - NS_RegisterMemoryReporter(mr); + NS_RegisterMemoryReporter(new ContentParentsMemoryReporter()); sCanLaunchSubprocesses = true; diff --git a/dom/workers/WorkerPrivate.cpp b/dom/workers/WorkerPrivate.cpp index 1fa8c10cec33..d5583d7a420d 100644 --- a/dom/workers/WorkerPrivate.cpp +++ b/dom/workers/WorkerPrivate.cpp @@ -1950,7 +1950,7 @@ struct WorkerPrivate::TimeoutInfo bool mCanceled; }; -class WorkerPrivate::MemoryReporter MOZ_FINAL : public nsIMemoryReporter +class WorkerPrivate::MemoryReporter MOZ_FINAL : public MemoryMultiReporter { friend class WorkerPrivate; @@ -1960,10 +1960,9 @@ class WorkerPrivate::MemoryReporter MOZ_FINAL : public nsIMemoryReporter bool mAlreadyMappedToAddon; public: - NS_DECL_THREADSAFE_ISUPPORTS - MemoryReporter(WorkerPrivate* aWorkerPrivate) - : mMutex(aWorkerPrivate->mMutex), mWorkerPrivate(aWorkerPrivate), + : MemoryMultiReporter("workers"), + mMutex(aWorkerPrivate->mMutex), mWorkerPrivate(aWorkerPrivate), mAlreadyMappedToAddon(false) { aWorkerPrivate->AssertIsOnWorkerThread(); @@ -1983,13 +1982,6 @@ public: NS_LITERAL_CSTRING(")/"); } - NS_IMETHOD - GetName(nsACString& aName) - { - aName.AssignLiteral("workers"); - return NS_OK; - } - NS_IMETHOD CollectReports(nsIMemoryReporterCallback* aCallback, nsISupports* aClosure) @@ -2072,8 +2064,6 @@ private: } }; -NS_IMPL_ISUPPORTS1(WorkerPrivate::MemoryReporter, nsIMemoryReporter) - template WorkerPrivateParent::WorkerPrivateParent( JSContext* aCx, diff --git a/gfx/thebes/gfxASurface.cpp b/gfx/thebes/gfxASurface.cpp index c8b869b2b297..1b08a4987d17 100644 --- a/gfx/thebes/gfxASurface.cpp +++ b/gfx/thebes/gfxASurface.cpp @@ -631,21 +631,13 @@ PR_STATIC_ASSERT(uint32_t(CAIRO_SURFACE_TYPE_SKIA) == static int64_t gSurfaceMemoryUsed[gfxSurfaceTypeMax] = { 0 }; -class SurfaceMemoryReporter MOZ_FINAL : - public nsIMemoryReporter +class SurfaceMemoryReporter MOZ_FINAL : public MemoryMultiReporter { public: SurfaceMemoryReporter() + : MemoryMultiReporter("gfx-surface") { } - NS_DECL_ISUPPORTS - - NS_IMETHOD GetName(nsACString &name) - { - name.AssignLiteral("gfx-surface"); - return NS_OK; - } - NS_IMETHOD CollectReports(nsIMemoryReporterCallback *aCb, nsISupports *aClosure) { @@ -673,8 +665,6 @@ public: } }; -NS_IMPL_ISUPPORTS1(SurfaceMemoryReporter, nsIMemoryReporter) - void gfxASurface::RecordMemoryUsedForSurfaceType(gfxSurfaceType aType, int32_t aBytes) diff --git a/gfx/thebes/gfxFont.cpp b/gfx/thebes/gfxFont.cpp index 45886688bbe8..f9cedc2daee3 100644 --- a/gfx/thebes/gfxFont.cpp +++ b/gfx/thebes/gfxFont.cpp @@ -1360,17 +1360,8 @@ gfxFontFamily::AddSizeOfIncludingThis(MallocSizeOf aMallocSizeOf, * shaped-word caches to free up memory. */ -NS_IMPL_ISUPPORTS1(gfxFontCache::MemoryReporter, nsIMemoryReporter) - NS_MEMORY_REPORTER_MALLOC_SIZEOF_FUN(FontCacheMallocSizeOf) -NS_IMETHODIMP -gfxFontCache::MemoryReporter::GetName(nsACString &aName) -{ - aName.AssignLiteral("font-cache"); - return NS_OK; -} - NS_IMETHODIMP gfxFontCache::MemoryReporter::CollectReports (nsIMemoryReporterCallback* aCb, diff --git a/gfx/thebes/gfxFont.h b/gfx/thebes/gfxFont.h index b302e9504361..fc43d51de728 100644 --- a/gfx/thebes/gfxFont.h +++ b/gfx/thebes/gfxFont.h @@ -950,12 +950,15 @@ public: FontCacheSizes* aSizes) const; protected: - class MemoryReporter MOZ_FINAL - : public nsIMemoryReporter + class MemoryReporter MOZ_FINAL : public mozilla::MemoryMultiReporter { public: - NS_DECL_ISUPPORTS - NS_DECL_NSIMEMORYREPORTER + MemoryReporter() + : MemoryMultiReporter("font-cache") + {} + + NS_IMETHOD CollectReports(nsIMemoryReporterCallback* aCb, + nsISupports* aClosure); }; // Observer for notifications that the font cache cares about diff --git a/gfx/thebes/gfxPlatformFontList.cpp b/gfx/thebes/gfxPlatformFontList.cpp index ed779ce2068d..58c16b7c6583 100644 --- a/gfx/thebes/gfxPlatformFontList.cpp +++ b/gfx/thebes/gfxPlatformFontList.cpp @@ -71,16 +71,11 @@ gfxFontListPrefObserver::Observe(nsISupports *aSubject, return NS_OK; } -NS_IMPL_ISUPPORTS1(gfxPlatformFontList::MemoryReporter, nsIMemoryReporter) - NS_MEMORY_REPORTER_MALLOC_SIZEOF_FUN(FontListMallocSizeOf) -NS_IMETHODIMP -gfxPlatformFontList::MemoryReporter::GetName(nsACString &aName) -{ - aName.AssignLiteral("font-list"); - return NS_OK; -} +gfxPlatformFontList::MemoryReporter::MemoryReporter() + : MemoryMultiReporter("font-list") +{} NS_IMETHODIMP gfxPlatformFontList::MemoryReporter::CollectReports diff --git a/gfx/thebes/gfxPlatformFontList.h b/gfx/thebes/gfxPlatformFontList.h index 4f3858c1001f..6b95f8b1efc3 100644 --- a/gfx/thebes/gfxPlatformFontList.h +++ b/gfx/thebes/gfxPlatformFontList.h @@ -178,12 +178,12 @@ public: void RemoveCmap(const gfxCharacterMap *aCharMap); protected: - class MemoryReporter MOZ_FINAL - : public nsIMemoryReporter + class MemoryReporter MOZ_FINAL : public mozilla::MemoryMultiReporter { public: - NS_DECL_ISUPPORTS - NS_DECL_NSIMEMORYREPORTER + MemoryReporter(); + NS_IMETHOD CollectReports(nsIMemoryReporterCallback *aCb, + nsISupports *aClosure); }; gfxPlatformFontList(bool aNeedFullnamePostscriptNames = true); diff --git a/gfx/thebes/gfxWindowsPlatform.cpp b/gfx/thebes/gfxWindowsPlatform.cpp index 891181416703..f8453504d5f6 100644 --- a/gfx/thebes/gfxWindowsPlatform.cpp +++ b/gfx/thebes/gfxWindowsPlatform.cpp @@ -207,70 +207,63 @@ typedef HRESULT (WINAPI*D3D11CreateDeviceFunc)( ID3D11DeviceContext *ppImmediateContext ); -class GPUAdapterReporter : public nsIMemoryReporter { - +class GPUAdapterReporter : public MemoryMultiReporter +{ // Callers must Release the DXGIAdapter after use or risk mem-leak static bool GetDXGIAdapter(IDXGIAdapter **DXGIAdapter) { ID3D10Device1 *D2D10Device; IDXGIDevice *DXGIDevice; bool result = false; - + if (D2D10Device = mozilla::gfx::Factory::GetDirect3D10Device()) { if (D2D10Device->QueryInterface(__uuidof(IDXGIDevice), (void **)&DXGIDevice) == S_OK) { result = (DXGIDevice->GetAdapter(DXGIAdapter) == S_OK); DXGIDevice->Release(); } } - + return result; } - -public: - NS_DECL_ISUPPORTS - // nsIMemoryReporter abstract method implementation - NS_IMETHOD - GetName(nsACString &aName) - { - aName.AssignLiteral("gpuadapter"); - return NS_OK; - } - - // nsIMemoryReporter abstract method implementation +public: + GPUAdapterReporter() + : MemoryMultiReporter("gpu-adapter") + {} + NS_IMETHOD CollectReports(nsIMemoryReporterCallback* aCb, nsISupports* aClosure) { int32_t winVers, buildNum; HANDLE ProcessHandle = GetCurrentProcess(); - + int64_t dedicatedBytesUsed = 0; int64_t sharedBytesUsed = 0; int64_t committedBytesUsed = 0; IDXGIAdapter *DXGIAdapter; - + HMODULE gdi32Handle; PFND3DKMTQS queryD3DKMTStatistics; - + winVers = gfxWindowsPlatform::WindowsOSVersion(&buildNum); - + // GPU memory reporting is not available before Windows 7 - if (winVers < gfxWindowsPlatform::kWindows7) + if (winVers < gfxWindowsPlatform::kWindows7) return NS_OK; - + if (gdi32Handle = LoadLibrary(TEXT("gdi32.dll"))) queryD3DKMTStatistics = (PFND3DKMTQS)GetProcAddress(gdi32Handle, "D3DKMTQueryStatistics"); - + if (queryD3DKMTStatistics && GetDXGIAdapter(&DXGIAdapter)) { // Most of this block is understood thanks to wj32's work on Process Hacker - + DXGI_ADAPTER_DESC adapterDesc; D3DKMTQS queryStatistics; - + DXGIAdapter->GetDesc(&adapterDesc); DXGIAdapter->Release(); - + memset(&queryStatistics, 0, sizeof(D3DKMTQS)); queryStatistics.Type = D3DKMTQS_PROCESS; queryStatistics.AdapterLuid = adapterDesc.AdapterLuid; @@ -278,29 +271,29 @@ public: if (NT_SUCCESS(queryD3DKMTStatistics(&queryStatistics))) { committedBytesUsed = queryStatistics.QueryResult.ProcessInfo.SystemMemory.BytesAllocated; } - + memset(&queryStatistics, 0, sizeof(D3DKMTQS)); queryStatistics.Type = D3DKMTQS_ADAPTER; queryStatistics.AdapterLuid = adapterDesc.AdapterLuid; if (NT_SUCCESS(queryD3DKMTStatistics(&queryStatistics))) { ULONG i; ULONG segmentCount = queryStatistics.QueryResult.AdapterInfo.NbSegments; - + for (i = 0; i < segmentCount; i++) { memset(&queryStatistics, 0, sizeof(D3DKMTQS)); queryStatistics.Type = D3DKMTQS_SEGMENT; queryStatistics.AdapterLuid = adapterDesc.AdapterLuid; queryStatistics.QuerySegment.SegmentId = i; - + if (NT_SUCCESS(queryD3DKMTStatistics(&queryStatistics))) { bool aperture; - + // SegmentInformation has a different definition in Win7 than later versions if (winVers < gfxWindowsPlatform::kWindows8) aperture = queryStatistics.QueryResult.SegmentInfoWin7.Aperture; else aperture = queryStatistics.QueryResult.SegmentInfoWin8.Aperture; - + memset(&queryStatistics, 0, sizeof(D3DKMTQS)); queryStatistics.Type = D3DKMTQS_PROCESS_SEGMENT; queryStatistics.AdapterLuid = adapterDesc.AdapterLuid; @@ -320,9 +313,9 @@ public: } } } - + FreeLibrary(gdi32Handle); - + #define REPORT(_path, _amount, _desc) \ do { \ nsresult rv; \ @@ -342,13 +335,12 @@ public: REPORT("gpu-shared", sharedBytesUsed, "In-process memory that is shared with the GPU."); - + #undef REPORT return NS_OK; } }; -NS_IMPL_ISUPPORTS1(GPUAdapterReporter, nsIMemoryReporter) static __inline void BuildKeyNameFromFontName(nsAString &aName) diff --git a/image/src/imgLoader.cpp b/image/src/imgLoader.cpp index 6dade7df6a47..e2d5123725c1 100644 --- a/image/src/imgLoader.cpp +++ b/image/src/imgLoader.cpp @@ -50,21 +50,12 @@ using namespace mozilla::image; NS_MEMORY_REPORTER_MALLOC_SIZEOF_FUN(ImagesMallocSizeOf) -class imgMemoryReporter MOZ_FINAL : - public nsIMemoryReporter +class imgMemoryReporter MOZ_FINAL : public MemoryMultiReporter { public: imgMemoryReporter() - { - } - - NS_DECL_ISUPPORTS - - NS_IMETHOD GetName(nsACString &name) - { - name.Assign("images"); - return NS_OK; - } + : MemoryMultiReporter("images") + {} NS_IMETHOD CollectReports(nsIMemoryReporterCallback *callback, nsISupports *closure) @@ -223,8 +214,6 @@ private: } }; -NS_IMPL_ISUPPORTS1(imgMemoryReporter, nsIMemoryReporter) - NS_IMPL_ISUPPORTS3(nsProgressNotificationProxy, nsIProgressEventSink, nsIChannelEventSink, diff --git a/js/xpconnect/src/XPCJSRuntime.cpp b/js/xpconnect/src/XPCJSRuntime.cpp index 3d5e48f05f59..acb6f2976100 100644 --- a/js/xpconnect/src/XPCJSRuntime.cpp +++ b/js/xpconnect/src/XPCJSRuntime.cpp @@ -2398,15 +2398,12 @@ ReportJSRuntimeExplicitTreeStats(const JS::RuntimeStats &rtStats, } // namespace xpc -class JSMainRuntimeCompartmentsReporter MOZ_FINAL : public nsIMemoryReporter +class JSMainRuntimeCompartmentsReporter MOZ_FINAL : public MemoryMultiReporter { public: - NS_DECL_THREADSAFE_ISUPPORTS - - NS_IMETHOD GetName(nsACString &name) { - name.AssignLiteral("js-main-runtime-compartments"); - return NS_OK; - } + JSMainRuntimeCompartmentsReporter() + : MemoryMultiReporter("js-main-runtime-compartments") + {} typedef js::Vector Paths; @@ -2445,8 +2442,6 @@ class JSMainRuntimeCompartmentsReporter MOZ_FINAL : public nsIMemoryReporter } }; -NS_IMPL_ISUPPORTS1(JSMainRuntimeCompartmentsReporter, nsIMemoryReporter) - NS_MEMORY_REPORTER_MALLOC_SIZEOF_FUN(OrphanMallocSizeOf) namespace xpc { diff --git a/modules/libpref/src/Preferences.cpp b/modules/libpref/src/Preferences.cpp index bd5005d36ab5..0160eb9c5119 100644 --- a/modules/libpref/src/Preferences.cpp +++ b/modules/libpref/src/Preferences.cpp @@ -208,13 +208,16 @@ Preferences::SizeOfIncludingThisAndOtherStuff(mozilla::MallocSizeOf aMallocSizeO return n; } -NS_MEMORY_REPORTER_MALLOC_SIZEOF_FUN(PreferencesMallocSizeOf) - -class PreferencesReporter MOZ_FINAL : public nsIMemoryReporter +class PreferenceServiceReporter MOZ_FINAL : public MemoryMultiReporter { public: - NS_DECL_ISUPPORTS - NS_DECL_NSIMEMORYREPORTER + PreferenceServiceReporter() + : MemoryMultiReporter("preference-service") + {} + + NS_IMETHOD CollectReports(nsIMemoryReporterCallback* aCallback, + nsISupports* aData); + protected: static const uint32_t kSuspectReferentCount = 1000; static PLDHashOperator CountReferents(PrefCallback* aKey, @@ -222,15 +225,6 @@ protected: void* aClosure); }; -NS_IMPL_ISUPPORTS1(PreferencesReporter, nsIMemoryReporter) - -NS_IMETHODIMP -PreferencesReporter::GetName(nsACString& aName) -{ - aName.AssignLiteral("preference-service"); - return NS_OK; -} - struct PreferencesReferentCount { PreferencesReferentCount() : numStrong(0), numWeakAlive(0), numWeakDead(0) {} size_t numStrong; @@ -242,9 +236,9 @@ struct PreferencesReferentCount { }; PLDHashOperator -PreferencesReporter::CountReferents(PrefCallback* aKey, - nsAutoPtr& aCallback, - void* aClosure) +PreferenceServiceReporter::CountReferents(PrefCallback* aKey, + nsAutoPtr& aCallback, + void* aClosure) { PreferencesReferentCount* referentCount = static_cast(aClosure); @@ -279,8 +273,8 @@ PreferencesReporter::CountReferents(PrefCallback* aKey, } NS_IMETHODIMP -PreferencesReporter::CollectReports(nsIMemoryReporterCallback* aCb, - nsISupports* aClosure) +PreferenceServiceReporter::CollectReports(nsIMemoryReporterCallback* aCb, + nsISupports* aClosure) { #define REPORT(_path, _kind, _units, _amount, _desc) \ do { \ @@ -293,7 +287,7 @@ PreferencesReporter::CollectReports(nsIMemoryReporterCallback* aCb, REPORT(NS_LITERAL_CSTRING("explicit/preferences"), nsIMemoryReporter::KIND_HEAP, nsIMemoryReporter::UNITS_BYTES, - Preferences::SizeOfIncludingThisAndOtherStuff(PreferencesMallocSizeOf), + Preferences::SizeOfIncludingThisAndOtherStuff(MallocSizeOf), "Memory used by the preferences system."); nsPrefBranch* rootBranch = @@ -347,7 +341,7 @@ class AddPreferencesMemoryReporterRunnable : public nsRunnable { NS_IMETHOD Run() { - return NS_RegisterMemoryReporter(new PreferencesReporter()); + return NS_RegisterMemoryReporter(new PreferenceServiceReporter()); } }; } // anonymous namespace diff --git a/modules/libpref/src/nsPrefBranch.h b/modules/libpref/src/nsPrefBranch.h index b9477f050ce4..8884e60a2846 100644 --- a/modules/libpref/src/nsPrefBranch.h +++ b/modules/libpref/src/nsPrefBranch.h @@ -24,13 +24,13 @@ #include "mozilla/MemoryReporting.h" namespace mozilla { -class PreferencesReporter; +class PreferenceServiceReporter; } // namespace mozilla; class nsPrefBranch; class PrefCallback : public PLDHashEntryHdr { - friend class mozilla::PreferencesReporter; + friend class mozilla::PreferenceServiceReporter; public: typedef PrefCallback* KeyType; @@ -178,7 +178,7 @@ class nsPrefBranch : public nsIPrefBranchInternal, public nsIObserver, public nsSupportsWeakReference { - friend class mozilla::PreferencesReporter; + friend class mozilla::PreferenceServiceReporter; public: NS_DECL_THREADSAFE_ISUPPORTS NS_DECL_NSIPREFBRANCH diff --git a/storage/src/mozStorageService.cpp b/storage/src/mozStorageService.cpp index b67fe1da4ad6..caf1379db3f2 100644 --- a/storage/src/mozStorageService.cpp +++ b/storage/src/mozStorageService.cpp @@ -59,7 +59,7 @@ StorageSQLiteDistinguishedAmount() return ::sqlite3_memory_used(); } -class StorageSQLiteReporter MOZ_FINAL : public nsIMemoryReporter +class StorageSQLiteReporter MOZ_FINAL : public MemoryMultiReporter { private: Service *mService; // a weakref because Service contains a strongref to this @@ -68,10 +68,9 @@ private: nsCString mSchemaDesc; public: - NS_DECL_THREADSAFE_ISUPPORTS - StorageSQLiteReporter(Service *aService) - : mService(aService) + : MemoryMultiReporter("storage-sqlite") + , mService(aService) { mStmtDesc = NS_LITERAL_CSTRING( "Memory (approximate) used by all prepared statements used by " @@ -86,12 +85,6 @@ public: "associated with connections to this database."); } - NS_IMETHOD GetName(nsACString &aName) - { - aName.AssignLiteral("storage-sqlite-multi"); - return NS_OK; - } - // Warning: To get a Connection's measurements requires holding its lock. // There may be a delay getting the lock if another thread is accessing the // Connection. This isn't very nice if CollectReports is called from the @@ -204,11 +197,6 @@ private: } }; -NS_IMPL_ISUPPORTS1( - StorageSQLiteReporter, - nsIMemoryReporter -) - //////////////////////////////////////////////////////////////////////////////// //// Service diff --git a/xpcom/base/nsCycleCollector.cpp b/xpcom/base/nsCycleCollector.cpp index 1bcd577a9e57..ba01e6f0f6cf 100644 --- a/xpcom/base/nsCycleCollector.cpp +++ b/xpcom/base/nsCycleCollector.cpp @@ -2387,21 +2387,14 @@ nsCycleCollector::CollectWhite() // Memory reporter //////////////////////// -class CycleCollectorReporter MOZ_FINAL : public nsIMemoryReporter +class CycleCollectorReporter MOZ_FINAL : public MemoryMultiReporter { public: CycleCollectorReporter(nsCycleCollector* aCollector) - : mCollector(aCollector) + : MemoryMultiReporter("cycle-collector"), + mCollector(aCollector) {} - NS_DECL_ISUPPORTS - - NS_IMETHOD GetName(nsACString& name) - { - name.AssignLiteral("cycle-collector"); - return NS_OK; - } - NS_IMETHOD CollectReports(nsIMemoryReporterCallback* aCb, nsISupports* aClosure) { @@ -2461,8 +2454,6 @@ class CycleCollectorReporter MOZ_FINAL : public nsIMemoryReporter nsCycleCollector* mCollector; }; -NS_IMPL_ISUPPORTS1(CycleCollectorReporter, nsIMemoryReporter) - //////////////////////////////////////////////////////////////////////// // Collector implementation diff --git a/xpcom/base/nsIMemoryReporter.idl b/xpcom/base/nsIMemoryReporter.idl index e94f80629357..4a6a551ba458 100644 --- a/xpcom/base/nsIMemoryReporter.idl +++ b/xpcom/base/nsIMemoryReporter.idl @@ -492,18 +492,18 @@ namespace mozilla { // memory uni-reporters. You just need to provide the following. // - The constant values: nameAndPath (which serves as both the reporters name, // and the path in its single report), kind, units, and description. They -// are passed to the MemoryUniReporter constructor. -// - A (private) Amount() or (public) GetAmount() method. It can use the +// are arguments to the MemoryUniReporter constructor. +// - A private Amount() or public GetAmount() method. It can use the // MallocSizeOf method if necessary. (There is also // MallocSizeOfOn{Alloc,Free}, which can be useful.) Use Amount() if the // reporter is infallible, and GetAmount() otherwise. (If you fail to // provide one or the other, you'll get assertion failures when the memory // reporter runs.) // -// The class name of subclasses should match the path, minus the "explicit" -// (if present), and with "Reporter" at the end. For example: -// - "explicit/dom/xyzzy" --> DOMXyzzyReporter -// - "js-compartments/system" --> JSCompartmentsSystemReporter +// The class name of subclasses should match the nameAndPath, minus the +// "explicit" (if present), and with "Reporter" at the end. For example: +// - nameAndPath == "explicit/dom/xyzzy" --> DOMXyzzyReporter +// - nameAndPath == "js-compartments/system" --> JSCompartmentsSystemReporter // class MemoryUniReporter : public nsIMemoryReporter { @@ -561,6 +561,46 @@ protected: const nsCString mDescription; }; +// The following base class reduces the amount of boilerplate code required for +// memory multi-reporters. You just need to provide the following. +// - The constant value: name. It is an argument to the MemoryMultiReporter +// constructor. The name of each multi-reporter should be unique. +// - A public CollectReports() method. It can use the MallocSizeOf method if +// necessary. (There is also MallocSizeOfOn{Alloc,Free}, which can be +// useful.) +// +// The class name of subclasses should match the name, with "Reporter" at +// the end. For example: +// - name == "foo" --> FooMultiReporter +// +class MemoryMultiReporter : public nsIMemoryReporter +{ +public: + MemoryMultiReporter(const char* aName) + : mName(aName) + {} + + virtual ~MemoryMultiReporter() {} + + NS_DECL_THREADSAFE_ISUPPORTS + + NS_IMETHOD GetName(nsACString& aName) + { + aName.Assign(mName); + return NS_OK; + } + + NS_IMETHOD CollectReports(nsIMemoryReporterCallback* aCb, + nsISupports* aClosure) = 0; + +protected: + NS_MEMORY_REPORTER_MALLOC_SIZEOF_FUN(MallocSizeOf) + NS_MEMORY_REPORTER_MALLOC_SIZEOF_ON_ALLOC_FUN(MallocSizeOfOnAlloc) + NS_MEMORY_REPORTER_MALLOC_SIZEOF_ON_FREE_FUN(MallocSizeOfOnFree) + + const nsCString mName; +}; + } // namespace mozilla %} diff --git a/xpcom/base/nsMemoryReporterManager.cpp b/xpcom/base/nsMemoryReporterManager.cpp index d45f4addfb5c..bf3f6ec5914f 100644 --- a/xpcom/base/nsMemoryReporterManager.cpp +++ b/xpcom/base/nsMemoryReporterManager.cpp @@ -667,20 +667,13 @@ private: namespace mozilla { namespace dmd { -class DMDReporter MOZ_FINAL : public nsIMemoryReporter +class DMDReporter MOZ_FINAL : public MemoryMultiReporter { public: DMDReporter() + : MemoryMultiReporter("dmd") {} - NS_DECL_ISUPPORTS - - NS_IMETHOD GetName(nsACString& aName) - { - aName.Assign("dmd"); - return NS_OK; - } - NS_IMETHOD CollectReports(nsIHandleReport* aHandleReport, nsISupports* aData) { @@ -721,8 +714,6 @@ public: } }; -NS_IMPL_ISUPPORTS1(DMDReporter, nsIMemoryReporter) - } // namespace dmd } // namespace mozilla @@ -1542,6 +1533,7 @@ nsMemoryReporterManager::SizeOfTab(nsIDOMWindow* aTopWindow, // thread-safe just to be safe. Memory reporters are created and destroyed // infrequently enough that the performance cost should be negligible. NS_IMPL_ISUPPORTS1(MemoryUniReporter, nsIMemoryReporter) +NS_IMPL_ISUPPORTS1(MemoryMultiReporter, nsIMemoryReporter) nsresult NS_RegisterMemoryReporter(nsIMemoryReporter* aReporter) diff --git a/xpcom/ds/nsObserverService.cpp b/xpcom/ds/nsObserverService.cpp index d233be2bd0a7..b62b5d188531 100644 --- a/xpcom/ds/nsObserverService.cpp +++ b/xpcom/ds/nsObserverService.cpp @@ -42,26 +42,22 @@ GetObserverServiceLog() namespace mozilla { -class ObserverServiceReporter MOZ_FINAL : public nsIMemoryReporter +class ObserverServiceReporter MOZ_FINAL : public MemoryMultiReporter { public: - NS_DECL_ISUPPORTS - NS_DECL_NSIMEMORYREPORTER + ObserverServiceReporter() + : MemoryMultiReporter("observer-service") + {} + + NS_IMETHOD CollectReports(nsIMemoryReporterCallback *aCb, + nsISupports *aClosure); + protected: static const size_t kSuspectReferentCount = 100; static PLDHashOperator CountReferents(nsObserverList* aObserverList, void* aClosure); }; -NS_IMPL_ISUPPORTS1(ObserverServiceReporter, nsIMemoryReporter) - -NS_IMETHODIMP -ObserverServiceReporter::GetName(nsACString& aName) -{ - aName.AssignLiteral("observer-service"); - return NS_OK; -} - struct SuspectObserver { SuspectObserver(const char* aTopic, size_t aReferentCount) : topic(aTopic), referentCount(aReferentCount) {}