okami.mom/strata

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

Niko1221Merge f-numa-card2: on two-socket Linux machines the engine runs on the graphics card's NUMA node2e279f758

main
3.1 KiB94 linesraw
1// home_cpus_test: the widest CPU set a thread of the engine takes (include/strata/platform/home_cpus.hpp).  Without a home
2// set a thread that widens gets every CPU the process may use (as before); with one it stays inside it, also when it
3// started on a single CPU (the host's, inherited).  No GPU.
4#include "strata/platform/home_cpus.hpp"
5
6#include <cstdio>
7#include <string>
8#include <thread>
9#include <vector>
10
11static int g_fail = 0;
12
13static void expect(bool ok, const std::string& what) {
14    if (!ok) {
15        ++g_fail;
16        std::fprintf(stderr, "FAIL: %s\n", what.c_str());
17    }
18}
19
20#if defined(__linux__)
21static std::vector<int> mine() {
22    std::vector<int> out;
23    cpu_set_t m;
24    CPU_ZERO(&m);
25    if (sched_getaffinity(0, sizeof m, &m) == 0)
26        for (int c = 0; c < CPU_SETSIZE; ++c)
27            if (CPU_ISSET(c, &m)) out.push_back(c);
28    return out;
29}
30
31static void pin_one(int cpu) {
32    cpu_set_t m;
33    CPU_ZERO(&m);
34    CPU_SET(cpu, &m);
35    (void) sched_setaffinity(0, sizeof m, &m);
36}
37#endif
38
39int main() {
40#if defined(__linux__)
41    const std::vector<int> start = mine();
42    if (start.size() < 2) {
43        std::printf("home_cpus_test: skipped (this process may run on %zu CPU)\n", start.size());
44        return 0;
45    }
46    {   // no home: the old behaviour, every CPU the process may use
47        const cpu_set_t h = strata::platform::home_mask();
48        expect(CPU_COUNT(&h) == CPU_SETSIZE, "no home: the mask is every CPU number");
49        std::vector<int> got;
50        std::thread t([&] {
51            pin_one(start.front());
52            strata::platform::widen_to_home();
53            got = mine();
54        });
55        t.join();
56        expect(got == start, "no home: a pinned thread widens back to all the process's CPUs");
57    }
58    const std::vector<int> half(start.begin(), start.begin() + (long) (start.size() / 2));
59    strata::platform::set_home_cpus({});   // empty: nothing changes
60    {
61        const cpu_set_t h = strata::platform::home_mask();
62        expect(CPU_COUNT(&h) == CPU_SETSIZE, "empty home list: still every CPU number");
63    }
64    strata::platform::set_home_cpus(half);
65    {
66        std::vector<int> got;
67        std::thread t([&] {
68            pin_one(start.front());
69            strata::platform::widen_to_home();
70            got = mine();
71        });
72        t.join();
73        expect(got == half, "home set: a pinned thread widens to the home CPUs only");
74        std::vector<int> got2;
75        std::thread u([&] {   // a thread that runs on every CPU of the process widens DOWN to the home set too
76            strata::platform::widen_to_home();
77            got2 = mine();
78        });
79        u.join();
80        expect(got2 == half, "home set: a wide thread ends on the home CPUs");
81    }
82    expect(mine() == start, "the calling thread is not moved by set_home_cpus");
83#else
84    strata::platform::set_home_cpus({0});
85    strata::platform::widen_to_home();
86    std::printf("home_cpus_test: not Linux, nothing to check\n");
87#endif
88    if (g_fail != 0) {
89        std::fprintf(stderr, "home_cpus_test: %d check(s) FAILED\n", g_fail);
90        return 1;
91    }
92    std::printf("home_cpus_test: all checks passed\n");
93    return 0;
94}