Skip to content

Qwen3.8-Flash-Next on any consumer hardware: one-click install for Windows / Linux. Strata inference engine, OpenAI/Anthropic API on localhost, optional image input.

C++

feat: per-NUMA-slice byte ranges of a complement slot

Baptiste Rajaut31d857f8175c
diff --git a/include/strata/core/expert_source.hpp b/include/strata/core/expert_source.hpp
index fe8bf184c..fb63a1cbc 100644
--- a/include/strata/core/expert_source.hpp
+++ b/include/strata/core/expert_source.hpp
@@ -28,6 +28,7 @@
#include "strata/kernels/cpu/pool.hpp"
#include <algorithm>
+#include <array>
#include <cmath>
#include <atomic>
#include <chrono>
@@ -115,6 +116,14 @@ uint64_t meminfo_bytes(const std::string& meminfo_text, const std::string& key);
/// SMALLEST node's free), i.e. what an even spread would find. Only lowers the figure, so the pages are then taken in
/// steps (which reclaim cache as they fault) instead of in one driver call; it never refuses and never caps.
uint64_t numa_pin_free(uint64_t global_free, const std::vector<uint64_t>& node_free);
+/// Byte ranges [first, second) of slice `slice` of 2 inside one complement slot at arena offset `slot_off`, whose roles
+/// (gate, up, down) are `role_bytes[0..2]` long and back to back. Role r's slice-0 bytes are [start_r, start_r + role_r/2),
+/// its slice-1 bytes the rest. Page rule (absolute arena offsets): any page holding a slice-0 byte belongs to slice 0,
+/// so slice-0 ranges round OUTWARDS (start down, end up) and slice-1 ranges round INWARDS (start up, end down) - this
+/// also settles pages shared across role and slot boundaries. Returns three ranges in arena offsets, possibly empty
+/// (first == second).
+std::array<std::pair<uint64_t, uint64_t>, 3> numa_slice_ranges(uint64_t slot_off, const uint64_t role_bytes[3],
+ int slice, uint64_t page = 4096);
/// The sentence appended to a "not enough RAM" start failure: how much of the RAM is file cache (Cached + Buffers of
/// `meminfo_text`, mapped pages included) and what to do about it; "" when under 1 GiB of cache (nothing to say).
std::string page_cache_hint(const std::string& meminfo_text);
diff --git a/src/core/expert_source.cpp b/src/core/expert_source.cpp
index ff62f0ec3..892ecd69a 100644
--- a/src/core/expert_source.cpp
+++ b/src/core/expert_source.cpp
@@ -126,6 +126,23 @@ uint64_t numa_pin_free(uint64_t global_free, const std::vector<uint64_t>& node_f
return std::min<uint64_t>(global_free, smallest * (uint64_t) node_free.size());
}
+std::array<std::pair<uint64_t, uint64_t>, 3> numa_slice_ranges(uint64_t slot_off, const uint64_t role_bytes[3],
+ int slice, uint64_t page) {
+ std::array<std::pair<uint64_t, uint64_t>, 3> out{};
+ uint64_t start = slot_off;
+ for (int r = 0; r < 3; ++r) {
+ const uint64_t mid = start + role_bytes[r] / 2, end = start + role_bytes[r];
+ if (slice == 0) {
+ out[r] = {start / page * page, (mid + page - 1) / page * page};
+ } else {
+ const uint64_t lo = (mid + page - 1) / page * page, hi = end / page * page;
+ out[r] = hi > lo ? std::make_pair(lo, hi) : std::make_pair(lo, lo);
+ }
+ start = end;
+ }
+ return out;
+}
+
std::string page_cache_hint(const std::string& meminfo_text) {
const uint64_t cache = meminfo_bytes(meminfo_text, "Cached") + meminfo_bytes(meminfo_text, "Buffers");
if (cache < (1ull << 30)) return "";
diff --git a/tests/core/file_expert_source_test.cpp b/tests/core/file_expert_source_test.cpp
index 83ced92e3..e570e5285 100644
--- a/tests/core/file_expert_source_test.cpp
+++ b/tests/core/file_expert_source_test.cpp
@@ -399,6 +399,37 @@ void test_pin_pacing() {
require(page_cache_hint("MemFree: 100 kB\nCached: 1000 kB\n").empty(), "a small cache was reported");
}
+// NUMA split: per-slice byte ranges of one complement slot (page rule: a page holding a slice-0 byte is slice 0's)
+void test_numa_slice_ranges() {
+ using strata::core::detail::numa_slice_ranges;
+ constexpr uint64_t P = 4096;
+ const uint64_t roles[3] = {870400, 870400, 921600};
+ const uint64_t slot_bytes = roles[0] + roles[1] + roles[2];
+ const uint64_t offs[3] = {0, 2662400ull * 7, P * 3 + 100};
+ for (uint64_t off : offs) {
+ const auto r0 = numa_slice_ranges(off, roles, 0), r1 = numa_slice_ranges(off, roles, 1);
+ for (int r = 0; r < 3; ++r) {
+ require(r0[r].first % P == 0 && r0[r].second % P == 0 && r1[r].first % P == 0 && r1[r].second % P == 0,
+ "a slice range boundary was not page aligned");
+ require(r0[r].first <= r0[r].second && r1[r].first <= r1[r].second, "a slice range was inverted");
+ }
+ // every page fully inside the slot is in exactly one of the six ranges (and none is in two)
+ for (uint64_t pg = (off + P - 1) / P * P; pg + P <= off + slot_bytes; pg += P) {
+ int hits = 0;
+ for (int r = 0; r < 3; ++r) {
+ hits += (pg >= r0[r].first && pg + P <= r0[r].second);
+ hits += (pg >= r1[r].first && pg + P <= r1[r].second);
+ }
+ require(hits == 1, "a page was in " + std::to_string(hits) + " slice ranges, not exactly one");
+ }
+ }
+ const auto a = numa_slice_ranges(0, roles, 0), b = numa_slice_ranges(0, roles, 1);
+ require(a[0].first <= 435199 && 435199 < a[0].second, "last slice-0 gate byte not in slice 0's gate range");
+ require(b[0].first <= 435200 + P && 435200 + P < b[0].second, "a slice-1 gate byte not in slice 1's gate range");
+ require(a[1].first <= 870400 && 870400 < a[1].second, "the gate/up boundary page was not slice 0's up range");
+ require(!(b[0].first <= 870400 && 870400 < b[0].second), "the gate/up boundary page was in slice 1's gate range");
+}
+
void test_resident_memory_budget() {
using strata::core::detail::clamp_resident_budget;
constexpr uint64_t GiB = 1ull << 30, margin = 256ull << 20, headroom = 4 * GiB;
@@ -947,6 +978,7 @@ int main(int argc, char** argv) {
test_resident_exchange();
test_resident_memory_budget();
test_pin_pacing();
+ test_numa_slice_ranges();
test_cgroup_memory_budget();
test_host_memory();
test_canonical_layout();