• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 	if (nr < 0) {
41 		fprintf(stderr, "Fill: %d\n", nr);
42 		goto err;
43 	}
44 
45 	ret = io_uring_submit(ring);
46 	if (ret != nr) {
47 		fprintf(stderr, "submit %d, wanted %d\n", ret, nr);
48 		goto err;
49 	}
50 	total += ret;
51 
52 	nr = fill_nops(ring);
53 	if (nr < 0) {
54 		fprintf(stderr, "Fill: %d\n", nr);
55 		goto err;
56 	}
57 
58 	ret = io_uring_submit(ring);
59 	if (ret != nr) {
60 		fprintf(stderr, "submit %d, wanted %d\n", ret, nr);
61 		goto err;
62 	}
63 	total += ret;
64 
65 	for (i = 0; i < total; i++) {
66 		ret = io_uring_wait_cqe(ring, &cqe);
67 		if (ret < 0) {
68 			fprintf(stderr, "wait completion %d\n", ret);
69 			goto err;
70 		}
71 
72 		io_uring_cqe_seen(ring, cqe);
73 	}
74 	return 0;
75 err:
76 	return 1;
77 }
78 
main(int argc,char * argv[])79 int main(int argc, char *argv[])
80 {
81 	struct io_uring ring;
82 	int ret, depth;
83 
84 	if (argc > 1)
85 		return 0;
86 
87 	depth = 1;
88 	while (depth <= MAX_ENTRIES) {
89 		ret = io_uring_queue_init(depth, &ring, 0);
90 		if (ret) {
91 			if (ret == -ENOMEM)
92 				break;
93 			fprintf(stderr, "ring setup failed: %d\n", ret);
94 			return 1;
95 		}
96 
97 		ret = test_nops(&ring);
98 		if (ret) {
99 			fprintf(stderr, "test_single_nop failed\n");
100 			return ret;
101 		}
102 		depth <<= 1;
103 		io_uring_queue_exit(&ring);
104 	}
105 
106 	return 0;
107 }
108