Bug 705602 - Use mallocSizeOf in prefixset memory reporter. r=gpc.
authorNicholas Nethercote <nnethercote@mozilla.com>
Tue, 13 Dec 2011 20:54:18 -0800
changeset 84356 03af7a7c3dc33775cd55aaaca814c57858126b81
parent 84355 54b7ede5ff3960cd05d6ba8b2991ce4a201d80d7
child 84357 47ab01e631b71783b018dde0f6f192b81525379c
push idunknown
push userunknown
push dateunknown
reviewersgpc
bugs705602
milestone11.0a1
Bug 705602 - Use mallocSizeOf in prefixset memory reporter. r=gpc.
toolkit/components/build/nsToolkitCompsCID.h
toolkit/components/url-classifier/nsIUrlClassifierPrefixSet.idl
toolkit/components/url-classifier/nsUrlClassifierDBService.cpp
toolkit/components/url-classifier/nsUrlClassifierPrefixSet.cpp
toolkit/components/url-classifier/nsUrlClassifierPrefixSet.h
--- a/toolkit/components/build/nsToolkitCompsCID.h
+++ b/toolkit/components/build/nsToolkitCompsCID.h
@@ -158,19 +158,19 @@
 // {59648a91-5a60-4122-8ff2-54b839c84aed}
 #define NS_PARENTALCONTROLSSERVICE_CID \
 { 0x580530e5, 0x118c, 0x4bc7, { 0xab, 0x88, 0xbc, 0x2c, 0xd2, 0xb9, 0x72, 0x23 } }
 
 // {e7f70966-9a37-48d7-8aeb-35998f31090e}
 #define NS_TYPEAHEADFIND_CID \
 { 0xe7f70966, 0x9a37, 0x48d7, { 0x8a, 0xeb, 0x35, 0x99, 0x8f, 0x31, 0x09, 0x0e} }
 
-// {51464459-4e46-4f31-8745-4acfa7c1e2f2}
+// {7902e243-9b00-4673-9288-1b7fc246a8f8}
 #define NS_URLCLASSIFIERPREFIXSET_CID \
-{ 0x51464459, 0x4e46, 0x4f31, { 0x87, 0x45, 0x4a, 0xcf, 0xa7, 0xc1, 0xe2, 0xf2} }
+{ 0x7902e243, 0x9b00, 0x4673, { 0x92, 0x88, 0x1b, 0x7f, 0xc2, 0x46, 0xa8, 0xf8} }
 
 // {5eb7c3c1-ec1f-4007-87cc-eefb37d68ce6}
 #define NS_URLCLASSIFIERDBSERVICE_CID \
 { 0x5eb7c3c1, 0xec1f, 0x4007, { 0x87, 0xcc, 0xee, 0xfb, 0x37, 0xd6, 0x8c, 0xe6} }
 
 // {c2be6dc0-ef1e-4abd-86a2-4f864ddc57f6}
 #define NS_URLCLASSIFIERSTREAMUPDATER_CID \
 { 0xc2be6dc0, 0xef1e, 0x4abd, { 0x86, 0xa2, 0x4f, 0x86, 0x4d, 0xdc, 0x57, 0xf6} }
--- a/toolkit/components/url-classifier/nsIUrlClassifierPrefixSet.idl
+++ b/toolkit/components/url-classifier/nsIUrlClassifierPrefixSet.idl
@@ -36,24 +36,23 @@
  *
  * ***** END LICENSE BLOCK ***** */
 
 #include "nsISupports.idl"
 #include "nsIFile.idl"
 
 interface nsIArray;
 
-[scriptable, uuid(51464459-4e46-4f31-8745-4acfa7c1e2f2)]
+[scriptable, uuid(7902e243-9b00-4673-9288-1b7fc246a8f8)]
 interface nsIUrlClassifierPrefixSet : nsISupports
 {
   void setPrefixes([const, array, size_is(aLength)] in unsigned long aPrefixes,
                    in unsigned long aLength);
   void addPrefixes([const, array, size_is(aLength)] in unsigned long aPrefixes,
                    in unsigned long aLength);
   boolean contains(in unsigned long aPrefix);
   boolean probe(in unsigned long aPrefix, in unsigned long aKey,
                 inout boolean aReady);
-  PRUint32 sizeOfIncludingThis();
   PRUint32 getKey();
   boolean isEmpty();
   void loadFromFile(in nsIFile aFile);
   void storeToFile(in nsIFile aFile);
 };
--- a/toolkit/components/url-classifier/nsUrlClassifierDBService.cpp
+++ b/toolkit/components/url-classifier/nsUrlClassifierDBService.cpp
@@ -3686,20 +3686,18 @@ nsUrlClassifierDBServiceWorker::LoadPref
   }
   if (!exists || NS_FAILED(rv)) {
     LOG(("no (usable) stored PrefixSet found, constructing from store"));
     rv = ConstructPrefixSet();
     NS_ENSURE_SUCCESS(rv, rv);
   }
 
 #ifdef DEBUG
-  PRUint32 size = 0;
-  rv = mPrefixSet->SizeOfIncludingThis(&size);
-  LOG(("SB tree done, size = %d bytes\n", size));
-  NS_ENSURE_SUCCESS(rv, rv);
+  LOG(("SB tree done, size = %d bytes\n",
+       mPrefixSet->SizeOfIncludingThis(moz_malloc_size_of)));
 #endif
 #if defined(PR_LOGGING)
   if (LOG_ENABLED()) {
     PRIntervalTime clockEnd = PR_IntervalNow();
     LOG(("Loading took %dms\n",
          PR_IntervalToMilliseconds(clockEnd - clockStart)));
   }
 #endif
--- a/toolkit/components/url-classifier/nsUrlClassifierPrefixSet.cpp
+++ b/toolkit/components/url-classifier/nsUrlClassifierPrefixSet.cpp
@@ -79,16 +79,19 @@ public:
 
 private:
   nsCString mPath;
   nsUrlClassifierPrefixSet * mParent;
 };
 
 NS_IMPL_THREADSAFE_ISUPPORTS1(nsPrefixSetReporter, nsIMemoryReporter)
 
+NS_MEMORY_REPORTER_MALLOC_SIZEOF_FUN(StoragePrefixSetMallocSizeOf,
+                                     "storage/prefixset")
+
 nsPrefixSetReporter::nsPrefixSetReporter(nsUrlClassifierPrefixSet * aParent,
                                          const nsACString & aName)
 : mParent(aParent)
 {
   mPath.Assign(NS_LITERAL_CSTRING("explicit/storage/prefixset"));
   if (!aName.IsEmpty()) {
     mPath.Append("/");
     mPath.Append(aName);
@@ -121,20 +124,18 @@ nsPrefixSetReporter::GetUnits(PRInt32 * 
 {
   *aUnits = nsIMemoryReporter::UNITS_BYTES;
   return NS_OK;
 }
 
 NS_IMETHODIMP
 nsPrefixSetReporter::GetAmount(PRInt64 * aAmount)
 {
-  PRUint32 size;
-  nsresult rv = mParent->SizeOfIncludingThis(&size);
-  *aAmount = size;
-  return rv;
+  *aAmount = mParent->SizeOfIncludingThis(StoragePrefixSetMallocSizeOf);
+  return NS_OK;
 }
 
 NS_IMETHODIMP
 nsPrefixSetReporter::GetDescription(nsACString & aDescription)
 {
   aDescription.Assign(NS_LITERAL_CSTRING("Memory used by a PrefixSet for "
                                          "UrlClassifier, in bytes."));
   return NS_OK;
@@ -328,26 +329,26 @@ nsUrlClassifierPrefixSet::Contains(PRUin
 
   if (diff == 0) {
     *aFound = true;
   }
 
   return NS_OK;
 }
 
-NS_IMETHODIMP
-nsUrlClassifierPrefixSet::SizeOfIncludingThis(PRUint32 * aSize)
+size_t
+nsUrlClassifierPrefixSet::SizeOfIncludingThis(nsMallocSizeOfFun mallocSizeOf)
 {
   MutexAutoLock lock(mPrefixSetLock);
-  size_t usable = moz_malloc_usable_size(this);
-  *aSize = (PRUint32)(usable ? usable : sizeof(*this));
-  *aSize += mDeltas.SizeOf();
-  *aSize += mIndexPrefixes.SizeOf();
-  *aSize += mIndexStarts.SizeOf();
-  return NS_OK;
+  size_t n = 0;
+  n += mallocSizeOf(this, sizeof(nsUrlClassifierPrefixSet));
+  n += mDeltas.SizeOf();
+  n += mIndexPrefixes.SizeOf();
+  n += mIndexStarts.SizeOf();
+  return n;
 }
 
 NS_IMETHODIMP
 nsUrlClassifierPrefixSet::IsEmpty(bool * aEmpty)
 {
   MutexAutoLock lock(mPrefixSetLock);
   *aEmpty = !mHasPrefixes;
   return NS_OK;
--- a/toolkit/components/url-classifier/nsUrlClassifierPrefixSet.h
+++ b/toolkit/components/url-classifier/nsUrlClassifierPrefixSet.h
@@ -65,27 +65,28 @@ public:
   // anything currently in the Prefix Set
   NS_IMETHOD AddPrefixes(const PRUint32* aArray, PRUint32 aLength);
   // Does the PrefixSet contain this prefix? not thread-safe
   NS_IMETHOD Contains(PRUint32 aPrefix, bool* aFound);
   // Do a lookup in the PrefixSet
   // if aReady is set, we will block until there are any entries
   // if not set, we will return in aReady whether we were ready or not
   NS_IMETHOD Probe(PRUint32 aPrefix, PRUint32 aKey, bool* aReady, bool* aFound);
-  // Return the estimated size of the set on disk and in memory,
-  // in bytes
-  NS_IMETHOD SizeOfIncludingThis(PRUint32* aSize);
   NS_IMETHOD IsEmpty(bool * aEmpty);
   NS_IMETHOD LoadFromFile(nsIFile* aFile);
   NS_IMETHOD StoreToFile(nsIFile* aFile);
   // Return a key that is used to randomize the collisions in the prefixes
   NS_IMETHOD GetKey(PRUint32* aKey);
 
   NS_DECL_ISUPPORTS
 
+  // Return the estimated size of the set on disk and in memory,
+  // in bytes
+  size_t SizeOfIncludingThis(nsMallocSizeOfFun mallocSizeOf);
+
 protected:
   static const PRUint32 DELTAS_LIMIT = 100;
   static const PRUint32 MAX_INDEX_DIFF = (1 << 16);
   static const PRUint32 PREFIXSET_VERSION_MAGIC = 1;
 
   mozilla::Mutex mPrefixSetLock;
   mozilla::CondVar mSetIsReady;
   nsRefPtr<nsPrefixSetReporter> mReporter;