Module: xenomai-forge Branch: master Commit: 9ee8ec177df7eb92d8faad476eb5db63e939a575 URL: http://git.xenomai.org/?p=xenomai-forge.git;a=commit;h=9ee8ec177df7eb92d8faad476eb5db63e939a575
Author: Philippe Gerum <r...@xenomai.org> Date: Thu Nov 17 16:23:05 2011 +0100 testsuite/unit: introduce cond-wakeup-time --- testsuite/unit/Makefile.am | 16 +++ testsuite/unit/Makefile.in | 49 ++++++++- testsuite/unit/cond-wakeup-time.c | 211 +++++++++++++++++++++++++++++++++++++ 3 files changed, 272 insertions(+), 4 deletions(-) diff --git a/testsuite/unit/Makefile.am b/testsuite/unit/Makefile.am index 8d22a58..1561eaa 100644 --- a/testsuite/unit/Makefile.am +++ b/testsuite/unit/Makefile.am @@ -3,6 +3,7 @@ testdir = @XENO_TEST_DIR@ CCLD = $(top_srcdir)/scripts/wrap-link.sh $(CC) test_PROGRAMS = \ + cond-wakeup-time \ wakeup-time \ rtdm @@ -94,6 +95,21 @@ wakeup_time_LDADD = \ $(coredep_lib) \ -lpthread -lrt -lm +cond_wakeup_time_SOURCES = cond-wakeup-time.c + +cond_wakeup_time_CPPFLAGS = \ + @XENO_USER_CFLAGS@ \ + -I$(top_srcdir)/include \ + -Wno-missing-prototypes + +cond_wakeup_time_LDFLAGS = $(XENO_POSIX_WRAPPERS) @XENO_USER_LDFLAGS@ + +cond_wakeup_time_LDADD = \ + ../../lib/alchemy/libalchemy.la \ + ../../lib/copperplate/libcopperplate.la \ + $(coredep_lib) \ + -lpthread -lrt -lm + rtdm_SOURCES = rtdm.c rtdm_CPPFLAGS = \ diff --git a/testsuite/unit/Makefile.in b/testsuite/unit/Makefile.in index 9cc35fe..83ec58e 100644 --- a/testsuite/unit/Makefile.in +++ b/testsuite/unit/Makefile.in @@ -35,7 +35,8 @@ POST_UNINSTALL = : build_triplet = @build@ host_triplet = @host@ target_triplet = @target@ -test_PROGRAMS = wakeup-time$(EXEEXT) rtdm$(EXEEXT) $(am__EXEEXT_1) +test_PROGRAMS = cond-wakeup-time$(EXEEXT) wakeup-time$(EXEEXT) \ + rtdm$(EXEEXT) $(am__EXEEXT_1) @XENO_COBALT_TRUE@am__append_1 = arith \ @XENO_COBALT_TRUE@ mutex-torture \ @XENO_COBALT_TRUE@ cond-torture \ @@ -93,6 +94,14 @@ cond_torture_OBJECTS = $(am_cond_torture_OBJECTS) cond_torture_LINK = $(LIBTOOL) --tag=CC $(AM_LIBTOOLFLAGS) \ $(LIBTOOLFLAGS) --mode=link $(CCLD) $(AM_CFLAGS) $(CFLAGS) \ $(cond_torture_LDFLAGS) $(LDFLAGS) -o $@ +am_cond_wakeup_time_OBJECTS = \ + cond_wakeup_time-cond-wakeup-time.$(OBJEXT) +cond_wakeup_time_OBJECTS = $(am_cond_wakeup_time_OBJECTS) +cond_wakeup_time_DEPENDENCIES = ../../lib/alchemy/libalchemy.la \ + ../../lib/copperplate/libcopperplate.la $(am__DEPENDENCIES_1) +cond_wakeup_time_LINK = $(LIBTOOL) --tag=CC $(AM_LIBTOOLFLAGS) \ + $(LIBTOOLFLAGS) --mode=link $(CCLD) $(AM_CFLAGS) $(CFLAGS) \ + $(cond_wakeup_time_LDFLAGS) $(LDFLAGS) -o $@ am__mutex_torture_SOURCES_DIST = mutex-torture.c @XENO_COBALT_TRUE@am_mutex_torture_OBJECTS = \ @XENO_COBALT_TRUE@ mutex_torture-mutex-torture.$(OBJEXT) @@ -131,11 +140,12 @@ LINK = $(LIBTOOL) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) \ --mode=link $(CCLD) $(AM_CFLAGS) $(CFLAGS) $(AM_LDFLAGS) \ $(LDFLAGS) -o $@ SOURCES = $(arith_SOURCES) $(check_vdso_SOURCES) \ - $(cond_torture_SOURCES) $(mutex_torture_SOURCES) \ - $(rtdm_SOURCES) $(wakeup_time_SOURCES) + $(cond_torture_SOURCES) $(cond_wakeup_time_SOURCES) \ + $(mutex_torture_SOURCES) $(rtdm_SOURCES) \ + $(wakeup_time_SOURCES) DIST_SOURCES = $(am__arith_SOURCES_DIST) \ $(am__check_vdso_SOURCES_DIST) \ - $(am__cond_torture_SOURCES_DIST) \ + $(am__cond_torture_SOURCES_DIST) $(cond_wakeup_time_SOURCES) \ $(am__mutex_torture_SOURCES_DIST) $(rtdm_SOURCES) \ $(wakeup_time_SOURCES) ETAGS = etags @@ -367,6 +377,19 @@ wakeup_time_LDADD = \ $(coredep_lib) \ -lpthread -lrt -lm +cond_wakeup_time_SOURCES = cond-wakeup-time.c +cond_wakeup_time_CPPFLAGS = \ + @XENO_USER_CFLAGS@ \ + -I$(top_srcdir)/include \ + -Wno-missing-prototypes + +cond_wakeup_time_LDFLAGS = $(XENO_POSIX_WRAPPERS) @XENO_USER_LDFLAGS@ +cond_wakeup_time_LDADD = \ + ../../lib/alchemy/libalchemy.la \ + ../../lib/copperplate/libcopperplate.la \ + $(coredep_lib) \ + -lpthread -lrt -lm + rtdm_SOURCES = rtdm.c rtdm_CPPFLAGS = \ @XENO_USER_CFLAGS@ \ @@ -467,6 +490,9 @@ check-vdso$(EXEEXT): $(check_vdso_OBJECTS) $(check_vdso_DEPENDENCIES) cond-torture$(EXEEXT): $(cond_torture_OBJECTS) $(cond_torture_DEPENDENCIES) @rm -f cond-torture$(EXEEXT) $(cond_torture_LINK) $(cond_torture_OBJECTS) $(cond_torture_LDADD) $(LIBS) +cond-wakeup-time$(EXEEXT): $(cond_wakeup_time_OBJECTS) $(cond_wakeup_time_DEPENDENCIES) + @rm -f cond-wakeup-time$(EXEEXT) + $(cond_wakeup_time_LINK) $(cond_wakeup_time_OBJECTS) $(cond_wakeup_time_LDADD) $(LIBS) mutex-torture$(EXEEXT): $(mutex_torture_OBJECTS) $(mutex_torture_DEPENDENCIES) @rm -f mutex-torture$(EXEEXT) $(mutex_torture_LINK) $(mutex_torture_OBJECTS) $(mutex_torture_LDADD) $(LIBS) @@ -487,6 +513,7 @@ distclean-compile: @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/arith-arith.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/check_vdso-check-vdso.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/cond_torture-cond-torture.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/cond_wakeup_time-cond-wakeup-time.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/mutex_torture-mutex-torture.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/rtdm-rtdm.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/wakeup_time-wakeup-time.Po@am__quote@ @@ -568,6 +595,20 @@ cond_torture-cond-torture.obj: cond-torture.c @AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ @am__fastdepCC_FALSE@ $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(cond_torture_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o cond_torture-cond-torture.obj `if test -f 'cond-torture.c'; then $(CYGPATH_W) 'cond-torture.c'; else $(CYGPATH_W) '$(srcdir)/cond-torture.c'; fi` +cond_wakeup_time-cond-wakeup-time.o: cond-wakeup-time.c +@am__fastdepCC_TRUE@ $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(cond_wakeup_time_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -MT cond_wakeup_time-cond-wakeup-time.o -MD -MP -MF $(DEPDIR)/cond_wakeup_time-cond-wakeup-time.Tpo -c -o cond_wakeup_time-cond-wakeup-time.o `test -f 'cond-wakeup-time.c' || echo '$(srcdir)/'`cond-wakeup-time.c +@am__fastdepCC_TRUE@ $(am__mv) $(DEPDIR)/cond_wakeup_time-cond-wakeup-time.Tpo $(DEPDIR)/cond_wakeup_time-cond-wakeup-time.Po +@AMDEP_TRUE@@am__fastdepCC_FALSE@ source='cond-wakeup-time.c' object='cond_wakeup_time-cond-wakeup-time.o' libtool=no @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCC_FALSE@ $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(cond_wakeup_time_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o cond_wakeup_time-cond-wakeup-time.o `test -f 'cond-wakeup-time.c' || echo '$(srcdir)/'`cond-wakeup-time.c + +cond_wakeup_time-cond-wakeup-time.obj: cond-wakeup-time.c +@am__fastdepCC_TRUE@ $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(cond_wakeup_time_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -MT cond_wakeup_time-cond-wakeup-time.obj -MD -MP -MF $(DEPDIR)/cond_wakeup_time-cond-wakeup-time.Tpo -c -o cond_wakeup_time-cond-wakeup-time.obj `if test -f 'cond-wakeup-time.c'; then $(CYGPATH_W) 'cond-wakeup-time.c'; else $(CYGPATH_W) '$(srcdir)/cond-wakeup-time.c'; fi` +@am__fastdepCC_TRUE@ $(am__mv) $(DEPDIR)/cond_wakeup_time-cond-wakeup-time.Tpo $(DEPDIR)/cond_wakeup_time-cond-wakeup-time.Po +@AMDEP_TRUE@@am__fastdepCC_FALSE@ source='cond-wakeup-time.c' object='cond_wakeup_time-cond-wakeup-time.obj' libtool=no @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCC_FALSE@ $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(cond_wakeup_time_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o cond_wakeup_time-cond-wakeup-time.obj `if test -f 'cond-wakeup-time.c'; then $(CYGPATH_W) 'cond-wakeup-time.c'; else $(CYGPATH_W) '$(srcdir)/cond-wakeup-time.c'; fi` + mutex_torture-mutex-torture.o: mutex-torture.c @am__fastdepCC_TRUE@ $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(mutex_torture_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -MT mutex_torture-mutex-torture.o -MD -MP -MF $(DEPDIR)/mutex_torture-mutex-torture.Tpo -c -o mutex_torture-mutex-torture.o `test -f 'mutex-torture.c' || echo '$(srcdir)/'`mutex-torture.c @am__fastdepCC_TRUE@ $(am__mv) $(DEPDIR)/mutex_torture-mutex-torture.Tpo $(DEPDIR)/mutex_torture-mutex-torture.Po diff --git a/testsuite/unit/cond-wakeup-time.c b/testsuite/unit/cond-wakeup-time.c new file mode 100644 index 0000000..aaa8ea5 --- /dev/null +++ b/testsuite/unit/cond-wakeup-time.c @@ -0,0 +1,211 @@ +#include <sys/mman.h> +#include <unistd.h> +#include <stdlib.h> +#include <math.h> +#include <stdio.h> +#include <string.h> +#include <signal.h> +#include <pthread.h> +#include <semaphore.h> +#include <alchemy/timer.h> +#include <linux/unistd.h> +#include <sys/mman.h> + +sem_t sem, cond_sem; + +pthread_t thread; + +pthread_cond_t cond; + +pthread_mutex_t mutex; + +static int flag, __condcw, __condw, __condcs, __conds; + +unsigned long long cond_wakeup_start; + +#define maxloops 100000 +#define maxdim 10000 +#define TYPE int + +pid_t get_tid(void) +{ + return syscall(__NR_gettid); +} + +#if 0 +static TYPE dot(TYPE *a, TYPE *b, int n) +{ + int k = n - 1; + TYPE s = 0; + for(; k >= 0; k--) + s = s + a[k]*b[k]; + + return s; +} + +TYPE crunch(void) +{ + TYPE a[maxdim], b[maxdim]; + int i; + + for (i = 0; i < maxdim; i++) + a[i] = b[i] = 3; + + return dot(a, b, maxdim); +} +#else +static inline void crunch(void) +{ +} +#endif + +long long traced = 0; + +void cleanup(void *dummy) +{ + pthread_mutex_unlock(&mutex); +} + +void *body_cond(void *arg) +{ + long long t; + + printf("body: pid=%d\n", get_tid()); + + pthread_cleanup_push(cleanup, NULL); + + for (;;) { + int waited = 0; + pthread_mutex_lock(&mutex); + while (flag == 0) { + waited = 1; + pthread_cond_wait(&cond, &mutex); + t = __xn_rdtsc() - cond_wakeup_start; + if (__condcw && t == __condw / __condcw) { + traced = t; + xntrace_user_freeze(t, 0); + } + __condw += t; + __condcw++; + } + flag = 0; + pthread_mutex_unlock(&mutex); + crunch(); + sem_post(&sem); + } + + pthread_cleanup_pop(0); + + return NULL; +} + +void *body_sem(void *arg) +{ + long long t; + + printf("body: pid=%d\n", get_tid()); + + for (;;) { + sem_wait(&cond_sem); + t = __xn_rdtsc() - cond_wakeup_start; +#if 1 + if (__condcw && t == __condw / __condcw) { + traced = t; + xntrace_user_freeze(t, 0); + } +#endif + __condw += t; + __condcw++; + crunch(); + pthread_mutex_lock(&mutex); + flag = 1; + pthread_cond_signal(&cond); + pthread_mutex_unlock(&mutex); + } + + return NULL; +} + +int main(int argc, char **argv) +{ + struct sched_param param; + pthread_attr_t thattr; + int count = 0, ret; + long long t; + + mlockall(MCL_CURRENT | MCL_FUTURE); + + printf("main: pid=%d\n", get_tid()); + sem_init(&sem, 0, 0); + sem_init(&cond_sem, 0, 0); + + pthread_mutex_init(&mutex, NULL); + pthread_cond_init(&cond, NULL); + + param.sched_priority = 98; + ret = pthread_setschedparam(pthread_self(), SCHED_FIFO, ¶m); + if (ret) + printf("failed setting main priority(%d)\n", ret); + + pthread_attr_init(&thattr); + memset(¶m, 0, sizeof(param)); + pthread_attr_setinheritsched(&thattr, PTHREAD_EXPLICIT_SCHED); + pthread_attr_setschedpolicy(&thattr, SCHED_FIFO); + pthread_attr_setstacksize(&thattr, 65536); + param.sched_priority = 99; + pthread_attr_setschedparam(&thattr, ¶m); + pthread_create(&thread, &thattr, body_cond, NULL); + + pthread_attr_destroy(&thattr); + + while (++count < 3000) { + t = __xn_rdtsc(); + pthread_mutex_lock(&mutex); + flag = 1; + pthread_cond_signal(&cond); + cond_wakeup_start = __xn_rdtsc(); + pthread_mutex_unlock(&mutex); + __condcs++; + __conds += cond_wakeup_start - t; + sem_wait(&sem); + } + + pthread_cancel(thread); + pthread_join(thread, NULL); + + printf("average cond lock+signal time: %Ld ns\n", rt_timer_tsc2ns(__conds) / __condcs); + printf("average cond unlock time: %Ld ns\n", rt_timer_tsc2ns(__condw) / __condcw); + + count = __condw = __condcw = 0; + + if (traced) + printf("traced: %Ld\n", rt_timer_tsc2ns(traced)); + + pthread_attr_init(&thattr); + memset(¶m, 0, sizeof(param)); + pthread_attr_setinheritsched(&thattr, PTHREAD_EXPLICIT_SCHED); + pthread_attr_setschedpolicy(&thattr, SCHED_FIFO); + pthread_attr_setstacksize(&thattr, 65536); + param.sched_priority = 99; + pthread_attr_setschedparam(&thattr, ¶m); + pthread_create(&thread, &thattr, body_sem, NULL); + + while (++count < 3000) { + t = __xn_rdtsc(); + cond_wakeup_start = t; + sem_post(&cond_sem); + + pthread_mutex_lock(&mutex); + while (!flag) + pthread_cond_wait(&cond, &mutex); + flag = 0; + pthread_mutex_unlock(&mutex); + } + + pthread_cancel(thread); + pthread_join(thread, NULL); + + printf("average sem wakeup time: %Ld ns\n", rt_timer_tsc2ns(__condw) / __condcw); + + exit(0); +} _______________________________________________ Xenomai-git mailing list Xenomai-git@gna.org https://mail.gna.org/listinfo/xenomai-git