This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
Re: [PATCH] Add malloc micro benchmark
- From: DJ Delorie <dj at redhat dot com>
- To: Wilco Dijkstra <Wilco dot Dijkstra at arm dot com>
- Cc: libc-alpha at sourceware dot org, nd at arm dot com
- Date: Fri, 08 Feb 2019 14:37:18 -0500
- Subject: Re: [PATCH] Add malloc micro benchmark
Looks good to me, although I'd like some additional comments in the test
code.
Wilco Dijkstra <Wilco.Dijkstra@arm.com> writes:
> -bench-malloc := malloc-thread
> +bench-malloc := malloc-thread malloc-simple
Adding a test, ok
> -$(objpfx)bench-malloc-thread: $(shared-thread-library)
> +$(addprefix $(objpfx)bench-,$(bench-malloc)): $(shared-thread-library)
Accepting a list of tests, ok
> - malloc-thread
> + malloc-thread malloc-simple
Adding a test, ok
> 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
I wonder if this could be done more elegantly, but I'm OK with a simple
approach for now. If we end up adding many more such tests we might
need to revisit this part.
> +/* Benchmark malloc and free functions.
> + Copyright (C) 2018 Free Software Foundation, Inc.
2019
> +
> +#include <pthread.h>
I would like to see a comment block somewhere in this code that
describes, to the casual future reader, what this test is looking for
and why it's different than other tests. I won't hold up my OK for it,
though.
> +#define NUM_ITERS 1000000
> +#define NUM_ALLOCS 4
> +#define MAX_ALLOCS 1600
How long does this test take to run, on average, compared to other
tests? Do we have to worry about increasing timeouts for slow hosts?
> +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);
> +}
Simple loop, but doesn't test for malloc returning NULL.
> + /* 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);
So we repeat the "main thread" case but now the heap is "messy" from the
now-joined thread... ok.