diff options
| author | Florian Fischer <florian.fl.fischer@fau.de> | 2019-02-01 16:35:20 +0100 |
|---|---|---|
| committer | Florian Fischer <florian.fl.fischer@fau.de> | 2019-02-01 16:35:20 +0100 |
| commit | 130765de719a3ddc475284e13749d09ff371a8e1 (patch) | |
| tree | c45ea8d47e53481022d641336ec2abd5cb588111 /src/benchmarks/loop/loop.c | |
| parent | 8221b8fb0e9ee491590932cd228f17b48155c0f7 (diff) | |
| download | allocbench-130765de719a3ddc475284e13749d09ff371a8e1.tar.gz allocbench-130765de719a3ddc475284e13749d09ff371a8e1.zip | |
rework build system #1
each benchmark has its own Makefile which must put it's binaries into
OBJDIR which is added to the PATH during execution.
Diffstat (limited to 'src/benchmarks/loop/loop.c')
| -rw-r--r-- | src/benchmarks/loop/loop.c | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/src/benchmarks/loop/loop.c b/src/benchmarks/loop/loop.c new file mode 100644 index 0000000..bc15808 --- /dev/null +++ b/src/benchmarks/loop/loop.c @@ -0,0 +1,87 @@ +#include <assert.h> +#include <malloc.h> +#include <pthread.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + + +static size_t _rand() { + static __thread size_t seed = 123456789; + size_t a = 1103515245; + size_t c = 12345; + size_t m = 1 << 31; + seed = (a * seed + c) % m; + return seed; +} + +typedef struct ThreadArgs { + double benchmark; + int allocations; + int max_size; +} ThreadArgs; + +static void* malloc_then_write(size_t size) { + void* ptr = malloc(size); + // Write to ptr + /* *((char*)ptr) = '!'; */ + return ptr; +} + +static void read_then_free(void* ptr) { + // Read before free + /* char s __attribute__((unused)) = *((char*)ptr); */ + free(ptr); +} +static void* test_thread_func(void* arg) { + ThreadArgs* args = (ThreadArgs*)arg; + + for(int i = 0; i < args->allocations; i++) { + void* ptr = malloc_then_write((_rand() % args->max_size) + 1); + read_then_free(ptr); + } + return NULL; +} + +int main(int argc, char* argv[]) { + pthread_t* threads; + int num_threads; + struct ThreadArgs thread_args; + + if (argc < 4) { + fprintf(stderr, "Usage: %s <num threads> <num allocations> <max size>\n", argv[0]); + return 1; + } + + 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)); + + for (int i = 0; i < num_threads; i++) { + if (0 != pthread_create(&threads[i], NULL, test_thread_func, &thread_args)) { + perror("pthread_create"); + return 1; + } + } + + for(int i = 0; i < num_threads; i++) { + if (0 != pthread_join(threads[i], NULL)) { + perror("pthread_join"); + return 1; + } + } + + if (argc == 5) + { + FILE* f = stdout; + if (strcmp(argv[4],"stdout") != 0) + f = fopen(argv[4], "w"); + malloc_info(0, f); + if (strcmp(argv[4],"stdout") != 0) + fclose(f); + } + + return 0; +} |
