Skip to contentSkip to conversation

test poll missing events

PATCH Applied Pavel Begunkov 2 participants 5 messages Nov 23, 2022 io_uring

lore
Share & tools
lore
#0 lore raw

cover test poll missing events

Pavel Begunkov (3):
tests: remove sigalarm from poll.c
tests: refactor poll.c
tests: check for missing multipoll events

 test/poll.c | 146 ++++++++++++++++++++++++++++++++++++++++++----------
 1 file changed, 120 insertions(+), 26 deletions(-)
Signature · 1 line
2.38.1
#1 lore raw

PATCH 1/3 tests: remove sigalarm from poll.c

Test loop handles timeouting and killing stuck tests, no need to have a separate sigalarm in poll.c

---
 test/poll.c | 14 --------------
 1 file changed, 14 deletions(-)
test/poll.c +0 -14
index 1cd57ba..42123bd 100644
@@ -14,12 +14,6 @@
 
 #include "liburing.h"
 
-static void sig_alrm(int sig)
-{
-	fprintf(stderr, "Timed out!\n");
-	exit(1);
-}
-
 int main(int argc, char *argv[])
 {
 	struct io_uring_cqe *cqe;
@@ -43,20 +37,12 @@ int main(int argc, char *argv[])
 		perror("fork");
 		exit(2);
 	case 0: {
-		struct sigaction act;
-
 		ret = io_uring_queue_init(1, &ring, 0);
 		if (ret) {
 			fprintf(stderr, "child: ring setup failed: %d\n", ret);
 			return 1;
 		}
 
-		memset(&act, 0, sizeof(act));
-		act.sa_handler = sig_alrm;
-		act.sa_flags = SA_RESTART;
-		sigaction(SIGALRM, &act, NULL);
-		alarm(1);
-
 		sqe = io_uring_get_sqe(&ring);
 		if (!sqe) {
 			fprintf(stderr, "get sqe failed\n");
Signature · 1 line
2.38.1
#2 lore raw

PATCH 2/3 tests: refactor poll.c

In reply to Pavel Begunkov

Extract all poll.c testing into a separate function

---
 test/poll.c | 49 ++++++++++++++++++++++++++++++++-----------------
 1 file changed, 32 insertions(+), 17 deletions(-)
test/poll.c +32 -17
index 42123bd..f0c1c40 100644
@@ -12,9 +12,10 @@
 #include <poll.h>
 #include <sys/wait.h>
 
+#include "helpers.h"
 #include "liburing.h"
 
-int main(int argc, char *argv[])
+static int test_basic(void)
 {
 	struct io_uring_cqe *cqe;
 	struct io_uring_sqe *sqe;
@@ -23,20 +24,16 @@ int main(int argc, char *argv[])
 	pid_t p;
 	int ret;
 
-	if (argc > 1)
-		return 0;
-
 	if (pipe(pipe1) != 0) {
 		perror("pipe");
 		return 1;
 	}
 
 	p = fork();
-	switch (p) {
-	case -1:
+	if (p == -1) {
 		perror("fork");
 		exit(2);
-	case 0: {
+	} else if (p == 0) {
 		ret = io_uring_queue_init(1, &ring, 0);
 		if (ret) {
 			fprintf(stderr, "child: ring setup failed: %d\n", ret);
@@ -78,18 +75,36 @@ int main(int argc, char *argv[])
 							(long) cqe->res);
 			return 1;
 		}
+
+		io_uring_queue_exit(&ring);
 		exit(0);
-		}
-	default:
-		do {
-			errno = 0;
-			ret = write(pipe1[1], "foo", 3);
-		} while (ret == -1 && errno == EINTR);
+	}
 
-		if (ret != 3) {
-			fprintf(stderr, "parent: bad write return %d\n", ret);
-			return 1;
-		}
+	do {
+		errno = 0;
+		ret = write(pipe1[1], "foo", 3);
+	} while (ret == -1 && errno == EINTR);
+
+	if (ret != 3) {
+		fprintf(stderr, "parent: bad write return %d\n", ret);
+		return 1;
+	}
+	close(pipe1[0]);
+	close(pipe1[1]);
+	return 0;
+}
+
+int main(int argc, char *argv[])
+{
+	int ret;
+
+	if (argc > 1)
 		return 0;
+
+	ret = test_basic();
+	if (ret) {
+		fprintf(stderr, "test_basic() failed %i\n", ret);
+		return T_EXIT_FAIL;
 	}
+	return 0;
 }
Signature · 1 line
2.38.1
#3 lore raw

PATCH 3/3 tests: check for missing multipoll events

In reply to Pavel Begunkov
---
 test/poll.c | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 93 insertions(+)
test/poll.c +93 -0
index f0c1c40..a7db7dc 100644
@@ -11,10 +11,17 @@
 #include <signal.h>
 #include <poll.h>
 #include <sys/wait.h>
+#include <error.h>
 
 #include "helpers.h"
 #include "liburing.h"
 
+static void do_setsockopt(int fd, int level, int optname, int val)
+{
+	if (setsockopt(fd, level, optname, &val, sizeof(val)))
+		error(1, errno, "setsockopt %d.%d: %d", level, optname, val);
+}
+
 static int test_basic(void)
 {
 	struct io_uring_cqe *cqe;
@@ -94,6 +101,85 @@ static int test_basic(void)
 	return 0;
 }
 
+static int test_missing_events(void)
+{
+	struct io_uring_cqe *cqe;
+	struct io_uring_sqe *sqe;
+	struct io_uring ring;
+	int i, ret, sp[2];
+	char buf[2] = {};
+	int res_mask = 0;
+
+	if (!t_probe_defer_taskrun())
+		return 0;
+
+	ret = io_uring_queue_init(8, &ring, IORING_SETUP_SINGLE_ISSUER |
+					    IORING_SETUP_DEFER_TASKRUN);
+	if (ret) {
+		fprintf(stderr, "ring setup failed: %d\n", ret);
+		return 1;
+	}
+
+	if (socketpair(AF_UNIX, SOCK_STREAM, 0, sp) != 0) {
+		perror("Failed to create Unix-domain socket pair\n");
+		return 1;
+	}
+	do_setsockopt(sp[0], SOL_SOCKET, SO_SNDBUF, 1);
+	ret = send(sp[0], buf, sizeof(buf), 0);
+	if (ret != sizeof(buf)) {
+		perror("send failed\n");
+		return 1;
+	}
+
+	sqe = io_uring_get_sqe(&ring);
+	io_uring_prep_poll_multishot(sqe, sp[0], POLLIN|POLLOUT);
+	ret = io_uring_submit(&ring);
+	if (ret != 1) {
+		fprintf(stderr, "sqe submit failed: %d\n", ret);
+		return 1;
+	}
+
+	/* trigger EPOLLIN */
+	ret = send(sp[1], buf, sizeof(buf), 0);
+	if (ret != sizeof(buf)) {
+		fprintf(stderr, "send sp[1] failed %i %i\n", ret, errno);
+		return 1;
+	}
+
+	/* trigger EPOLLOUT */
+	ret = recv(sp[1], buf, sizeof(buf), 0);
+	if (ret != sizeof(buf)) {
+		perror("recv failed\n");
+		return 1;
+	}
+
+	for (i = 0; ; i++) {
+		if (i == 0)
+			ret = io_uring_wait_cqe(&ring, &cqe);
+		else
+			ret = io_uring_peek_cqe(&ring, &cqe);
+
+		if (i != 0 && ret == -EAGAIN) {
+			break;
+		}
+		if (ret) {
+			fprintf(stderr, "wait completion %d, %i\n", ret, i);
+			return 1;
+		}
+		res_mask |= cqe->res;
+		io_uring_cqe_seen(&ring, cqe);
+	}
+
+	if ((res_mask & (POLLIN|POLLOUT)) != (POLLIN|POLLOUT)) {
+		fprintf(stderr, "missing poll events %i\n", res_mask);
+		return 1;
+	}
+	io_uring_queue_exit(&ring);
+	close(sp[0]);
+	close(sp[1]);
+	return 0;
+}
+
 int main(int argc, char *argv[])
 {
 	int ret;
@@ -106,5 +192,12 @@ int main(int argc, char *argv[])
 		fprintf(stderr, "test_basic() failed %i\n", ret);
 		return T_EXIT_FAIL;
 	}
+
+	ret = test_missing_events();
+	if (ret) {
+		fprintf(stderr, "test_missing_events() failed %i\n", ret);
+		return T_EXIT_FAIL;
+	}
+
 	return 0;
 }
Signature · 1 line
2.38.1
maintainer Applied
#4 lore raw
In reply to Pavel Begunkov
Quoted 9 lines · Pavel Begunkov

Pavel Begunkov (3):
tests: remove sigalarm from poll.c
tests: refactor poll.c
tests: check for missing multipoll events

test/poll.c | 146 ++++++++++++++++++++++++++++++++++++++++++----------

 1 file changed, 120 insertions(+), 26 deletions(-)

[...]

Applied, thanks!

[1/3] tests: remove sigalarm from poll.c

      commit: b532e9cb82a320186d6f9860ee2fdf2af0012dec
[2/3] tests: refactor poll.c
      commit: 81107fed5e17217caf7c6c51e6b468269de6403f
[3/3] tests: check for missing multipoll events
      commit: b90a28636e5b5efe6dc1383acc90aec61814d9ba

Best regards,

Signature · 1 line
Jens Axboe