Files
ttynvt/nvttest/nvttest.c
T
2023-02-15 19:33:41 +01:00

145 lines
3.1 KiB
C

/*
* 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;
}