xref: /aosp_15_r20/external/liburing/test/nop-all-sizes.c (revision 25da2bea747f3a93b4c30fd9708b0618ef55a0e6)
1 /* SPDX-License-Identifier: MIT */
2 /*
3  * Description: exercise full filling of SQ and CQ ring
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 
15 #define MAX_ENTRIES	32768
16 
fill_nops(struct io_uring * ring)17 static int fill_nops(struct io_uring *ring)
18 {
19 	struct io_uring_sqe *sqe;
20 	int filled = 0;
21 
22 	do {
23 		sqe = io_uring_get_sqe(ring);
24 		if (!sqe)
25 			break;
26 
27 		io_uring_prep_nop(sqe);
28 		filled++;
29 	} while (1);
30 
31 	return filled;
32 }
33 
test_nops(struct io_uring * ring)34 static int test_nops(struct io_uring *ring)
35 {
36 	struct io_uring_cqe *cqe;
37 	int ret, nr, total = 0, i;
38 
39 	nr = fill_nops(ring);
40 
41 	ret = io_uring_submit(ring);
42 	if (ret != nr) {
43 		fprintf(stderr, "submit %d, wanted %d\n", ret, nr);
44 		goto err;
45 	}
46 	total += ret;
47 
48 	nr = fill_nops(ring);
49 
50 	ret = io_uring_submit(ring);
51 	if (ret != nr) {
52 		fprintf(stderr, "submit %d, wanted %d\n", ret, nr);
53 		goto err;
54 	}
55 	total += ret;
56 
57 	for (i = 0; i < total; i++) {
58 		ret = io_uring_wait_cqe(ring, &cqe);
59 		if (ret < 0) {
60 			fprintf(stderr, "wait completion %d\n", ret);
61 			goto err;
62 		}
63 
64 		io_uring_cqe_seen(ring, cqe);
65 	}
66 	return 0;
67 err:
68 	return 1;
69 }
70 
main(int argc,char * argv[])71 int main(int argc, char *argv[])
72 {
73 	struct io_uring ring;
74 	int ret, depth;
75 
76 	if (argc > 1)
77 		return 0;
78 
79 	depth = 1;
80 	while (depth <= MAX_ENTRIES) {
81 		ret = io_uring_queue_init(depth, &ring, 0);
82 		if (ret) {
83 			if (ret == -ENOMEM)
84 				break;
85 			fprintf(stderr, "ring setup failed: %d\n", ret);
86 			return 1;
87 		}
88 
89 		ret = test_nops(&ring);
90 		if (ret) {
91 			fprintf(stderr, "test_single_nop failed\n");
92 			return ret;
93 		}
94 		depth <<= 1;
95 		io_uring_queue_exit(&ring);
96 	}
97 
98 	return 0;
99 }
100