From 865c5a957bf1ae703d87fa0f1004f3e2318a1546 Mon Sep 17 00:00:00 2001 From: Patrick Pelissier Date: Wed, 16 Aug 2017 23:50:34 +0200 Subject: [PATCH] Add new bench for liblfds. --- bench/Makefile | 8 +++ bench/bench-liblfds.c | 142 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 150 insertions(+) create mode 100644 bench/bench-liblfds.c diff --git a/bench/Makefile b/bench/Makefile index 384ea6b6..85030b40 100644 --- a/bench/Makefile +++ b/bench/Makefile @@ -192,6 +192,14 @@ bench-libsrt0: @./bench-libsrt.exe 43 @./bench-libsrt.exe 50 +# LIBLFDS shall point to the directory where the bin/build/inc/obj/src directories are +# The library shall be built. +bench-liblfds: + if test -n "$${LIBLFDS}" ; then make bench-liblfds0 ; else echo "Nothing to be done." ; fi + +bench-liblfds0: + $(CC) $(CFLAGS) $(CPPFLAGS) -I "$${LIBLFDS}/inc" bench-liblfds.c -o bench-liblfds.exe $${LIBLFDS}/bin/liblfds711.so -lpthread + @LD_LIBRARY_PATH="$${LIBLFDS}/bin:$${LD_LIBRARY_PATH}" ./bench-liblfds.exe 60 ############################################################################ diff --git a/bench/bench-liblfds.c b/bench/bench-liblfds.c new file mode 100644 index 00000000..70da5a70 --- /dev/null +++ b/bench/bench-liblfds.c @@ -0,0 +1,142 @@ +#define NDEBUG + +#include +#include +#include + +#include "liblfds711.h" + +#include "m-mutex.h" +#define MULTI_THREAD_MEASURE +#include "common.h" + + +/********************************************************************************************/ + +struct lfds711_queue_bmm_state g_buff; +struct lfds711_queue_bmm_state g_final; + +static void final(void *arg) +{ + size_t *p_n = arg; + size_t n = *p_n; + unsigned long long j, s = 0; + for(int i = 0; i < n;i++) { + int rv; + do { + void *p; + rv = lfds711_queue_bmm_dequeue(&g_final, &p, NULL); + j = (uintptr_t)p; + } while (rv == 0); + s += j; + } + g_result = s; +} + +static void conso(void *arg) +{ + unsigned int j; + size_t *p_n = arg; + size_t n = *p_n; + unsigned long long s = 0; + for(int i = 0; i < n;i++) { + int rv; + do { + void *p; + rv = lfds711_queue_bmm_dequeue(&g_buff, &p, NULL); + j = (uintptr_t) p; + } while (rv == 0); + s += j; + } + int rv; + do { + rv = lfds711_queue_bmm_enqueue(&g_final, (void*)(uintptr_t)s, NULL); + } while (rv == 0); +} + +static void prod(void *arg) +{ + size_t *p_n = arg; + size_t n = *p_n; + size_t r = n; + for(unsigned int i = 0; i < n;i++) { + int rv; + do { + rv = lfds711_queue_bmm_enqueue(&g_buff, (void*)(uintptr_t)r, NULL); + } while (rv == 0); + r = r * 31421U + 6927U; + } +} + +static void test_buffer(size_t n) +{ + const int cpu_count = get_cpu_count(); + const int prod_count = cpu_count/2; + const int conso_count = cpu_count - prod_count; + if (cpu_count < 2) { + fprintf(stderr, "WARNING: Can not measure Buffer performance.\n"); + return; + } + // Init + const size_t size = m_core_roundpow2(64*cpu_count); + struct lfds711_queue_bmm_element g_buff_elements[size]; + struct lfds711_queue_bmm_element g_final_elements[size]; + lfds711_queue_bmm_init_valid_on_current_logical_core(&g_buff, g_buff_elements, + size, NULL); + lfds711_queue_bmm_init_valid_on_current_logical_core(&g_final, g_final_elements, + size, NULL); + + // Create thread + m_thread_t idx_p[prod_count]; + m_thread_t idx_c[conso_count]; + m_thread_t idx_final; + for(int i = 0; i < prod_count; i++) { + m_thread_create (idx_p[i], prod, &n); + } + for(int i = 0; i < conso_count; i++) { + m_thread_create (idx_c[i], conso, &n); + } + size_t n2 = conso_count; + m_thread_create(idx_final, final, &n2); + + // Wait for jobs to be done. + for(int i = 0; i < prod_count; i++) { + m_thread_join(idx_p[i]); + } + for(int i = 0; i < conso_count; i++) { + m_thread_join(idx_c[i]); + } + m_thread_join(idx_final); + + // Clear & quit + lfds711_queue_bmm_cleanup(&g_final, NULL); + lfds711_queue_bmm_cleanup(&g_buff, NULL); +} + + +/********************************************************************************************/ + +int main(int argc, const char *argv[]) +{ + int n = (argc > 1) ? atoi(argv[1]) : 0; + /* if (n == 10) + test_function("List time",10000000, test_list); + if (n == 20) + test_function("Array time", 100000000, test_array); + if (n == 30) + test_function("Rbtree time", 1000000, test_rbtree); + if (n == 40) + test_function("Dict time", 1000000, test_dict); + if (n == 42) + test_function("DictOA time", 1000000, test_dict_oa); + if (n == 41) + test_function("DictB time", 1000000, test_dict_big); + if (n == 43) + test_function("DictS time", 1000000, test_dict_str); + if (n == 50) + test_function("Sort time", 10000000, test_sort);*/ + if (n == 60) + test_function("Buffer time", 1000000, test_buffer); + exit(0); +} +