xref: /aosp_15_r20/external/liburing/test/sq-poll-share.c (revision 25da2bea747f3a93b4c30fd9708b0618ef55a0e6)
1 /* SPDX-License-Identifier: MIT */
2 /*
3  * Description: test SQPOLL with IORING_SETUP_ATTACH_WQ
4  */
5 #include <errno.h>
6 #include <stdio.h>
7 #include <unistd.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <fcntl.h>
11 #include <sys/types.h>
12 #include <poll.h>
13 #include <sys/eventfd.h>
14 #include <sys/resource.h>
15 
16 #include "helpers.h"
17 #include "liburing.h"
18 
19 #define FILE_SIZE	(128 * 1024 * 1024)
20 #define BS		4096
21 #define BUFFERS		64
22 
23 #define NR_RINGS	4
24 
25 static struct iovec *vecs;
26 
wait_io(struct io_uring * ring,int nr_ios)27 static int wait_io(struct io_uring *ring, int nr_ios)
28 {
29 	struct io_uring_cqe *cqe;
30 
31 	while (nr_ios) {
32 		int ret = io_uring_wait_cqe(ring, &cqe);
33 
34 		if (ret == -EAGAIN) {
35 			continue;
36 		} else if (ret) {
37 			fprintf(stderr, "io_uring_wait_cqe failed %i\n", ret);
38 			return 1;
39 		}
40 		if (cqe->res != BS) {
41 			fprintf(stderr, "Unexpected ret %d\n", cqe->res);
42 			return 1;
43 		}
44 		io_uring_cqe_seen(ring, cqe);
45 		nr_ios--;
46 	}
47 
48 	return 0;
49 }
50 
queue_io(struct io_uring * ring,int fd,int nr_ios)51 static int queue_io(struct io_uring *ring, int fd, int nr_ios)
52 {
53 	unsigned long off;
54 	int i;
55 
56 	i = 0;
57 	off = 0;
58 	while (nr_ios) {
59 		struct io_uring_sqe *sqe;
60 
61 		sqe = io_uring_get_sqe(ring);
62 		if (!sqe)
63 			break;
64 		io_uring_prep_read(sqe, fd, vecs[i].iov_base, vecs[i].iov_len, off);
65 		nr_ios--;
66 		i++;
67 		off += BS;
68 	}
69 
70 	io_uring_submit(ring);
71 	return i;
72 }
73 
main(int argc,char * argv[])74 int main(int argc, char *argv[])
75 {
76 	struct io_uring rings[NR_RINGS];
77 	int rets[NR_RINGS];
78 	unsigned long ios;
79 	int i, ret, fd;
80 	char *fname;
81 
82 	if (argc > 1) {
83 		fname = argv[1];
84 	} else {
85 		fname = ".basic-rw-poll-share";
86 		t_create_file(fname, FILE_SIZE);
87 	}
88 
89 	vecs = t_create_buffers(BUFFERS, BS);
90 
91 	fd = open(fname, O_RDONLY | O_DIRECT);
92 	if (fname != argv[1])
93 		unlink(fname);
94 	if (fd < 0) {
95 		perror("open");
96 		return -1;
97 	}
98 
99 	for (i = 0; i < NR_RINGS; i++) {
100 		struct io_uring_params p = { };
101 
102 		p.flags = IORING_SETUP_SQPOLL;
103 		if (i) {
104 			p.wq_fd = rings[0].ring_fd;
105 			p.flags |= IORING_SETUP_ATTACH_WQ;
106 		}
107 		ret = io_uring_queue_init_params(BUFFERS, &rings[i], &p);
108 		if (ret) {
109 			fprintf(stderr, "queue_init: %d/%d\n", ret, i);
110 			goto err;
111 		}
112 		/* no sharing for non-fixed either */
113 		if (!(p.features & IORING_FEAT_SQPOLL_NONFIXED)) {
114 			fprintf(stdout, "No SQPOLL sharing, skipping\n");
115 			return 0;
116 		}
117 	}
118 
119 	ios = 0;
120 	while (ios < (FILE_SIZE / BS)) {
121 		for (i = 0; i < NR_RINGS; i++) {
122 			ret = queue_io(&rings[i], fd, BUFFERS);
123 			if (ret < 0)
124 				goto err;
125 			rets[i] = ret;
126 		}
127 		for (i = 0; i < NR_RINGS; i++) {
128 			if (wait_io(&rings[i], rets[i]))
129 				goto err;
130 		}
131 		ios += BUFFERS;
132 	}
133 
134 	return 0;
135 err:
136 	return 1;
137 }
138