From c2db02a84b47571f052e5f35b690ec6d1b814ad3 Mon Sep 17 00:00:00 2001 From: Vojtech Horky Date: Wed, 9 Jan 2019 09:27:21 +0100 Subject: [PATCH] hbench: add fibril_mutex benchmark Not very sophisticated but not completely unreasonable. --- uspace/app/hbench/Makefile | 3 +- uspace/app/hbench/benchlist.c | 1 + uspace/app/hbench/hbench.h | 1 + .../hbench/{benchlist.c => synch/fibril_mutex.c} | 80 ++++++++++++++++++---- 4 files changed, 69 insertions(+), 16 deletions(-) copy uspace/app/hbench/{benchlist.c => synch/fibril_mutex.c} (51%) diff --git a/uspace/app/hbench/Makefile b/uspace/app/hbench/Makefile index 5c365d073..bbadd69e7 100644 --- a/uspace/app/hbench/Makefile +++ b/uspace/app/hbench/Makefile @@ -42,6 +42,7 @@ SOURCES = \ ipc/ns_ping.c \ ipc/ping_pong.c \ malloc/malloc1.c \ - malloc/malloc2.c + malloc/malloc2.c \ + synch/fibril_mutex.c include $(USPACE_PREFIX)/Makefile.common diff --git a/uspace/app/hbench/benchlist.c b/uspace/app/hbench/benchlist.c index ed0b66424..d5625cd62 100644 --- a/uspace/app/hbench/benchlist.c +++ b/uspace/app/hbench/benchlist.c @@ -39,6 +39,7 @@ benchmark_t *benchmarks[] = { &benchmark_dir_read, + &benchmark_fibril_mutex, &benchmark_file_read, &benchmark_malloc1, &benchmark_malloc2, diff --git a/uspace/app/hbench/hbench.h b/uspace/app/hbench/hbench.h index 022c33d3b..e8c3fdfe3 100644 --- a/uspace/app/hbench/hbench.h +++ b/uspace/app/hbench/hbench.h @@ -90,6 +90,7 @@ extern void bench_param_cleanup(void); /* Put your benchmark descriptors here (and also to benchlist.c). */ extern benchmark_t benchmark_dir_read; +extern benchmark_t benchmark_fibril_mutex; extern benchmark_t benchmark_file_read; extern benchmark_t benchmark_malloc1; extern benchmark_t benchmark_malloc2; diff --git a/uspace/app/hbench/benchlist.c b/uspace/app/hbench/synch/fibril_mutex.c similarity index 51% copy from uspace/app/hbench/benchlist.c copy to uspace/app/hbench/synch/fibril_mutex.c index ed0b66424..247c4605d 100644 --- a/uspace/app/hbench/benchlist.c +++ b/uspace/app/hbench/synch/fibril_mutex.c @@ -1,6 +1,5 @@ /* - * Copyright (c) 2018 Jiri Svoboda - * Copyright (c) 2018 Vojtech Horky + * Copyright (c) 2019 Vojtech Horky * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -30,23 +29,74 @@ /** @addtogroup hbench * @{ */ -/** - * @file + +#include +#include +#include "../hbench.h" + +/* + * Simple benchmark for fibril mutexes. There are two fibrils that compete + * over the same mutex as that is the simplest scenario. */ -#include -#include "hbench.h" +typedef struct { + fibril_mutex_t mutex; + uint64_t counter; + atomic_bool done; +} shared_t; -benchmark_t *benchmarks[] = { - &benchmark_dir_read, - &benchmark_file_read, - &benchmark_malloc1, - &benchmark_malloc2, - &benchmark_ns_ping, - &benchmark_ping_pong -}; +static errno_t competitor(void *arg) +{ + shared_t *shared = arg; + fibril_detach(fibril_get_id()); + + while (true) { + fibril_mutex_lock(&shared->mutex); + uint64_t local = shared->counter; + fibril_mutex_unlock(&shared->mutex); + if (local == 0) { + break; + } + } + + atomic_store(&shared->done, true); + + return EOK; +} -size_t benchmark_count = sizeof(benchmarks) / sizeof(benchmarks[0]); +static bool runner(benchmeter_t *meter, uint64_t size, + char *error, size_t error_size) +{ + shared_t shared; + fibril_mutex_initialize(&shared.mutex); + shared.counter = size; + atomic_store(&shared.done, false); + + fid_t other = fibril_create(competitor, &shared); + fibril_add_ready(other); + + benchmeter_start(meter); + for (uint64_t i = 0; i < size; i++) { + fibril_mutex_lock(&shared.mutex); + shared.counter--; + fibril_mutex_unlock(&shared.mutex); + } + benchmeter_stop(meter); + + while (!atomic_load(&shared.done)) { + fibril_yield(); + } + + return true; +} + +benchmark_t benchmark_fibril_mutex = { + .name = "fibril_mutex", + .desc = "Speed of mutex lock/unlock operations", + .entry = &runner, + .setup = NULL, + .teardown = NULL +}; /** @} */ -- 2.11.4.GIT