Bug 1423000 - Use mozjemalloc mutexes in logalloc. r?njn draft
authorMike Hommey <mh+mozilla@glandium.org>
Sun, 03 Dec 2017 14:23:23 +0900
changeset 707241 1fc376243668bec806794257d9d50202d302b6c3
parent 707240 a2316ce065bca594af8c528bbb24f5909ce46029
child 707242 7220d09079da082be6cb3ecc75bf53244a100fb5
push id92052
push userbmo:mh+mozilla@glandium.org
push dateMon, 04 Dec 2017 23:34:17 +0000
reviewersnjn
bugs1423000
milestone59.0a1
Bug 1423000 - Use mozjemalloc mutexes in logalloc. r?njn Instead of the chromium one, which required some tricks.
memory/replace/logalloc/LogAlloc.cpp
memory/replace/logalloc/logalloc.mozbuild
--- a/memory/replace/logalloc/LogAlloc.cpp
+++ b/memory/replace/logalloc/LogAlloc.cpp
@@ -14,33 +14,32 @@
 #include <process.h>
 #else
 #include <unistd.h>
 #include <pthread.h>
 #endif
 
 #include "replace_malloc.h"
 #include "FdPrintf.h"
-
-#include "base/lock.h"
+#include "Mutex.h"
 
 static malloc_table_t sFuncs;
 static intptr_t sFd = 0;
 static bool sStdoutOrStderr = false;
 
-static Lock sLock;
+static Mutex sMutex;
 
 static void
 prefork() {
-  sLock.Acquire();
+  sMutex.Lock();
 }
 
 static void
 postfork() {
-  sLock.Release();
+  sMutex.Unlock();
 }
 
 static size_t
 GetPid()
 {
   return size_t(getpid());
 }
 
@@ -76,113 +75,113 @@ class LogAllocBridge : public ReplaceMal
 /* Do a simple, text-form, log of all calls to replace-malloc functions.
  * Use locking to guarantee that an allocation that did happen is logged
  * before any other allocation/free happens.
  */
 
 static void*
 replace_malloc(size_t aSize)
 {
-  AutoLock lock(sLock);
+  MutexAutoLock lock(sMutex);
   void* ptr = sFuncs.malloc(aSize);
   if (ptr) {
     FdPrintf(sFd, "%zu %zu malloc(%zu)=%p\n", GetPid(), GetTid(), aSize, ptr);
   }
   return ptr;
 }
 
 #ifndef LOGALLOC_MINIMAL
 static int
 replace_posix_memalign(void** aPtr, size_t aAlignment, size_t aSize)
 {
-  AutoLock lock(sLock);
+  MutexAutoLock lock(sMutex);
   int ret = sFuncs.posix_memalign(aPtr, aAlignment, aSize);
   if (ret == 0) {
     FdPrintf(sFd, "%zu %zu posix_memalign(%zu,%zu)=%p\n", GetPid(), GetTid(),
              aAlignment, aSize, *aPtr);
   }
   return ret;
 }
 
 static void*
 replace_aligned_alloc(size_t aAlignment, size_t aSize)
 {
-  AutoLock lock(sLock);
+  MutexAutoLock lock(sMutex);
   void* ptr = sFuncs.aligned_alloc(aAlignment, aSize);
   if (ptr) {
     FdPrintf(sFd, "%zu %zu aligned_alloc(%zu,%zu)=%p\n", GetPid(), GetTid(),
              aAlignment, aSize, ptr);
   }
   return ptr;
 }
 #endif
 
 static void*
 replace_calloc(size_t aNum, size_t aSize)
 {
-  AutoLock lock(sLock);
+  MutexAutoLock lock(sMutex);
   void* ptr = sFuncs.calloc(aNum, aSize);
   if (ptr) {
     FdPrintf(sFd, "%zu %zu calloc(%zu,%zu)=%p\n", GetPid(), GetTid(), aNum,
              aSize, ptr);
   }
   return ptr;
 }
 
 static void*
 replace_realloc(void* aPtr, size_t aSize)
 {
-  AutoLock lock(sLock);
+  MutexAutoLock lock(sMutex);
   void* new_ptr = sFuncs.realloc(aPtr, aSize);
   if (new_ptr || !aSize) {
     FdPrintf(sFd, "%zu %zu realloc(%p,%zu)=%p\n", GetPid(), GetTid(), aPtr,
              aSize, new_ptr);
   }
   return new_ptr;
 }
 
 static void
 replace_free(void* aPtr)
 {
-  AutoLock lock(sLock);
+  MutexAutoLock lock(sMutex);
   if (aPtr) {
     FdPrintf(sFd, "%zu %zu free(%p)\n", GetPid(), GetTid(), aPtr);
   }
   sFuncs.free(aPtr);
 }
 
 static void*
 replace_memalign(size_t aAlignment, size_t aSize)
 {
-  AutoLock lock(sLock);
+  MutexAutoLock lock(sMutex);
   void* ptr = sFuncs.memalign(aAlignment, aSize);
   if (ptr) {
     FdPrintf(sFd, "%zu %zu memalign(%zu,%zu)=%p\n", GetPid(), GetTid(),
              aAlignment, aSize, ptr);
   }
   return ptr;
 }
 
 #ifndef LOGALLOC_MINIMAL
 static void*
 replace_valloc(size_t aSize)
 {
-  AutoLock lock(sLock);
+  MutexAutoLock lock(sMutex);
   void* ptr = sFuncs.valloc(aSize);
   if (ptr) {
     FdPrintf(sFd, "%zu %zu valloc(%zu)=%p\n", GetPid(), GetTid(), aSize, ptr);
   }
   return ptr;
 }
 #endif
 
 static void
 replace_jemalloc_stats(jemalloc_stats_t* aStats)
 {
-  AutoLock lock(sLock);
+  MutexAutoLock lock(sMutex);
   sFuncs.jemalloc_stats(aStats);
   FdPrintf(sFd, "%zu %zu jemalloc_stats()\n", GetPid(), GetTid());
 }
 
 void
 replace_init(malloc_table_t* aTable, ReplaceMallocBridge** aBridge)
 {
   /* Initialize output file descriptor from the MALLOC_LOG environment
@@ -232,16 +231,17 @@ replace_init(malloc_table_t* aTable, Rep
 #endif
   }
 
   // Don't initialize if we weren't passed a valid MALLOC_LOG.
   if (sFd == 0) {
     return;
   }
 
+  sMutex.Init();
   static LogAllocBridge bridge;
   sFuncs = *aTable;
 #define MALLOC_FUNCS MALLOC_FUNCS_MALLOC_BASE
 #define MALLOC_DECL(name, ...) aTable->name = replace_ ## name;
 #include "malloc_decls.h"
   aTable->jemalloc_stats = replace_jemalloc_stats;
 #ifndef LOGALLOC_MINIMAL
   aTable->posix_memalign = replace_posix_memalign;
--- a/memory/replace/logalloc/logalloc.mozbuild
+++ b/memory/replace/logalloc/logalloc.mozbuild
@@ -7,29 +7,18 @@
 SOURCES += [
     'FdPrintf.cpp',
     'LogAlloc.cpp',
 ]
 
 DisableStlWrapping()
 NO_PGO = True
 DEFINES['MOZ_NO_MOZALLOC'] = True
-# Avoid Lock_impl code depending on mozilla::Logger.
-DEFINES['NDEBUG'] = True
-DEFINES['DEBUG'] = False
 
-# Use locking code from the chromium stack.
-if CONFIG['OS_TARGET'] == 'WINNT':
-    SOURCES += [
-        '../../../ipc/chromium/src/base/lock_impl_win.cc',
-    ]
-else:
-    SOURCES += [
-        '../../../ipc/chromium/src/base/lock_impl_posix.cc',
-    ]
-
-include('/ipc/chromium/chromium-config.mozbuild')
+LOCAL_INCLUDES += [
+    '/memory/build',
+]
 
 # Android doesn't have pthread_atfork, but we have our own in mozglue.
 if CONFIG['OS_TARGET'] == 'Android' and FORCE_SHARED_LIB:
     USE_LIBS += [
         'mozglue',
     ]