diff --git a/cmake/backends/cpu.cmake b/cmake/backends/cpu.cmake index 92c8e75a74..f6313e39d5 100644 --- a/cmake/backends/cpu.cmake +++ b/cmake/backends/cpu.cmake @@ -462,6 +462,19 @@ if(STRATA_BUILD_TESTS) target_include_directories(cgroup_cpu_test PRIVATE include) target_compile_features(cgroup_cpu_test PRIVATE cxx_std_17) add_test(NAME cgroup_cpu_test COMMAND cgroup_cpu_test) + # when the engine restricts itself to the graphics card's NUMA node (mock machines, a fake sysfs tree, a two-thread restriction) + add_executable(card_node_test src/kernels/cpu/card_node_test.cpp) + target_include_directories(card_node_test PRIVATE include) + target_compile_features(card_node_test PRIVATE cxx_std_17) + find_package(Threads REQUIRED) + target_link_libraries(card_node_test PRIVATE Threads::Threads) + add_test(NAME card_node_test COMMAND card_node_test) + # the CPU set the background copy threads widen to (the process's own, not every CPU number) + add_executable(home_cpus_test src/core/home_cpus_test.cpp) + target_include_directories(home_cpus_test PRIVATE include) + target_compile_features(home_cpus_test PRIVATE cxx_std_17) + target_link_libraries(home_cpus_test PRIVATE Threads::Threads) + add_test(NAME home_cpus_test COMMAND home_cpus_test) # #3204: when the HIP start-up warm-up of hipBLAS may run (pure policy, no GPU) add_executable(warm_policy_test src/prefill/warm_policy_test.cpp) target_include_directories(warm_policy_test PRIVATE include) diff --git a/include/strata/kernels/cpu/card_node.hpp b/include/strata/kernels/cpu/card_node.hpp new file mode 100644 index 0000000000..8e05692a74 --- /dev/null +++ b/include/strata/kernels/cpu/card_node.hpp @@ -0,0 +1,366 @@ +// include/strata/kernels/cpu/card_node.hpp - run the engine on the graphics card's NUMA node (Linux, CUDA and HIP). +// +// On a machine with two memory nodes the card sits on one of them. Left alone, the scheduler put a start's threads on +// either node, and the CPU expert pool spread over both. On a two-socket RTX 5090 host, decode with CPU experts read +// 1.11-1.18 of the two-node default over three rounds with a 32-worker pool on one node (UD 4-bit set). So at start-up, +// before any thread exists and before the first large allocation, the engine restricts itself to the card's node when +// that is safe, which is what `taskset -c ` did by hand. +// +// Pure decision first (decide_card_node, no system calls, so a test can feed it a made-up machine), then the readers of +// sysfs and /proc, each with a root directory a test can point at a fake tree. The caller (generate.cpp) gathers the +// inputs, asks, and applies the answer with restrict_process_cpus(). STRATA_NUMA_CARD=0 (or off / false / no) is the +// way back: the engine then never restricts itself. In every case the decision does not take, nothing changes. +#pragma once + +#include "strata/kernels/cpu/pool_plan.hpp" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#if defined(__linux__) +#include +#include +#include +#include +#endif + +namespace strata::kernels::cpu { + +/// One logical CPU the process may run on: its NUMA node, CPU package and core (-1: not known). +struct CardNodeCpu { + int cpu = -1; + int node = -1; + long pkg = -1; + long core = -1; +}; + +/// Pinned host memory the engine allocates once the threads sit on the card's node, as far as it is known before the +/// model is opened: the host copy of the K/V cache (KV streaming) and the staging buffers. The K/V copy of this model +/// geometry is about 12,700 bytes per context cell (measured: 1.55 GiB at 131,072 cells on the RTX 4090 pod), counted +/// at the full --max-context even when part of it stays in VRAM; the staging buffers and the other pinned scratch are +/// small (well under 1 GiB) and get a fixed 2 GiB. The expert arena is NOT counted: this rule does not place it (it +/// lands where first touch and the kernel's fallback put it; on the pod it stayed on node 0 with every thread on node 1). +inline constexpr uint64_t kCardNodeKvBytesPerCell = 12700; +inline constexpr uint64_t kCardNodeStagingBytes = 2ull << 30; +inline uint64_t card_node_pinned_need(int64_t max_context) { + return (max_context > 0 ? (uint64_t) max_context : 0) * kCardNodeKvBytesPerCell + kCardNodeStagingBytes; +} + +struct CardNodeInput { + bool switch_off = false; ///< STRATA_NUMA_CARD=0 / off / false / no + std::string user_placed; ///< non-empty: the person placed the threads (--pool-cores, STRATA_NUMA_SPLIT, an --aux-cpus list): why + int nodes_with_cpus = 1; ///< NUMA nodes of the machine that have CPUs + std::vector mask; ///< the CPUs the process may run on now + bool mems_known = false; ///< mems_allowed was read (/proc/self/status Mems_allowed_list) + std::vector mems_allowed; ///< the memory nodes the process may allocate from + std::vector card_nodes; ///< the node of each card the run uses (-1: not known) + int64_t card_node_free_bytes = -1; ///< MemFree of the card's node (-1: not known) + uint64_t pinned_need_bytes = 0; ///< card_node_pinned_need(--max-context) + double cpu_quota = 0; ///< CPUs the container's quota allows (0: none) + bool hybrid = false; ///< a CPU with performance and efficiency cores + int today_workers = 0; ///< the pool's worker count without this rule (the default, or --pool-workers) + int user_workers = 0; ///< --pool-workers (0: the default) +}; + +struct CardNodeDecision { + bool on = false; ///< restrict the process to `cpus` + int node = -1; ///< the card's node (when known) + std::vector cpus; ///< the process's CPUs on that node, ascending + int node_cores = 0; ///< physical cores among `cpus` + int node_workers = 0; ///< the pool's default worker count on `cpus` alone + std::string why; ///< why the process is left as it is ("" when restricted) +}; + +/// The process's CPUs on one node (ascending), and the pool's plan input for them. +inline PoolPlanInput card_node_plan_input(const std::vector& cpus, double quota) { + PoolPlanInput in; + in.logical = (int) cpus.size(); + in.cpu_quota = quota; + std::map> cores; // package -> its cores + bool known = !cpus.empty(); + for (const CardNodeCpu& c : cpus) { + if (c.pkg < 0 || c.core < 0) { known = false; break; } + cores[c.pkg].insert(c.core); + } + if (known) { + // the host thread's package first, as pool_plan_input orders it: the package of the lowest CPU number + const auto lowest = std::min_element(cpus.begin(), cpus.end(), + [](const CardNodeCpu& a, const CardNodeCpu& b) { return a.cpu < b.cpu; }); + const long first = lowest->pkg; + in.cores_per_package.push_back((int) cores[first].size()); + for (const auto& kv : cores) + if (kv.first != first) in.cores_per_package.push_back((int) kv.second.size()); + } + return in; +} + +/// Restrict the process to the card's node? Yes only when all of these hold; otherwise `why` names the first that does not: +/// the switch is not off; the machine has two or more nodes with CPUs; the process's CPUs span more than one node (a +/// taskset / numactl / cpuset restriction to one node is left as it is); every CPU's node is known; the run uses at least +/// one card and every card reports the same, non-negative node; the process may allocate memory on that node; the node +/// has the pinned memory the engine allocates next (card_node_pinned_need); the person did not place the threads +/// themselves; the CPU is not hybrid; the node has CPUs of the process; and the pool keeps its size there: the pool's own +/// plan on the node's CPUs alone gives at least the worker count it would have without the rule (with --pool-workers N: +/// the node has N + 1 physical cores, one per worker and the host's). The worker count must not drop: measured on an +/// RTX 5090 host, 32 workers on one node (either node, within 1-3 %) beat 32 spread over two nodes by 14-22 %, but 16 +/// on one node only matched 32 spread and 8 on one node lost (0.84-0.93). A container quota bounds both counts alike, so it never stops +/// the rule and the pool keeps the quota's count. +inline CardNodeDecision decide_card_node(const CardNodeInput& in) { + CardNodeDecision d; + if (in.switch_off) { d.why = "STRATA_NUMA_CARD=0"; return d; } + if (in.nodes_with_cpus < 2) { d.why = "one NUMA node"; return d; } + std::set mask_nodes; + for (const CardNodeCpu& c : in.mask) { + if (c.node < 0) { d.why = "CPU " + std::to_string(c.cpu) + " has no known NUMA node"; return d; } + mask_nodes.insert(c.node); + } + if (mask_nodes.size() < 2) { + d.why = in.mask.empty() ? "the process's CPUs are not known" : "the process already runs on one NUMA node"; + return d; + } + if (in.card_nodes.empty()) { d.why = "no card"; return d; } + const int node = in.card_nodes.front(); + for (int n : in.card_nodes) { + if (n < 0) { d.why = "a card's NUMA node is not known"; return d; } + if (n != node) { d.why = "the cards sit on different NUMA nodes"; return d; } + } + d.node = node; + if (in.mems_known && std::find(in.mems_allowed.begin(), in.mems_allowed.end(), node) == in.mems_allowed.end()) { + d.why = "the process may not allocate memory on node " + std::to_string(node); + return d; + } + if (in.card_node_free_bytes < 0) { d.why = "node " + std::to_string(node) + "'s free memory is not known"; return d; } + if ((uint64_t) in.card_node_free_bytes < in.pinned_need_bytes) { + d.why = "node " + std::to_string(node) + " has " + std::to_string(in.card_node_free_bytes >> 20) + " MiB free, under the " + + std::to_string(in.pinned_need_bytes >> 20) + " MiB of pinned memory the engine allocates next"; + return d; + } + if (!in.user_placed.empty()) { d.why = in.user_placed; return d; } + if (in.hybrid) { d.why = "a hybrid CPU"; return d; } + std::vector on; + for (const CardNodeCpu& c : in.mask) + if (c.node == node) on.push_back(c); + std::sort(on.begin(), on.end(), [](const CardNodeCpu& a, const CardNodeCpu& b) { return a.cpu < b.cpu; }); + if (on.empty()) { d.why = "the process has no CPU on node " + std::to_string(node); return d; } + const PoolPlan plan = plan_pool_workers(card_node_plan_input(on, in.cpu_quota)); + d.node_cores = plan.cores; + d.node_workers = plan.workers; + for (const CardNodeCpu& c : on) d.cpus.push_back(c.cpu); + if (in.user_workers > 0) { + if (in.user_workers + 1 > plan.cores) { + d.why = "node " + std::to_string(node) + " has " + std::to_string(plan.cores) + " physical cores, too few for " + + std::to_string(in.user_workers) + " workers and the host"; + d.cpus.clear(); + return d; + } + } else if (plan.workers < in.today_workers) { + d.why = "node " + std::to_string(node) + " fits " + std::to_string(plan.workers) + " pool workers, under the " + + std::to_string(in.today_workers) + " the pool uses now"; + d.cpus.clear(); + return d; + } + d.on = true; + return d; +} + +// ---- readers (Linux sysfs and /proc; a root directory so a test can use a fake tree) -------------------------------------- + +/// "0-31,64-95\n" -> {0..31, 64..95}. Whitespace around the text is ignored; false (and empty) for anything else. +inline bool card_node_parse_cpulist(std::string text, std::vector& out) { + out.clear(); + while (!text.empty() && std::isspace((unsigned char) text.back())) text.pop_back(); + size_t i = 0; + while (i < text.size() && std::isspace((unsigned char) text[i])) ++i; + if (i == text.size()) return true; // an empty list (a node without CPUs) + auto number = [&](long& v) { + if (i >= text.size() || !std::isdigit((unsigned char) text[i])) return false; + v = 0; + while (i < text.size() && std::isdigit((unsigned char) text[i])) { + v = v * 10 + (text[i] - '0'); + if (v > 1000000) return false; + ++i; + } + return true; + }; + for (;;) { + long lo = 0, hi = 0; + if (!number(lo)) { out.clear(); return false; } + hi = lo; + if (i < text.size() && text[i] == '-') { + ++i; + if (!number(hi) || hi < lo) { out.clear(); return false; } + } + for (long c = lo; c <= hi; ++c) out.push_back((int) c); + if (i == text.size()) break; + if (text[i] != ',') { out.clear(); return false; } + ++i; + } + std::sort(out.begin(), out.end()); + out.erase(std::unique(out.begin(), out.end()), out.end()); + return true; +} + +inline bool card_node_read_file(const std::string& path, std::string& text) { + std::ifstream f(path); + if (!f) return false; + std::ostringstream s; + s << f.rdbuf(); + text = s.str(); + return true; +} + +/// node -> its CPUs, from /devices/system/node/node/cpulist (nodes without CPUs are left out). +inline std::map> card_node_node_cpus(const std::string& sys_root) { + std::map> nodes; +#if defined(__linux__) + const std::string dir = sys_root + "/devices/system/node"; + if (DIR* dd = opendir(dir.c_str())) { + while (const dirent* e = readdir(dd)) { + const std::string name = e->d_name; + if (name.size() < 5 || name.compare(0, 4, "node") != 0) continue; + if (!std::all_of(name.begin() + 4, name.end(), [](unsigned char c) { return std::isdigit(c) != 0; })) continue; + std::string text; + std::vector cpus; + if (!card_node_read_file(dir + "/" + name + "/cpulist", text) || !card_node_parse_cpulist(text, cpus) || cpus.empty()) + continue; + nodes[std::atoi(name.c_str() + 4)] = cpus; + } + closedir(dd); + } +#else + (void) sys_root; +#endif + return nodes; +} + +/// The node, package and core of each CPU in `cpus` (node from `nodes`, the others from /devices/system/cpu/cpu/topology). +inline std::vector card_node_describe(const std::string& sys_root, const std::vector& cpus, + const std::map>& nodes) { + auto read_id = [&](int cpu, const char* what) -> long { + std::string text; + if (!card_node_read_file(sys_root + "/devices/system/cpu/cpu" + std::to_string(cpu) + "/topology/" + what, text)) return -1; + char* end = nullptr; + const long v = std::strtol(text.c_str(), &end, 10); + return end != text.c_str() && v >= 0 ? v : -1; + }; + std::vector out; + for (int c : cpus) { + CardNodeCpu x; + x.cpu = c; + for (const auto& kv : nodes) + if (std::binary_search(kv.second.begin(), kv.second.end(), c)) { x.node = kv.first; break; } + x.pkg = read_id(c, "physical_package_id"); + x.core = read_id(c, "core_id"); + out.push_back(x); + } + return out; +} + +/// A card's PCI address in sysfs's spelling ("0000:e1:00.0") from the runtime's domain / bus / device numbers. +inline std::string card_node_bdf(unsigned domain, unsigned bus, unsigned device, unsigned function = 0) { + char s[32]; + std::snprintf(s, sizeof s, "%04x:%02x:%02x.%x", domain & 0xffffu, bus & 0xffu, device & 0x1fu, function & 7u); + return s; +} + +/// /bus/pci/devices//numa_node: the card's node, -1 when the file is missing, unreadable or says -1. +inline int card_node_pci_node(const std::string& sys_root, const std::string& bdf) { + std::string text; + if (!card_node_read_file(sys_root + "/bus/pci/devices/" + bdf + "/numa_node", text)) return -1; + char* end = nullptr; + const long v = std::strtol(text.c_str(), &end, 10); + return end != text.c_str() && v >= 0 && v < 4096 ? (int) v : -1; +} + +/// "Node 1 MemFree: 112233 kB" of /devices/system/node/node/meminfo, in bytes; -1 when not readable. +inline int64_t card_node_free_bytes(const std::string& sys_root, int node) { + std::string text; + if (node < 0 || !card_node_read_file(sys_root + "/devices/system/node/node" + std::to_string(node) + "/meminfo", text)) return -1; + std::istringstream in(text); + std::string line; + while (std::getline(in, line)) { + const size_t at = line.find("MemFree:"); + if (at == std::string::npos) continue; + char* end = nullptr; + const char* p = line.c_str() + at + 8; + const long long kb = std::strtoll(p, &end, 10); + if (end == p || kb < 0) return -1; + return (int64_t) kb * 1024; + } + return -1; +} + +/// The Mems_allowed_list line of /proc/self/status text ("Mems_allowed_list:\t0-1"). False when it is not there. +inline bool card_node_mems_allowed(const std::string& status_text, std::vector& mems) { + std::istringstream in(status_text); + std::string line; + while (std::getline(in, line)) { + if (line.compare(0, 18, "Mems_allowed_list:") != 0) continue; + return card_node_parse_cpulist(line.substr(18), mems); + } + mems.clear(); + return false; +} + +/// The switch: STRATA_NUMA_CARD set to 0 / off / false / no (any case). +inline bool card_node_switch_off(const char* v) { + if (v == nullptr) return false; + std::string s(v); + std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c) { return (char) std::tolower(c); }); + return s == "0" || s == "off" || s == "false" || s == "no"; +} + +#if defined(__linux__) +/// Restricts every thread of the process that exists now to its own CPUs among `cpus` (the calling thread first; a +/// thread with none of them keeps its mask), as `taskset` before the start would have: threads created later inherit +/// the restriction from the thread that creates them. Returns how many threads were changed; `failed` counts refusals. +inline int restrict_process_cpus(const std::vector& cpus, int& failed) { + failed = 0; + auto apply = [&](int tid) -> int { + cpu_set_t now, to; + CPU_ZERO(&now); + CPU_ZERO(&to); + if (sched_getaffinity(tid, sizeof now, &now) != 0) { ++failed; return 0; } + int n = 0; + for (int c : cpus) + if (c >= 0 && c < CPU_SETSIZE && CPU_ISSET(c, &now)) { CPU_SET(c, &to); ++n; } + if (n == 0 || CPU_EQUAL(&now, &to)) return 0; + if (sched_setaffinity(tid, sizeof to, &to) != 0) { ++failed; return 0; } + return 1; + }; + const int self = (int) syscall(SYS_gettid); + int changed = apply(self); + if (DIR* d = opendir("/proc/self/task")) { + while (const dirent* e = readdir(d)) { + if (e->d_name[0] < '0' || e->d_name[0] > '9') continue; + const int tid = std::atoi(e->d_name); + if (tid != self) changed += apply(tid); + } + closedir(d); + } + return changed; +} + +/// The CPUs the calling thread may run on now (ascending). +inline std::vector card_node_thread_cpus() { + std::vector out; + cpu_set_t now; + CPU_ZERO(&now); + if (sched_getaffinity(0, sizeof now, &now) == 0) + for (int c = 0; c < CPU_SETSIZE; ++c) + if (CPU_ISSET(c, &now)) out.push_back(c); + return out; +} +#endif + +} // namespace strata::kernels::cpu diff --git a/include/strata/platform/home_cpus.hpp b/include/strata/platform/home_cpus.hpp new file mode 100644 index 0000000000..442d763e58 --- /dev/null +++ b/include/strata/platform/home_cpus.hpp @@ -0,0 +1,66 @@ +// strata/platform/home_cpus.hpp - the widest CPU set a thread of the engine may take. +// +// A few threads first widen their own CPU mask before they work: the background copy threads (STRATA_CKPT_ASYNC, +// STRATA_PARK_ASYNC) and the checkpoint helpers start on the host's single CPU, which they inherited, and must not take +// turns with the decode there. They used to widen to every CPU number; the kernel then keeps the CPUs the cgroup allows, +// so a thread left a CPU set the person gave the process (taskset, numactl) or the one the engine gave itself on the +// card's NUMA node (strata/kernels/cpu/card_node.hpp). widen_to_home() widens to the CPUs the process had when it set +// its placement at start-up (set_home_cpus, called once on the main thread before any of these threads exists); without +// that call (one NUMA node, STRATA_NUMA_CARD=0, or any start the rule leaves as it is) it widens to every CPU, as before. +// +// Linux only (the other systems get an empty function). Header-only: the callers sit in libraries that do not link each other. +#pragma once + +#include + +#if defined(__linux__) +#include +#endif + +namespace strata::platform { + +#if defined(__linux__) +namespace home_cpus_detail { +struct Home { + bool set = false; + cpu_set_t mask; +}; +inline Home& home() { + static Home h; + return h; +} +} // namespace home_cpus_detail + +/// The CPUs widen_to_home() gives a thread from now on. Call once at start-up, before the threads that widen exist. +/// An empty list (or none of them below CPU_SETSIZE) changes nothing. +inline void set_home_cpus(const std::vector& cpus) { + cpu_set_t m; + CPU_ZERO(&m); + int n = 0; + for (int c : cpus) + if (c >= 0 && c < CPU_SETSIZE) { CPU_SET(c, &m); ++n; } + if (n == 0) return; + home_cpus_detail::home().mask = m; + home_cpus_detail::home().set = true; +} + +/// The mask widen_to_home() uses: the home CPUs, or every CPU number when none were set. +inline cpu_set_t home_mask() { + if (home_cpus_detail::home().set) return home_cpus_detail::home().mask; + cpu_set_t all; + CPU_ZERO(&all); + for (int c = 0; c < CPU_SETSIZE; ++c) CPU_SET(c, &all); + return all; +} + +/// Widens the calling thread to the home CPUs (see the top of this file). The kernel keeps the CPUs the cgroup allows. +inline void widen_to_home() { + const cpu_set_t m = home_mask(); + (void) sched_setaffinity(0, sizeof m, &m); +} +#else +inline void set_home_cpus(const std::vector&) {} +inline void widen_to_home() {} +#endif + +} // namespace strata::platform diff --git a/src/core/conversation_copy.hpp b/src/core/conversation_copy.hpp index 81f303e3c3..1e19feaa67 100644 --- a/src/core/conversation_copy.hpp +++ b/src/core/conversation_copy.hpp @@ -3,6 +3,7 @@ #include "strata/core/conversation_cache.hpp" #include "strata/core/progress.hpp" #include "strata/platform/aux_cpus.hpp" +#include "strata/platform/home_cpus.hpp" #include @@ -124,13 +125,12 @@ inline long test_copy_fail_at() { /// First thing a background copy thread does (STRATA_CKPT_ASYNC, STRATA_PARK_ASYNC): a thread the host creates /// inherits the host's one CPU (strata/platform/aux_cpus.hpp), where its memcpys would take turns with the decode. /// Wide first: the mask the aux placement keeps as this thread's own is the one restore() puts back (a request with -/// aux_cpus=0), and the host's one CPU must not be it. No output depends on where a thread runs. +/// aux_cpus=0), and the host's one CPU must not be it. Wide means the process's own CPU set from the start-up +/// (strata/platform/home_cpus.hpp: every CPU unless the engine placed itself on the card's NUMA node). No output +/// depends on where a thread runs. inline void background_thread_placement() { #if defined(__linux__) - cpu_set_t all; - CPU_ZERO(&all); - for (int c = 0; c < CPU_SETSIZE; ++c) CPU_SET(c, &all); - (void) sched_setaffinity(0, sizeof all, &all); // the kernel keeps the CPUs the process may use + strata::platform::widen_to_home(); // the kernel keeps the CPUs the cgroup allows strata::aux_cpus::pin_current_thread(); // (a no-op while the feature is off) #endif } diff --git a/src/core/home_cpus_test.cpp b/src/core/home_cpus_test.cpp new file mode 100644 index 0000000000..efb106f25c --- /dev/null +++ b/src/core/home_cpus_test.cpp @@ -0,0 +1,94 @@ +// home_cpus_test: the widest CPU set a thread of the engine takes (include/strata/platform/home_cpus.hpp). Without a home +// set a thread that widens gets every CPU the process may use (as before); with one it stays inside it, also when it +// started on a single CPU (the host's, inherited). No GPU. +#include "strata/platform/home_cpus.hpp" + +#include +#include +#include +#include + +static int g_fail = 0; + +static void expect(bool ok, const std::string& what) { + if (!ok) { + ++g_fail; + std::fprintf(stderr, "FAIL: %s\n", what.c_str()); + } +} + +#if defined(__linux__) +static std::vector mine() { + std::vector out; + cpu_set_t m; + CPU_ZERO(&m); + if (sched_getaffinity(0, sizeof m, &m) == 0) + for (int c = 0; c < CPU_SETSIZE; ++c) + if (CPU_ISSET(c, &m)) out.push_back(c); + return out; +} + +static void pin_one(int cpu) { + cpu_set_t m; + CPU_ZERO(&m); + CPU_SET(cpu, &m); + (void) sched_setaffinity(0, sizeof m, &m); +} +#endif + +int main() { +#if defined(__linux__) + const std::vector start = mine(); + if (start.size() < 2) { + std::printf("home_cpus_test: skipped (this process may run on %zu CPU)\n", start.size()); + return 0; + } + { // no home: the old behaviour, every CPU the process may use + const cpu_set_t h = strata::platform::home_mask(); + expect(CPU_COUNT(&h) == CPU_SETSIZE, "no home: the mask is every CPU number"); + std::vector got; + std::thread t([&] { + pin_one(start.front()); + strata::platform::widen_to_home(); + got = mine(); + }); + t.join(); + expect(got == start, "no home: a pinned thread widens back to all the process's CPUs"); + } + const std::vector half(start.begin(), start.begin() + (long) (start.size() / 2)); + strata::platform::set_home_cpus({}); // empty: nothing changes + { + const cpu_set_t h = strata::platform::home_mask(); + expect(CPU_COUNT(&h) == CPU_SETSIZE, "empty home list: still every CPU number"); + } + strata::platform::set_home_cpus(half); + { + std::vector got; + std::thread t([&] { + pin_one(start.front()); + strata::platform::widen_to_home(); + got = mine(); + }); + t.join(); + expect(got == half, "home set: a pinned thread widens to the home CPUs only"); + std::vector got2; + std::thread u([&] { // a thread that runs on every CPU of the process widens DOWN to the home set too + strata::platform::widen_to_home(); + got2 = mine(); + }); + u.join(); + expect(got2 == half, "home set: a wide thread ends on the home CPUs"); + } + expect(mine() == start, "the calling thread is not moved by set_home_cpus"); +#else + strata::platform::set_home_cpus({0}); + strata::platform::widen_to_home(); + std::printf("home_cpus_test: not Linux, nothing to check\n"); +#endif + if (g_fail != 0) { + std::fprintf(stderr, "home_cpus_test: %d check(s) FAILED\n", g_fail); + return 1; + } + std::printf("home_cpus_test: all checks passed\n"); + return 0; +} diff --git a/src/kernels/cpu/card_node_test.cpp b/src/kernels/cpu/card_node_test.cpp new file mode 100644 index 0000000000..e3ed992606 --- /dev/null +++ b/src/kernels/cpu/card_node_test.cpp @@ -0,0 +1,339 @@ +// card_node_test: when the engine restricts itself to the graphics card's NUMA node (include/strata/kernels/cpu/card_node.hpp). +// The decision on made-up machines, the sysfs / proc readers on a fake tree in a temp directory, and (Linux) the restriction +// of a two-thread process to a subset of its own CPUs. No GPU, no model. +#include "strata/kernels/cpu/card_node.hpp" + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace cpu = strata::kernels::cpu; +namespace fs = std::filesystem; + +static int g_fail = 0; + +static void expect(bool ok, const std::string& what) { + if (!ok) { + ++g_fail; + std::fprintf(stderr, "FAIL: %s\n", what.c_str()); + } +} + +// A two-socket machine, one node per socket, 2-way SMT: node k has cores [0, cores_k), CPU numbers as Linux gives them +// (node 0's first threads, node 1's first threads, then node 0's siblings, node 1's siblings). +static std::vector machine(int cores0, int cores1) { + std::vector m; + int n = 0; + for (int pass = 0; pass < 2; ++pass) { + for (int c = 0; c < cores0; ++c) m.push_back({n++, 0, 0, c}); + for (int c = 0; c < cores1; ++c) m.push_back({n++, 1, 1, c}); + } + return m; +} + +static std::vector cpus_of(const std::vector& m, int node) { + std::vector out; + for (const auto& c : m) + if (c.node == node) out.push_back(c.cpu); + return out; +} + +// The RTX 4090 pod's shape: 2 x 32 cores, 128 logical, a 13.6-CPU quota (12 workers), card on node 1, plenty of free memory. +static cpu::CardNodeInput pod() { + cpu::CardNodeInput in; + in.nodes_with_cpus = 2; + in.mask = machine(32, 32); + in.mems_known = true; + in.mems_allowed = {0, 1}; + in.card_nodes = {1}; + in.card_node_free_bytes = 100ll << 30; + in.pinned_need_bytes = cpu::card_node_pinned_need(131072); + in.cpu_quota = 13.6; + in.today_workers = 12; + return in; +} + +static void decisions() { + // two nodes, the card on node 1: restricted to node 1's 64 logical CPUs, the pool keeps the quota's 12 workers + { + const auto d = cpu::decide_card_node(pod()); + expect(d.on && d.why.empty(), "pod: restricted (" + d.why + ")"); + expect(d.node == 1, "pod: node 1"); + expect(d.cpus == cpus_of(machine(32, 32), 1) && d.cpus.size() == 64, "pod: node 1's 64 CPUs"); + expect(d.node_cores == 32 && d.node_workers == 12, "pod: 32 cores, 12 workers on the node (quota)"); + } + // the card on node 0 + { + auto in = pod(); + in.card_nodes = {0}; + const auto d = cpu::decide_card_node(in); + expect(d.on && d.node == 0 && d.cpus == cpus_of(in.mask, 0), "card on node 0: node 0's CPUs"); + } + // one node: nothing + { + auto in = pod(); + in.nodes_with_cpus = 1; + const auto d = cpu::decide_card_node(in); + expect(!d.on && d.why == "one NUMA node" && d.cpus.empty(), "one node: left as is"); + } + // the switch off + { + auto in = pod(); + in.switch_off = true; + const auto d = cpu::decide_card_node(in); + expect(!d.on && d.why == "STRATA_NUMA_CARD=0", "switch off: left as is"); + } + // the card's node not known (sysfs -1 or unreadable) + { + auto in = pod(); + in.card_nodes = {-1}; + expect(!cpu::decide_card_node(in).on, "card node -1: left as is"); + in.card_nodes = {}; + expect(!cpu::decide_card_node(in).on, "no card: left as is"); + } + // two cards on different nodes; two on the same node + { + auto in = pod(); + in.card_nodes = {1, 0}; + const auto d = cpu::decide_card_node(in); + expect(!d.on && d.why == "the cards sit on different NUMA nodes", "cards on nodes 1 and 0: left as is"); + in.card_nodes = {1, 1}; + expect(cpu::decide_card_node(in).on, "two cards on node 1: restricted"); + in.card_nodes = {1, -1}; + expect(!cpu::decide_card_node(in).on, "one card's node unknown: left as is"); + } + // a process already restricted to one node (taskset / numactl / cpuset): left as is, also when it is the other node + { + auto in = pod(); + std::vector one; + for (const auto& c : in.mask) + if (c.node == 0) one.push_back(c); + in.mask = one; + const auto d = cpu::decide_card_node(in); + expect(!d.on && d.why == "the process already runs on one NUMA node", "pre-restricted to node 0: left as is"); + std::vector part; // a subset that still spans both nodes (a cpuset over both): the rule applies + for (const auto& c : pod().mask) + if (c.cpu % 4 == 0) part.push_back(c); + in.mask = part; + in.today_workers = 7; // that mask's own pool: 16 cores in all, the host's socket's 8 -> 7 workers + const auto e = cpu::decide_card_node(in); + expect(e.on && e.cpus.size() == 16 && e.node_workers == 7, "a mask over both nodes, 16 of node 1's CPUs: restricted to those 16"); + in.today_workers = 12; + expect(!cpu::decide_card_node(in).on, "the same mask with 12 workers today: 7 on the node, left as is"); + } + // a CPU whose node is not known + { + auto in = pod(); + in.mask[5].node = -1; + expect(!cpu::decide_card_node(in).on, "a CPU without a node: left as is"); + } + // a node with too few CPUs for the pool: 4 + 60 cores, no quota, today's pool spans both sockets: 32 workers + { + cpu::CardNodeInput in; + in.nodes_with_cpus = 2; + in.mask = machine(4, 60); + in.card_nodes = {0}; + in.card_node_free_bytes = 100ll << 30; + in.today_workers = 32; + const auto d = cpu::decide_card_node(in); + expect(!d.on && d.node_workers == 3 && d.cpus.empty(), "card on a 4-core node, 32 workers today: left as is (" + d.why + ")"); + in.card_nodes = {1}; + const auto e = cpu::decide_card_node(in); + expect(e.on && e.node_workers == 32, "card on the 60-core node: restricted, 32 workers"); + // a 16-core node against 32 spread: 16 pinned only matched 32 spread in the measurement, so the count must not drop + in.mask = machine(16, 16); + in.card_nodes = {1}; + in.today_workers = 30; + expect(!cpu::decide_card_node(in).on, "2 x 16 cores, 30 workers today, 15 on a node: left as is"); + } + // --pool-workers N: the node needs N + 1 physical cores + { + auto in = pod(); + in.user_workers = in.today_workers = 31; + expect(cpu::decide_card_node(in).on, "--pool-workers 31 on a 32-core node: restricted"); + in.user_workers = in.today_workers = 32; + expect(!cpu::decide_card_node(in).on, "--pool-workers 32 on a 32-core node: left as is"); + in.user_workers = in.today_workers = 4; + expect(cpu::decide_card_node(in).on, "--pool-workers 4: restricted"); + } + // a container quota smaller than the node: the pool is bounded by the quota either way and the rule does not move it + { + cpu::CardNodeInput in; + in.nodes_with_cpus = 2; + in.mask = machine(96, 96); // the second RTX 5090 host: 2 x EPYC 9654, card on node 0, a 46-CPU quota + in.card_nodes = {0}; + in.card_node_free_bytes = 100ll << 30; + in.cpu_quota = 46; + in.today_workers = 32; + const auto d = cpu::decide_card_node(in); + expect(d.on && d.node_workers == 32 && d.cpus.size() == 192, "quota 46 on 2 x 96 cores: restricted, 32 workers"); + in.cpu_quota = 17.55; // the first RTX 5090 host's quota: 16 workers either way + in.today_workers = 16; + const auto e = cpu::decide_card_node(in); + expect(e.on && e.node_workers == 16, "quota 17.55: restricted, 16 workers"); + // a quota larger than one node (2 x 8 cores, 30 CPUs): today 15 workers, a node 7: left as is + in.mask = machine(8, 8); + in.cpu_quota = 30; + in.today_workers = 15; + expect(!cpu::decide_card_node(in).on, "quota above one node's CPUs: left as is"); + } + // memory: the process may not allocate on the card's node, or the node is short of free memory + { + auto in = pod(); + in.mems_allowed = {0}; + const auto d = cpu::decide_card_node(in); + expect(!d.on && d.why == "the process may not allocate memory on node 1", "mems_allowed without the card's node: left as is"); + in.mems_known = false; // not readable: not a reason to stop + expect(cpu::decide_card_node(in).on, "mems_allowed not known: restricted"); + in = pod(); + in.card_node_free_bytes = (int64_t) (2.6 * 1073741824.0); // the 2.6 GB free seen on one node of the first 5090 host + const auto e = cpu::decide_card_node(in); + expect(!e.on && e.why.find("MiB free, under the") != std::string::npos, "2.6 GiB free on the card's node: left as is (" + e.why + ")"); + in.card_node_free_bytes = -1; + expect(!cpu::decide_card_node(in).on, "free memory not known: left as is"); + in.card_node_free_bytes = (int64_t) in.pinned_need_bytes; + expect(cpu::decide_card_node(in).on, "free memory exactly the need: restricted"); + expect(cpu::card_node_pinned_need(131072) == 131072ull * 12700 + (2ull << 30), "pinned need at 131,072 cells"); + expect(cpu::card_node_pinned_need(0) == (2ull << 30) && cpu::card_node_pinned_need(-5) == (2ull << 30), "pinned need without a context"); + } + // the person placed the threads; a hybrid CPU; the process has no CPU on the card's node + { + auto in = pod(); + in.user_placed = "--pool-cores is set"; + const auto d = cpu::decide_card_node(in); + expect(!d.on && d.why == "--pool-cores is set", "--pool-cores: left as is"); + in = pod(); + in.hybrid = true; + expect(!cpu::decide_card_node(in).on, "hybrid: left as is"); + in = pod(); + in.nodes_with_cpus = 3; + in.card_nodes = {2}; + in.mems_known = false; + const auto e = cpu::decide_card_node(in); + expect(!e.on && e.why == "the process has no CPU on node 2", "card on a node the process has no CPU on: left as is"); + } +} + +static void write(const fs::path& p, const std::string& text) { + fs::create_directories(p.parent_path()); + std::ofstream(p) << text; +} + +static void readers() { + const fs::path root = fs::temp_directory_path() / ("card_node_test_" + std::to_string((long long) std::time(nullptr))); + fs::remove_all(root); + const fs::path node = root / "devices/system/node"; + write(node / "node0/cpulist", "0-1,4-5\n"); + write(node / "node1/cpulist", "2-3,6-7\n"); + write(node / "node2/cpulist", "\n"); // a memory-only node (CXL): not counted + write(node / "online", "0-2\n"); + fs::create_directories(node / "nodeX"); + write(node / "node1/meminfo", "Node 1 MemTotal: 264000000 kB\nNode 1 MemFree: 112233 kB\nNode 1 MemUsed: 1 kB\n"); + write(node / "node0/meminfo", "Node 0 MemTotal: 5 kB\n"); // no MemFree line + for (int c = 0; c < 8; ++c) { + const fs::path t = root / ("devices/system/cpu/cpu" + std::to_string(c)) / "topology"; + write(t / "physical_package_id", std::to_string((c / 2) % 2) + "\n"); + write(t / "core_id", std::to_string(c % 2) + "\n"); + } + write(root / "bus/pci/devices/0000:e1:00.0/numa_node", "1\n"); + write(root / "bus/pci/devices/0000:21:00.0/numa_node", "-1\n"); + write(root / "bus/pci/devices/0000:22:00.0/numa_node", "junk\n"); + +#if defined(__linux__) + const auto nodes = cpu::card_node_node_cpus(root.string()); + expect(nodes.size() == 2, "fake sysfs: 2 nodes with CPUs (memory-only node2 and nodeX left out)"); + expect(nodes.count(0) && nodes.at(0) == std::vector({0, 1, 4, 5}), "fake sysfs: node 0's CPUs"); + expect(nodes.count(1) && nodes.at(1) == std::vector({2, 3, 6, 7}), "fake sysfs: node 1's CPUs"); + const auto desc = cpu::card_node_describe(root.string(), {0, 3, 6, 9}, nodes); + expect(desc.size() == 4 && desc[0].node == 0 && desc[1].node == 1 && desc[2].node == 1 && desc[3].node == -1, "describe: nodes"); + expect(desc[1].pkg == 1 && desc[1].core == 1 && desc[3].pkg == -1 && desc[3].core == -1, "describe: package and core, missing CPU -1"); + expect(cpu::card_node_node_cpus((root / "nothing").string()).empty(), "no node folder: no nodes"); +#endif + expect(cpu::card_node_pci_node(root.string(), "0000:e1:00.0") == 1, "pci numa_node 1"); + expect(cpu::card_node_pci_node(root.string(), "0000:21:00.0") == -1, "pci numa_node -1"); + expect(cpu::card_node_pci_node(root.string(), "0000:22:00.0") == -1, "pci numa_node junk: -1"); + expect(cpu::card_node_pci_node(root.string(), "0000:99:00.0") == -1, "pci device missing: -1"); + expect(cpu::card_node_free_bytes(root.string(), 1) == 112233ll * 1024, "node 1 MemFree"); + expect(cpu::card_node_free_bytes(root.string(), 0) == -1, "node 0 without MemFree: -1"); + expect(cpu::card_node_free_bytes(root.string(), 5) == -1, "node 5 missing: -1"); + expect(cpu::card_node_free_bytes(root.string(), -1) == -1, "node -1: -1"); + expect(cpu::card_node_bdf(0, 0xE1, 0) == "0000:e1:00.0", "bdf 0000:e1:00.0"); + expect(cpu::card_node_bdf(0x10, 0x3b, 0x1f, 1) == "0010:3b:1f.1", "bdf 0010:3b:1f.1"); + fs::remove_all(root); + + std::vector v; + expect(cpu::card_node_parse_cpulist("0-31,64-95\n", v) && v.size() == 64 && v.front() == 0 && v.back() == 95, "cpulist 0-31,64-95"); + expect(cpu::card_node_parse_cpulist(" 3 ", v) && v == std::vector({3}), "cpulist 3"); + expect(cpu::card_node_parse_cpulist("\n", v) && v.empty(), "cpulist empty"); + expect(!cpu::card_node_parse_cpulist("3-1", v) && v.empty(), "cpulist reversed: refused"); + expect(!cpu::card_node_parse_cpulist("1,,2", v), "cpulist empty item: refused"); + expect(!cpu::card_node_parse_cpulist("a", v), "cpulist letters: refused"); + std::vector mems; + expect(cpu::card_node_mems_allowed("Name:\tstrata\nMems_allowed:\t00000003\nMems_allowed_list:\t0-1\nVoluntary: 1\n", mems) && + mems == std::vector({0, 1}), "Mems_allowed_list 0-1"); + expect(cpu::card_node_mems_allowed("Mems_allowed_list:\t1\n", mems) && mems == std::vector({1}), "Mems_allowed_list 1"); + expect(!cpu::card_node_mems_allowed("Name:\tx\n", mems) && mems.empty(), "no Mems_allowed_list line"); + for (const char* off : {"0", "off", "OFF", "false", "no"}) expect(cpu::card_node_switch_off(off), std::string("switch off: ") + off); + for (const char* on : {"1", "", "on", "yes"}) expect(!cpu::card_node_switch_off(on), std::string("switch not off: '") + on + "'"); + expect(!cpu::card_node_switch_off(nullptr), "switch unset: not off"); +} + +#if defined(__linux__) +// restrict_process_cpus on this process: a second thread exists before the call and must be restricted too +static void restriction() { + const std::vector mine = cpu::card_node_thread_cpus(); + if (mine.size() < 2) { + std::printf("card_node_test: restriction skipped (this process may run on %zu CPU)\n", mine.size()); + return; + } + std::atomic stop{false}; + std::atomic other_tid{0}; + std::vector other_after; + std::thread t([&] { + other_tid = (int) syscall(SYS_gettid); + while (!stop.load()) std::this_thread::sleep_for(std::chrono::milliseconds(2)); + other_after = cpu::card_node_thread_cpus(); + }); + while (other_tid.load() == 0) std::this_thread::sleep_for(std::chrono::milliseconds(1)); + const std::vector half(mine.begin(), mine.begin() + (long) (mine.size() / 2)); + std::vector want = half; + want.push_back(100000); // a CPU the process does not have is ignored + int refused = -1; + const int changed = cpu::restrict_process_cpus(want, refused); + expect(changed >= 2 && refused == 0, "restriction: both threads changed (" + std::to_string(changed) + ", refused " + + std::to_string(refused) + ")"); + expect(cpu::card_node_thread_cpus() == half, "restriction: the calling thread has the subset"); + std::vector created_after; + std::thread n([&] { created_after = cpu::card_node_thread_cpus(); }); + n.join(); + expect(created_after == half, "restriction: a thread created afterwards inherits the subset"); + stop = true; + t.join(); + expect(other_after == half, "restriction: the thread that existed before has the subset"); + int refused2 = -1; + expect(cpu::restrict_process_cpus(half, refused2) == 0 && refused2 == 0, "restriction: a second call changes nothing"); + std::vector none = {100000}; + expect(cpu::restrict_process_cpus(none, refused2) == 0 && cpu::card_node_thread_cpus() == half, + "restriction: no common CPU keeps the mask"); +} +#endif + +int main() { + decisions(); + readers(); +#if defined(__linux__) + restriction(); +#endif + if (g_fail != 0) { + std::fprintf(stderr, "card_node_test: %d check(s) FAILED\n", g_fail); + return 1; + } + std::printf("card_node_test: all checks passed\n"); + return 0; +} diff --git a/src/program/generate.cpp b/src/program/generate.cpp index 5a49eec75f..47312c40fb 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -63,6 +63,9 @@ #include "strata/kernels/gfx_arch.hpp" #include "strata/kernels/cpu/expert.hpp" #include "strata/kernels/cpu/pool.hpp" +#include "strata/kernels/cpu/card_node.hpp" +#include "strata/kernels/cpu/numa_pool.hpp" +#include "strata/platform/home_cpus.hpp" #include "strata/platform/aux_cpus.hpp" #include "strata/kernels/cpu/expert_layout.hpp" #include "strata/kernels/cpu/hadamard.hpp" @@ -2495,11 +2498,9 @@ public: static void spread_current_thread() { #if defined(__linux__) // wide first: the mask the aux placement keeps as this thread's own is the one restore() puts back (a request - // with aux_cpus=0), and the host's one CPU must not be it - cpu_set_t all; - CPU_ZERO(&all); - for (int c = 0; c < CPU_SETSIZE; ++c) CPU_SET(c, &all); - (void) sched_setaffinity(0, sizeof all, &all); // the kernel keeps the CPUs the process may use + // with aux_cpus=0), and the host's one CPU must not be it; wide = the process's CPU set from the start-up + // (strata/platform/home_cpus.hpp) + strata::platform::widen_to_home(); // the kernel keeps the CPUs the cgroup allows strata::aux_cpus::pin_current_thread(); // (a no-op while the feature is off) #endif } @@ -4171,6 +4172,87 @@ int main(int argc, char** argv) { } #endif strata::core::set_coupled_draft(o.coupled_draft); +#if defined(__linux__) && !defined(STRATA_GPU_METAL) + { // THE CARD'S NUMA NODE (strata/kernels/cpu/card_node.hpp): on a machine with two or more memory nodes the process + // restricts itself to the CPUs of the card's node, here, before the pool, the host thread and the stagers exist + // and before the first large allocation, so every thread and the pinned memory they touch first follow. One node, + // STRATA_NUMA_CARD=0, or any case decide_card_node leaves as it is: nothing changes. + namespace kc = strata::kernels::cpu; + kc::CardNodeInput ci; + ci.switch_off = kc::card_node_switch_off(std::getenv("STRATA_NUMA_CARD")); + const std::map> node_cpus = + ci.switch_off ? std::map>{} : kc::card_node_node_cpus("/sys"); + ci.nodes_with_cpus = ci.switch_off ? 1 : (std::max)(1, (int) node_cpus.size()); + std::string cards_text; + if (!ci.switch_off && ci.nodes_with_cpus >= 2) { + ci.mask = kc::card_node_describe("/sys", kc::card_node_thread_cpus(), node_cpus); + std::string status; + if (kc::card_node_read_file("/proc/self/status", status)) ci.mems_known = kc::card_node_mems_allowed(status, ci.mems_allowed); + // the cards the run uses: the primary one; with a layer split, helper caches or a peer card, every visible one + const char* pv = std::getenv("STRATA_PRIMARY_DEVICE"); + const int primary = pv != nullptr && std::atoi(pv) > 0 ? std::atoi(pv) : 0; + bool several = !o.layer_split.empty() || o.peer_device >= 1; + for (int r = 0; r < 3; ++r) several = several || o.expert_cache_remote[(size_t) r] > 0 || o.expert_cache_remote_auto[(size_t) r]; + int n_dev = 0; + if (cudaGetDeviceCount(&n_dev) != cudaSuccess) n_dev = 0; + (void) cudaGetLastError(); + for (int d = several ? 0 : primary; d < n_dev && (several || d == primary); ++d) { + cudaDeviceProp p{}; + int node = -1; + std::string bdf = "?"; + if (cudaGetDeviceProperties(&p, d) == cudaSuccess) { + bdf = kc::card_node_bdf((unsigned) p.pciDomainID, (unsigned) p.pciBusID, (unsigned) p.pciDeviceID); + node = kc::card_node_pci_node("/sys", bdf); + } else { + (void) cudaGetLastError(); + } + ci.card_nodes.push_back(node); + cards_text += (cards_text.empty() ? "" : ", ") + std::string("CUDA") + std::to_string(d) + " " + bdf + " node " + std::to_string(node); + } + if (ci.card_nodes.empty() && primary >= n_dev) ci.card_nodes.push_back(-1); // the primary is not visible: unknown + if (!ci.card_nodes.empty()) ci.card_node_free_bytes = kc::card_node_free_bytes("/sys", ci.card_nodes.front()); + ci.pinned_need_bytes = kc::card_node_pinned_need(o.max_context); + // the person placed the threads: their placement stays + std::string pc = o.pool_cores, ac = o.aux_cpus; + if (pc.empty()) + if (const char* e = std::getenv("STRATA_POOL_CORES")) pc = e; + if (ac.empty()) + if (const char* e = std::getenv("STRATA_AUX_CPUS")) ac = e; + const char* split_env = std::getenv("STRATA_NUMA_SPLIT"); + if (!pc.empty()) ci.user_placed = "--pool-cores is set"; + else if (split_env != nullptr && *split_env != '\0') ci.user_placed = "STRATA_NUMA_SPLIT is set"; + else if (!ac.empty() && ac != "auto" && ac != "off") ci.user_placed = "an --aux-cpus list is set"; + // the pool's worker count without the rule, as ExpertPool picks it (the span plan when the per-node split may run) + const kc::CpuTopology topo = kc::detect_cpu_topology(true, o.pool_affinity); + ci.hybrid = topo.is_hybrid; + ci.cpu_quota = topo.cpu_quota; + ci.user_workers = o.pool_workers > 0 ? o.pool_workers : 0; + kc::PoolPlanInput pin = kc::pool_plan_input(topo); + pin.span_packages = kc::pick_numa_pool(std::getenv("STRATA_NUMA_POOL"), split_env, ci.nodes_with_cpus).choice == + kc::NumaPoolChoice::Auto; + ci.today_workers = ci.user_workers > 0 ? ci.user_workers + : (std::min)(kc::plan_pool_workers(pin).workers, + (std::max)(1, (int) topo.worker_cores.size())); + } + const kc::CardNodeDecision cd = kc::decide_card_node(ci); + int moved = 0, refused = 0; + if (cd.on) moved = kc::restrict_process_cpus(cd.cpus, refused); + // the threads that widen their mask later (background copies, checkpoint helpers) widen to the process's CPU set + // as it is now: the card's node, or the set the process was started with (a taskset is kept); the switch off: + // every CPU, as before + if (!ci.switch_off) strata::platform::set_home_cpus(kc::card_node_thread_cpus()); + if (std::getenv("STRATA_TRACE") != nullptr) { + std::string cpus; + for (size_t i = 0; i < cd.cpus.size(); ++i) cpus += (i ? "," : "") + std::to_string(cd.cpus[i]); + std::fprintf(stderr, "strata trace: card node: %d nodes with CPUs; cards: %s; card node free %lld MiB, pinned need %llu MiB; " + "pool workers now %d, on the card's node %d (%d cores); %s%s (%d threads changed, %d refused)\n", + ci.nodes_with_cpus, cards_text.empty() ? "not read" : cards_text.c_str(), + (long long) (ci.card_node_free_bytes < 0 ? -1 : ci.card_node_free_bytes >> 20), + (unsigned long long) (ci.pinned_need_bytes >> 20), ci.today_workers, cd.node_workers, cd.node_cores, + cd.on ? "restricted to CPUs " : "left as is: ", cd.on ? cpus.c_str() : cd.why.c_str(), moved, refused); + } + } +#endif { // --host-core / STRATA_HOST_CORE, before the pool and the session pin any thread std::string hc = o.host_core; if (hc.empty())