aboutsummaryrefslogtreecommitdiff
path: root/test/poll-cancel-ton.c
blob: e9d612edd8ff7dffe1c4689822d80443363e4496 (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
/* SPDX-License-Identifier: MIT */
/*
 * Description: test massive amounts of poll with cancel
 *
 */
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#include <sys/poll.h>
#include <sys/wait.h>
#include <sys/signal.h>

#include "liburing.h"

#define POLL_COUNT	30000

static void *sqe_index[POLL_COUNT];

static int reap_events(struct io_uring *ring, unsigned nr_events, int nowait)
{
	struct io_uring_cqe *cqe;
	int i, ret = 0;

	for (i = 0; i < nr_events; i++) {
		if (!i && !nowait)
			ret = io_uring_wait_cqe(ring, &cqe);
		else
			ret = io_uring_peek_cqe(ring, &cqe);
		if (ret) {
			if (ret != -EAGAIN)
				fprintf(stderr, "cqe peek failed: %d\n", ret);
			break;
		}
		io_uring_cqe_seen(ring, cqe);
	}

	return i ? i : ret;
}

static int del_polls(struct io_uring *ring, int fd, int nr)
{
	int batch, i, ret;
	struct io_uring_sqe *sqe;

	while (nr) {
		batch = 1024;
		if (batch > nr)
			batch = nr;

		for (i = 0; i < batch; i++) {
			void *data;

			sqe = io_uring_get_sqe(ring);
			data = sqe_index[lrand48() % nr];
			io_uring_prep_poll_remove(sqe, data);
		}

		ret = io_uring_submit(ring);
		if (ret != batch) {
			fprintf(stderr, "%s: failed submit, %d\n", __FUNCTION__, ret);
			return 1;
		}
		nr -= batch;
		ret = reap_events(ring, 2 * batch, 0);
	}
	return 0;
}

static int add_polls(struct io_uring *ring, int fd, int nr)
{
	int pending, batch, i, count, ret;
	struct io_uring_sqe *sqe;

	pending = count = 0;
	while (nr) {
		batch = 1024;
		if (batch > nr)
			batch = nr;

		for (i = 0; i < batch; i++) {
			sqe = io_uring_get_sqe(ring);
			io_uring_prep_poll_add(sqe, fd, POLLIN);
			sqe_index[count++] = sqe;
			sqe->user_data = (unsigned long) sqe;
		}

		ret = io_uring_submit(ring);
		if (ret != batch) {
			fprintf(stderr, "%s: failed submit, %d\n", __FUNCTION__, ret);
			return 1;
		}
		nr -= batch;
		pending += batch;
		reap_events(ring, batch, 1);
	}
	return 0;
}

int main(int argc, char *argv[])
{
	struct io_uring ring;
	struct io_uring_params p = { };
	int pipe1[2];
	int ret;

	if (argc > 1)
		return 0;

	if (pipe(pipe1) != 0) {
		perror("pipe");
		return 1;
	}

	p.flags = IORING_SETUP_CQSIZE;
	p.cq_entries = 16384;
	ret = io_uring_queue_init_params(1024, &ring, &p);
	if (ret) {
		if (ret == -EINVAL) {
			fprintf(stdout, "No CQSIZE, trying without\n");
			ret = io_uring_queue_init(1024, &ring, 0);
			if (ret) {
				fprintf(stderr, "ring setup failed: %d\n", ret);
				return 1;
			}
		}
	}

	add_polls(&ring, pipe1[0], 30000);
#if 0
	usleep(1000);
#endif
	del_polls(&ring, pipe1[0], 30000);

	io_uring_queue_exit(&ring);
	return 0;
}