okami.mom/strata

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

Niko1221Merge f-ci-green-0215: the four red GitHub checks (cmake-cpu on MSVC, py-tools on a CRLF checkout and on macOS, the Windows snapshot time)b749503db

main
17.0 KiB344 linesraw
1// card_node_test: when the engine restricts itself to the graphics card's NUMA node (include/strata/kernels/cpu/card_node.hpp).
2// The decision on made-up machines, the sysfs / proc readers on a fake tree in a temp directory, and (Linux) the restriction
3// of a two-thread process to a subset of its own CPUs.  No GPU, no model.
4#include "strata/kernels/cpu/card_node.hpp"
5
6#include <atomic>
7#include <chrono>
8#include <cstdio>
9#include <ctime>
10#include <filesystem>
11#include <fstream>
12#include <string>
13#include <thread>
14#include <vector>
15
16namespace cpu = strata::kernels::cpu;
17namespace fs = std::filesystem;
18
19static int g_fail = 0;
20
21static void expect(bool ok, const std::string& what) {
22    if (!ok) {
23        ++g_fail;
24        std::fprintf(stderr, "FAIL: %s\n", what.c_str());
25    }
26}
27
28// A two-socket machine, one node per socket, 2-way SMT: node k has cores [0, cores_k), CPU numbers as Linux gives them
29// (node 0's first threads, node 1's first threads, then node 0's siblings, node 1's siblings).
30static std::vector<cpu::CardNodeCpu> machine(int cores0, int cores1) {
31    std::vector<cpu::CardNodeCpu> m;
32    int n = 0;
33    for (int pass = 0; pass < 2; ++pass) {
34        for (int c = 0; c < cores0; ++c) m.push_back({n++, 0, 0, c});
35        for (int c = 0; c < cores1; ++c) m.push_back({n++, 1, 1, c});
36    }
37    return m;
38}
39
40static std::vector<int> cpus_of(const std::vector<cpu::CardNodeCpu>& m, int node) {
41    std::vector<int> out;
42    for (const auto& c : m)
43        if (c.node == node) out.push_back(c.cpu);
44    return out;
45}
46
47// 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.
48static cpu::CardNodeInput pod() {
49    cpu::CardNodeInput in;
50    in.nodes_with_cpus = 2;
51    in.mask = machine(32, 32);
52    in.mems_known = true;
53    in.mems_allowed = {0, 1};
54    in.card_nodes = {1};
55    in.card_node_free_bytes = 100ll << 30;
56    in.pinned_need_bytes = cpu::card_node_pinned_need(131072);
57    in.cpu_quota = 13.6;
58    in.today_workers = 12;
59    return in;
60}
61
62static void decisions() {
63    // two nodes, the card on node 1: restricted to node 1's 64 logical CPUs, the pool keeps the quota's 12 workers
64    {
65        const auto d = cpu::decide_card_node(pod());
66        expect(d.on && d.why.empty(), "pod: restricted (" + d.why + ")");
67        expect(d.node == 1, "pod: node 1");
68        expect(d.cpus == cpus_of(machine(32, 32), 1) && d.cpus.size() == 64, "pod: node 1's 64 CPUs");
69        expect(d.node_cores == 32 && d.node_workers == 12, "pod: 32 cores, 12 workers on the node (quota)");
70    }
71    // the card on node 0
72    {
73        auto in = pod();
74        in.card_nodes = {0};
75        const auto d = cpu::decide_card_node(in);
76        expect(d.on && d.node == 0 && d.cpus == cpus_of(in.mask, 0), "card on node 0: node 0's CPUs");
77    }
78    // one node: nothing
79    {
80        auto in = pod();
81        in.nodes_with_cpus = 1;
82        const auto d = cpu::decide_card_node(in);
83        expect(!d.on && d.why == "one NUMA node" && d.cpus.empty(), "one node: left as is");
84    }
85    // the switch off
86    {
87        auto in = pod();
88        in.switch_off = true;
89        const auto d = cpu::decide_card_node(in);
90        expect(!d.on && d.why == "STRATA_NUMA_CARD=0", "switch off: left as is");
91    }
92    // the card's node not known (sysfs -1 or unreadable)
93    {
94        auto in = pod();
95        in.card_nodes = {-1};
96        expect(!cpu::decide_card_node(in).on, "card node -1: left as is");
97        in.card_nodes = {};
98        expect(!cpu::decide_card_node(in).on, "no card: left as is");
99    }
100    // two cards on different nodes; two on the same node
101    {
102        auto in = pod();
103        in.card_nodes = {1, 0};
104        const auto d = cpu::decide_card_node(in);
105        expect(!d.on && d.why == "the cards sit on different NUMA nodes", "cards on nodes 1 and 0: left as is");
106        in.card_nodes = {1, 1};
107        expect(cpu::decide_card_node(in).on, "two cards on node 1: restricted");
108        in.card_nodes = {1, -1};
109        expect(!cpu::decide_card_node(in).on, "one card's node unknown: left as is");
110    }
111    // a process already restricted to one node (taskset / numactl / cpuset): left as is, also when it is the other node
112    {
113        auto in = pod();
114        std::vector<cpu::CardNodeCpu> one;
115        for (const auto& c : in.mask)
116            if (c.node == 0) one.push_back(c);
117        in.mask = one;
118        const auto d = cpu::decide_card_node(in);
119        expect(!d.on && d.why == "the process already runs on one NUMA node", "pre-restricted to node 0: left as is");
120        std::vector<cpu::CardNodeCpu> part;   // a subset that still spans both nodes (a cpuset over both): the rule applies
121        for (const auto& c : pod().mask)
122            if (c.cpu % 4 == 0) part.push_back(c);
123        in.mask = part;
124        in.today_workers = 7;   // that mask's own pool: 16 cores in all, the host's socket's 8 -> 7 workers
125        const auto e = cpu::decide_card_node(in);
126        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");
127        in.today_workers = 12;
128        expect(!cpu::decide_card_node(in).on, "the same mask with 12 workers today: 7 on the node, left as is");
129    }
130    // a CPU whose node is not known
131    {
132        auto in = pod();
133        in.mask[5].node = -1;
134        expect(!cpu::decide_card_node(in).on, "a CPU without a node: left as is");
135    }
136    // a node with too few CPUs for the pool: 4 + 60 cores, no quota, today's pool spans both sockets: 32 workers
137    {
138        cpu::CardNodeInput in;
139        in.nodes_with_cpus = 2;
140        in.mask = machine(4, 60);
141        in.card_nodes = {0};
142        in.card_node_free_bytes = 100ll << 30;
143        in.today_workers = 32;
144        const auto d = cpu::decide_card_node(in);
145        expect(!d.on && d.node_workers == 3 && d.cpus.empty(), "card on a 4-core node, 32 workers today: left as is (" + d.why + ")");
146        in.card_nodes = {1};
147        const auto e = cpu::decide_card_node(in);
148        expect(e.on && e.node_workers == 32, "card on the 60-core node: restricted, 32 workers");
149        // a 16-core node against 32 spread: 16 pinned only matched 32 spread in the measurement, so the count must not drop
150        in.mask = machine(16, 16);
151        in.card_nodes = {1};
152        in.today_workers = 30;
153        expect(!cpu::decide_card_node(in).on, "2 x 16 cores, 30 workers today, 15 on a node: left as is");
154    }
155    // --pool-workers N: the node needs N + 1 physical cores
156    {
157        auto in = pod();
158        in.user_workers = in.today_workers = 31;
159        expect(cpu::decide_card_node(in).on, "--pool-workers 31 on a 32-core node: restricted");
160        in.user_workers = in.today_workers = 32;
161        expect(!cpu::decide_card_node(in).on, "--pool-workers 32 on a 32-core node: left as is");
162        in.user_workers = in.today_workers = 4;
163        expect(cpu::decide_card_node(in).on, "--pool-workers 4: restricted");
164    }
165    // a container quota smaller than the node: the pool is bounded by the quota either way and the rule does not move it
166    {
167        cpu::CardNodeInput in;
168        in.nodes_with_cpus = 2;
169        in.mask = machine(96, 96);   // the second RTX 5090 host: 2 x EPYC 9654, card on node 0, a 46-CPU quota
170        in.card_nodes = {0};
171        in.card_node_free_bytes = 100ll << 30;
172        in.cpu_quota = 46;
173        in.today_workers = 32;
174        const auto d = cpu::decide_card_node(in);
175        expect(d.on && d.node_workers == 32 && d.cpus.size() == 192, "quota 46 on 2 x 96 cores: restricted, 32 workers");
176        in.cpu_quota = 17.55;   // the first RTX 5090 host's quota: 16 workers either way
177        in.today_workers = 16;
178        const auto e = cpu::decide_card_node(in);
179        expect(e.on && e.node_workers == 16, "quota 17.55: restricted, 16 workers");
180        // a quota larger than one node (2 x 8 cores, 30 CPUs): today 15 workers, a node 7: left as is
181        in.mask = machine(8, 8);
182        in.cpu_quota = 30;
183        in.today_workers = 15;
184        expect(!cpu::decide_card_node(in).on, "quota above one node's CPUs: left as is");
185    }
186    // memory: the process may not allocate on the card's node, or the node is short of free memory
187    {
188        auto in = pod();
189        in.mems_allowed = {0};
190        const auto d = cpu::decide_card_node(in);
191        expect(!d.on && d.why == "the process may not allocate memory on node 1", "mems_allowed without the card's node: left as is");
192        in.mems_known = false;   // not readable: not a reason to stop
193        expect(cpu::decide_card_node(in).on, "mems_allowed not known: restricted");
194        in = pod();
195        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
196        const auto e = cpu::decide_card_node(in);
197        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 + ")");
198        in.card_node_free_bytes = -1;
199        expect(!cpu::decide_card_node(in).on, "free memory not known: left as is");
200        in.card_node_free_bytes = (int64_t) in.pinned_need_bytes;
201        expect(cpu::decide_card_node(in).on, "free memory exactly the need: restricted");
202        expect(cpu::card_node_pinned_need(131072) == 131072ull * 12700 + (2ull << 30), "pinned need at 131,072 cells");
203        expect(cpu::card_node_pinned_need(0) == (2ull << 30) && cpu::card_node_pinned_need(-5) == (2ull << 30), "pinned need without a context");
204    }
205    // the person placed the threads; a hybrid CPU; the process has no CPU on the card's node
206    {
207        auto in = pod();
208        in.user_placed = "--pool-cores is set";
209        const auto d = cpu::decide_card_node(in);
210        expect(!d.on && d.why == "--pool-cores is set", "--pool-cores: left as is");
211        in = pod();
212        in.hybrid = true;
213        expect(!cpu::decide_card_node(in).on, "hybrid: left as is");
214        in = pod();
215        in.nodes_with_cpus = 3;
216        in.card_nodes = {2};
217        in.mems_known = false;
218        const auto e = cpu::decide_card_node(in);
219        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");
220    }
221}
222
223static void write(const fs::path& p, const std::string& text) {
224    fs::create_directories(p.parent_path());
225    std::ofstream(p) << text;
226}
227
228static void readers() {
229    const fs::path root = fs::temp_directory_path() / ("card_node_test_" + std::to_string((long long) std::time(nullptr)));
230    fs::remove_all(root);
231    const fs::path node = root / "devices/system/node";
232    write(node / "node0/cpulist", "0-1,4-5\n");
233    write(node / "node1/cpulist", "2-3,6-7\n");
234    write(node / "node2/cpulist", "\n");   // a memory-only node (CXL): not counted
235    write(node / "online", "0-2\n");
236    fs::create_directories(node / "nodeX");
237    write(node / "node1/meminfo", "Node 1 MemTotal:       264000000 kB\nNode 1 MemFree:        112233 kB\nNode 1 MemUsed: 1 kB\n");
238    write(node / "node0/meminfo", "Node 0 MemTotal: 5 kB\n");   // no MemFree line
239    for (int c = 0; c < 8; ++c) {
240        const fs::path t = root / ("devices/system/cpu/cpu" + std::to_string(c)) / "topology";
241        write(t / "physical_package_id", std::to_string((c / 2) % 2) + "\n");
242        write(t / "core_id", std::to_string(c % 2) + "\n");
243    }
244#if !defined(_WIN32)
245    // (a PCI address has colons, which no Windows file name may have: create_directories throws there.  The engine reads sysfs on Linux only.)
246    write(root / "bus/pci/devices/0000:e1:00.0/numa_node", "1\n");
247    write(root / "bus/pci/devices/0000:21:00.0/numa_node", "-1\n");
248    write(root / "bus/pci/devices/0000:22:00.0/numa_node", "junk\n");
249#endif
250
251#if defined(__linux__)
252    const auto nodes = cpu::card_node_node_cpus(root.string());
253    expect(nodes.size() == 2, "fake sysfs: 2 nodes with CPUs (memory-only node2 and nodeX left out)");
254    expect(nodes.count(0) && nodes.at(0) == std::vector<int>({0, 1, 4, 5}), "fake sysfs: node 0's CPUs");
255    expect(nodes.count(1) && nodes.at(1) == std::vector<int>({2, 3, 6, 7}), "fake sysfs: node 1's CPUs");
256    const auto desc = cpu::card_node_describe(root.string(), {0, 3, 6, 9}, nodes);
257    expect(desc.size() == 4 && desc[0].node == 0 && desc[1].node == 1 && desc[2].node == 1 && desc[3].node == -1, "describe: nodes");
258    expect(desc[1].pkg == 1 && desc[1].core == 1 && desc[3].pkg == -1 && desc[3].core == -1, "describe: package and core, missing CPU -1");
259    expect(cpu::card_node_node_cpus((root / "nothing").string()).empty(), "no node folder: no nodes");
260#endif
261#if !defined(_WIN32)
262    expect(cpu::card_node_pci_node(root.string(), "0000:e1:00.0") == 1, "pci numa_node 1");
263    expect(cpu::card_node_pci_node(root.string(), "0000:21:00.0") == -1, "pci numa_node -1");
264    expect(cpu::card_node_pci_node(root.string(), "0000:22:00.0") == -1, "pci numa_node junk: -1");
265    expect(cpu::card_node_pci_node(root.string(), "0000:99:00.0") == -1, "pci device missing: -1");
266#endif
267    expect(cpu::card_node_free_bytes(root.string(), 1) == 112233ll * 1024, "node 1 MemFree");
268    expect(cpu::card_node_free_bytes(root.string(), 0) == -1, "node 0 without MemFree: -1");
269    expect(cpu::card_node_free_bytes(root.string(), 5) == -1, "node 5 missing: -1");
270    expect(cpu::card_node_free_bytes(root.string(), -1) == -1, "node -1: -1");
271    expect(cpu::card_node_bdf(0, 0xE1, 0) == "0000:e1:00.0", "bdf 0000:e1:00.0");
272    expect(cpu::card_node_bdf(0x10, 0x3b, 0x1f, 1) == "0010:3b:1f.1", "bdf 0010:3b:1f.1");
273    fs::remove_all(root);
274
275    std::vector<int> v;
276    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");
277    expect(cpu::card_node_parse_cpulist(" 3 ", v) && v == std::vector<int>({3}), "cpulist 3");
278    expect(cpu::card_node_parse_cpulist("\n", v) && v.empty(), "cpulist empty");
279    expect(!cpu::card_node_parse_cpulist("3-1", v) && v.empty(), "cpulist reversed: refused");
280    expect(!cpu::card_node_parse_cpulist("1,,2", v), "cpulist empty item: refused");
281    expect(!cpu::card_node_parse_cpulist("a", v), "cpulist letters: refused");
282    std::vector<int> mems;
283    expect(cpu::card_node_mems_allowed("Name:\tstrata\nMems_allowed:\t00000003\nMems_allowed_list:\t0-1\nVoluntary: 1\n", mems) &&
284               mems == std::vector<int>({0, 1}), "Mems_allowed_list 0-1");
285    expect(cpu::card_node_mems_allowed("Mems_allowed_list:\t1\n", mems) && mems == std::vector<int>({1}), "Mems_allowed_list 1");
286    expect(!cpu::card_node_mems_allowed("Name:\tx\n", mems) && mems.empty(), "no Mems_allowed_list line");
287    for (const char* off : {"0", "off", "OFF", "false", "no"}) expect(cpu::card_node_switch_off(off), std::string("switch off: ") + off);
288    for (const char* on : {"1", "", "on", "yes"}) expect(!cpu::card_node_switch_off(on), std::string("switch not off: '") + on + "'");
289    expect(!cpu::card_node_switch_off(nullptr), "switch unset: not off");
290}
291
292#if defined(__linux__)
293// restrict_process_cpus on this process: a second thread exists before the call and must be restricted too
294static void restriction() {
295    const std::vector<int> mine = cpu::card_node_thread_cpus();
296    if (mine.size() < 2) {
297        std::printf("card_node_test: restriction skipped (this process may run on %zu CPU)\n", mine.size());
298        return;
299    }
300    std::atomic<bool> stop{false};
301    std::atomic<int> other_tid{0};
302    std::vector<int> other_after;
303    std::thread t([&] {
304        other_tid = (int) syscall(SYS_gettid);
305        while (!stop.load()) std::this_thread::sleep_for(std::chrono::milliseconds(2));
306        other_after = cpu::card_node_thread_cpus();
307    });
308    while (other_tid.load() == 0) std::this_thread::sleep_for(std::chrono::milliseconds(1));
309    const std::vector<int> half(mine.begin(), mine.begin() + (long) (mine.size() / 2));
310    std::vector<int> want = half;
311    want.push_back(100000);   // a CPU the process does not have is ignored
312    int refused = -1;
313    const int changed = cpu::restrict_process_cpus(want, refused);
314    expect(changed >= 2 && refused == 0, "restriction: both threads changed (" + std::to_string(changed) + ", refused " +
315                                             std::to_string(refused) + ")");
316    expect(cpu::card_node_thread_cpus() == half, "restriction: the calling thread has the subset");
317    std::vector<int> created_after;
318    std::thread n([&] { created_after = cpu::card_node_thread_cpus(); });
319    n.join();
320    expect(created_after == half, "restriction: a thread created afterwards inherits the subset");
321    stop = true;
322    t.join();
323    expect(other_after == half, "restriction: the thread that existed before has the subset");
324    int refused2 = -1;
325    expect(cpu::restrict_process_cpus(half, refused2) == 0 && refused2 == 0, "restriction: a second call changes nothing");
326    std::vector<int> none = {100000};
327    expect(cpu::restrict_process_cpus(none, refused2) == 0 && cpu::card_node_thread_cpus() == half,
328           "restriction: no common CPU keeps the mask");
329}
330#endif
331
332int main() {
333    decisions();
334    readers();
335#if defined(__linux__)
336    restriction();
337#endif
338    if (g_fail != 0) {
339        std::fprintf(stderr, "card_node_test: %d check(s) FAILED\n", g_fail);
340        return 1;
341    }
342    std::printf("card_node_test: all checks passed\n");
343    return 0;
344}