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
//! B-tree leaf prefetch — Phase 5 / PLAN.md backlog 3.6.
//!
//! Issues OS-level read-ahead hints for the next leaf block in a
//! range scan, so the kernel can DMA the page into the buffer pool
//! while the cursor is still consuming the current one.
//!
//! Mirrors PG's `BufferPrefetchPage` via `posix_fadvise(WILLNEED)`
//! on Linux and `madvise(MADV_WILLNEED)` on macOS / BSD.
//!
//! ## Why
//!
//! reddb's range scan in `btree/cursor.rs` walks leaves
//! sequentially. The buffer-pool fetch for leaf N+1 happens only
//! after the cursor finishes leaf N, so the disk read serializes
//! with the CPU's tuple processing. Prefetch breaks that
//! dependency: as soon as the cursor lands on leaf N's halfway
//! point, we tell the kernel to start fetching N+1 in the
//! background.
//!
//! ## Wiring
//!
//! Phase 5 wiring adds a single call site in
//! `btree/cursor.rs::advance_leaf` that checks "are we past 50%
//! of the current leaf?" and if so calls `prefetch_page(next_leaf_id)`.
//! The cursor already knows `next_leaf_id` from the leaf header.
//!
//! The actual `posix_fadvise` syscall is OS-specific and behind
//! a stub on platforms that don't support it (Windows). reddb
//! ships Linux-first so the Linux path is the one this module
//! actually exercises.
use AsRawFd;
/// Errors raised by the prefetch path. Most are silent — a
/// failed prefetch is a perf miss, not a correctness bug, so
/// callers should log and continue.
/// Tell the OS to start fetching the byte range
/// `[offset, offset + length)` of `file` into the page cache.
/// Returns `Ok(())` when the syscall succeeds (no guarantee
/// the data actually arrives — that's up to the kernel).
///
/// **Linux**: invokes `posix_fadvise(fd, off, len, POSIX_FADV_WILLNEED)`.
/// **macOS / BSD**: stub returning `Unsupported`. A future commit
/// adds `fcntl(F_RDADVISE)` for Darwin.
/// **Windows**: stub returning `Unsupported`.
/// Prefetch a single page identified by `(file, page_id, page_size)`.
/// Convenience wrapper for `prefetch_range` that does the
/// `offset = page_id * page_size` math.
/// Tiny libc shim — only the one function we need, declared
/// extern so we don't pull the full `libc` crate into the dep
/// graph. Linux ABI is stable for this call.