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 <sys/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";
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 (fd < 0) {
93 perror("open");
94 return -1;
95 }
96
97 for (i = 0; i < NR_RINGS; i++) {
98 struct io_uring_params p = { };
99
100 p.flags = IORING_SETUP_SQPOLL;
101 if (i) {
102 p.wq_fd = rings[0].ring_fd;
103 p.flags |= IORING_SETUP_ATTACH_WQ;
104 }
105 ret = io_uring_queue_init_params(BUFFERS, &rings[i], &p);
106 if (ret) {
107 fprintf(stderr, "queue_init: %d/%d\n", ret, i);
108 goto err;
109 }
110 /* no sharing for non-fixed either */
111 if (!(p.features & IORING_FEAT_SQPOLL_NONFIXED)) {
112 fprintf(stdout, "No SQPOLL sharing, skipping\n");
113 return 0;
114 }
115 }
116
117 ios = 0;
118 while (ios < (FILE_SIZE / BS)) {
119 for (i = 0; i < NR_RINGS; i++) {
120 ret = queue_io(&rings[i], fd, BUFFERS);
121 if (ret < 0)
122 goto err;
123 rets[i] = ret;
124 }
125 for (i = 0; i < NR_RINGS; i++) {
126 if (wait_io(&rings[i], rets[i]))
127 goto err;
128 }
129 ios += BUFFERS;
130 }
131
132 if (fname != argv[1])
133 unlink(fname);
134 return 0;
135 err:
136 if (fname != argv[1])
137 unlink(fname);
138 return 1;
139 }
140