aboutsummaryrefslogtreecommitdiffstats
path: root/test/defer-tw-timeout.c
blob: 4313eae9fbffcc83d005a2a96daec59361384799 (plain)
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
/* SPDX-License-Identifier: MIT */
/*
 * Description: test waiting for more events than what will be posted with
 *		a timeout with DEFER_TASKRUN. All kernels should time out,
 *		but a non-buggy kernel will end up with one CQE available
 *		for reaping. Buggy kernels will not have processed the
 *		task_work and will have 0 events.
 *
 */
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>

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

struct d {
	int fd;
};

static void *thread_fn(void *data)
{
	struct d *d = data;
	int ret;

	usleep(100000);
	ret = write(d->fd, "Hello", 5);
	if (ret < 0)
		perror("write");
	return NULL;
}

static int test_poll(struct io_uring *ring)
{
	struct io_uring_cqe *cqe;
	struct io_uring_sqe *sqe;
	struct __kernel_timespec ts;
	int ret, fds[2], i;
	pthread_t thread;
	char buf[32];
	struct d d;
	void *tret;

	if (pipe(fds) < 0) {
		perror("pipe");
		return 1;
	}
	d.fd = fds[1];

	sqe = io_uring_get_sqe(ring);
	io_uring_prep_read(sqe, fds[0], buf, sizeof(buf), 0);

	pthread_create(&thread, NULL, thread_fn, &d);

	ts.tv_sec = 1;
	ts.tv_nsec = 0;

	ret = io_uring_submit_and_wait_timeout(ring, &cqe, 2, &ts, NULL);
	if (ret != 1) {
		fprintf(stderr, "unexpected wait ret %d\n", ret);
		return T_EXIT_FAIL;
	}

	for (i = 0; i < 2; i++) {
		ret = io_uring_peek_cqe(ring, &cqe);
		if (ret)
			break;
		io_uring_cqe_seen(ring, cqe);
	}

	if (i != 1) {
		fprintf(stderr, "Got %d request, expected 1\n", i);
		return T_EXIT_FAIL;
	}

	pthread_join(thread, &tret);
	return T_EXIT_PASS;
}

static int test_file(struct io_uring *ring, char *__fname)
{
	struct io_uring_cqe *cqe;
	struct io_uring_sqe *sqe;
	struct __kernel_timespec ts;
	char filename[64], *fname;
	int fd, ret, i;
	void *buf;

	if (!__fname) {
		fname = filename;
		sprintf(fname, ".defer-tw-timeout.%d", getpid());
		t_create_file(fname, 128*1024);
	} else {
		fname = __fname;
	}

	fd = open(fname, O_RDONLY | O_DIRECT);
	if (fd < 0) {
		if (errno == EINVAL) {
			if (!__fname)
				unlink(fname);
			return T_EXIT_SKIP;
		}
		perror("open");
		if (!__fname)
			unlink(fname);
		return T_EXIT_FAIL;
	}

	if (!__fname)
		unlink(fname);

	if (posix_memalign(&buf, 4096, 4096)) {
		close(fd);
		return T_EXIT_FAIL;
	}

	sqe = io_uring_get_sqe(ring);
	io_uring_prep_read(sqe, fd, buf, 4096, 0);

	ts.tv_sec = 1;
	ts.tv_nsec = 0;

	ret = io_uring_submit_and_wait_timeout(ring, &cqe, 2, &ts, NULL);
	if (ret != 1) {
		fprintf(stderr, "unexpected wait ret %d\n", ret);
		close(fd);
		return T_EXIT_FAIL;
	}

	for (i = 0; i < 2; i++) {
		ret = io_uring_peek_cqe(ring, &cqe);
		if (ret)
			break;
		io_uring_cqe_seen(ring, cqe);
	}

	if (i != 1) {
		fprintf(stderr, "Got %d request, expected 1\n", i);
		close(fd);
		return T_EXIT_FAIL;
	}

	close(fd);
	return T_EXIT_PASS;
}

int main(int argc, char *argv[])
{
	struct io_uring ring;
	char *fname = NULL;
	int ret;

	ret = io_uring_queue_init(8, &ring, IORING_SETUP_SINGLE_ISSUER | IORING_SETUP_DEFER_TASKRUN);
	if (ret == -EINVAL)
		return T_EXIT_SKIP;

	if (argc > 1)
		fname = argv[1];

	ret = test_file(&ring, fname);
	if (ret != T_EXIT_PASS)
		return ret;

	ret = test_poll(&ring);
	if (ret != T_EXIT_PASS)
		return ret;

	return T_EXIT_PASS;
}