diff --git a/include/strata/core/expert_source.hpp b/include/strata/core/expert_source.hpp index c6dcf2af..e67c81c8 100644 --- a/include/strata/core/expert_source.hpp +++ b/include/strata/core/expert_source.hpp @@ -615,6 +615,7 @@ public: struct IoCounters { uint64_t cached_bytes = 0, uncached_bytes = 0; ///< file reads at hand-out, by page-cache residency uint64_t pread_bytes = 0, pread_us = 0, pread_n = 0; ///< bytes read with pread, thread time, calls + uint64_t pf_demand = 0; ///< blobs the layer asked for that the workers were handed uint64_t pf_jobs = 0, pf_read_blobs = 0, pf_resident_skips = 0, pf_dropped = 0; ///< prefetch workers uint64_t pf_used = 0, pf_unused = 0; ///< prefetched blobs a layer then used / never used uint64_t crit_us = 0; ///< wall time the decode waited on file reads (prefetch + staged_blob) @@ -685,6 +686,7 @@ private: std::atomic file_read_bytes_{0}; // ---- the Linux I/O path (set_io_prefetch) bool io_pf_ = false; + bool io_fill_ = false; ///< experts.bin: the workers only fill the page cache (large preads), no staging, nothing waits bool io_ahead_ = true; ///< STRATA_IO_PF_AHEAD=0: no read-ahead workers' queue, demand preads only bool io_stats_ = false; int io_threads_ = 4; @@ -696,13 +698,14 @@ private: std::vector stage_pf_; ///< per stage buffer: filled ahead, not yet used by a layer std::vector io_fds_; ///< per mapped file: a plain descriptor (POSIX_FADV_RANDOM) for the preads mutable std::atomic io_cached_{0}, io_uncached_{0}, io_pread_bytes_{0}, io_pread_us_{0}, io_pread_n_{0}; - std::atomic io_pf_jobs_{0}, io_pf_blobs_{0}, io_pf_skips_{0}, io_pf_dropped_{0}, io_pf_used_{0}, io_pf_unused_{0}, + std::atomic io_pf_jobs_{0}, io_pf_demand_{0}, io_pf_blobs_{0}, io_pf_skips_{0}, io_pf_dropped_{0}, io_pf_used_{0}, io_pf_unused_{0}, io_crit_us_{0}, io_crit_n_{0}; struct Span { const uint8_t* p; uint64_t n; int fd; uint64_t off; uint64_t at; }; /// The (up to 3) file ranges of a blob: pointer in the mapping, size, descriptor and file offset, offset in the blob. int spans(int64_t layer, int64_t expert, Span* out) const; /// Bytes of the blob's pages in the page cache (mincore); `total` the blob's bytes. - uint64_t cached_bytes(int64_t layer, int64_t expert, uint64_t& total) const; + /// `exact` false: four sampled pages (a blob is cached when they are; a handful of syscalls, not hundreds of pages' walk). + uint64_t cached_bytes(int64_t layer, int64_t expert, uint64_t& total, bool exact = true) const; void io_worker(); void io_enqueue(int64_t layer, int64_t expert); void io_stop(); diff --git a/src/core/expert_source.cpp b/src/core/expert_source.cpp index ce84c022..7ce9b791 100644 --- a/src/core/expert_source.cpp +++ b/src/core/expert_source.cpp @@ -429,6 +429,19 @@ uint64_t resident_in(const uint8_t* p, uint64_t n) { for (unsigned char c : vec) pages += c & 1; return std::min(n, pages * 4096); } +/// A cheap residency probe: the first, last and two inner pages; the bytes of the range when all four are cached, else 0. +uint64_t resident_sampled(const uint8_t* p, uint64_t n) { + if (n == 0) return 0; + const uintptr_t a = (uintptr_t) p & ~(uintptr_t) 4095, e = ((uintptr_t) p + (uintptr_t) n - 1) & ~(uintptr_t) 4095; + const uintptr_t pages = (e - a) / 4096; + const uintptr_t at[4] = {a, a + (pages / 3) * 4096, a + (pages * 2 / 3) * 4096, e}; + for (uintptr_t x : at) { + unsigned char v = 0; + if (mincore((void*) x, 4096, &v) != 0 || (v & 1) == 0) return 0; + } + return n; +} +bool resident_sampled_n(const uint8_t* p, uint64_t n) { return resident_sampled(p, n) >= n; } /// The whole range with pread (a short read continues); false on an error or an end of file. bool pread_full(int fd, uint8_t* dst, uint64_t n, uint64_t off) { uint64_t done = 0; @@ -463,7 +476,7 @@ int FileExpertSource::spans(int64_t layer, int64_t expert, Span* out) const { return 1; } -uint64_t FileExpertSource::cached_bytes(int64_t layer, int64_t expert, uint64_t& total) const { +uint64_t FileExpertSource::cached_bytes(int64_t layer, int64_t expert, uint64_t& total, bool exact) const { total = 0; #if defined(__linux__) Span sp[3]; @@ -471,7 +484,7 @@ uint64_t FileExpertSource::cached_bytes(int64_t layer, int64_t expert, uint64_t& uint64_t res = 0; for (int i = 0; i < ns; ++i) { total += sp[i].n; - res += resident_in(sp[i].p, sp[i].n); + res += exact ? resident_in(sp[i].p, sp[i].n) : resident_sampled(sp[i].p, sp[i].n); } return res; #else @@ -488,6 +501,7 @@ FileExpertSource::IoCounters FileExpertSource::io_counters() const { c.pread_us = io_pread_us_.load(std::memory_order_relaxed); c.pread_n = io_pread_n_.load(std::memory_order_relaxed); c.pf_jobs = io_pf_jobs_.load(std::memory_order_relaxed); + c.pf_demand = io_pf_demand_.load(std::memory_order_relaxed); c.pf_read_blobs = io_pf_blobs_.load(std::memory_order_relaxed); c.pf_resident_skips = io_pf_skips_.load(std::memory_order_relaxed); c.pf_dropped = io_pf_dropped_.load(std::memory_order_relaxed); @@ -519,8 +533,12 @@ void FileExpertSource::set_io_prefetch(bool on, int threads) { for (uint64_t b : layer_blob_bytes_) stage_blob_ = std::max(stage_blob_, b); } if (!warm_stamp_) warm_stamp_.reset(new std::atomic[(size_t) (n_layers_ * n_expert_)]()); - io_threads_ = threads > 0 ? threads : 4; + io_threads_ = threads > 0 ? threads : 8; if (const char* v = std::getenv("STRATA_IO_PF_AHEAD")) io_ahead_ = std::atoi(v) != 0; + { // experts.bin: fill the page cache from the workers and let the kernels read the mapping (STRATA_IO_PF_STAGE=1: stage) + const char* v = std::getenv("STRATA_IO_PF_STAGE"); + io_fill_ = role_ptr_.empty() && !(v != nullptr && std::atoi(v) != 0); + } io_pf_ = true; io_stats_ = true; io_quit_ = false; @@ -577,8 +595,31 @@ void FileExpertSource::io_worker() { const size_t index = (size_t) layer * (size_t) n_expert_ + (size_t) expert; if (complement_ready_ && resident_blob(index) != nullptr) continue; if (!override_.empty() && override_[index] != nullptr) continue; + if (io_fill_) { // read what is not cached into a scratch buffer: the pages stay in the page cache for the kernels +#if defined(__linux__) + static thread_local std::vector scratch; + Span sp[3]; + const int ns = spans(layer, expert, sp); + bool any = false; + for (int i = 0; i < ns; ++i) { + if (sp[i].fd < 0 || resident_sampled_n(sp[i].p, sp[i].n)) continue; + if (scratch.size() < sp[i].n) scratch.resize((size_t) sp[i].n); + const auto t0 = std::chrono::steady_clock::now(); + if (pread_full(sp[i].fd, scratch.data(), sp[i].n, sp[i].off)) { + io_pread_bytes_.fetch_add(sp[i].n, std::memory_order_relaxed); + io_pread_us_.fetch_add((uint64_t) std::chrono::duration(std::chrono::steady_clock::now() - t0).count(), + std::memory_order_relaxed); + io_pread_n_.fetch_add(1, std::memory_order_relaxed); + any = true; + } + } + if (any) io_pf_blobs_.fetch_add(1, std::memory_order_relaxed); + else io_pf_skips_.fetch_add(1, std::memory_order_relaxed); +#endif + continue; + } uint64_t total = 0; - if (cached_bytes(layer, expert, total) >= total) { + if (cached_bytes(layer, expert, total, false) >= total) { io_pf_skips_.fetch_add(1, std::memory_order_relaxed); continue; } @@ -614,21 +655,27 @@ void FileExpertSource::io_worker() { // pread into a staging buffer, not faulted in by the kernels one small request at a time. const uint8_t* FileExpertSource::io_blob(int64_t layer, int64_t expert) { const int64_t key = layer * n_expert_ + expert; + if (io_fill_) { + const uint8_t* b = mapped_blob(layer, expert); + if (b != nullptr) { + const uint64_t total = layer_blob_bytes_[(size_t) layer]; + file_read_bytes_.fetch_add(total, std::memory_order_relaxed); + } + return b; + } bool have; { std::lock_guard lk(stage_mu_); have = stage_of_.find(key) != stage_of_.end(); } if (!have) { - uint64_t total = 0; - const uint64_t cached = cached_bytes(layer, expert, total); - if (total > 0 && cached >= total) { - const uint8_t* b = mapped_blob(layer, expert); - if (b != nullptr) { - io_cached_.fetch_add(total, std::memory_order_relaxed); - file_read_bytes_.fetch_add(total, std::memory_order_relaxed); - return b; - } + // prefetch() (every decode layer calls it first) staged what was not cached: what is not staged is cached + const uint8_t* b = mapped_blob(layer, expert); + if (b != nullptr) { + const uint64_t total = layer_blob_bytes_[(size_t) layer]; + io_cached_.fetch_add(total, std::memory_order_relaxed); + file_read_bytes_.fetch_add(total, std::memory_order_relaxed); + return b; } } return staged_blob(layer, expert); @@ -1160,6 +1207,20 @@ const uint8_t* FileExpertSource::staged_blob(int64_t layer, int64_t expert) { void FileExpertSource::prefetch(int64_t layer, const int64_t* experts, int64_t n) { if (!staged() || n <= 0 || layer < 0 || layer >= n_layers_) return; + if (io_fill_ && io_pf_ && direct_.empty()) { // hand the layer's uncached blobs to the workers and go on: the kernels + for (int64_t i = 0; i < n; ++i) { // that reach one before it lands wait on its pages, the rest compute + const int64_t e = experts[i]; + if (e < 0 || e >= n_expert_) continue; + const size_t index = (size_t) layer * (size_t) n_expert_ + (size_t) e; + if (complement_ready_ && resident_blob(index) != nullptr) continue; + if (!override_.empty() && override_[index] != nullptr) continue; + uint64_t total = 0; + if (cached_bytes(layer, e, total, false) >= total) continue; + io_pf_demand_.fetch_add(1, std::memory_order_relaxed); + io_enqueue(layer, e); + } + return; + } std::vector todo; // experts.bin with io prefetch: a blob whose pages are cached is handed out as the mapping (io_blob); only the // uncached ones are read here, together, on the fetch threads @@ -1171,7 +1232,7 @@ void FileExpertSource::prefetch(int64_t layer, const int64_t* experts, int64_t n const int64_t e = experts[i]; if (e < 0 || e >= n_expert_) continue; uint64_t total = 0; - cold[(size_t) i] = cached_bytes(layer, e, total) < total ? 1 : 0; + cold[(size_t) i] = cached_bytes(layer, e, total, false) < total ? 1 : 0; } } const auto t0 = std::chrono::steady_clock::now(); diff --git a/src/program/generate.cpp b/src/program/generate.cpp index 9887cf92..9e9e3711 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -3753,8 +3753,8 @@ int main(int argc, char** argv) { src.set_io_prefetch(true, th != nullptr ? std::atoi(th) : 0); if (src.io_prefetch()) std::fprintf(stderr, "strata generate: file tier io prefetch ON (STRATA_IO_PREFETCH=1): whole-blob preads, " - "predicted experts read ahead on %s I/O threads (STRATA_IO_PF_THREADS, default 4)\n", - th != nullptr ? th : "4"); + "the layer's uncached experts and the predicted ones read by %s I/O threads (STRATA_IO_PF_THREADS, default 8)\n", + th != nullptr ? th : "8"); else std::fprintf(stderr, "strata generate: STRATA_IO_PREFETCH=1 is not available here (Linux mapped file tier " "only, not with unbuffered reads); the file tier reads as before\n");