Linux: read ahead of the mapped loads at start

On Linux every load phase read its files one page fault (one 128 KB readahead)
at a time, so a cold start spent most of its time waiting on the drive at
queue depth ~2. A new platform::advise_willneed asks the kernel for a range in
128 KiB steps (WILLNEED reads at most one readahead window per call), and is
called before the weight load, the native dense upload, the profile fill (256
pairs ahead, STRATA_FILL_AHEAD), the peer tier's fill (--peer-device), the
resident RAM copy and the MTP draft files. STRATA_READ_AHEAD=0 turns it off.
Each phase logs its time.

Gen3 NVMe, RTX 5090, 32 GB, Q2_0 resident at 262K: ready in 70 s, was ~920 s.
This commit is contained in:
Jesus Gulfo authored and Niko1221 committed 2026-10-05 15:58:38 +02:00
1 parent 2de94bca5e
commit cb33c0bac5
10 files changed
+137 -10

No files matched your search

+33
View File
@@ -8,6 +8,9 @@
#include <dxgi1_4.h>
#include <cstring>
#else
#include <algorithm>
#include <cstdlib>
#include <fcntl.h>
#include <sys/mman.h>
#include <unistd.h>
#endif
@@ -108,6 +111,10 @@ uint64_t total_physical_memory() {
ms.dwLength = sizeof ms;
return GlobalMemoryStatusEx(&ms) ? (uint64_t) ms.ullTotalPhys : 0;
}
bool read_ahead_enabled() { return false; }
void advise_willneed(const void*, uint64_t) {}
void advise_willneed(int, uint64_t, uint64_t) {}
#else
LockResult lock_resident(void* p, uint64_t bytes) {
LockResult r;
@@ -133,6 +140,32 @@ uint64_t total_physical_memory() {
const long pages = sysconf(_SC_PHYS_PAGES), page = sysconf(_SC_PAGE_SIZE);
return pages > 0 && page > 0 ? (uint64_t) pages * (uint64_t) page : 0;
}
namespace {
constexpr uint64_t kAdviseStep = 128ull << 10;
}
bool read_ahead_enabled() {
static const bool on = [] {
const char* v = std::getenv("STRATA_READ_AHEAD");
return v == nullptr || std::atoi(v) != 0;
}();
return on;
}
void advise_willneed(const void* p, uint64_t bytes) {
if (p == nullptr || bytes == 0 || !read_ahead_enabled()) return;
const long ps = sysconf(_SC_PAGE_SIZE);
const uintptr_t pg = ps > 0 ? (uintptr_t) ps : 4096, end = (uintptr_t) p + bytes;
for (uintptr_t a = (uintptr_t) p & ~(pg - 1); a < end; a += kAdviseStep)
(void) madvise((void*) a, (size_t) std::min<uintptr_t>(kAdviseStep, end - a), MADV_WILLNEED);
}
void advise_willneed(int fd, uint64_t offset, uint64_t bytes) {
if (fd < 0 || !read_ahead_enabled()) return;
for (uint64_t at = 0; at < bytes; at += kAdviseStep)
(void) posix_fadvise(fd, (off_t) (offset + at), (off_t) std::min(kAdviseStep, bytes - at), POSIX_FADV_WILLNEED);
}
#endif
} // namespace strata::platform