xref: /aosp_15_r20/external/liburing/test/sq-space_left.c (revision 25da2bea747f3a93b4c30fd9708b0618ef55a0e6)
1 /* SPDX-License-Identifier: MIT */
2 /*
3  * Description: test SQ queue space left
4  *
5  */
6 #include <errno.h>
7 #include <stdio.h>
8 #include <unistd.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <fcntl.h>
12 
13 #include "liburing.h"
14 
test_left(void)15 static int test_left(void)
16 {
17 	struct io_uring_sqe *sqe;
18 	struct io_uring ring;
19 	int ret, i = 0, s;
20 
21 	ret = io_uring_queue_init(8, &ring, 0);
22 	if (ret) {
23 		fprintf(stderr, "ring setup failed: %d\n", ret);
24 		return 1;
25 
26 	}
27 
28 	if ((s = io_uring_sq_space_left(&ring)) != 8) {
29 		fprintf(stderr, "Got %d SQEs left, expected %d\n", s, 8);
30 		goto err;
31 	}
32 
33 	i = 0;
34 	while ((sqe = io_uring_get_sqe(&ring)) != NULL) {
35 		i++;
36 		if ((s = io_uring_sq_space_left(&ring)) != 8 - i) {
37 			fprintf(stderr, "Got %d SQEs left, expected %d\n", s, 8 - i);
38 			goto err;
39 		}
40 	}
41 
42 	if (i != 8) {
43 		fprintf(stderr, "Got %d SQEs, expected %d\n", i, 8);
44 		goto err;
45 	}
46 
47 	io_uring_queue_exit(&ring);
48 	return 0;
49 err:
50 	io_uring_queue_exit(&ring);
51 	return 1;
52 }
53 
test_sync(void)54 static int test_sync(void)
55 {
56 	struct io_uring_sqe *sqe;
57 	struct io_uring ring;
58 	int ret, i;
59 
60 	ret = io_uring_queue_init(32, &ring, 0);
61 	if (ret) {
62 		fprintf(stderr, "ring setup failed: %d\n", ret);
63 		return 1;
64 
65 	}
66 
67 	/* prep 8 NOPS */
68 	for (i = 0; i < 8; i++) {
69 		sqe = io_uring_get_sqe(&ring);
70 		if (!sqe) {
71 			fprintf(stderr, "get sqe failed\n");
72 			goto err;
73 		}
74 		io_uring_prep_nop(sqe);
75 	}
76 
77 	/* prep known bad command, this should terminate submission */
78 	sqe = io_uring_get_sqe(&ring);
79 	if (!sqe) {
80 		fprintf(stderr, "get sqe failed\n");
81 		goto err;
82 	}
83 	io_uring_prep_nop(sqe);
84 	sqe->opcode = 0xfe;
85 
86 	/* prep 8 NOPS */
87 	for (i = 0; i < 8; i++) {
88 		sqe = io_uring_get_sqe(&ring);
89 		if (!sqe) {
90 			fprintf(stderr, "get sqe failed\n");
91 			goto err;
92 		}
93 		io_uring_prep_nop(sqe);
94 	}
95 
96 	/* we should have 8 + 1 + 8 pending now */
97 	ret = io_uring_sq_ready(&ring);
98 	if (ret != 17) {
99 		fprintf(stderr, "%d ready, wanted 17\n", ret);
100 		goto err;
101 	}
102 
103 	ret = io_uring_submit(&ring);
104 
105 	/* should submit 8 successfully, then error #9 and stop */
106 	if (ret != 9) {
107 		fprintf(stderr, "submitted %d, wanted 9\n", ret);
108 		goto err;
109 	}
110 
111 	/* should now have 8 ready, with 9 gone */
112 	ret = io_uring_sq_ready(&ring);
113 	if (ret != 8) {
114 		fprintf(stderr, "%d ready, wanted 8\n", ret);
115 		goto err;
116 	}
117 
118 	ret = io_uring_submit(&ring);
119 
120 	/* the last 8 should submit fine */
121 	if (ret != 8) {
122 		fprintf(stderr, "submitted %d, wanted 8\n", ret);
123 		goto err;
124 	}
125 
126 	ret = io_uring_sq_ready(&ring);
127 	if (ret) {
128 		fprintf(stderr, "%d ready, wanted 0\n", ret);
129 		goto err;
130 	}
131 
132 	io_uring_queue_exit(&ring);
133 	return 0;
134 err:
135 	io_uring_queue_exit(&ring);
136 	return 1;
137 }
138 
main(int argc,char * argv[])139 int main(int argc, char *argv[])
140 {
141 	int ret;
142 
143 	if (argc > 1)
144 		return 0;
145 
146 	ret = test_left();
147 	if (ret) {
148 		fprintf(stderr, "test_left failed\n");
149 		return ret;
150 	}
151 
152 	ret = test_sync();
153 	if (ret) {
154 		fprintf(stderr, "test_sync failed\n");
155 		return ret;
156 	}
157 
158 	return 0;
159 }
160