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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
|
#define _GNU_SOURCE
#include <sys/mman.h>
#include <unistd.h>
#include "file-scanner.h"
#include "utils.h"
void
file_scanner__init (struct file_scanner *fs, int fd)
{
union {
int i;
void *p;
} ret;
ASSERT (fs != NULL);
ASSERT (fd >= 0);
fs->to_mark_as_scanned = 0;
fs->input_fd = fd; // Open with O_DIRECT?
ret.i = posix_fadvise (fs->input_fd, 0, 0, POSIX_FADV_SEQUENTIAL);
ASSERT (ret.i == 0);
struct stat st;
ret.i = fstat (fs->input_fd, &st);
ASSERT (ret.i == 0);
ASSERT (st.st_size > 0);
fs->input_size = st.st_size;
// Create backing memory (can't be virtual in order to double map)
fs->memfd = memfd_create ("file-scanner", 0); // TODO(cmmm): gen UID from fs
ASSERT (fs->memfd != -1);
ret.i = ftruncate (fs->memfd, RING_SIZE*READ_SIZE);
ASSERT (ret.i == 0);
// Reserve contiguous virtual memory region for double mapping memfd
fs->buffer = mmap (NULL, 2*RING_SIZE*READ_SIZE, PROT_NONE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
ASSERT (fs->buffer != MAP_FAILED);
// Double map buffer (to cleanly handle wraparound)
ret.p = mmap (fs->buffer, RING_SIZE*READ_SIZE,
PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED, fs->memfd, 0);
ASSERT (ret.p != MAP_FAILED);
ret.p = mmap (fs->buffer + RING_SIZE*READ_SIZE, RING_SIZE*READ_SIZE,
PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED, fs->memfd, 0);
ASSERT (ret.p != MAP_FAILED);
ret.i = io_uring_queue_init (IO_URING_SIZE, &fs->io_uring, 0);
ASSERT (ret.i == 0);
struct iovec iov[RING_SIZE];
for (int i = 0; i < RING_SIZE; ++i) {
iov[i].iov_base = fs->buffer + i*READ_SIZE;
iov[i].iov_len = READ_SIZE;
}
io_uring_register_buffers (&fs->io_uring, iov, RING_SIZE);
io_uring_register_files (&fs->io_uring, &fs->input_fd, 1);
fs->scanner.cursor = fs->buffer;
fs->scanner.end = fs->buffer; // NOTE(cmmm): haven't read anything yet
// Fill io_uring queue
size_t read_size = MIN (4 << 10, // Start with reading a single page
fs->input_size - fs->input_submitted);
fs->input_scanned = 0;
fs->input_submitted = 0;
int submitted;
for (submitted = 0; submitted < IO_URING_SIZE;) {
// Getting a submission queue entry (SQE)
struct io_uring_sqe *sqe = io_uring_get_sqe (&fs->io_uring);
ASSERT (sqe != NULL);
submitted += 1;
// Prepare a read
// printf ("prepared: %lu @ %lu\n", read_size, fs->input_submitted);
io_uring_prep_read_fixed (sqe, 0, fs->buffer + fs->input_submitted,
read_size, fs->input_submitted,
fs->input_submitted/READ_SIZE);
sqe->flags |= IOSQE_FIXED_FILE;
fs->input_submitted += read_size;
io_uring_sqe_set_data64 (sqe, fs->input_submitted);
if (fs->input_submitted == RING_SIZE*READ_SIZE ||
fs->input_submitted == fs->input_size)
break;
if (read_size > fs->input_size - fs->input_submitted)
read_size = fs->input_size - fs->input_submitted; // For last read
else if (read_size < READ_SIZE && fs->input_submitted != read_size)
read_size *= 2; // Ramp-up
}
// Submit SQEs to the kernel
ret.i = io_uring_submit (&fs->io_uring);
ASSERT_MSG (ret.i == submitted, "io_uring_submit: %s", strerror (-ret.i));
}
// void
// file_scanner__reinit (struct file_scanner *fs, int fd)
// {
// union {
// int i;
// void *p;
// } ret;
// ASSERT (fs);
// ASSERT (fd >= 0);
// fs->input_fd = fd; // Open with O_DIRECT?
// posix_fadvise (fs->input_fd, 0, 0, POSIX_FADV_SEQUENTIAL);
// struct stat st;
// ret = fstat (fs->input_fd, &st);
// ASSERT (ret.i != -1);
// fs->input_bytes_to_submit = st.st_size;
// fs->scanner.begin = fs->buffer;
// fs->scanner.end = fs->buffer; // NOTE(cmmm): Haven't read anything yet
// // Reset io_uring queues
// memset (&fs->io_uring.sq, 0, sizeof (fs->io_uring.sq));
// memset (&fs->io_uring.cq, 0, sizeof (fs->io_uring.cq));
// io_uring_cq_advance (&fs->io_uring, fs->io_uring.cq.ring_tail);
// // Fill io_uring submission queue
// size_t read_size = 4 << 10; // Start with reading a single page
// size_t bytes_submitted = 0;
// for (int enqueued = 0; enqueued < IO_URING_SIZE; ++enqueued) {
// // Getting a submission queue entry (SQE)
// struct io_uring_sqe *sqe = io_uring_get_sqe (&fs->io_uring);
// ASSERT (sqe != NULL);
// // Prepare a read
// io_uring_prep_read (
// sqe, fs->input_fd, fs->buffer + bytes_submitted, read_size, bytes_submitted);
// io_uring_sqe_set_data (sqe, bytes_submitted);
// bytes_submitted += read_size;
// fs->input_bytes_to_submit -= read_size;
// if (fs->input_bytes_to_submit == 0)
// break;
// if (fs->input_bytes_to_submit < read_size)
// read_size = fs->input_bytes_to_submit; // For last read
// else if (read_size < RING_SIZE && bytes_submitted != read_size)
// read_size *= 2; // Ramp-up
// }
// // Submit SQEs to the kernel
// ret = io_uring_submit (&fs->io_uring);
// ASSERT (ret == 0);
// }
void
file_scanner__deinit (struct file_scanner *fs)
{
ASSERT (fs != NULL);
io_uring_queue_exit (&fs->io_uring);
munmap (fs->buffer + RING_SIZE*READ_SIZE, RING_SIZE*READ_SIZE);
munmap (fs->buffer, RING_SIZE*READ_SIZE);
close (fs->memfd);
}
static void
mark_scanned (struct file_scanner *fs, size_t offset)
{
if (offset == 0) return;
ASSERT (fs != NULL);
ASSERT (fs->input_scanned + offset <= fs->input_submitted);
int buffers_to_read = (fs->input_scanned + offset)/READ_SIZE -
(fs->input_scanned )/READ_SIZE;
fs->input_scanned += offset;
// printf ("Reading %d buffers (scanned %lu)\n", buffers_to_read, offset);
if (!buffers_to_read) return;
for (int i = 0; i < buffers_to_read; ++i) {
// Getting a submission queue entry (SQE)
struct io_uring_sqe *sqe = io_uring_get_sqe (&fs->io_uring);
ASSERT (sqe != NULL);
// Prepare a read
size_t read_size = MIN (READ_SIZE, fs->input_size - fs->input_submitted);
io_uring_prep_read_fixed (sqe, 0, &fs->buffer[fs->input_submitted %
(RING_SIZE*READ_SIZE)],
read_size, fs->input_submitted,
fs->input_submitted/READ_SIZE % RING_SIZE);
sqe->flags |= IOSQE_FIXED_FILE;
fs->input_submitted += read_size;
io_uring_sqe_set_data64 (sqe, fs->input_submitted);
}
// submit SQEs to the kernel
int ret = io_uring_submit (&fs->io_uring);
ASSERT_MSG (ret == buffers_to_read, "io_uring_submit: %s", strerror (-ret));
}
void
wrap_scanner (struct file_scanner *fs)
{
if (fs->scanner.cursor - fs->buffer < RING_SIZE*READ_SIZE)
return;
fs->scanner.end = fs->scanner.cursor;
fs->scanner.cursor -= RING_SIZE*READ_SIZE;
}
void
file_scanner__skip_until (struct file_scanner *fs, char c)
{
ASSERT (fs != NULL);
mark_scanned (fs, fs->to_mark_as_scanned);
fs->to_mark_as_scanned = 0;
do {
wrap_scanner (fs);
const char *begin = fs->scanner.cursor;
scanner__skip_until (&fs->scanner, c);
// printf ("%s %p %p %p\n", __func__, fs->buffer, fs->scanner.cursor, fs->scanner.end);
mark_scanned (fs, fs->scanner.cursor - begin);
} while ( scanner__reached_end (&fs->scanner) &&
!file_scanner__reached_end (fs));
}
struct string_view
file_scanner__get_until (struct file_scanner *fs, char c)
{
ASSERT (fs != NULL);
mark_scanned (fs, fs->to_mark_as_scanned);
fs->to_mark_as_scanned = 0;
wrap_scanner (fs);
struct string_view sv = { .begin = fs->scanner.cursor };
do {
scanner__skip_until (&fs->scanner, c);
// printf ("%s %p %p %p\n", __func__, fs->buffer, fs->scanner.cursor, fs->scanner.end);
} while ( scanner__reached_end (&fs->scanner) &&
!file_scanner__reached_end (fs));
sv.end = fs->scanner.cursor;
fs->to_mark_as_scanned = sv.end - sv.begin;
return sv;
}
// TODO(cmmm): Could potentially be used to skip reads, has to be
// implemented differently
void
file_scanner__skip_n (struct file_scanner *fs, size_t n)
{
ASSERT (fs != NULL);
mark_scanned (fs, fs->to_mark_as_scanned);
fs->to_mark_as_scanned = 0;
do {
wrap_scanner (fs);
const char *begin = fs->scanner.cursor;
scanner__skip_n (&fs->scanner, n);
// printf ("%s %p %p %p\n", __func__, fs->buffer, fs->scanner.cursor, fs->scanner.end);
size_t skipped = fs->scanner.cursor - begin;
n -= skipped;
mark_scanned (fs, skipped);
if (n == 0) break;
} while ( scanner__reached_end (&fs->scanner) &&
!file_scanner__reached_end (fs));
}
bool
file_scanner__reached_end (struct file_scanner *fs)
{
ASSERT (fs != NULL);
mark_scanned (fs, fs->to_mark_as_scanned);
if (!scanner__reached_end (&fs->scanner)) return false;
if (fs->input_scanned == fs->input_size) return true;
// Wait for completion
struct io_uring_cqe *cqe;
int ret = io_uring_wait_cqe (&fs->io_uring, &cqe);
ASSERT (ret == 0);
ASSERT (cqe->res != -1);
ASSERT (cqe->res != 0);
int res = cqe->res;
size_t end = io_uring_cqe_get_data64 (cqe);
io_uring_cqe_seen (&fs->io_uring, cqe);
// printf ("res: %d, end: %lu\n", res, end);
fs->scanner.end = fs->buffer + end % (RING_SIZE*READ_SIZE) + (RING_SIZE*READ_SIZE);
// printf ("%s %p %p %p\n", __func__, fs->buffer, fs->scanner.cursor, fs->scanner.end);
return false;
}
|