diff --git a/Makefile.am b/Makefile.am index a00a682..f8b13c8 100644 --- a/Makefile.am +++ b/Makefile.am @@ -7,4 +7,5 @@ MAINTAINERCLEANFILES = \ SUBDIRS = lib src if BUILD_TESTTOOLS SUBDIRS += nvtsrv +SUBDIRS += nvttest endif diff --git a/configure.ac b/configure.ac index c77efe5..fd7278d 100644 --- a/configure.ac +++ b/configure.ac @@ -43,6 +43,7 @@ Makefile lib/Makefile src/Makefile nvtsrv/Makefile +nvttest/Makefile ]) AC_OUTPUT diff --git a/nvttest/Makefile.am b/nvttest/Makefile.am new file mode 100644 index 0000000..ab13e79 --- /dev/null +++ b/nvttest/Makefile.am @@ -0,0 +1,7 @@ + +bin_PROGRAMS = nvttest + +nvttest_SOURCES = \ + nvttest.c + +nvttest_CPPFLAGS = -I$(top_builddir) -I$(top_srcdir) $(CFLAGS_WARN) diff --git a/nvttest/nvttest.c b/nvttest/nvttest.c new file mode 100644 index 0000000..89c98d7 --- /dev/null +++ b/nvttest/nvttest.c @@ -0,0 +1,144 @@ +/* + * ttynvt stress tool + */ +#include +#include +#include +#include +#include +#include + +#include + +#include +#include + + +static const char *dev = "/dev/ttyNVT0"; + +typedef struct { + int nthread; + int ithread; + int nloop; + bool opt_rand_delay; +} topts_t; + +static void *_worker(void *arg) +{ + topts_t *topts = arg; + int iloop; + int fd, err, nw; + struct termios ios; + + for (iloop = 0; iloop < topts->nloop; iloop++) + { + printf("Thread %d/%d run %d/%d\n", + topts->ithread, topts->nthread, iloop + 1, topts->nloop); + { + fd = open(dev, O_RDWR); + if (fd < 0) + { + printf("Open '%s' failed: %m\n", dev); + break; + } + + memset(&ios, 0, sizeof(ios)); + err = ioctl(fd, TCGETS, &ios); + if (err != 0) + { + printf("ioctl(TCGETS) failed: %m\n"); + break; + } + err = ioctl(fd, TCSETS, &ios); + if (err != 0) + { + printf("ioctl(TCSETS) failed: %m\n"); + break; + } + close(fd); + } + + { + fd = open(dev, O_RDWR); + if (fd < 0) + { + printf("Open '%s' failed: %m\n", dev); + break; + } + + nw = dprintf(fd, "Hallo\n"); + if (nw <= 0) + { + printf("write failed: %m\n"); + break; + } + close(fd); + } + + if (topts->opt_rand_delay) + usleep(rand() & 0xfff); + } + + return NULL; +} + +int main(int argc, char **argv) +{ + int err; + int opt; + int ithr, nthr; + topts_t topts = { }, *popts; + pthread_t *ptids; + + nthr = 1; + topts.nloop = 1; + + while ((opt = getopt(argc, argv, "n:rt:")) != -1) + { + switch (opt) + { + case 'n': + topts.nloop = atoi(optarg); + break; + case 'r': + topts.opt_rand_delay = true; + break; + case 't': + nthr = atoi(optarg); + break; + default: /* '?' */ + fprintf(stderr, "Usage: %s [-n nloop]\n", argv[0]); + exit(EXIT_FAILURE); + } + } + + topts.nthread = nthr; + + ptids = malloc(nthr * sizeof(pthread_t)); + + for (ithr = 0; ithr < nthr; ithr++) + { + printf("Create worker thread %d/%d\n", ithr + 1, nthr); + + popts = malloc(sizeof(topts_t)); + *popts = topts; + popts->ithread = ithr + 1; + + err = pthread_create(&ptids[ithr], NULL, _worker, popts); + if (err) + { + printf("Failed to create thread %d/%d\n", ithr, nthr); + break; + } + } + + for (ithr = 0; ithr < nthr; ithr++) + { + if (ptids[ithr] == 0) + break; + pthread_join(ptids[ithr], NULL); + printf("Joined worker thread %d/%d\n", ithr + 1, nthr); + } + + return 0; +}