nvttest: Add test tool
This commit is contained in:
@@ -7,4 +7,5 @@ MAINTAINERCLEANFILES = \
|
|||||||
SUBDIRS = lib src
|
SUBDIRS = lib src
|
||||||
if BUILD_TESTTOOLS
|
if BUILD_TESTTOOLS
|
||||||
SUBDIRS += nvtsrv
|
SUBDIRS += nvtsrv
|
||||||
|
SUBDIRS += nvttest
|
||||||
endif
|
endif
|
||||||
|
|||||||
@@ -43,6 +43,7 @@ Makefile
|
|||||||
lib/Makefile
|
lib/Makefile
|
||||||
src/Makefile
|
src/Makefile
|
||||||
nvtsrv/Makefile
|
nvtsrv/Makefile
|
||||||
|
nvttest/Makefile
|
||||||
])
|
])
|
||||||
AC_OUTPUT
|
AC_OUTPUT
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,7 @@
|
|||||||
|
|
||||||
|
bin_PROGRAMS = nvttest
|
||||||
|
|
||||||
|
nvttest_SOURCES = \
|
||||||
|
nvttest.c
|
||||||
|
|
||||||
|
nvttest_CPPFLAGS = -I$(top_builddir) -I$(top_srcdir) $(CFLAGS_WARN)
|
||||||
@@ -0,0 +1,144 @@
|
|||||||
|
/*
|
||||||
|
* ttynvt stress tool
|
||||||
|
*/
|
||||||
|
#include <pthread.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#include <fcntl.h>
|
||||||
|
|
||||||
|
#include <sys/ioctl.h>
|
||||||
|
#include <asm/termbits.h>
|
||||||
|
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user