Bug 682215 - add a memory reporter for memory from libvpx; r=ted,njn
authorNathan Froyd <froydnj@mozilla.com>
Tue, 25 Feb 2014 16:35:13 -0500
changeset 191891 3719528f82525aece9f62148f54fb02ca3020f1b
parent 191890 af491832ff954900098bac1053d9d59359c984bd
child 191892 cef5e324878788d2d31722b19fda612fb7b37612
push id474
push userasasaki@mozilla.com
push dateMon, 02 Jun 2014 21:01:02 +0000
treeherdermozilla-release@967f4cf1b31c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersted, njn
bugs682215
milestone30.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 682215 - add a memory reporter for memory from libvpx; r=ted,njn
layout/media/symbols.def.in
xpcom/build/moz.build
xpcom/build/nsXPComInit.cpp
--- a/layout/media/symbols.def.in
+++ b/layout/media/symbols.def.in
@@ -56,16 +56,17 @@ vpx_codec_enc_init_ver
 vpx_codec_vp8_cx
 vpx_codec_vp9_cx
 vpx_img_set_rect
 vpx_img_wrap
 vpx_codec_get_cx_data
 vpx_codec_enc_config_default
 vpx_img_alloc
 vpx_codec_encode
+vpx_mem_set_functions
 #endif
 #endif
 #ifdef MOZ_VORBIS
 ogg_page_bos
 ogg_page_granulepos
 ogg_page_serialno
 ogg_set_mem_functions
 ogg_stream_check
--- a/xpcom/build/moz.build
+++ b/xpcom/build/moz.build
@@ -86,8 +86,13 @@ LOCAL_INCLUDES += [
     '../ds',
     '../glue',
     '../io',
     '../reflect/xptinfo/src',
     '../threads',
     '/chrome/src',
     '/docshell/base',
 ]
+
+if CONFIG['MOZ_VPX']:
+    LOCAL_INCLUDES += [
+        '/media/libvpx',
+    ]
--- a/xpcom/build/nsXPComInit.cpp
+++ b/xpcom/build/nsXPComInit.cpp
@@ -125,16 +125,19 @@ extern nsresult nsStringInputStreamConst
 #include "mozilla/ClearOnShutdown.h"
 #include "mozilla/SystemMemoryReporter.h"
 
 #ifdef MOZ_VISUAL_EVENT_TRACER
 #include "mozilla/VisualEventTracer.h"
 #endif
 
 #include "ogg/ogg.h"
+#ifdef MOZ_VPX
+#include "vpx_mem/vpx_mem.h"
+#endif
 
 #include "GeckoProfiler.h"
 
 #include "jsapi.h"
 
 using namespace mozilla;
 using base::AtExitManager;
 using mozilla::ipc::BrowserProcessSubThread;
@@ -464,16 +467,90 @@ private:
             "Memory allocated through libogg for Ogg, Theora, and related media files.");
     }
 };
 
 NS_IMPL_ISUPPORTS1(OggReporter, nsIMemoryReporter)
 
 /* static */ Atomic<size_t> OggReporter::sAmount;
 
+#ifdef MOZ_VPX
+class VPXReporter MOZ_FINAL : public nsIMemoryReporter
+{
+public:
+    NS_DECL_ISUPPORTS
+
+    VPXReporter()
+    {
+#ifdef DEBUG
+        // There must be only one instance of this class, due to |sAmount|
+        // being static.
+        static bool hasRun = false;
+        MOZ_ASSERT(!hasRun);
+        hasRun = true;
+#endif
+        sAmount = 0;
+    }
+
+    static void* Alloc(size_t size)
+    {
+        void* p = malloc(size);
+        sAmount += MallocSizeOfOnAlloc(p);
+        return p;
+    }
+
+    static void* Realloc(void* p, size_t size)
+    {
+        sAmount -= MallocSizeOfOnFree(p);
+        void *pnew = realloc(p, size);
+        if (pnew) {
+            sAmount += MallocSizeOfOnAlloc(pnew);
+        } else {
+            // realloc failed;  undo the decrement from above
+            sAmount += MallocSizeOfOnAlloc(p);
+        }
+        return pnew;
+    }
+
+    static void* Calloc(size_t nmemb, size_t size)
+    {
+        void* p = calloc(nmemb, size);
+        sAmount += MallocSizeOfOnAlloc(p);
+        return p;
+    }
+
+    static void Free(void* p)
+    {
+        sAmount -= MallocSizeOfOnFree(p);
+        free(p);
+    }
+
+private:
+    // |sAmount| can be (implicitly) accessed by multiple threads, so it
+    // must be thread-safe.
+    static Atomic<size_t> sAmount;
+
+    MOZ_DEFINE_MALLOC_SIZE_OF(MallocSizeOf)
+    MOZ_DEFINE_MALLOC_SIZE_OF_ON_ALLOC(MallocSizeOfOnAlloc)
+    MOZ_DEFINE_MALLOC_SIZE_OF_ON_FREE(MallocSizeOfOnFree)
+
+    NS_IMETHODIMP
+    CollectReports(nsIHandleReportCallback* aHandleReport, nsISupports* aData)
+    {
+        return MOZ_COLLECT_REPORT(
+            "explicit/media/libvpx", KIND_HEAP, UNITS_BYTES, sAmount,
+            "Memory allocated through libvpx for WebM media files.");
+    }
+};
+
+NS_IMPL_ISUPPORTS1(VPXReporter, nsIMemoryReporter)
+
+/* static */ Atomic<size_t> VPXReporter::sAmount;
+#endif /* MOZ_VPX */
+
 EXPORT_XPCOM_API(nsresult)
 NS_InitXPCOM2(nsIServiceManager* *result,
               nsIFile* binDirectory,
               nsIDirectoryServiceProvider* appFileLocationProvider)
 {
     mozPoisonValueInit();
 
     char aLocal;
@@ -623,16 +700,27 @@ NS_InitXPCOM2(nsIServiceManager* *result
     mozilla::SetICUMemoryFunctions();
 
     // Do the same for libogg.
     ogg_set_mem_functions(OggReporter::Alloc,
                           OggReporter::Calloc,
                           OggReporter::Realloc,
                           OggReporter::Free);
 
+#ifdef MOZ_VPX
+    // And for VPX.
+    vpx_mem_set_functions(VPXReporter::Alloc,
+                          VPXReporter::Calloc,
+                          VPXReporter::Realloc,
+                          VPXReporter::Free,
+                          memcpy,
+                          memset,
+                          memmove);
+#endif
+
     // Initialize the JS engine.
     if (!JS_Init()) {
         NS_RUNTIMEABORT("JS_Init failed");
     }
 
     rv = nsComponentManagerImpl::gComponentManager->Init();
     if (NS_FAILED(rv))
     {
@@ -670,20 +758,22 @@ NS_InitXPCOM2(nsIServiceManager* *result
 
     // We only want the SystemMemoryReporter running in one process, because it
     // profiles the entire system.  The main process is the obvious place for
     // it.
     if (XRE_GetProcessType() == GeckoProcessType_Default) {
         mozilla::SystemMemoryReporter::Init();
     }
 
-    // The memory reporter manager is up and running -- register a reporter for
-    // ICU's and libogg's memory usage.
+    // The memory reporter manager is up and running -- register our reporters.
     RegisterStrongMemoryReporter(new ICUReporter());
     RegisterStrongMemoryReporter(new OggReporter());
+#ifdef MOZ_VPX
+    RegisterStrongMemoryReporter(new VPXReporter());
+#endif
 
     mozilla::Telemetry::Init();
 
     mozilla::HangMonitor::Startup();
     mozilla::BackgroundHangMonitor::Startup();
 
     const MessageLoop* const loop = MessageLoop::current();
     sMainHangMonitor = new mozilla::BackgroundHangMonitor(