Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tetragon: Add bench_reader application #1512

Merged
merged 1 commit into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion contrib/tester-progs/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@ PROGS = sigkill-tester \
uprobe-test-1 \
uprobe-test-2 \
lseek-pipe \
threads-tester
threads-tester \
bench-reader

all: $(PROGS)

%: %.c
$(GCC) -Wall $< -o $@

bench-reader: bench-reader.c
$(GCC) -Wall $< -o $@ -lpthread

threads-tester: threads-tester.c
$(GCC) -Wall -fno-inline $< -o $@ -lcap -lpthread

Expand Down
107 changes: 107 additions & 0 deletions contrib/tester-progs/bench-reader.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@

#define _GNU_SOURCE

#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <pthread.h>
#include <sys/sysinfo.h>
#include <sched.h>
#include <time.h>

struct thread_arg {
int id;
int cpu;
int fd;
size_t io_size;
useconds_t nsleep;
};

void *
reader(void *arg_)
{
struct thread_arg *arg = (struct thread_arg *)arg_;

cpu_set_t set;
CPU_ZERO(&set);
CPU_SET(arg->cpu, &set);
if (sched_setaffinity(0, sizeof(set), &set) == -1) {
perror("sched_setaffinity");
exit(1);
}

printf("start thread %d (cpu:%d)\n", arg->id, arg->cpu);
char buff[arg->io_size];
for (;;) {
read(arg->fd, buff, sizeof(buff));
if (arg->nsleep) {
const struct timespec req = {
.tv_nsec = arg->nsleep,
};

nanosleep(&req, NULL);
}
}
return NULL;
}

void
usage(char *prog)
{
fprintf(stderr, "Usage: %s [-t <nthreads>] [-w wait] [-s nsleep]\n", prog);
}

int
main(int argc, char **argv)
{

int zero_fd;
int wait_time = 60*10, nthreads, opt, ncpus, io_size = 10;
useconds_t thread_nsleep = 100;

ncpus = get_nprocs();
nthreads = ncpus;

while ((opt = getopt(argc, argv, "t:s:w:i:")) != -1) {
switch (opt) {
case 't':
nthreads = atoi(optarg);
break;
case 'w':
wait_time = atoi(optarg);
break;
case 's':
thread_nsleep = atoi(optarg);
break;
case 'i':
io_size = atoi(optarg);
break;
default: /* '?' */
usage(argv[0]);
exit(1);
}
}

printf("nthreads=%d wait_time=%d thread_nsleep=%d io_size=%d\n", nthreads, wait_time, thread_nsleep, io_size);
zero_fd = open("/dev/zero", O_RDONLY);
if (zero_fd < 0) {
perror("open");
exit(1);
}

pthread_t tids[nthreads];
struct thread_arg args[nthreads];

for (int i = 0; i < nthreads; i++) {
struct thread_arg *arg = &args[i];
arg->fd = zero_fd;
arg->nsleep = thread_nsleep;
arg->io_size = io_size;
arg->id = i;
arg->cpu = i % ncpus;
pthread_create(&tids[i], NULL, reader, arg);
}

sleep(wait_time);
}