• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: MIT */
2 #include <errno.h>
3 #include <stdio.h>
4 #include <unistd.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <signal.h>
8 #include <poll.h>
9 #include <sys/time.h>
10 #include <sys/wait.h>
11 
12 #include "liburing.h"
13 
14 static char buf[4096];
15 static unsigned long runtime_ms = 10000;
16 
gettimeofday_ms(void)17 static unsigned long gettimeofday_ms(void)
18 {
19 	struct timeval tv;
20 
21 	gettimeofday(&tv, NULL);
22 	return (tv.tv_sec * 1000) + (tv.tv_usec / 1000);
23 }
24 
main(void)25 int main(void)
26 {
27 	unsigned long tstop;
28 	unsigned long nr_reqs = 0;
29 	struct io_uring_cqe *cqe;
30 	struct io_uring_sqe *sqe;
31 	struct io_uring ring;
32 	int pipe1[2];
33 	int ret, i, qd = 32;
34 
35 	if (pipe(pipe1) != 0) {
36 		perror("pipe");
37 		return 1;
38 	}
39 
40 	ret = io_uring_queue_init(1024, &ring, IORING_SETUP_SINGLE_ISSUER);
41 	if (ret == -EINVAL) {
42 		fprintf(stderr, "can't single\n");
43 		ret = io_uring_queue_init(1024, &ring, 0);
44 	}
45 	if (ret) {
46 		fprintf(stderr, "child: ring setup failed: %d\n", ret);
47 		return 1;
48 	}
49 
50 	ret = io_uring_register_files(&ring, pipe1, 2);
51 	if (ret < 0) {
52 		fprintf(stderr, "io_uring_register_files failed\n");
53 		return 1;
54 	}
55 
56 	ret = io_uring_register_ring_fd(&ring);
57 	if (ret < 0) {
58 		fprintf(stderr, "io_uring_register_ring_fd failed\n");
59 		return 1;
60 	}
61 
62 	tstop = gettimeofday_ms() + runtime_ms;
63 	do {
64 		for (i = 0; i < qd; i++) {
65 			sqe = io_uring_get_sqe(&ring);
66 			io_uring_prep_poll_add(sqe, 0, POLLIN);
67 			sqe->flags |= IOSQE_FIXED_FILE;
68 			sqe->user_data = 1;
69 		}
70 
71 		ret = io_uring_submit(&ring);
72 		if (ret != qd) {
73 			fprintf(stderr, "child: sqe submit failed: %d\n", ret);
74 			return 1;
75 		}
76 
77 		ret = write(pipe1[1], buf, 1);
78 		if (ret != 1) {
79 			fprintf(stderr, "write failed %i\n", errno);
80 			return 1;
81 		}
82 		ret = read(pipe1[0], buf, 1);
83 		if (ret != 1) {
84 			fprintf(stderr, "read failed %i\n", errno);
85 			return 1;
86 		}
87 
88 		for (i = 0; i < qd; i++) {
89 			ret = io_uring_wait_cqe(&ring, &cqe);
90 			if (ret < 0) {
91 				fprintf(stderr, "child: wait completion %d\n", ret);
92 				break;
93 			}
94 			io_uring_cqe_seen(&ring, cqe);
95 			nr_reqs++;
96 		}
97 	} while (gettimeofday_ms() < tstop);
98 
99 	fprintf(stderr, "requests/s: %lu\n", nr_reqs * 1000UL / runtime_ms);
100 	return 0;
101 }
102