aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/liburing/test/iopoll-overflow.c
blob: 96152fcfe48d6611af3e83d4788bd8bc80c9313e (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
#include "../config-host.h"
/* SPDX-License-Identifier: MIT */
/*
 * Description: IOPOLL with overflow test case
 */
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <poll.h>
#include <sys/eventfd.h>
#include <sys/resource.h>
#include "helpers.h"
#include "liburing.h"
#include "../src/syscall.h"

#define FILE_SIZE	(128 * 1024)
#define BS		4096
#define BUFFERS		(FILE_SIZE / BS)

static struct iovec *vecs;

static int test(struct io_uring *ring, int fd)
{
	struct io_uring_sqe *sqe;
	int i, j, ret;
	loff_t off;

	off = FILE_SIZE - BS;
	for (j = 0; j < 8; j++) {
		for (i = 0; i < BUFFERS; i++) {
			sqe = io_uring_get_sqe(ring);
			io_uring_prep_read(sqe, fd, vecs[i].iov_base,
						vecs[i].iov_len, off);
			if (!off)
				off = FILE_SIZE - BS;
			else
				off -= BS;
		}
		ret = io_uring_submit(ring);
		if (ret != BUFFERS) {
			fprintf(stderr, "submitted %d\n", ret);
			return T_EXIT_FAIL;
		}
	}

	sleep(1);

	ret = __sys_io_uring_enter(ring->ring_fd, 0, BUFFERS * 8,
					IORING_ENTER_GETEVENTS, NULL);

	for (i = 0; i < BUFFERS * 8; i++) {
		struct io_uring_cqe *cqe;

		ret = io_uring_wait_cqe(ring, &cqe);
		if (ret) {
			fprintf(stderr, "wait=%d\n", ret);
			return T_EXIT_FAIL;
		}
		io_uring_cqe_seen(ring, cqe);
	}

	return T_EXIT_PASS;
}

int main(int argc, char *argv[])
{
	struct io_uring_params p = { };
	struct io_uring ring;
	char buf[256];
	char *fname;
	int ret, fd;

	p.flags = IORING_SETUP_IOPOLL | IORING_SETUP_CQSIZE;
	p.cq_entries = 64;
	ret = t_create_ring_params(64, &ring, &p);
	if (ret == T_SETUP_SKIP)
		return 0;
	if (ret != T_SETUP_OK) {
		fprintf(stderr, "ring create failed: %d\n", ret);
		return 1;
	}

	if (argc > 1) {
		fname = argv[1];
	} else {
		srand((unsigned)time(NULL));
		snprintf(buf, sizeof(buf), ".basic-rw-%u-%u",
			(unsigned)rand(), (unsigned)getpid());
		fname = buf;
		t_create_file(fname, FILE_SIZE);
	}

	fd = open(fname, O_RDONLY | O_DIRECT);
	if (fd < 0) {
		if (errno == EINVAL) {
			if (fname != argv[1])
				unlink(fname);
			return T_EXIT_SKIP;
		}
		perror("open");
		goto err;
	}

	vecs = t_create_buffers(BUFFERS, BS);

	ret = test(&ring, fd);

	if (fname != argv[1])
		unlink(fname);
	return ret;
err:
	if (fname != argv[1])
		unlink(fname);
	return T_EXIT_FAIL;
}