Bug 414946: Re-enable jemalloc on OSX 10.6 only. r=khuey
authorPaul Biggar <pbiggar@mozilla.com>
Mon, 25 Jul 2011 17:20:18 -0700
changeset 78789 9cdfb406ad3f475d22d9805fb04c53696785d17c
parent 78788 3ba2964cd000ef54ea0b3820f63c9e6879cb3659
child 78790 d5954910540eee975f02801e79029f6853c707bf
push id506
push userclegnitto@mozilla.com
push dateWed, 09 Nov 2011 02:03:18 +0000
treeherdermozilla-aurora@63587fc7bb93 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerskhuey
bugs414946
milestone10.0a1
Bug 414946: Re-enable jemalloc on OSX 10.6 only. r=khuey
configure.in
memory/jemalloc/jemalloc.c
--- a/configure.in
+++ b/configure.in
@@ -2022,16 +2022,20 @@ case "$target" in
     esac
     ;;
 
 *-darwin*)
     MKSHLIB='$(CXX) $(CXXFLAGS) $(DSO_PIC_CFLAGS) $(DSO_LDOPTS) -o $@'
     MKCSHLIB='$(CC) $(CFLAGS) $(DSO_PIC_CFLAGS) $(DSO_LDOPTS) -o $@'
     MOZ_OPTIMIZE_FLAGS="-O3"
     _PEDANTIC=
+    # Due to performance regressions, statically disable jemalloc on 10.5. See bug 414946.
+    if test "$HAVE_64BIT_OS"; then
+        MOZ_MEMORY=1
+    fi
     CFLAGS="$CFLAGS -fno-common"
     CXXFLAGS="$CXXFLAGS -fno-common"
     DLL_SUFFIX=".dylib"
     DSO_LDOPTS=''
     STRIP="$STRIP -x -S"
     # Check whether we're targeting OS X or iOS
     AC_CACHE_CHECK(for iOS target,
                    ac_cv_ios_target,
--- a/memory/jemalloc/jemalloc.c
+++ b/memory/jemalloc/jemalloc.c
@@ -1288,36 +1288,40 @@ static void	_malloc_postfork(void);
  * This means we need to statically know the correct struct size to use on all
  * OSX releases, and have a fallback for unknown future versions. The struct
  * sizes defined in osx_zone_types.h.
  *
  * For OSX 10.8 and later, we may expect the malloc_zone_t struct to change
  * again, and need to dynamically account for this. By simply leaving
  * malloc_zone_t alone, we don't quite deal with the problem, because there
  * remain calls to jemalloc through the mozalloc interface. We check this
- * dynamically on each allocation, using the CHECK_DARWIN macro.
+ * dynamically on each allocation, using the CHECK_DARWIN macro and
+ * osx_use_jemalloc.
  *
  *
  * [1] Mozilla is built as a universal binary on Mac, supporting i386 and
  *     x86_64. The i386 target is built using the 10.5 SDK, even if it runs on
  *     10.6. The x86_64 target is built using the 10.6 SDK, even if it runs on
- *     10.7 or later.
+ *     10.7 or later, or 10.5.
  *
  * FIXME:
  *   When later versions of OSX come out (10.8 and up), we need to check their
  *   malloc_zone_t versions. If they're greater than 8, we need a new version
  *   of malloc_zone_t adapted into osx_zone_types.h.
  */
 
 #include "osx_zone_types.h"
 
 #define LEOPARD_MALLOC_ZONE_T_VERSION 3
 #define SNOW_LEOPARD_MALLOC_ZONE_T_VERSION 6
 #define LION_MALLOC_ZONE_T_VERSION 8
 
+static bool osx_use_jemalloc = false;
+
+
 /*
  * Avoid lots of casts below by allowing access to l_jemalloc_zone through a
  * malloc_zone_t pointer.
  */
 static lion_malloc_zone l_jemalloc_zone, l_szone;
 static malloc_zone_t * const jemalloc_zone = (malloc_zone_t*)(&l_jemalloc_zone);
 static malloc_zone_t * szone = (malloc_zone_t*)(&l_szone);
 
@@ -1327,20 +1331,16 @@ static malloc_introspection_t * const zo
 	(malloc_introspection_t*)(&l_zone_introspect);
 static malloc_introspection_t * const ozone_introspect =
 	(malloc_introspection_t*)(&l_ozone_introspect);
 static malloc_zone_t *create_zone(unsigned version);
 static void szone2ozone(malloc_zone_t *zone, size_t size);
 static size_t zone_version_size(int version);
 #endif
 
-/* On unknown future versions of OSX, dynamically decide not to use jemalloc. */
-static bool use_jemalloc = false;
-
-
 /*
  * End function prototypes.
  */
 /******************************************************************************/
 
 /*
  * umax2s() provides minimal integer printing functionality, which is
  * especially useful for situations where allocation in vsnprintf() calls would
@@ -5827,24 +5827,30 @@ MALLOC_OUT:
 	malloc_initialized = true;
 
 #ifdef MOZ_MEMORY_DARWIN
     /*
      * Overwrite the default memory allocator to use jemalloc everywhere.
      */
     default_zone = malloc_default_zone();
 
-    /* Don't use jemalloc on as-yet-unreleased versions of OSX. */
-    use_jemalloc = (default_zone->version <= LION_MALLOC_ZONE_T_VERSION);
+    /*
+     * We only use jemalloc with the 10.6 SDK:
+     *   - With the 10.5 SDK, madvise doesn't work, leading to a 20% memory
+     *     usage regression (bug 670492).
+     *   - With the 10.7 SDK, jemalloc causes the browser to hang (bug 670175).
+     */
+
+    osx_use_jemalloc = (default_zone->version == SNOW_LEOPARD_MALLOC_ZONE_T_VERSION);
 
     /* Allow us dynamically turn off jemalloc for testing. */
 	if (getenv("NO_MAC_JEMALLOC"))
-        use_jemalloc = false;
-
-    if (use_jemalloc) {
+        osx_use_jemalloc = false;
+
+    if (osx_use_jemalloc) {
         size_t size;
 
         /* Register the custom zone. */
         malloc_zone_register(create_zone(default_zone->version));
 
         /*
          * Convert the default szone to an "overlay zone" that is capable
          * of deallocating szone-allocated objects, but allocating new
@@ -5932,33 +5938,27 @@ wrap(strndup)(const char *src, size_t le
 char *
 wrap(strdup)(const char *src) {
 	size_t len = strlen(src);
 	return wrap(strndup)(src, len);
 }
 #endif
 
 /*
- * We are not able to assume that we can replace the OSX allocator with
- * jemalloc on future unreleased versions of OSX. Despite this, we call
- * jemalloc functions directly from mozalloc. Since it's pretty dangerous to
- * mix the allocators, we need to call the OSX allocators from the functions
- * below, when use_jemalloc is not (dynamically) set.
+ * Even though we compile with MOZ_MEMORY, we may have to dynamically decide
+ * not to use jemalloc, as discussed above. However, we call jemalloc
+ * functions directly from mozalloc. Since it's pretty dangerous to mix the
+ * allocators, we need to call the OSX allocators from the functions below,
+ * when osx_use_jemalloc is not (dynamically) set.
  *
- * We call memalign from mozalloc, but the 10.5 SDK doesn't have a memalign
- * function to forward the call to. However, use_jemalloc will _always_ be true
- * on 10.5, so we just omit these checks statically. This allows us to build
- * successfully on 10.5, and also makes it undetectably faster.
- *
- * FIXME:
- *   This may lead to problems when using 32-bit plugins with a 64-bit process,
- *   on OSX 10.8 or higher.
+ * memalign is unavailable on Leopard, so we can't dynamically do this there.
+ * However, we don't use jemalloc on Leopard, so we can ignore this.
  */
 #if defined(MOZ_MEMORY_DARWIN) && !defined(__i386__)
-#define DARWIN_ONLY(A) if (!use_jemalloc) { A; }
+#define DARWIN_ONLY(A) if (!osx_use_jemalloc) { A; }
 #else
 #define DARWIN_ONLY(A)
 #endif
 
 void *
 malloc(size_t size)
 {
 	void *ret;