From 6fba6e14e5e6d844f300635c8c27f7ec243608ba Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Tue, 2 May 2023 11:12:29 +0200 Subject: add multi-threaded benchmarks for NUMA evaluation and the likes --- mbw.c | 206 ++++++++++++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 163 insertions(+), 43 deletions(-) (limited to 'mbw.c') diff --git a/mbw.c b/mbw.c index ca439ce..5579d95 100644 --- a/mbw.c +++ b/mbw.c @@ -6,13 +6,18 @@ #include #include #include -#include +#include #include #include #include #include #include +#ifdef MULTITHREADED +#include +#include +#endif + /* how many runs to average by default */ #define DEFAULT_NR_LOOPS 40 @@ -51,6 +56,19 @@ * watch out for swap usage (or turn off swap) */ +#ifdef MULTITHREADED +unsigned long num_threads = 1; +volatile unsigned int done = 0; +pthread_t *threads; +sem_t start_sem, stop_sem, sync_sem; +#endif + +long *arr_a, *arr_b; /* the two arrays to be copied from/to */ +unsigned long long arr_size=0; /* array size (elements in array) */ +unsigned int test_type; +/* fixed memcpy block size for -t2 */ +unsigned long long block_size=DEFAULT_BLOCK_SIZE; + void usage() { printf("mbw memory benchmark v%s, https://github.com/raas/mbw\n", VERSION); @@ -72,13 +90,13 @@ void usage() /* allocate a test array and fill it with data * so as to force Linux to _really_ allocate it */ -long *make_array(unsigned long long asize) +long *make_array() { unsigned long long t; unsigned int long_size=sizeof(long); long *a; - a=calloc(asize, long_size); + a=calloc(arr_size, long_size); if(NULL==a) { perror("Error allocating memory"); @@ -86,37 +104,109 @@ long *make_array(unsigned long long asize) } /* make sure both arrays are allocated, fill with pattern */ - for(t=0; t= block_size; t-=block_size, src+=block_size){ + dst=(char *) memcpy(dst, src, block_size) + block_size; + } + if(t) { + dst=(char *) memcpy(dst, src, t) + t; + } + } else if(test_type==TEST_DUMB) { /* dumb test */ + for(t=dumb_start; t= block_size; t-=block_size, src+=block_size){ dst=(char *) memcpy(dst, src, block_size) + block_size; @@ -125,13 +215,14 @@ double worker(unsigned long long asize, long *a, long *b, int type, unsigned lon dst=(char *) memcpy(dst, src, t) + t; } gettimeofday(&endtime, NULL); - } else if(type==TEST_DUMB) { /* dumb test */ + } else if(test_type==TEST_DUMB) { /* dumb test */ gettimeofday(&starttime, NULL); - for(t=0; tMAX_TESTS-1) { @@ -254,24 +346,42 @@ int main(int argc, char **argv) /* ------------------------------------------------------ */ +#ifdef MULTITHREADED + if (sem_init(&start_sem, 0, 0) != 0) { + err(1, "sem_init"); + } + if (sem_init(&stop_sem, 0, 0) != 0) { + err(1, "sem_init"); + } + if (sem_init(&sync_sem, 0, 0) != 0) { + err(1, "sem_init"); + } + threads = calloc(num_threads, sizeof(pthread_t)); + for (i=0; i < num_threads; i++) { + if (pthread_create(&threads[i], NULL, thread_worker, (void*)(unsigned long)i) != 0) { + err(1, "pthread_create"); + } + } +#endif + long_size=sizeof(long); /* the size of long on this platform */ - asize=1024*1024/long_size*mt; /* how many longs then in one array? */ + arr_size=1024*1024/long_size*mt; /* how many longs then in one array? */ - if(asize*long_size < block_size) { + if(arr_size*long_size < block_size) { printf("Error: array size larger than block size (%llu bytes)!\n", block_size); exit(1); } if(!quiet) { printf("Long uses %d bytes. ", long_size); - printf("Allocating 2*%lld elements = %lld bytes of memory.\n", asize, 2*asize*long_size); + printf("Allocating 2*%lld elements = %lld bytes of memory.\n", arr_size, 2*arr_size*long_size); if(tests[2]) { printf("Using %lld bytes as blocks for memcpy block copy test.\n", block_size); } } - a=make_array(asize); - b=make_array(asize); + arr_a=make_array(); + arr_b=make_array(); /* ------------------------------------------------------ */ if(!quiet) { @@ -279,24 +389,34 @@ int main(int argc, char **argv) } /* run all tests requested, the proper number of times */ - for(testno=0; testno