diff options
| author | 2026-11-10 08:29:58 +0000 | |
|---|---|---|
| committer | 2026-11-10 08:33:56 +0000 | |
| commit | 776276d5c7e67356a880f3e31076d32cdc1c1882 (patch) | |
| tree | cbde131f3d46338adee9c67ac0f988295736bdd4 /src/file-scanner.c | |
| download | coursework-776276d5c7e67356a880f3e31076d32cdc1c1882.tar.gz | |
Diffstat (limited to 'src/file-scanner.c')
| -rw-r--r-- | src/file-scanner.c | 301 |
1 files changed, 301 insertions, 0 deletions
diff --git a/src/file-scanner.c b/src/file-scanner.c new file mode 100644 index 0000000..6ae217a --- /dev/null +++ b/src/file-scanner.c | |||
| @@ -0,0 +1,301 @@ | |||
| 1 | #define _GNU_SOURCE | ||
| 2 | #include <sys/mman.h> | ||
| 3 | #include <unistd.h> | ||
| 4 | |||
| 5 | #include "file-scanner.h" | ||
| 6 | #include "utils.h" | ||
| 7 | |||
| 8 | void | ||
| 9 | file_scanner__init (struct file_scanner *fs, int fd) | ||
| 10 | { | ||
| 11 | union { | ||
| 12 | int i; | ||
| 13 | void *p; | ||
| 14 | } ret; | ||
| 15 | |||
| 16 | ASSERT (fs != NULL); | ||
| 17 | ASSERT (fd >= 0); | ||
| 18 | |||
| 19 | fs->to_mark_as_scanned = 0; | ||
| 20 | |||
| 21 | fs->input_fd = fd; // Open with O_DIRECT? | ||
| 22 | ret.i = posix_fadvise (fs->input_fd, 0, 0, POSIX_FADV_SEQUENTIAL); | ||
| 23 | ASSERT (ret.i == 0); | ||
| 24 | |||
| 25 | struct stat st; | ||
| 26 | ret.i = fstat (fs->input_fd, &st); | ||
| 27 | ASSERT (ret.i == 0); | ||
| 28 | ASSERT (st.st_size > 0); | ||
| 29 | fs->input_size = st.st_size; | ||
| 30 | |||
| 31 | // Create backing memory (can't be virtual in order to double map) | ||
| 32 | fs->memfd = memfd_create ("file-scanner", 0); // TODO(cmmm): gen UID from fs | ||
| 33 | ASSERT (fs->memfd != -1); | ||
| 34 | ret.i = ftruncate (fs->memfd, RING_SIZE*READ_SIZE); | ||
| 35 | ASSERT (ret.i == 0); | ||
| 36 | |||
| 37 | // Reserve contiguous virtual memory region for double mapping memfd | ||
| 38 | fs->buffer = mmap (NULL, 2*RING_SIZE*READ_SIZE, PROT_NONE, | ||
| 39 | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); | ||
| 40 | ASSERT (fs->buffer != MAP_FAILED); | ||
| 41 | |||
| 42 | // Double map buffer (to cleanly handle wraparound) | ||
| 43 | ret.p = mmap (fs->buffer, RING_SIZE*READ_SIZE, | ||
| 44 | PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED, fs->memfd, 0); | ||
| 45 | ASSERT (ret.p != MAP_FAILED); | ||
| 46 | ret.p = mmap (fs->buffer + RING_SIZE*READ_SIZE, RING_SIZE*READ_SIZE, | ||
| 47 | PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED, fs->memfd, 0); | ||
| 48 | ASSERT (ret.p != MAP_FAILED); | ||
| 49 | |||
| 50 | ret.i = io_uring_queue_init (IO_URING_SIZE, &fs->io_uring, 0); | ||
| 51 | ASSERT (ret.i == 0); | ||
| 52 | |||
| 53 | struct iovec iov[RING_SIZE]; | ||
| 54 | for (int i = 0; i < RING_SIZE; ++i) { | ||
| 55 | iov[i].iov_base = fs->buffer + i*READ_SIZE; | ||
| 56 | iov[i].iov_len = READ_SIZE; | ||
| 57 | } | ||
| 58 | io_uring_register_buffers (&fs->io_uring, iov, RING_SIZE); | ||
| 59 | io_uring_register_files (&fs->io_uring, &fs->input_fd, 1); | ||
| 60 | |||
| 61 | fs->scanner.cursor = fs->buffer; | ||
| 62 | fs->scanner.end = fs->buffer; // NOTE(cmmm): haven't read anything yet | ||
| 63 | |||
| 64 | // Fill io_uring queue | ||
| 65 | size_t read_size = MIN (4 << 10, // Start with reading a single page | ||
| 66 | fs->input_size - fs->input_submitted); | ||
| 67 | fs->input_scanned = 0; | ||
| 68 | fs->input_submitted = 0; | ||
| 69 | int submitted; | ||
| 70 | for (submitted = 0; submitted < IO_URING_SIZE;) { | ||
| 71 | // Getting a submission queue entry (SQE) | ||
| 72 | struct io_uring_sqe *sqe = io_uring_get_sqe (&fs->io_uring); | ||
| 73 | ASSERT (sqe != NULL); | ||
| 74 | submitted += 1; | ||
| 75 | |||
| 76 | // Prepare a read | ||
| 77 | // printf ("prepared: %lu @ %lu\n", read_size, fs->input_submitted); | ||
| 78 | io_uring_prep_read_fixed (sqe, 0, fs->buffer + fs->input_submitted, | ||
| 79 | read_size, fs->input_submitted, | ||
| 80 | fs->input_submitted/READ_SIZE); | ||
| 81 | sqe->flags |= IOSQE_FIXED_FILE; | ||
| 82 | fs->input_submitted += read_size; | ||
| 83 | io_uring_sqe_set_data64 (sqe, fs->input_submitted); | ||
| 84 | |||
| 85 | if (fs->input_submitted == RING_SIZE*READ_SIZE || | ||
| 86 | fs->input_submitted == fs->input_size) | ||
| 87 | break; | ||
| 88 | if (read_size > fs->input_size - fs->input_submitted) | ||
| 89 | read_size = fs->input_size - fs->input_submitted; // For last read | ||
| 90 | else if (read_size < READ_SIZE && fs->input_submitted != read_size) | ||
| 91 | read_size *= 2; // Ramp-up | ||
| 92 | } | ||
| 93 | |||
| 94 | // Submit SQEs to the kernel | ||
| 95 | ret.i = io_uring_submit (&fs->io_uring); | ||
| 96 | ASSERT_MSG (ret.i == submitted, "io_uring_submit: %s", strerror (-ret.i)); | ||
| 97 | } | ||
| 98 | |||
| 99 | // void | ||
| 100 | // file_scanner__reinit (struct file_scanner *fs, int fd) | ||
| 101 | // { | ||
| 102 | // union { | ||
| 103 | // int i; | ||
| 104 | // void *p; | ||
| 105 | // } ret; | ||
| 106 | |||
| 107 | // ASSERT (fs); | ||
| 108 | // ASSERT (fd >= 0); | ||
| 109 | |||
| 110 | // fs->input_fd = fd; // Open with O_DIRECT? | ||
| 111 | // posix_fadvise (fs->input_fd, 0, 0, POSIX_FADV_SEQUENTIAL); | ||
| 112 | |||
| 113 | // struct stat st; | ||
| 114 | // ret = fstat (fs->input_fd, &st); | ||
| 115 | // ASSERT (ret.i != -1); | ||
| 116 | // fs->input_bytes_to_submit = st.st_size; | ||
| 117 | |||
| 118 | // fs->scanner.begin = fs->buffer; | ||
| 119 | // fs->scanner.end = fs->buffer; // NOTE(cmmm): Haven't read anything yet | ||
| 120 | |||
| 121 | // // Reset io_uring queues | ||
| 122 | // memset (&fs->io_uring.sq, 0, sizeof (fs->io_uring.sq)); | ||
| 123 | // memset (&fs->io_uring.cq, 0, sizeof (fs->io_uring.cq)); | ||
| 124 | // io_uring_cq_advance (&fs->io_uring, fs->io_uring.cq.ring_tail); | ||
| 125 | |||
| 126 | // // Fill io_uring submission queue | ||
| 127 | // size_t read_size = 4 << 10; // Start with reading a single page | ||
| 128 | // size_t bytes_submitted = 0; | ||
| 129 | // for (int enqueued = 0; enqueued < IO_URING_SIZE; ++enqueued) { | ||
| 130 | // // Getting a submission queue entry (SQE) | ||
| 131 | // struct io_uring_sqe *sqe = io_uring_get_sqe (&fs->io_uring); | ||
| 132 | // ASSERT (sqe != NULL); | ||
| 133 | |||
| 134 | // // Prepare a read | ||
| 135 | // io_uring_prep_read ( | ||
| 136 | // sqe, fs->input_fd, fs->buffer + bytes_submitted, read_size, bytes_submitted); | ||
| 137 | // io_uring_sqe_set_data (sqe, bytes_submitted); | ||
| 138 | // bytes_submitted += read_size; | ||
| 139 | // fs->input_bytes_to_submit -= read_size; | ||
| 140 | // if (fs->input_bytes_to_submit == 0) | ||
| 141 | // break; | ||
| 142 | // if (fs->input_bytes_to_submit < read_size) | ||
| 143 | // read_size = fs->input_bytes_to_submit; // For last read | ||
| 144 | // else if (read_size < RING_SIZE && bytes_submitted != read_size) | ||
| 145 | // read_size *= 2; // Ramp-up | ||
| 146 | // } | ||
| 147 | |||
| 148 | // // Submit SQEs to the kernel | ||
| 149 | // ret = io_uring_submit (&fs->io_uring); | ||
| 150 | // ASSERT (ret == 0); | ||
| 151 | // } | ||
| 152 | |||
| 153 | void | ||
| 154 | file_scanner__deinit (struct file_scanner *fs) | ||
| 155 | { | ||
| 156 | ASSERT (fs != NULL); | ||
| 157 | |||
| 158 | io_uring_queue_exit (&fs->io_uring); | ||
| 159 | |||
| 160 | munmap (fs->buffer + RING_SIZE*READ_SIZE, RING_SIZE*READ_SIZE); | ||
| 161 | munmap (fs->buffer, RING_SIZE*READ_SIZE); | ||
| 162 | |||
| 163 | close (fs->memfd); | ||
| 164 | } | ||
| 165 | |||
| 166 | static void | ||
| 167 | mark_scanned (struct file_scanner *fs, size_t offset) | ||
| 168 | { | ||
| 169 | if (offset == 0) return; | ||
| 170 | |||
| 171 | ASSERT (fs != NULL); | ||
| 172 | ASSERT (fs->input_scanned + offset <= fs->input_submitted); | ||
| 173 | |||
| 174 | int buffers_to_read = (fs->input_scanned + offset)/READ_SIZE - | ||
| 175 | (fs->input_scanned )/READ_SIZE; | ||
| 176 | |||
| 177 | fs->input_scanned += offset; | ||
| 178 | |||
| 179 | // printf ("Reading %d buffers (scanned %lu)\n", buffers_to_read, offset); | ||
| 180 | if (!buffers_to_read) return; | ||
| 181 | for (int i = 0; i < buffers_to_read; ++i) { | ||
| 182 | // Getting a submission queue entry (SQE) | ||
| 183 | struct io_uring_sqe *sqe = io_uring_get_sqe (&fs->io_uring); | ||
| 184 | ASSERT (sqe != NULL); | ||
| 185 | |||
| 186 | // Prepare a read | ||
| 187 | size_t read_size = MIN (READ_SIZE, fs->input_size - fs->input_submitted); | ||
| 188 | io_uring_prep_read_fixed (sqe, 0, &fs->buffer[fs->input_submitted % | ||
| 189 | (RING_SIZE*READ_SIZE)], | ||
| 190 | read_size, fs->input_submitted, | ||
| 191 | fs->input_submitted/READ_SIZE % RING_SIZE); | ||
| 192 | sqe->flags |= IOSQE_FIXED_FILE; | ||
| 193 | fs->input_submitted += read_size; | ||
| 194 | io_uring_sqe_set_data64 (sqe, fs->input_submitted); | ||
| 195 | } | ||
| 196 | |||
| 197 | // submit SQEs to the kernel | ||
| 198 | int ret = io_uring_submit (&fs->io_uring); | ||
| 199 | ASSERT_MSG (ret == buffers_to_read, "io_uring_submit: %s", strerror (-ret)); | ||
| 200 | } | ||
| 201 | |||
| 202 | void | ||
| 203 | wrap_scanner (struct file_scanner *fs) | ||
| 204 | { | ||
| 205 | if (fs->scanner.cursor - fs->buffer < RING_SIZE*READ_SIZE) | ||
| 206 | return; | ||
| 207 | |||
| 208 | fs->scanner.end = fs->scanner.cursor; | ||
| 209 | fs->scanner.cursor -= RING_SIZE*READ_SIZE; | ||
| 210 | } | ||
| 211 | |||
| 212 | void | ||
| 213 | file_scanner__skip_until (struct file_scanner *fs, char c) | ||
| 214 | { | ||
| 215 | ASSERT (fs != NULL); | ||
| 216 | |||
| 217 | mark_scanned (fs, fs->to_mark_as_scanned); | ||
| 218 | fs->to_mark_as_scanned = 0; | ||
| 219 | |||
| 220 | do { | ||
| 221 | wrap_scanner (fs); | ||
| 222 | const char *begin = fs->scanner.cursor; | ||
| 223 | scanner__skip_until (&fs->scanner, c); | ||
| 224 | // printf ("%s %p %p %p\n", __func__, fs->buffer, fs->scanner.cursor, fs->scanner.end); | ||
| 225 | mark_scanned (fs, fs->scanner.cursor - begin); | ||
| 226 | } while ( scanner__reached_end (&fs->scanner) && | ||
| 227 | !file_scanner__reached_end (fs)); | ||
| 228 | } | ||
| 229 | |||
| 230 | struct string_view | ||
| 231 | file_scanner__get_until (struct file_scanner *fs, char c) | ||
| 232 | { | ||
| 233 | ASSERT (fs != NULL); | ||
| 234 | |||
| 235 | mark_scanned (fs, fs->to_mark_as_scanned); | ||
| 236 | fs->to_mark_as_scanned = 0; | ||
| 237 | wrap_scanner (fs); | ||
| 238 | |||
| 239 | struct string_view sv = { .begin = fs->scanner.cursor }; | ||
| 240 | do { | ||
| 241 | scanner__skip_until (&fs->scanner, c); | ||
| 242 | // printf ("%s %p %p %p\n", __func__, fs->buffer, fs->scanner.cursor, fs->scanner.end); | ||
| 243 | } while ( scanner__reached_end (&fs->scanner) && | ||
| 244 | !file_scanner__reached_end (fs)); | ||
| 245 | |||
| 246 | sv.end = fs->scanner.cursor; | ||
| 247 | fs->to_mark_as_scanned = sv.end - sv.begin; | ||
| 248 | |||
| 249 | return sv; | ||
| 250 | } | ||
| 251 | |||
| 252 | // TODO(cmmm): Could potentially be used to skip reads, has to be | ||
| 253 | // implemented differently | ||
| 254 | void | ||
| 255 | file_scanner__skip_n (struct file_scanner *fs, size_t n) | ||
| 256 | { | ||
| 257 | ASSERT (fs != NULL); | ||
| 258 | |||
| 259 | mark_scanned (fs, fs->to_mark_as_scanned); | ||
| 260 | fs->to_mark_as_scanned = 0; | ||
| 261 | |||
| 262 | do { | ||
| 263 | wrap_scanner (fs); | ||
| 264 | const char *begin = fs->scanner.cursor; | ||
| 265 | scanner__skip_n (&fs->scanner, n); | ||
| 266 | // printf ("%s %p %p %p\n", __func__, fs->buffer, fs->scanner.cursor, fs->scanner.end); | ||
| 267 | size_t skipped = fs->scanner.cursor - begin; | ||
| 268 | n -= skipped; | ||
| 269 | mark_scanned (fs, skipped); | ||
| 270 | if (n == 0) break; | ||
| 271 | } while ( scanner__reached_end (&fs->scanner) && | ||
| 272 | !file_scanner__reached_end (fs)); | ||
| 273 | } | ||
| 274 | |||
| 275 | bool | ||
| 276 | file_scanner__reached_end (struct file_scanner *fs) | ||
| 277 | { | ||
| 278 | ASSERT (fs != NULL); | ||
| 279 | |||
| 280 | mark_scanned (fs, fs->to_mark_as_scanned); | ||
| 281 | |||
| 282 | if (!scanner__reached_end (&fs->scanner)) return false; | ||
| 283 | if (fs->input_scanned == fs->input_size) return true; | ||
| 284 | |||
| 285 | // Wait for completion | ||
| 286 | struct io_uring_cqe *cqe; | ||
| 287 | int ret = io_uring_wait_cqe (&fs->io_uring, &cqe); | ||
| 288 | ASSERT (ret == 0); | ||
| 289 | ASSERT (cqe->res != -1); | ||
| 290 | ASSERT (cqe->res != 0); | ||
| 291 | |||
| 292 | int res = cqe->res; | ||
| 293 | size_t end = io_uring_cqe_get_data64 (cqe); | ||
| 294 | io_uring_cqe_seen (&fs->io_uring, cqe); | ||
| 295 | |||
| 296 | // printf ("res: %d, end: %lu\n", res, end); | ||
| 297 | fs->scanner.end = fs->buffer + end % (RING_SIZE*READ_SIZE) + (RING_SIZE*READ_SIZE); | ||
| 298 | // printf ("%s %p %p %p\n", __func__, fs->buffer, fs->scanner.cursor, fs->scanner.end); | ||
| 299 | |||
| 300 | return false; | ||
| 301 | } | ||
