Bug 831193 (part 8) - Don't use NS_MEMORY_REPORTER_IMPLEMENT in StartupCache.cpp. r=nfroyd.
authorNicholas Nethercote <nnethercote@mozilla.com>
Thu, 17 Jan 2013 16:45:11 -0800
changeset 157578 5c86a02a4c2f5f15e8f6b14d1c40c30243c01517
parent 157577 24791c073aeee2fd1af5199c4bc6e9c3b6425a3b
child 157579 24b0e5ef846ae160051aa86a8ff10a3af2fc0781
push id2961
push userlsblakk@mozilla.com
push dateMon, 28 Oct 2013 21:59:28 +0000
treeherdermozilla-beta@73ef4f13486f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersnfroyd
bugs831193
milestone26.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 831193 (part 8) - Don't use NS_MEMORY_REPORTER_IMPLEMENT in StartupCache.cpp. r=nfroyd.
startupcache/StartupCache.cpp
startupcache/StartupCache.h
--- a/startupcache/StartupCache.cpp
+++ b/startupcache/StartupCache.cpp
@@ -1,9 +1,10 @@
-/* -*-  Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*- */
+/* -*- Mode: C++; tab-width: 8; 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/. */
 
 #include "prio.h"
 #include "prtypes.h"
 #include "pldhash.h"
 #include "nsXPCOMStrings.h"
@@ -47,72 +48,76 @@
 #define SC_WORDSIZE "4"
 #else
 #define SC_WORDSIZE "8"
 #endif
 
 namespace mozilla {
 namespace scache {
 
-static int64_t
-GetStartupCacheMappingSize()
+class StartupCacheMappingReporter MOZ_FINAL : public MemoryReporterBase
 {
-    mozilla::scache::StartupCache* sc = mozilla::scache::StartupCache::GetSingleton();
+public:
+  StartupCacheMappingReporter()
+    : MemoryReporterBase("explicit/startup-cache/mapping",
+                         KIND_NONHEAP, UNITS_BYTES,
+"Memory used to hold the mapping of the startup cache from file.  This memory "
+"is likely to be swapped out shortly after start-up.")
+  {}
+private:
+  int64_t Amount() MOZ_OVERRIDE
+  {
+    mozilla::scache::StartupCache* sc =
+      mozilla::scache::StartupCache::GetSingleton();
     return sc ? sc->SizeOfMapping() : 0;
-}
-
-NS_MEMORY_REPORTER_IMPLEMENT(StartupCacheMapping,
-    "explicit/startup-cache/mapping",
-    KIND_NONHEAP,
-    nsIMemoryReporter::UNITS_BYTES,
-    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.")
+  }
+};
 
-NS_MEMORY_REPORTER_MALLOC_SIZEOF_FUN(StartupCacheDataMallocSizeOf)
-
-static int64_t
-GetStartupCacheDataSize()
+class StartupCacheDataReporter MOZ_FINAL : public MemoryReporterBase
 {
-    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.")
+public:
+  StartupCacheDataReporter()
+    : MemoryReporterBase("explicit/startup-cache/data", KIND_HEAP, UNITS_BYTES,
+"Memory used by the startup cache for things other than the file mapping.")
+  {}
+private:
+  int64_t Amount() MOZ_OVERRIDE
+  {
+    mozilla::scache::StartupCache* sc =
+      mozilla::scache::StartupCache::GetSingleton();
+    return sc ? sc->HeapSizeOfIncludingThis(MallocSizeOf) : 0;
+  }
+};
 
 static const char sStartupCacheName[] = "startupCache." SC_WORDSIZE "." SC_ENDIAN;
 static NS_DEFINE_CID(kZipReaderCID, NS_ZIPREADER_CID);
 
 StartupCache*
-StartupCache::GetSingleton() 
+StartupCache::GetSingleton()
 {
   if (!gStartupCache) {
     if (XRE_GetProcessType() != GeckoProcessType_Default) {
       return nullptr;
     }
 
     StartupCache::InitSingleton();
   }
 
   return StartupCache::gStartupCache;
 }
 
 void
 StartupCache::DeleteSingleton()
 {
   delete StartupCache::gStartupCache;
+  StartupCache::gStartupCache = nullptr;
 }
 
 nsresult
-StartupCache::InitSingleton() 
+StartupCache::InitSingleton()
 {
   nsresult rv;
   StartupCache::gStartupCache = new StartupCache();
 
   rv = StartupCache::gStartupCache->Init();
   if (NS_FAILED(rv)) {
     delete StartupCache::gStartupCache;
     StartupCache::gStartupCache = nullptr;
@@ -120,52 +125,50 @@ StartupCache::InitSingleton()
   return rv;
 }
 
 StartupCache* StartupCache::gStartupCache;
 bool StartupCache::gShutdownInitiated;
 bool StartupCache::gIgnoreDiskCache;
 enum StartupCache::TelemetrifyAge StartupCache::gPostFlushAgeAction = StartupCache::IGNORE_AGE;
 
-StartupCache::StartupCache() 
-  : mArchive(nullptr), mStartupWriteInitiated(false), mWriteThread(nullptr),
-    mMappingMemoryReporter(nullptr), mDataMemoryReporter(nullptr) { }
+StartupCache::StartupCache()
+  : mArchive(nullptr), mStartupWriteInitiated(false), mWriteThread(nullptr)
+{ }
 
-StartupCache::~StartupCache() 
+StartupCache::~StartupCache()
 {
   if (mTimer) {
     mTimer->Cancel();
   }
 
   // Generally, the in-memory table should be empty here,
-  // but an early shutdown means either mTimer didn't run 
+  // but an early shutdown means either mTimer didn't run
   // or the write thread is still running.
   WaitOnWriteThread();
 
   // If we shutdown quickly timer wont have fired. Instead of writing
   // it on the main thread and block the shutdown we simply wont update
   // the startup cache. Always do this if the file doesn't exist since
   // we use it part of the packge step.
   if (!mArchive) {
     WriteToDisk();
   }
 
   gStartupCache = nullptr;
-  (void)::NS_UnregisterMemoryReporter(mMappingMemoryReporter);
-  (void)::NS_UnregisterMemoryReporter(mDataMemoryReporter);
-  mMappingMemoryReporter = nullptr;
-  mDataMemoryReporter = nullptr;
+  NS_UnregisterMemoryReporter(mMappingReporter);
+  NS_UnregisterMemoryReporter(mDataReporter);
 }
 
 nsresult
-StartupCache::Init() 
+StartupCache::Init()
 {
   // workaround for bug 653936
   nsCOMPtr<nsIProtocolHandler> jarInitializer(do_GetService(NS_NETWORK_PROTOCOL_CONTRACTID_PREFIX "jar"));
-  
+
   nsresult rv;
   mTable.Init();
 #ifdef DEBUG
   mWriteObjectMap.Init();
 #endif
 
   // This allows to override the startup cache filename
   // which is useful from xpcshell, when there is no ProfLDS directory to keep cache in.
@@ -201,55 +204,55 @@ StartupCache::Init()
     // Try to create the directory if it's not there yet
     rv = file->Create(nsIFile::DIRECTORY_TYPE, 0777);
     if (NS_FAILED(rv) && rv != NS_ERROR_FILE_ALREADY_EXISTS)
       return rv;
 
     rv = file->AppendNative(NS_LITERAL_CSTRING(sStartupCacheName));
 
     NS_ENSURE_SUCCESS(rv, rv);
-    
+
     mFile = do_QueryInterface(file);
   }
 
   NS_ENSURE_TRUE(mFile, NS_ERROR_UNEXPECTED);
 
   mObserverService = do_GetService("@mozilla.org/observer-service;1");
-  
+
   if (!mObserverService) {
     NS_WARNING("Could not get observerService.");
     return NS_ERROR_UNEXPECTED;
   }
-  
-  mListener = new StartupCacheListener();  
+
+  mListener = new StartupCacheListener();
   rv = mObserverService->AddObserver(mListener, NS_XPCOM_SHUTDOWN_OBSERVER_ID,
                                      false);
   NS_ENSURE_SUCCESS(rv, rv);
   rv = mObserverService->AddObserver(mListener, "startupcache-invalidate",
                                      false);
   NS_ENSURE_SUCCESS(rv, rv);
 
   rv = LoadArchive(RECORD_AGE);
-  
+
   // Sometimes we don't have a cache yet, that's ok.
   // If it's corrupted, just remove it and start over.
   if (gIgnoreDiskCache || (NS_FAILED(rv) && rv != NS_ERROR_FILE_NOT_FOUND)) {
     NS_WARNING("Failed to load startupcache file correctly, removing!");
     InvalidateCache();
   }
 
-  mMappingMemoryReporter = new NS_MEMORY_REPORTER_NAME(StartupCacheMapping);
-  mDataMemoryReporter    = new NS_MEMORY_REPORTER_NAME(StartupCacheData);
-  (void)::NS_RegisterMemoryReporter(mMappingMemoryReporter);
-  (void)::NS_RegisterMemoryReporter(mDataMemoryReporter);
+  mMappingReporter = new StartupCacheMappingReporter();
+  mDataReporter    = new StartupCacheDataReporter();
+  NS_RegisterMemoryReporter(mMappingReporter);
+  NS_RegisterMemoryReporter(mDataReporter);
 
   return NS_OK;
 }
 
-/** 
+/**
  * LoadArchive can be called from the main thread or while reloading cache on write thread.
  */
 nsresult
 StartupCache::LoadArchive(enum TelemetrifyAge flag)
 {
   if (gIgnoreDiskCache)
     return NS_ERROR_FAILURE;
 
--- a/startupcache/StartupCache.h
+++ b/startupcache/StartupCache.h
@@ -13,16 +13,18 @@
 #include "nsITimer.h"
 #include "nsIObserverService.h"
 #include "nsIObserver.h"
 #include "nsIOutputStream.h"
 #include "nsIFile.h"
 #include "mozilla/Attributes.h"
 #include "mozilla/MemoryReporting.h"
 
+class nsIMemoryReporter;
+
 /**
  * The StartupCache is a persistent cache of simple key-value pairs,
  * where the keys are null-terminated c-strings and the values are 
  * arbitrary data, passed as a (char*, size) tuple. 
  *
  * Clients should use the GetSingleton() static method to access the cache. It 
  * will be available from the end of XPCOM init (NS_InitXPCOM3 in nsXPComInit.cpp), 
  * until XPCOM shutdown begins. The GetSingleton() method will return null if the cache
@@ -59,18 +61,16 @@
  *
  * Some utility functions are provided in StartupCacheUtils. These functions wrap the
  * buffers into object streams, which may be useful for serializing objects. Note
  * the above caution about multiply-referenced objects, though -- the streams are just
  * as 'dumb' as the underlying buffers about multiply-referenced objects. They just
  * provide some convenience in writing out data.
  */
 
-class nsIMemoryReporter;
-
 namespace mozilla {
 namespace scache {
 
 struct CacheEntry 
 {
   nsAutoArrayPtr<char> data;
   uint32_t size;
 
@@ -172,18 +172,18 @@ private:
   static StartupCache *gStartupCache;
   static bool gShutdownInitiated;
   static bool gIgnoreDiskCache;
   PRThread *mWriteThread;
 #ifdef DEBUG
   nsTHashtable<nsISupportsHashKey> mWriteObjectMap;
 #endif
 
-  nsIMemoryReporter* mMappingMemoryReporter;
-  nsIMemoryReporter* mDataMemoryReporter;
+  nsCOMPtr<nsIMemoryReporter> mMappingReporter;
+  nsCOMPtr<nsIMemoryReporter> mDataReporter;
 };
 
 // This debug outputstream attempts to detect if clients are writing multiple
 // references to the same object. We only support that if that object
 // is a singleton.
 #ifdef DEBUG
 class StartupCacheDebugOutputStream MOZ_FINAL
   : public nsIObjectOutputStream