From 05c7eb3523242133054dec3edb297b78a2fd6acd Mon Sep 17 00:00:00 2001 From: Florian Fischer Date: Mon, 17 Sep 2018 13:55:17 +0200 Subject: reduce loop to a simple loop of malloc/free pairs --- benchmarks/bench_loop.c | 53 ++++++++++--------------------------------------- 1 file changed, 11 insertions(+), 42 deletions(-) (limited to 'benchmarks') diff --git a/benchmarks/bench_loop.c b/benchmarks/bench_loop.c index af7ff10..12d4760 100644 --- a/benchmarks/bench_loop.c +++ b/benchmarks/bench_loop.c @@ -15,18 +15,9 @@ static size_t _rand() { return seed; } -/* -* Available Benchmarks: -* 1.x: do malloc()/free() in a loop -* 1: simple loop -* 1.1: keep num_kept_allocations befor freeing -* 1.2: keep num_kept_allocations then free all stored -*/ - typedef struct ThreadArgs { double benchmark; int allocations; - int num_kept_allocations; int max_size; } ThreadArgs; @@ -44,30 +35,10 @@ static void read_then_free(void* ptr) { } static void* test_thread_func(void* arg) { ThreadArgs* args = (ThreadArgs*)arg; - void** ptrs = (void**)calloc(args->num_kept_allocations, sizeof(void*)); for(int i = 0; i < args->allocations; i++) { - int pos = i % args->num_kept_allocations; - - if (args->benchmark == 1.1) { - if (i >= args->num_kept_allocations) { - read_then_free(ptrs[pos]); - } - } - - if (args->benchmark == 1.2) { - if (pos == 0 && ptrs[pos] != NULL) { - for (int y = 0; y < args->num_kept_allocations; y++) { - read_then_free(ptrs[y]); - } - } - } - - ptrs[pos] = malloc_then_write((_rand() % args->max_size) + 1); - - if (args->benchmark == 1.0) { - read_then_free(ptrs[pos]); - } + void* ptr = malloc_then_write((_rand() % args->max_size) + 1); + read_then_free(ptr); } return NULL; } @@ -77,16 +48,14 @@ int main(int argc, char* argv[]) { int num_threads; struct ThreadArgs thread_args; - if (argc < 6) { - fprintf(stderr, "Usage: %s \n", argv[0]); + if (argc < 4) { + fprintf(stderr, "Usage: %s \n", argv[0]); return 1; } - thread_args.benchmark = atof(argv[1]); - num_threads = atoi(argv[2]); - thread_args.allocations = atoi(argv[3]); - thread_args.max_size = atoi(argv[4]); - thread_args.num_kept_allocations = atoi(argv[5]); + num_threads = atoi(argv[1]); + thread_args.allocations = atoi(argv[2]); + thread_args.max_size = atoi(argv[3]); threads = (pthread_t*)malloc(num_threads * sizeof(pthread_t)); @@ -104,13 +73,13 @@ int main(int argc, char* argv[]) { } } - if (argc == 7) + if (argc == 5) { FILE* f = stdout; - if (strcmp(argv[6],"stdout") != 0) - f = fopen(argv[6], "w"); + if (strcmp(argv[4],"stdout") != 0) + f = fopen(argv[4], "w"); malloc_info(0, f); - if (strcmp(argv[6],"stdout") != 0) + if (strcmp(argv[4],"stdout") != 0) fclose(f); } -- cgit v1.2.3