Bug 802894 - Add a memory reporter for nsEffectiveTLDService. r=jduell.
authorNicholas Nethercote <nnethercote@mozilla.com>
Fri, 19 Oct 2012 05:07:13 -0700
changeset 112003 883af38f81c5
parent 112002 10eca8d80a84
child 112004 1858a5369d22
push id93
push usernmatsakis@mozilla.com
push dateWed, 31 Oct 2012 21:26:57 +0000
reviewersjduell
bugs802894
milestone19.0a1
Bug 802894 - Add a memory reporter for nsEffectiveTLDService. r=jduell.
dom/base/nsWindowMemoryReporter.cpp
netwerk/dns/nsEffectiveTLDService.cpp
netwerk/dns/nsEffectiveTLDService.h
--- a/dom/base/nsWindowMemoryReporter.cpp
+++ b/dom/base/nsWindowMemoryReporter.cpp
@@ -300,20 +300,16 @@ nsWindowMemoryReporter::CollectReports(n
   WindowArray windows;
   windowsById->Enumerate(GetWindows, &windows);
 
   // Get the IDs of all the "ghost" windows.
   nsTHashtable<nsUint64HashKey> ghostWindows;
   ghostWindows.Init();
   CheckForGhostWindows(&ghostWindows);
 
-  nsCOMPtr<nsIEffectiveTLDService> tldService = do_GetService(
-    NS_EFFECTIVETLDSERVICE_CONTRACTID);
-  NS_ENSURE_STATE(tldService);
-
   WindowPaths windowPaths;
   windowPaths.Init();
 
   // Collect window memory usage.
   nsWindowSizes windowTotalSizes(NULL);
   for (uint32_t i = 0; i < windows.Length(); i++) {
     nsresult rv = CollectWindowReports(windows[i], &windowTotalSizes,
                                        &ghostWindows, &windowPaths,
--- a/netwerk/dns/nsEffectiveTLDService.cpp
+++ b/netwerk/dns/nsEffectiveTLDService.cpp
@@ -6,20 +6,20 @@
 // This service reads a file of rules describing TLD-like domain names.  For a
 // complete description of the expected file format and parsing rules, see
 // http://wiki.mozilla.org/Gecko:Effective_TLD_Service
 
 #include "mozilla/Util.h"
 
 #include "nsEffectiveTLDService.h"
 #include "nsIIDNService.h"
+#include "nsIMemoryReporter.h"
 #include "nsNetUtil.h"
 #include "prnetdb.h"
 
-
 using namespace mozilla;
 
 NS_IMPL_ISUPPORTS1(nsEffectiveTLDService, nsIEffectiveTLDService)
 
 // ----------------------------------------------------------------------
 
 #define ETLD_STR_NUM_1(line) str##line
 #define ETLD_STR_NUM(line) ETLD_STR_NUM_1(line)
@@ -52,16 +52,35 @@ nsDomainEntry::FuncForStaticAsserts(void
 }
 
 #undef ETLD_ENTRY_OFFSET
 #undef ETLD_STR_NUM
 #undef ETLD_STR_NUM1
 
 // ----------------------------------------------------------------------
 
+static nsEffectiveTLDService *gService = nullptr;
+
+NS_MEMORY_REPORTER_MALLOC_SIZEOF_FUN(EffectiveTLDServiceMallocSizeOf,
+                                     "effective-tld-service")
+
+static int64_t
+GetEffectiveTLDSize()
+{
+  return gService->SizeOfIncludingThis(EffectiveTLDServiceMallocSizeOf);
+}
+
+NS_MEMORY_REPORTER_IMPLEMENT(
+  EffectiveTLDService,
+  "explicit/xpcom/effective-TLD-service",
+  KIND_HEAP,
+  nsIMemoryReporter::UNITS_BYTES,
+  GetEffectiveTLDSize,
+  "Memory used by the effective TLD service.")
+
 nsresult
 nsEffectiveTLDService::Init()
 {
   const ETLDEntry *entries = nsDomainEntry::entries;
 
   // We'll probably have to rehash at least once, since nsTHashtable doesn't
   // use a perfect hash, but at least we'll save a few rehashes along the way.
   // Next optimization here is to precompute the hash using something like
@@ -81,19 +100,46 @@ nsEffectiveTLDService::Init()
     NS_ASSERTION(NS_SUCCEEDED(NormalizeHostname(normalizedName)),
                  "normalization failure!");
     NS_ASSERTION(name.Equals(normalizedName), "domain not normalized!");
 #endif
     nsDomainEntry *entry = mHash.PutEntry(domain);
     NS_ENSURE_TRUE(entry, NS_ERROR_OUT_OF_MEMORY);
     entry->SetData(&entries[i]);
   }
+
+  MOZ_ASSERT(!gService);
+  gService = this;
+  mReporter = new NS_MEMORY_REPORTER_NAME(EffectiveTLDService);
+  (void)::NS_RegisterMemoryReporter(mReporter);
+
   return NS_OK;
 }
 
+nsEffectiveTLDService::~nsEffectiveTLDService()
+{
+  (void)::NS_UnregisterMemoryReporter(mReporter);
+  mReporter = nullptr;
+  gService = nullptr;
+}
+
+size_t
+nsEffectiveTLDService::SizeOfIncludingThis(nsMallocSizeOfFun aMallocSizeOf)
+{
+  size_t n = aMallocSizeOf(this);
+  n += mHash.SizeOfExcludingThis(nullptr, aMallocSizeOf);
+
+  // Measurement of the following members may be added later if DMD finds it is
+  // worthwhile:
+  // - mReporter
+  // - mIDNService
+
+  return n;
+}
+
 // External function for dealing with URI's correctly.
 // Pulls out the host portion from an nsIURI, and calls through to
 // GetPublicSuffixFromHost().
 NS_IMETHODIMP
 nsEffectiveTLDService::GetPublicSuffix(nsIURI     *aURI,
                                        nsACString &aPublicSuffix)
 {
   NS_ENSURE_ARG_POINTER(aURI);
--- a/netwerk/dns/nsEffectiveTLDService.h
+++ b/netwerk/dns/nsEffectiveTLDService.h
@@ -6,16 +6,17 @@
 #include "nsIEffectiveTLDService.h"
 
 #include "nsTHashtable.h"
 #include "nsString.h"
 #include "nsCOMPtr.h"
 #include "mozilla/Attributes.h"
 
 class nsIIDNService;
+class nsIMemoryReporter;
 
 #define ETLD_ENTRY_N_INDEX_BITS 30
 
 // struct for static data generated from effective_tld_names.dat
 struct ETLDEntry {
   uint32_t strtab_index : ETLD_ENTRY_N_INDEX_BITS;
   uint32_t exception : 1;
   uint32_t wild : 1;
@@ -104,16 +105,19 @@ class nsEffectiveTLDService MOZ_FINAL : 
 {
 public:
   NS_DECL_ISUPPORTS
   NS_DECL_NSIEFFECTIVETLDSERVICE
 
   nsEffectiveTLDService() { }
   nsresult Init();
 
+  size_t SizeOfIncludingThis(nsMallocSizeOfFun aMallocSizeOf);
+
 private:
   nsresult GetBaseDomainInternal(nsCString &aHostname, uint32_t aAdditionalParts, nsACString &aBaseDomain);
   nsresult NormalizeHostname(nsCString &aHostname);
-  ~nsEffectiveTLDService() { }
+  ~nsEffectiveTLDService();
 
+  nsIMemoryReporter*          mReporter;
   nsTHashtable<nsDomainEntry> mHash;
   nsCOMPtr<nsIIDNService>     mIDNService;
 };