aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/liburing/test/sq-space_left.c
blob: 9402377df7ff5c07595760b154060e3d8c86c245 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include "../config-host.h"
/* SPDX-License-Identifier: MIT */
/*
 * Description: test SQ queue space left
 *
 */
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>

#include "liburing.h"

static int test_left(void)
{
	struct io_uring_sqe *sqe;
	struct io_uring ring;
	int ret, i = 0, s;

	ret = io_uring_queue_init(8, &ring, 0);
	if (ret) {
		fprintf(stderr, "ring setup failed: %d\n", ret);
		return 1;

	}

	if ((s = io_uring_sq_space_left(&ring)) != 8) {
		fprintf(stderr, "Got %d SQEs left, expected %d\n", s, 8);
		goto err;
	}

	i = 0;
	while ((sqe = io_uring_get_sqe(&ring)) != NULL) {
		i++;
		if ((s = io_uring_sq_space_left(&ring)) != 8 - i) {
			fprintf(stderr, "Got %d SQEs left, expected %d\n", s, 8 - i);
			goto err;
		}
	}

	if (i != 8) {
		fprintf(stderr, "Got %d SQEs, expected %d\n", i, 8);
		goto err;
	}

	io_uring_queue_exit(&ring);
	return 0;
err:
	io_uring_queue_exit(&ring);
	return 1;
}

static int test_sync(void)
{
	struct io_uring_sqe *sqe;
	struct io_uring ring;
	int ret, i;

	ret = io_uring_queue_init(32, &ring, 0);
	if (ret) {
		fprintf(stderr, "ring setup failed: %d\n", ret);
		return 1;

	}

	/* prep 8 NOPS */
	for (i = 0; i < 8; i++) {
		sqe = io_uring_get_sqe(&ring);
		if (!sqe) {
			fprintf(stderr, "get sqe failed\n");
			goto err;
		}
		io_uring_prep_nop(sqe);
	}

	/* prep known bad command, this should terminate submission */
	sqe = io_uring_get_sqe(&ring);
	if (!sqe) {
		fprintf(stderr, "get sqe failed\n");
		goto err;
	}
	io_uring_prep_nop(sqe);
	sqe->opcode = 0xfe;

	/* prep 8 NOPS */
	for (i = 0; i < 8; i++) {
		sqe = io_uring_get_sqe(&ring);
		if (!sqe) {
			fprintf(stderr, "get sqe failed\n");
			goto err;
		}
		io_uring_prep_nop(sqe);
	}

	/* we should have 8 + 1 + 8 pending now */
	ret = io_uring_sq_ready(&ring);
	if (ret != 17) {
		fprintf(stderr, "%d ready, wanted 17\n", ret);
		goto err;
	}

	ret = io_uring_submit(&ring);

	/* should submit 8 successfully, then error #9 and stop */
	if (ret != 9) {
		fprintf(stderr, "submitted %d, wanted 9\n", ret);
		goto err;
	}

	/* should now have 8 ready, with 9 gone */
	ret = io_uring_sq_ready(&ring);
	if (ret != 8) {
		fprintf(stderr, "%d ready, wanted 8\n", ret);
		goto err;
	}

	ret = io_uring_submit(&ring);

	/* the last 8 should submit fine */
	if (ret != 8) {
		fprintf(stderr, "submitted %d, wanted 8\n", ret);
		goto err;
	}

	ret = io_uring_sq_ready(&ring);
	if (ret) {
		fprintf(stderr, "%d ready, wanted 0\n", ret);
		goto err;
	}

	io_uring_queue_exit(&ring);
	return 0;
err:
	io_uring_queue_exit(&ring);
	return 1;
}

int main(int argc, char *argv[])
{
	int ret;

	if (argc > 1)
		return 0;

	ret = test_left();
	if (ret) {
		fprintf(stderr, "test_left failed\n");
		return ret;
	}

	ret = test_sync();
	if (ret) {
		fprintf(stderr, "test_sync failed\n");
		return ret;
	}

	return 0;
}