aboutsummaryrefslogtreecommitdiff
path: root/test/fadvise.c
blob: 278a045d76743945d1d821931c4b26509df1536d (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
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
/* SPDX-License-Identifier: MIT */
/*
 * Description: basic fadvise test
 */
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/time.h>

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

#define FILE_SIZE	(128 * 1024)
#define LOOPS		100
#define MIN_LOOPS	10

static unsigned long long utime_since(const struct timeval *s,
				      const struct timeval *e)
{
	long long sec, usec;

	sec = e->tv_sec - s->tv_sec;
	usec = (e->tv_usec - s->tv_usec);
	if (sec > 0 && usec < 0) {
		sec--;
		usec += 1000000;
	}

	sec *= 1000000;
	return sec + usec;
}

static unsigned long long utime_since_now(struct timeval *tv)
{
	struct timeval end;

	gettimeofday(&end, NULL);
	return utime_since(tv, &end);
}

static int do_fadvise(struct io_uring *ring, int fd, off_t offset, off_t len,
		      int advice)
{
	struct io_uring_sqe *sqe;
	struct io_uring_cqe *cqe;
	int ret;

	sqe = io_uring_get_sqe(ring);
	if (!sqe) {
		fprintf(stderr, "failed to get sqe\n");
		return 1;
	}

	io_uring_prep_fadvise(sqe, fd, offset, len, advice);
	sqe->user_data = advice;
	ret = io_uring_submit_and_wait(ring, 1);
	if (ret != 1) {
		fprintf(stderr, "submit: %d\n", ret);
		return 1;
	}

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

	ret = cqe->res;
	if (ret == -EINVAL || ret == -EBADF) {
		fprintf(stdout, "Fadvise not supported, skipping\n");
		unlink(".fadvise.tmp");
		exit(0);
	} else if (ret) {
		fprintf(stderr, "cqe->res=%d\n", cqe->res);
	}
	io_uring_cqe_seen(ring, cqe);
	return ret;
}

static long do_read(int fd, char *buf)
{
	struct timeval tv;
	int ret;
	long t;

	ret = lseek(fd, 0, SEEK_SET);
	if (ret) {
		perror("lseek");
		return -1;
	}
	
	gettimeofday(&tv, NULL);
	ret = read(fd, buf, FILE_SIZE);
	t = utime_since_now(&tv);
	if (ret < 0) {
		perror("read");
		return -1;
	} else if (ret != FILE_SIZE) {
		fprintf(stderr, "short read1: %d\n", ret);
		return -1;
	}

	return t;
}

static int test_fadvise(struct io_uring *ring, const char *filename)
{
	unsigned long cached_read, uncached_read, cached_read2;
	int fd, ret;
	char *buf;

	fd = open(filename, O_RDONLY);
	if (fd < 0) {
		perror("open");
		return 1;
	}

	buf = t_malloc(FILE_SIZE);

	cached_read = do_read(fd, buf);
	if (cached_read == -1)
		return 1;

	ret = do_fadvise(ring, fd, 0, FILE_SIZE, POSIX_FADV_DONTNEED);
	if (ret)
		return 1;

	uncached_read = do_read(fd, buf);
	if (uncached_read == -1)
		return 1;

	ret = do_fadvise(ring, fd, 0, FILE_SIZE, POSIX_FADV_DONTNEED);
	if (ret)
		return 1;

	ret = do_fadvise(ring, fd, 0, FILE_SIZE, POSIX_FADV_WILLNEED);
	if (ret)
		return 1;

	fsync(fd);

	cached_read2 = do_read(fd, buf);
	if (cached_read2 == -1)
		return 1;

	if (cached_read < uncached_read &&
	    cached_read2 < uncached_read)
		return 0;

	return 2;
}

int main(int argc, char *argv[])
{
	struct io_uring ring;
	int ret, i, good, bad;
	char *fname;

	if (argc > 1) {
		fname = argv[1];
	} else {
		fname = ".fadvise.tmp";
		t_create_file(fname, FILE_SIZE);
	}
	if (io_uring_queue_init(8, &ring, 0)) {
		fprintf(stderr, "ring creation failed\n");
		goto err;
	}

	good = bad = 0;
	for (i = 0; i < LOOPS; i++) {
		ret = test_fadvise(&ring, fname);
		if (ret == 1) {
			fprintf(stderr, "read_fadvise failed\n");
			goto err;
		} else if (!ret)
			good++;
		else if (ret == 2)
			bad++;
		if (i >= MIN_LOOPS && !bad)
			break;
	}

	/* too hard to reliably test, just ignore */
	if (0 && bad > good) {
		fprintf(stderr, "Suspicious timings\n");
		goto err;
	}

	if (fname != argv[1])
		unlink(fname);
	io_uring_queue_exit(&ring);
	return 0;
err:
	if (fname != argv[1])
		unlink(fname);
	return 1;
}