diff mbox series

[liburing,2/3] examples: add a simple single-shot poll benchmark

Message ID c73aebd699e851a36a8a85e263bedc56aa57e505.1655213733.git.asml.silence@gmail.com (mailing list archive)
State New
Headers show
Series single-issuer and poll benchmark | expand

Commit Message

Pavel Begunkov June 14, 2022, 2:36 p.m. UTC
Signed-off-by: Pavel Begunkov <asml.silence@gmail.com>
---
 examples/Makefile     |   3 +-
 examples/poll-bench.c | 108 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 110 insertions(+), 1 deletion(-)
 create mode 100644 examples/poll-bench.c

Comments

Ammar Faizi June 14, 2022, 2:57 p.m. UTC | #1
On 6/14/22 9:36 PM, Pavel Begunkov wrote:
> +int main(void)
> +{
> +	unsigned long tstop;
> +	unsigned long nr_reqs = 0;
> +	struct io_uring_cqe *cqe;
> +	struct io_uring_sqe *sqe;
> +	struct io_uring ring;
> +	int pipe1[2];
> +	int ret, i, qd = 128;
> +
> +	if (argc > 1)
> +		return 0;
> +

Hi Pavel,

I am testing this and it doesn't compile, the main(void) needs fixing.

```
   poll-bench.c: In function ‘main’:
   poll-bench.c:39:13: error: ‘argc’ undeclared (first use in this function)
      39 |         if (argc > 1)
         |             ^~~~
   poll-bench.c:39:13: note: each undeclared identifier is reported only once for each function it appears in
   make[1]: *** [Makefile:34: poll-bench] Error 1
   make[1]: *** Waiting for unfinished jobs....
   make[1]: Leaving directory '/home/ammarfaizi2/app/liburing/examples'
   make: *** [Makefile:12: all] Error 2
```
Pavel Begunkov June 14, 2022, 3:05 p.m. UTC | #2
On 6/14/22 15:57, Ammar Faizi wrote:
> On 6/14/22 9:36 PM, Pavel Begunkov wrote:
>> +int main(void)
>> +{
>> +    unsigned long tstop;
>> +    unsigned long nr_reqs = 0;
>> +    struct io_uring_cqe *cqe;
>> +    struct io_uring_sqe *sqe;
>> +    struct io_uring ring;
>> +    int pipe1[2];
>> +    int ret, i, qd = 128;
>> +
>> +    if (argc > 1)
>> +        return 0;
>> +
> 
> Hi Pavel,
> 
> I am testing this and it doesn't compile, the main(void) needs fixing.

Indeed, some of final changes escaped git add. Thanks Ammar

> 
> ```
>    poll-bench.c: In function ‘main’:
>    poll-bench.c:39:13: error: ‘argc’ undeclared (first use in this function)
>       39 |         if (argc > 1)
>          |             ^~~~
>    poll-bench.c:39:13: note: each undeclared identifier is reported only once for each function it appears in
>    make[1]: *** [Makefile:34: poll-bench] Error 1
>    make[1]: *** Waiting for unfinished jobs....
>    make[1]: Leaving directory '/home/ammarfaizi2/app/liburing/examples'
>    make: *** [Makefile:12: all] Error 2
> ```
>
diff mbox series

Patch

diff --git a/examples/Makefile b/examples/Makefile
index 95a45f9..8e7067f 100644
--- a/examples/Makefile
+++ b/examples/Makefile
@@ -13,7 +13,8 @@  endif
 example_srcs := \
 	io_uring-cp.c \
 	io_uring-test.c \
-	link-cp.c
+	link-cp.c \
+	poll-bench.c
 
 all_targets :=
 
diff --git a/examples/poll-bench.c b/examples/poll-bench.c
new file mode 100644
index 0000000..72ba8ef
--- /dev/null
+++ b/examples/poll-bench.c
@@ -0,0 +1,108 @@ 
+/* SPDX-License-Identifier: MIT */
+/*
+ * Description: test io_uring poll handling
+ *
+ */
+#include <errno.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <string.h>
+#include <signal.h>
+#include <poll.h>
+#include <sys/time.h>
+#include <sys/wait.h>
+
+#include "liburing.h"
+
+static char buf[4096];
+static unsigned long runtime_ms = 30000;
+
+static unsigned long gettimeofday_ms(void)
+{
+	struct timeval tv;
+
+	gettimeofday(&tv, NULL);
+	return (tv.tv_sec * 1000) + (tv.tv_usec / 1000);
+}
+
+int main(void)
+{
+	unsigned long tstop;
+	unsigned long nr_reqs = 0;
+	struct io_uring_cqe *cqe;
+	struct io_uring_sqe *sqe;
+	struct io_uring ring;
+	int pipe1[2];
+	int ret, i, qd = 128;
+
+	if (argc > 1)
+		return 0;
+
+	if (pipe(pipe1) != 0) {
+		perror("pipe");
+		return 1;
+	}
+
+	ret = io_uring_queue_init(1024, &ring, IORING_SETUP_SINGLE_ISSUER);
+	if (ret == -EINVAL) {
+		fprintf(stderr, "can't single\n");
+		ret = io_uring_queue_init(1024, &ring, 0);
+	}
+	if (ret) {
+		fprintf(stderr, "child: ring setup failed: %d\n", ret);
+		return 1;
+	}
+
+	ret = io_uring_register_files(&ring, pipe1, 2);
+	if (ret < 0) {
+		fprintf(stderr, "io_uring_register_files failed\n");
+		return 1;
+	}
+
+	ret = io_uring_register_ring_fd(&ring);
+	if (ret < 0) {
+		fprintf(stderr, "io_uring_register_ring_fd failed\n");
+		return 1;
+	}
+
+	tstop = gettimeofday_ms() + runtime_ms;
+	do {
+		for (i = 0; i < qd; i++) {
+			sqe = io_uring_get_sqe(&ring);
+			io_uring_prep_poll_add(sqe, 0, POLLIN);
+			sqe->flags |= IOSQE_FIXED_FILE;
+			sqe->user_data = 1;
+		}
+
+		ret = io_uring_submit(&ring);
+		if (ret != qd) {
+			fprintf(stderr, "child: sqe submit failed: %d\n", ret);
+			return 1;
+		}
+
+		ret = write(pipe1[1], buf, 1);
+		if (ret != 1) {
+			fprintf(stderr, "write failed %i\n", errno);
+			return 1;
+		}
+		ret = read(pipe1[0], buf, 1);
+		if (ret != 1) {
+			fprintf(stderr, "read failed %i\n", errno);
+			return 1;
+		}
+
+		for (i = 0; i < qd; i++) {
+			ret = io_uring_wait_cqe(&ring, &cqe);
+			if (ret < 0) {
+				fprintf(stderr, "child: wait completion %d\n", ret);
+				break;
+			}
+			io_uring_cqe_seen(&ring, cqe);
+			nr_reqs++;
+		}
+	} while (gettimeofday_ms() < tstop);
+
+	fprintf(stderr, "requests/s: %lu\n", nr_reqs * 1000UL / runtime_ms);
+	return 0;
+}