diff --git a/benchtests/Makefile b/benchtests/Makefile
index 74b3821ccfea6912e68578ad2598d68a9e38223c..5052bbbfe79f6d5a0b16c427dfc4807271805e61 100644
--- a/benchtests/Makefile
+++ b/benchtests/Makefile
@@ -90,7 +90,7 @@ CFLAGS-bench-trunc.c += -fno-builtin
CFLAGS-bench-truncf.c += -fno-builtin
ifeq (${BENCHSET},)
-bench-malloc := malloc-thread
+bench-malloc := malloc-thread malloc-simple
else
bench-malloc := $(filter malloc-%,${BENCHSET})
endif
@@ -98,7 +98,7 @@ endif
$(addprefix $(objpfx)bench-,$(bench-math)): $(libm)
$(addprefix $(objpfx)bench-,$(math-benchset)): $(libm)
$(addprefix $(objpfx)bench-,$(bench-pthread)): $(shared-thread-library)
-$(objpfx)bench-malloc-thread: $(shared-thread-library)
+$(addprefix $(objpfx)bench-,$(bench-malloc)): $(shared-thread-library)
@@ -165,7 +165,7 @@ bench-clean:
ifneq ($(strip ${BENCHSET}),)
VALIDBENCHSETNAMES := bench-pthread bench-math bench-string string-benchset \
wcsmbs-benchset stdlib-benchset stdio-common-benchset math-benchset \
- malloc-thread
+ malloc-thread malloc-simple
INVALIDBENCHSETNAMES := $(filter-out ${VALIDBENCHSETNAMES},${BENCHSET})
ifneq (${INVALIDBENCHSETNAMES},)
$(info The following values in BENCHSET are invalid: ${INVALIDBENCHSETNAMES})
@@ -201,10 +201,18 @@ bench-set: $(binaries-benchset)
bench-malloc: $(binaries-bench-malloc)
for run in $^; do \
+ echo "$${run}"; \
+ if [ `basename $${run}` = "bench-malloc-thread" ]; then \
for thr in 1 8 16 32; do \
echo "Running $${run} $${thr}"; \
- $(run-bench) $${thr} > $${run}-$${thr}.out; \
- done;\
+ $(run-bench) $${thr} > $${run}-$${thr}.out; \
+ done;\
+ else \
+ for thr in 8 16 32 64 128 256 512 1024 2048 4096; do \
+ echo "Running $${run} $${thr}"; \
+ $(run-bench) $${thr} > $${run}-$${thr}.out; \
+ done;\
+ fi;\
done
# Build and execute the benchmark functions. This target generates JSON
diff --git a/benchtests/bench-malloc-simple.c b/benchtests/bench-malloc-simple.c
new file mode 100644
index 0000000000000000000000000000000000000000..151c38de50c5e747e05d69c717452241a47d7d22
--- /dev/null
+++ b/benchtests/bench-malloc-simple.c
@@ -0,0 +1,201 @@
+/* Benchmark malloc and free functions.
+ Copyright (C) 2018 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, see
+ . */
+
+#include
+#include
+#include
+#include
+#include
+#include "bench-timing.h"
+#include "json-lib.h"
+
+#define NUM_ITERS 1000000
+#define NUM_ALLOCS 4
+#define MAX_ALLOCS 1000
+
+typedef struct
+{
+ size_t iters;
+ size_t size;
+ int n;
+ timing_t elapsed;
+} malloc_args;
+
+static void
+do_benchmark (malloc_args *args, int **arr)
+{
+ timing_t start, stop;
+ size_t iters = args->iters;
+ size_t size = args->size;
+ int n = args->n;
+
+ TIMING_NOW (start);
+
+ for (int j = 0; j < iters; j++)
+ {
+ for (int i = 0; i < n; i++)
+ arr[i] = malloc (size);
+
+ for (int i = 0; i < n; i++)
+ free (arr[i]);
+ }
+
+ TIMING_NOW (stop);
+
+ TIMING_DIFF (args->elapsed, start, stop);
+}
+
+static malloc_args tests[4][NUM_ALLOCS];
+static int allocs[NUM_ALLOCS] = { 25, 100, 400, MAX_ALLOCS };
+
+static void *
+thread_test (void *p)
+{
+ int **arr = (int**)p;
+
+ /* Run benchmark multi-threaded. */
+ for (int i = 0; i < NUM_ALLOCS; i++)
+ do_benchmark (&tests[2][i], arr);
+
+ return p;
+}
+
+void
+bench (unsigned long size)
+{
+ size_t iters = NUM_ITERS;
+ int **arr = (int**) malloc (MAX_ALLOCS * sizeof (void*));
+ unsigned long res;
+
+ TIMING_INIT (res);
+ (void) res;
+
+ /* Set tcache count to default. */
+ mallopt (M_TCACHE_COUNT, -1);
+
+ for (int t = 0; t <= 3; t++)
+ for (int i = 0; i < NUM_ALLOCS; i++)
+ {
+ tests[t][i].n = allocs[i];
+ tests[t][i].size = size;
+ tests[t][i].iters = iters / allocs[i];
+
+ /* Do a quick warmup run. */
+ if (t == 0)
+ do_benchmark (&tests[0][i], arr);
+ }
+
+ /* Run benchmark single threaded in main_arena. */
+ for (int i = 0; i < NUM_ALLOCS; i++)
+ do_benchmark (&tests[0][i], arr);
+
+ /* Run benchmark in a thread_arena. */
+ pthread_t t;
+ pthread_create (&t, NULL, thread_test, (void*)arr);
+ pthread_join (t, NULL);
+
+ /* Repeat benchmark in main_arena with SINGLE_THREAD_P == false. */
+ for (int i = 0; i < NUM_ALLOCS; i++)
+ do_benchmark (&tests[1][i], arr);
+
+ /* Increase size of tcache. */
+ mallopt (M_TCACHE_COUNT, 100);
+
+ /* Run again but with larger tcache. */
+ for (int i = 0; i < NUM_ALLOCS; i++)
+ do_benchmark (&tests[3][i], arr);
+
+ mallopt (M_TCACHE_COUNT, -1);
+
+ free (arr);
+
+ json_ctx_t json_ctx;
+
+ json_init (&json_ctx, 0, stdout);
+
+ json_document_begin (&json_ctx);
+
+ json_attr_string (&json_ctx, "timing_type", TIMING_TYPE);
+
+ json_attr_object_begin (&json_ctx, "functions");
+
+ json_attr_object_begin (&json_ctx, "malloc");
+
+ char s[100];
+ double iters2 = iters;
+
+ json_attr_object_begin (&json_ctx, "");
+ json_attr_double (&json_ctx, "malloc_block_size", size);
+
+ struct rusage usage;
+ getrusage (RUSAGE_SELF, &usage);
+ json_attr_double (&json_ctx, "max_rss", usage.ru_maxrss);
+
+ for (int i = 0; i < NUM_ALLOCS; i++)
+ {
+ sprintf (s, "main_arena_st_allocs_%04d_time", allocs[i]);
+ json_attr_double (&json_ctx, s, tests[0][i].elapsed / iters2);
+ }
+
+ for (int i = 0; i < NUM_ALLOCS; i++)
+ {
+ sprintf (s, "main_arena_mt_allocs_%04d_time", allocs[i]);
+ json_attr_double (&json_ctx, s, tests[1][i].elapsed / iters2);
+ }
+
+ for (int i = 0; i < NUM_ALLOCS; i++)
+ {
+ sprintf (s, "big_tcache_mt_allocs_%04d_time", allocs[i]);
+ json_attr_double (&json_ctx, s, tests[3][i].elapsed / iters2);
+ }
+
+ for (int i = 0; i < NUM_ALLOCS; i++)
+ {
+ sprintf (s, "thread_arena__allocs_%04d_time", allocs[i]);
+ json_attr_double (&json_ctx, s, tests[2][i].elapsed / iters2);
+ }
+
+ json_attr_object_end (&json_ctx);
+
+ json_attr_object_end (&json_ctx);
+
+ json_attr_object_end (&json_ctx);
+
+ json_document_end (&json_ctx);
+}
+
+static void usage (const char *name)
+{
+ fprintf (stderr, "%s: \n", name);
+ exit (1);
+}
+
+int
+main (int argc, char **argv)
+{
+ long val = 16;
+ if (argc == 2)
+ val = strtol (argv[1], NULL, 0);
+
+ if (argc > 2 || val <= 0)
+ usage (argv[0]);
+
+ bench (val);
+
+ return 0;
+}
diff --git a/malloc/malloc.h b/malloc/malloc.h
index 339ab64c7d336873211a9057a923d87e8c1e025d..a047385a4fc8d7b3bb3e120a94440193dba306ed 100644
--- a/malloc/malloc.h
+++ b/malloc/malloc.h
@@ -121,6 +121,7 @@ extern struct mallinfo mallinfo (void) __THROW;
#define M_PERTURB -6
#define M_ARENA_TEST -7
#define M_ARENA_MAX -8
+#define M_TCACHE_COUNT -9
/* General SVID/XPG interface to tunable parameters. */
extern int mallopt (int __param, int __val) __THROW;
diff --git a/malloc/malloc.c b/malloc/malloc.c
index 0c9e0748b4c10988f6fe99ac2e5b21b8b7b603c3..a07438d276ff4c8177552e1c0d186ee7c8bd7692 100644
--- a/malloc/malloc.c
+++ b/malloc/malloc.c
@@ -5177,6 +5177,11 @@ __libc_mallopt (int param_number, int value)
if (value > 0)
do_set_arena_max (value);
break;
+#if USE_TCACHE
+ case M_TCACHE_COUNT:
+ do_set_tcache_count (value >= 0 ? value : TCACHE_FILL_COUNT);
+ break;
+#endif
}
__libc_lock_unlock (av->mutex);
return res;