Improve performance of jemalloc svelte config.

Change the decay time from zero to one second, to avoid problems with
the purge interferring with allocations taking too long.

Also, decrease the arenas from 2 to 1 for the svelte config to offset
the PSS increase caused by the decay time change.

Bug: 30077848
Change-Id: Ie7688a8bfd50f0ba7d5f0e594a37baca43596e41
diff --git a/Android.bp b/Android.bp
index 18a1e6a..a6ab9dc 100644
--- a/Android.bp
+++ b/Android.bp
@@ -23,8 +23,6 @@
 ]
 
 // These parameters change the way jemalloc works.
-//   ANDROID_ALWAYS_PURGE
-//     If defined, always purge immediately when a page is purgeable.
 //   ANDROID_MAX_ARENAS=XX
 //     The total number of arenas will be less than or equal to this number.
 //     The number of arenas will be calculated as 2 * the number of cpus
@@ -43,11 +41,11 @@
 //     1 << XX is the default chunk size used by the system. Decreasing this
 //     usually decreases the amount of PSS used, but can increase
 //     fragmentation.
+
+// Default to a single arena for svelte configurations to minimize
+// PSS consumed by jemalloc.
 common_cflags += [
-    "-DANDROID_ALWAYS_PURGE",
-    "-DANDROID_MAX_ARENAS=2",
-    "-DANDROID_TCACHE_NSLOTS_SMALL_MAX=8",
-    "-DANDROID_TCACHE_NSLOTS_LARGE=16",
+    "-DANDROID_MAX_ARENAS=1",
     "-DANDROID_LG_TCACHE_MAXCLASS_DEFAULT=16",
 ]
 
@@ -59,7 +57,13 @@
 common_product_variables = {
     // Only enable the tcache on non-svelte configurations, to save PSS.
     malloc_not_svelte: {
-        cflags: ["-DJEMALLOC_TCACHE"],
+        cflags: [
+            "-UANDROID_MAX_ARENAS",
+            "-DANDROID_MAX_ARENAS=2",
+            "-DJEMALLOC_TCACHE",
+            "-DANDROID_TCACHE_NSLOTS_SMALL_MAX=8",
+            "-DANDROID_TCACHE_NSLOTS_LARGE=16",
+        ],
     },
 }
 
diff --git a/Android.mk b/Android.mk
index 43cb8a1..34f2681 100644
--- a/Android.mk
+++ b/Android.mk
@@ -24,8 +24,6 @@
 	-Wno-type-limits \
 
 # These parameters change the way jemalloc works.
-#   ANDROID_ALWAYS_PURGE
-#     If defined, always purge immediately when a page is purgeable.
 #   ANDROID_MAX_ARENAS=XX
 #     The total number of arenas will be less than or equal to this number.
 #     The number of arenas will be calculated as 2 * the number of cpus
@@ -45,16 +43,22 @@
 #     usually decreases the amount of PSS used, but can increase
 #     fragmentation.
 jemalloc_common_cflags += \
-	-DANDROID_ALWAYS_PURGE \
-	-DANDROID_MAX_ARENAS=2 \
-	-DANDROID_TCACHE_NSLOTS_SMALL_MAX=8 \
-	-DANDROID_TCACHE_NSLOTS_LARGE=16 \
 	-DANDROID_LG_TCACHE_MAXCLASS_DEFAULT=16 \
 
-# Only enable the tcache on non-svelte configurations, to save PSS.
-ifneq ($(MALLOC_SVELTE),true)
+ifeq ($(MALLOC_SVELTE),true)
+# Use a single arena on svelte devices to keep the PSS consumption as low
+# as possible.
 jemalloc_common_cflags += \
-	-DJEMALLOC_TCACHE
+	-DANDROID_MAX_ARENAS=1 \
+
+else
+# Only enable the tcache on non-svelte configurations, to save PSS.
+jemalloc_common_cflags += \
+	-DANDROID_MAX_ARENAS=2 \
+	-DJEMALLOC_TCACHE \
+	-DANDROID_TCACHE_NSLOTS_SMALL_MAX=8 \
+	-DANDROID_TCACHE_NSLOTS_LARGE=16 \
+
 endif
 
 # Use a 512K chunk size on 32 bit systems.
diff --git a/include/jemalloc/internal/arena.h b/include/jemalloc/internal/arena.h
index f234ec6..47096b4 100644
--- a/include/jemalloc/internal/arena.h
+++ b/include/jemalloc/internal/arena.h
@@ -30,12 +30,13 @@
 	purge_mode_limit = 2
 } purge_mode_t;
 /* ANDROID change */
-/* Use the decay mode purge method. By setting the DECAY_TIME_DEFAULT to
- * zero, this forces all pages to be purged as soon as they are not in use.
+/* Use the decay mode purge method. Do not set this value to zero, since
+ * forcing a purge immediately affects performance negatively. Using a
+ * small value provides a compromise between performance and extra PSS.
  */
 #define	PURGE_DEFAULT		purge_mode_decay
 /* Default decay time in seconds. */
-#define	DECAY_TIME_DEFAULT	0
+#define	DECAY_TIME_DEFAULT	1
 /* End ANDROID change */
 /* Number of event ticks between time checks. */
 #define	DECAY_NTICKS_PER_UPDATE	1000