aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/liburing/test/link_drain.c
blob: 3f4e4aef2c6dc43bd5415f09b7d39d2c818665d8 (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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#include "../config-host.h"
/* SPDX-License-Identifier: MIT */
/*
 * Description: test io_uring link io with drain io
 *
 */
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>

#include "helpers.h"
#include "liburing.h"

static int test_link_drain_one(struct io_uring *ring)
{
	struct io_uring_cqe *cqe;
	struct io_uring_sqe *sqe[5];
	struct iovec iovecs;
	int i, fd, ret;
	off_t off = 0;
	char data[5] = {0};
	char expect[5] = {0, 1, 2, 3, 4};

	fd = open("testfile", O_WRONLY | O_CREAT, 0644);
	if (fd < 0) {
		perror("open");
		return 1;
	}

	iovecs.iov_base = t_malloc(4096);
	iovecs.iov_len = 4096;

	for (i = 0; i < 5; i++) {
		sqe[i] = io_uring_get_sqe(ring);
		if (!sqe[i]) {
			printf("get sqe failed\n");
			goto err;
		}
	}

	/* normal heavy io */
	io_uring_prep_writev(sqe[0], fd, &iovecs, 1, off);
	sqe[0]->user_data = 0;

	/* link io */
	io_uring_prep_nop(sqe[1]);
	sqe[1]->flags |= IOSQE_IO_LINK;
	sqe[1]->user_data = 1;

	/* link drain io */
	io_uring_prep_nop(sqe[2]);
	sqe[2]->flags |= (IOSQE_IO_LINK | IOSQE_IO_DRAIN);
	sqe[2]->user_data = 2;

	/* link io */
	io_uring_prep_nop(sqe[3]);
	sqe[3]->user_data = 3;

	/* normal nop io */
	io_uring_prep_nop(sqe[4]);
	sqe[4]->user_data = 4;

	ret = io_uring_submit(ring);
	if (ret < 0) {
		printf("sqe submit failed\n");
		goto err;
	} else if (ret < 5) {
		printf("Submitted only %d\n", ret);
		goto err;
	}

	for (i = 0; i < 5; i++) {
		ret = io_uring_wait_cqe(ring, &cqe);
		if (ret < 0) {
			printf("child: wait completion %d\n", ret);
			goto err;
		}

		data[i] = cqe->user_data;
		io_uring_cqe_seen(ring, cqe);
	}

	if (memcmp(data, expect, 5) != 0)
		goto err;

	free(iovecs.iov_base);
	close(fd);
	unlink("testfile");
	return 0;
err:
	free(iovecs.iov_base);
	close(fd);
	unlink("testfile");
	return 1;
}

static int test_link_drain_multi(struct io_uring *ring)
{
	struct io_uring_cqe *cqe;
	struct io_uring_sqe *sqe[9];
	struct iovec iovecs;
	int i, fd, ret;
	off_t off = 0;
	char data[9] = {0};
	char expect[9] = {0, 1, 2, 3, 4, 5, 6, 7, 8};

	fd = open("testfile", O_WRONLY | O_CREAT, 0644);
	if (fd < 0) {
		perror("open");
		return 1;
	}
	unlink("testfile");

	iovecs.iov_base = t_malloc(4096);
	iovecs.iov_len = 4096;

	for (i = 0; i < 9; i++) {
		sqe[i] = io_uring_get_sqe(ring);
		if (!sqe[i]) {
			printf("get sqe failed\n");
			goto err;
		}
	}

	/* normal heavy io */
	io_uring_prep_writev(sqe[0], fd, &iovecs, 1, off);
	sqe[0]->user_data = 0;

	/* link1 io head */
	io_uring_prep_nop(sqe[1]);
	sqe[1]->flags |= IOSQE_IO_LINK;
	sqe[1]->user_data = 1;

	/* link1 drain io */
	io_uring_prep_nop(sqe[2]);
	sqe[2]->flags |= (IOSQE_IO_LINK | IOSQE_IO_DRAIN);
	sqe[2]->user_data = 2;

	/* link1 io end*/
	io_uring_prep_nop(sqe[3]);
	sqe[3]->user_data = 3;

	/* link2 io head */
	io_uring_prep_nop(sqe[4]);
	sqe[4]->flags |= IOSQE_IO_LINK;
	sqe[4]->user_data = 4;

	/* link2 io */
	io_uring_prep_nop(sqe[5]);
	sqe[5]->flags |= IOSQE_IO_LINK;
	sqe[5]->user_data = 5;

	/* link2 drain io */
	io_uring_prep_writev(sqe[6], fd, &iovecs, 1, off);
	sqe[6]->flags |= (IOSQE_IO_LINK | IOSQE_IO_DRAIN);
	sqe[6]->user_data = 6;

	/* link2 io end */
	io_uring_prep_nop(sqe[7]);
	sqe[7]->user_data = 7;

	/* normal io */
	io_uring_prep_nop(sqe[8]);
	sqe[8]->user_data = 8;

	ret = io_uring_submit(ring);
	if (ret < 0) {
		printf("sqe submit failed\n");
		goto err;
	} else if (ret < 9) {
		printf("Submitted only %d\n", ret);
		goto err;
	}

	for (i = 0; i < 9; i++) {
		ret = io_uring_wait_cqe(ring, &cqe);
		if (ret < 0) {
			printf("child: wait completion %d\n", ret);
			goto err;
		}

		data[i] = cqe->user_data;
		io_uring_cqe_seen(ring, cqe);
	}

	if (memcmp(data, expect, 9) != 0)
		goto err;

	free(iovecs.iov_base);
	close(fd);
	return 0;
err:
	free(iovecs.iov_base);
	close(fd);
	return 1;

}

static int test_drain(bool defer)
{
	struct io_uring ring;
	int i, ret;
	unsigned int flags = 0;

	if (defer)
		flags = IORING_SETUP_SINGLE_ISSUER |
			IORING_SETUP_DEFER_TASKRUN;

	ret = io_uring_queue_init(100, &ring, flags);
	if (ret) {
		printf("ring setup failed\n");
		return 1;
	}

	for (i = 0; i < 1000; i++) {
		ret = test_link_drain_one(&ring);
		if (ret) {
			fprintf(stderr, "test_link_drain_one failed\n");
			break;
		}
		ret = test_link_drain_multi(&ring);
		if (ret) {
			fprintf(stderr, "test_link_drain_multi failed\n");
			break;
		}
	}

	return ret;
}

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

	if (argc > 1)
		return T_EXIT_SKIP;

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

	if (t_probe_defer_taskrun()) {
		ret = test_drain(true);
		if (ret) {
			fprintf(stderr, "test_drain(true) failed\n");
			return T_EXIT_FAIL;
		}
	}

	return T_EXIT_PASS;
}