glibc/benchtests/bench-malloc-simple.c

186 lines
4.4 KiB
C
Raw Normal View History

/* Benchmark malloc and free functions.
Copyright (C) 2019 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
Prefer https to http for gnu.org and fsf.org URLs Also, change sources.redhat.com to sourceware.org. This patch was automatically generated by running the following shell script, which uses GNU sed, and which avoids modifying files imported from upstream: sed -ri ' s,(http|ftp)(://(.*\.)?(gnu|fsf|sourceware)\.org($|[^.]|\.[^a-z])),https\2,g s,(http|ftp)(://(.*\.)?)sources\.redhat\.com($|[^.]|\.[^a-z]),https\2sourceware.org\4,g ' \ $(find $(git ls-files) -prune -type f \ ! -name '*.po' \ ! -name 'ChangeLog*' \ ! -path COPYING ! -path COPYING.LIB \ ! -path manual/fdl-1.3.texi ! -path manual/lgpl-2.1.texi \ ! -path manual/texinfo.tex ! -path scripts/config.guess \ ! -path scripts/config.sub ! -path scripts/install-sh \ ! -path scripts/mkinstalldirs ! -path scripts/move-if-change \ ! -path INSTALL ! -path locale/programs/charmap-kw.h \ ! -path po/libc.pot ! -path sysdeps/gnu/errlist.c \ ! '(' -name configure \ -execdir test -f configure.ac -o -f configure.in ';' ')' \ ! '(' -name preconfigure \ -execdir test -f preconfigure.ac ';' ')' \ -print) and then by running 'make dist-prepare' to regenerate files built from the altered files, and then executing the following to cleanup: chmod a+x sysdeps/unix/sysv/linux/riscv/configure # Omit irrelevant whitespace and comment-only changes, # perhaps from a slightly-different Autoconf version. git checkout -f \ sysdeps/csky/configure \ sysdeps/hppa/configure \ sysdeps/riscv/configure \ sysdeps/unix/sysv/linux/csky/configure # Omit changes that caused a pre-commit check to fail like this: # remote: *** error: sysdeps/powerpc/powerpc64/ppc-mcount.S: trailing lines git checkout -f \ sysdeps/powerpc/powerpc64/ppc-mcount.S \ sysdeps/unix/sysv/linux/s390/s390-64/syscall.S # Omit change that caused a pre-commit check to fail like this: # remote: *** error: sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S: last line does not end in newline git checkout -f sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S
2019-09-07 05:40:42 +00:00
<https://www.gnu.org/licenses/>. */
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <sys/resource.h>
#include "bench-timing.h"
#include "json-lib.h"
/* Benchmark the malloc/free performance of a varying number of blocks of a
given size. This enables performance tracking of the t-cache and fastbins.
It tests 3 different scenarios: single-threaded using main arena,
multi-threaded using thread-arena, and main arena with SINGLE_THREAD_P
false. */
#define NUM_ITERS 200000
#define NUM_ALLOCS 4
#define MAX_ALLOCS 1600
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[3][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*));
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);
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, "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: <alloc_size>\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;
}