okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-022-numa: NUMA-split CPU pool on multi-node machines (#3201)4bd591269

main
8.7 KiB146 linesraw
1// numa_pool_test: when the CPU expert pool splits per NUMA node (include/strata/kernels/cpu/numa_pool.hpp), on mock machines.
2// No threads, no OS topology: a fake sysfs node folder in a temp directory and a made-up core -> node map. It runs anywhere.
3#include "strata/kernels/cpu/numa_pool.hpp"
4
5#include <cstdio>
6#include <ctime>
7#include <filesystem>
8#include <fstream>
9#include <string>
10#include <vector>
11
12namespace cpu = strata::kernels::cpu;
13namespace fs = std::filesystem;
14
15static int g_fail = 0;
16
17static void expect(bool ok, const std::string& what) {
18    if (!ok) {
19        ++g_fail;
20        std::fprintf(stderr, "FAIL: %s\n", what.c_str());
21    }
22}
23
24// /sys/devices/system/node with `node<i>/cpulist` files; an empty string is a memory-only node
25static fs::path make_nodes(const fs::path& root, const std::vector<std::string>& cpulists, bool noise = true) {
26    fs::remove_all(root);
27    fs::create_directories(root);
28    for (size_t i = 0; i < cpulists.size(); ++i) {
29        fs::create_directories(root / ("node" + std::to_string(i)));
30        std::ofstream(root / ("node" + std::to_string(i)) / "cpulist") << cpulists[i] << "\n";
31    }
32    if (noise) {   // files and folders in that directory that are not nodes
33        std::ofstream(root / "online") << "0-1\n";
34        std::ofstream(root / "possible") << "0-1\n";
35        fs::create_directories(root / "nodes");
36        fs::create_directories(root / "node");
37        fs::create_directories(root / "nodeX");
38    }
39    return root;
40}
41
42int main() {
43    const fs::path tmp = fs::temp_directory_path() / ("strata_numa_pool_test_" + std::to_string((long long) std::time(nullptr)));
44
45    // ---- the node count of a sysfs node folder
46    expect(cpu::numa_cpu_node_count_at((tmp / "missing").string()) == 0, "no folder: 0 nodes");
47    expect(cpu::numa_cpu_node_count_at(make_nodes(tmp, {"0-23"}).string()) == 1, "one node");
48    expect(cpu::numa_cpu_node_count_at(make_nodes(tmp, {"0-11,24-35", "12-23,36-47"}).string()) == 2, "two sockets: 2 nodes");
49    expect(cpu::numa_cpu_node_count_at(make_nodes(tmp, {"0-7", "8-15", "16-23", "24-31"}).string()) == 4, "four nodes");
50    expect(cpu::numa_cpu_node_count_at(make_nodes(tmp, {"0-23", ""}).string()) == 1, "a memory-only node (CXL) is not a node of CPUs");
51    expect(cpu::numa_cpu_node_count_at(make_nodes(tmp, {"0-11", "12-23", ""}).string()) == 2, "two nodes of CPUs and a memory-only one");
52
53    // ---- pick_numa_pool: one node is today's path
54    using C = cpu::NumaPoolChoice;
55    auto pick = [](const char* pool, const char* split, int nodes) { return cpu::pick_numa_pool(pool, split, nodes); };
56    for (int nodes : {0, 1}) {
57        for (const char* pool : {(const char*) nullptr, "", "1", "garbage"}) {
58            for (const char* split : {(const char*) nullptr, ""}) {
59                const auto p = pick(pool, split, nodes);
60                expect(p.choice == C::Unchanged && p.log.empty(), "one node, nothing set: Unchanged and silent");
61            }
62        }
63        const auto zero = pick("0", nullptr, nodes);
64        expect(zero.choice == C::Unchanged && zero.log.empty(), "one node, STRATA_NUMA_POOL=0: Unchanged and silent");
65        expect(pick(nullptr, "1", nodes).choice == C::Unchanged, "one node, STRATA_NUMA_SPLIT=1: the old variable decides");
66        expect(pick(nullptr, "test", nodes).choice == C::Unchanged, "one node, STRATA_NUMA_SPLIT=test: the old variable decides");
67    }
68    {
69        const auto p = pick(nullptr, nullptr, 2);
70        expect(p.choice == C::Auto && p.log.empty(), "two nodes, nothing set: Auto");
71        expect(pick("1", nullptr, 2).choice == C::Auto, "two nodes, STRATA_NUMA_POOL=1: Auto");
72        expect(pick("", "", 2).choice == C::Auto, "two nodes, empty variables: Auto");
73        expect(pick(nullptr, nullptr, 4).choice == C::Auto, "four nodes: Auto (the decision refuses it, with a line)");
74        for (const char* off : {"0", "off", "OFF", "false", "No"}) {
75            const auto o = pick(off, nullptr, 2);
76            expect(o.choice == C::Off, std::string("two nodes, STRATA_NUMA_POOL=") + off + ": Off");
77            expect(o.log.find("STRATA_NUMA_POOL=0") != std::string::npos && o.log.find("2 NUMA nodes") != std::string::npos,
78                   "the off line names the switch and the node count");
79        }
80        expect(pick("0", "1", 2).choice == C::Off, "STRATA_NUMA_POOL=0 beats STRATA_NUMA_SPLIT=1");
81        expect(pick("0", "test", 1).choice == C::Off, "STRATA_NUMA_POOL=0 beats STRATA_NUMA_SPLIT=test, even on one node");
82        expect(pick(nullptr, "1", 2).choice == C::Unchanged, "two nodes, STRATA_NUMA_SPLIT=1: the old variable decides");
83        expect(pick(nullptr, "0", 2).choice == C::Unchanged, "two nodes, STRATA_NUMA_SPLIT=0: the old variable decides (off)");
84    }
85
86    // ---- decide_numa_split on a made-up 2 x 12-core box (host core 0; cores 0-11 node 0, 12-23 node 1)
87    auto node_of = [](int core) { return core < 0 ? -1 : core < 12 ? 0 : 1; };
88    auto cores = [](std::initializer_list<int> v) { return std::vector<int>(v); };
89    {   // 22 workers interleaved over the nodes + the host
90        std::vector<int> c;
91        for (int i = 0; i < 11; ++i) { c.push_back(1 + i); c.push_back(12 + i); }
92        c.push_back(0);   // the host
93        const auto d = cpu::decide_numa_split(c, 22, true, false, node_of);
94        expect(d.on, "22 workers + host on 2 nodes: on");
95        expect(d.nodes[0] == 0 && d.nodes[1] == 1, "the nodes are 0 and 1");
96        expect(d.per[0] == 12 && d.per[1] == 11, "12 threads on node 0 (the host counted), 11 on node 1");
97        expect(d.slice.size() == c.size() && d.slice.back() == 0, "the host drains in slice 0");
98        expect(d.slice[0] == 0 && d.slice[1] == 1, "workers alternate between the slices");
99    }
100    {   // all workers on one node: refused
101        const auto d = cpu::decide_numa_split(cores({1, 2, 3, 4, 0}), 4, true, false, node_of);
102        expect(!d.on && d.why.find("span 1 NUMA node(s)") != std::string::npos, "one node's threads only: refused with the count");
103    }
104    {   // a node with under half of the other's threads: unbalanced
105        std::vector<int> c = {1, 2, 3, 4, 5, 6, 7, 8, 9, 12, 0};
106        const auto d = cpu::decide_numa_split(c, 10, true, false, node_of);
107        expect(!d.on && d.why.find("unbalanced") != std::string::npos, "10 + 1 threads: refused as unbalanced");
108        expect(d.nodes[0] == -1 && d.slice.empty() && d.per[0] == 0, "a refused split leaves no plan behind");
109    }
110    {   // three nodes
111        auto three = [](int core) { return core < 8 ? 0 : core < 16 ? 1 : 2; };
112        const auto d = cpu::decide_numa_split(cores({1, 9, 17, 2, 10, 18, 0}), 6, true, false, three);
113        expect(!d.on && d.why.find("span 3 NUMA node(s)") != std::string::npos && d.why.find("exactly 2") != std::string::npos,
114               "three nodes: refused, exactly 2 are supported");
115    }
116    {
117        const auto base = cores({1, 12, 2, 13, 0});
118        expect(cpu::decide_numa_split(base, 4, true, false, node_of).on, "4 workers + host, balanced: on");
119        const auto rot = cpu::decide_numa_split(base, 4, true, true, node_of);
120        expect(!rot.on && rot.why.find("STRATA_EXCHANGE_ROTATE=1") != std::string::npos, "rotating slots: refused");
121        const auto nopin = cpu::decide_numa_split(base, 4, false, false, node_of);
122        expect(!nopin.on && nopin.why.find("not pinned") != std::string::npos, "unpinned threads: refused");
123        const auto nocore = cpu::decide_numa_split(cores({1, -1, 2, 13}), 4, true, false, node_of);
124        expect(!nocore.on && nocore.why.find("worker 1 has no core") != std::string::npos, "a worker without a core: refused");
125        const auto nohost = cpu::decide_numa_split(cores({1, 12, -1}), 2, true, false, node_of);
126        expect(!nohost.on && nohost.why.find("host thread has no core") != std::string::npos, "a host without a core: refused");
127        auto unknown = [](int core) { return core == 12 ? -1 : 0; };
128        const auto un = cpu::decide_numa_split(base, 4, true, false, unknown);
129        expect(!un.on && un.why.find("CPU 12 has no known NUMA node") != std::string::npos, "a core with no node: refused");
130        const auto one = cpu::decide_numa_split(cores({1}), 1, true, false, node_of);
131        expect(!one.on && one.why.find("needs 2 or more") != std::string::npos, "one thread: refused");
132    }
133    {   // a single-node machine fed to the decision (STRATA_NUMA_SPLIT=1 on one node): refused, never half-on
134        auto flat = [](int core) { return core < 0 ? -1 : 0; };
135        const auto d = cpu::decide_numa_split(cores({1, 2, 3, 4, 0}), 4, true, false, flat);
136        expect(!d.on, "every core on node 0: off");
137    }
138
139    fs::remove_all(tmp);
140    if (g_fail) {
141        std::fprintf(stderr, "numa_pool_test: %d failure(s)\n", g_fail);
142        return 1;
143    }
144    std::printf("numa_pool_test: OK\n");
145    return 0;
146}