Commit 3e2d4df5 authored by Brian Geffon's avatar Brian Geffon Committed by Namhyung Kim
Browse files

perf tools: sched-pipe bench: add (-n) nonblocking benchmark

The -n mode will benchmark pipes in a non-blocking mode using
epoll_wait.

This specific mode was added to demonstrate the broken sync nature
of epoll: https://lore.kernel.org/lkml/20240426-zupfen-jahrzehnt-5be786bcdf04@brauner



Signed-off-by: default avatarBrian Geffon <bgeffon@google.com>
Reviewed-by: default avatarIan Rogers <irogers@google.com>
Cc: Steven Rostedt <rostedt@goodmis.org>
Link: https://lore.kernel.org/r/20241016190009.866615-1-bgeffon@google.com


Signed-off-by: default avatarNamhyung Kim <namhyung@kernel.org>
parent 915a3776
Loading
Loading
Loading
Loading
+36 −7
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@
#include <errno.h>
#include <fcntl.h>
#include <assert.h>
#include <sys/epoll.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/syscall.h>
@@ -34,6 +35,8 @@ struct thread_data {
	int			nr;
	int			pipe_read;
	int			pipe_write;
	struct epoll_event      epoll_ev;
	int			epoll_fd;
	bool			cgroup_failed;
	pthread_t		pthread;
};
@@ -44,6 +47,7 @@ static int loops = LOOPS_DEFAULT;
/* Use processes by default: */
static bool			threaded;

static bool			nonblocking;
static char			*cgrp_names[2];
static struct cgroup		*cgrps[2];

@@ -81,6 +85,7 @@ static int parse_two_cgroups(const struct option *opt __maybe_unused,
}

static const struct option options[] = {
	OPT_BOOLEAN('n', "nonblocking",	&nonblocking,	"Use non-blocking operations"),
	OPT_INTEGER('l', "loop",	&loops,		"Specify number of loops"),
	OPT_BOOLEAN('T', "threaded",	&threaded,	"Specify threads/process based task setup"),
	OPT_CALLBACK('G', "cgroups", NULL, "SEND,RECV",
@@ -165,11 +170,25 @@ static void exit_cgroup(int nr)
	free(cgrp_names[nr]);
}

static inline int read_pipe(struct thread_data *td)
{
	int ret, m;
retry:
	if (nonblocking) {
		ret = epoll_wait(td->epoll_fd, &td->epoll_ev, 1, -1);
		if (ret < 0)
			return ret;
	}
	ret = read(td->pipe_read, &m, sizeof(int));
	if (nonblocking && ret < 0 && errno == EWOULDBLOCK)
		goto retry;
	return ret;
}

static void *worker_thread(void *__tdata)
{
	struct thread_data *td = __tdata;
	int m = 0, i;
	int ret;
	int i, ret, m = 0;

	ret = enter_cgroup(td->nr);
	if (ret < 0) {
@@ -177,16 +196,23 @@ static void *worker_thread(void *__tdata)
		return NULL;
	}

	if (nonblocking) {
		td->epoll_ev.events = EPOLLIN;
		td->epoll_fd = epoll_create(1);
		BUG_ON(td->epoll_fd < 0);
		BUG_ON(epoll_ctl(td->epoll_fd, EPOLL_CTL_ADD, td->pipe_read, &td->epoll_ev) < 0);
	}

	for (i = 0; i < loops; i++) {
		if (!td->nr) {
			ret = read(td->pipe_read, &m, sizeof(int));
			ret = read_pipe(td);
			BUG_ON(ret != sizeof(int));
			ret = write(td->pipe_write, &m, sizeof(int));
			BUG_ON(ret != sizeof(int));
		} else {
			ret = write(td->pipe_write, &m, sizeof(int));
			BUG_ON(ret != sizeof(int));
			ret = read(td->pipe_read, &m, sizeof(int));
			ret = read_pipe(td);
			BUG_ON(ret != sizeof(int));
		}
	}
@@ -209,13 +235,16 @@ int bench_sched_pipe(int argc, const char **argv)
	 * discarding returned value of read(), write()
	 * causes error in building environment for perf
	 */
	int __maybe_unused ret, wait_stat;
	int __maybe_unused ret, wait_stat, flags = 0;
	pid_t pid, retpid __maybe_unused;

	argc = parse_options(argc, argv, options, bench_sched_pipe_usage, 0);

	BUG_ON(pipe(pipe_1));
	BUG_ON(pipe(pipe_2));
	if (nonblocking)
		flags |= O_NONBLOCK;

	BUG_ON(pipe2(pipe_1, flags));
	BUG_ON(pipe2(pipe_2, flags));

	gettimeofday(&start, NULL);