From 2f4c700a11691e8ab31cb43e8ff1b4254822a094 Mon Sep 17 00:00:00 2001 From: Matthew Dillon Date: Wed, 27 Jul 2016 11:22:56 -0700 Subject: [PATCH] test - Add umtx1 code * Add umtx1 code - fast context switch tests * Make blib.c thread-safe. --- test/sysperf/Makefile | 9 ++++- test/sysperf/blib.c | 6 ++-- test/sysperf/umtx1.c | 95 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 106 insertions(+), 4 deletions(-) create mode 100644 test/sysperf/umtx1.c diff --git a/test/sysperf/Makefile b/test/sysperf/Makefile index eae531adef..7ebf953ded 100644 --- a/test/sysperf/Makefile +++ b/test/sysperf/Makefile @@ -2,7 +2,8 @@ TARGETS=/tmp/sc1 /tmp/sc2 /tmp/sc3 /tmp/sc4 /tmp/sc5 /tmp/sc6 /tmp/sc7 \ /tmp/loop1 /tmp/loop2 /tmp/loop3 /tmp/loop4 \ /tmp/call1 /tmp/call2 /tmp/call3 /tmp/cmp \ /tmp/mt2 /tmp/mt3 /tmp/mt4 \ - /tmp/fork1 /tmp/pipe1 /tmp/pipe2 \ + /tmp/fork1 /tmp/pipe1 /tmp/pipe2 /tmp/pipe3 \ + /tmp/umtx1 \ /tmp/sp1 \ /tmp/sw1 /tmp/sw2 /tmp/sw3 \ /tmp/mbw1 \ @@ -53,6 +54,12 @@ all: $(TARGETS) /tmp/pipe2: pipe2.c blib.c $(CC) $(CFLAGS) pipe2.c blib.c -o /tmp/pipe2 +/tmp/pipe3: pipe3.c blib.c + $(CC) $(CFLAGS) pipe3.c blib.c -pthread -o /tmp/pipe3 + +/tmp/umtx1: umtx1.c blib.c + $(CC) $(CFLAGS) umtx1.c blib.c -pthread -o /tmp/umtx1 + /tmp/sp1: socketpair.c blib.c $(CC) $(CFLAGS) socketpair.c blib.c -o /tmp/sp1 diff --git a/test/sysperf/blib.c b/test/sysperf/blib.c index 99ff2824ba..6539eccd46 100644 --- a/test/sysperf/blib.c +++ b/test/sysperf/blib.c @@ -12,9 +12,9 @@ #include #include -static struct timeval tv1; -static struct timeval tv2; -static long long last_us; +static __thread struct timeval tv1; +static __thread struct timeval tv2; +static __thread long long last_us; void start_timing(void) diff --git a/test/sysperf/umtx1.c b/test/sysperf/umtx1.c new file mode 100644 index 0000000000..786954754a --- /dev/null +++ b/test/sysperf/umtx1.c @@ -0,0 +1,95 @@ +/* + * umtx1.c + */ + +#include +#include +#include +#include +#include +#include +#include "blib.h" + +static void *do_child(void *arg); +static void *do_parent(void *arg); + +u_int mtx; +u_long total; + +int +main(int ac, char **av) +{ + pthread_t td1; + pthread_t td2; + int n; + int k; + int status; + + printf("tests umtx hand-off loop\n"); + for (n = 1; n; --n) { + if (fork() == 0) { + start_timing(); + for (k = 0; k < 10; ++k) { + pthread_create(&td1, NULL, do_child, NULL); + pthread_create(&td2, NULL, do_parent, NULL); + pthread_join(td2, NULL); + pthread_join(td1, NULL); + } + stop_timing(total, "total"); + _exit(0); + } + } + while (wait3(&status, 0, NULL) <= 0 || errno == EINTR) + ; + + return 0; +} + +static +void * +do_child(void *arg __unused) +{ + for (;;) { + while (mtx == 0) + umtx_sleep(&mtx, 0, 0); + if (atomic_swap_int(&mtx, 0) == 2) { + umtx_wakeup(&mtx, 0); + pthread_yield(); + break; + } + umtx_wakeup(&mtx, 0); + } + return NULL; +} + +static +void * +do_parent(void *arg __unused) +{ + int j; + int loops; + + for (j = 0; j < 1000000; ++j) { + atomic_swap_int(&mtx, 1); + umtx_wakeup(&mtx, 0); + pthread_yield(); + while (mtx == 1) + umtx_sleep(&mtx, 1, 0); + } + start_timing(); + for (j = 0; j < 1000000; ++j) { + atomic_swap_int(&mtx, 1); + umtx_wakeup(&mtx, 0); + while (mtx == 1) + umtx_sleep(&mtx, 1, 0); + } + stop_timing(j, "mtx1"); + atomic_add_long(&total, j * 2); + + atomic_swap_int(&mtx, 2); + umtx_wakeup(&mtx, 0); + while (mtx == 2) + umtx_sleep(&mtx, 2, 0); + + return NULL; +} -- 2.11.4.GIT