From 776276d5c7e67356a880f3e31076d32cdc1c1882 Mon Sep 17 00:00:00 2001 From: Martin Michalec Date: Tue, 10 Nov 2026 11:29:58 +0300 Subject: publish --- src/file-scanner.c | 301 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 301 insertions(+) create mode 100644 src/file-scanner.c (limited to 'src/file-scanner.c') 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 @@ +#define _GNU_SOURCE +#include +#include + +#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; +} -- cgit v1.3.1