okami.mom/strata

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

Niko1221Merge branch 'f-142-mgpu' into f-14243b105a75

main
8.9 KiB217 linesraw
1// aux_cpus_test: the CPU-set derivation of --aux-cpus (pure functions, no GPU) and, on Linux, the placement of live
2// threads (a thread of the process moved to the aux set by the sweep, one that pins itself at creation, the owned ones
3// left alone, and the original placement put back).
4#include "strata/platform/aux_cpus.hpp"
5
6#include <array>
7#include <atomic>
8#include <chrono>
9#include <cstdio>
10#include <thread>
11
12using namespace strata::aux_cpus;
13
14namespace {
15int failures = 0;
16#define CHECK(c)                                                                                                  \
17    do {                                                                                                          \
18        if (!(c)) {                                                                                               \
19            std::fprintf(stderr, "FAILED %s:%d: %s\n", __FILE__, __LINE__, #c);                                   \
20            ++failures;                                                                                           \
21        }                                                                                                         \
22    } while (0)
23
24using V = std::vector<int>;
25
26std::vector<LogicalCpu> cpus(std::initializer_list<std::array<long, 3>> rows) {
27    std::vector<LogicalCpu> out;
28    for (const auto& r : rows) out.push_back({(int) r[0], r[1], r[2]});
29    return out;
30}
31
32// SMT machine: CPU i and i + n are the two threads of core i
33std::vector<LogicalCpu> smt(int n_cores, int n_threads_total) {
34    std::vector<LogicalCpu> out;
35    for (int c = 0; c < n_threads_total; ++c) out.push_back({c, 0, c % n_cores});
36    return out;
37}
38
39void test_parse() {
40    V v;
41    CHECK(parse_cpu_list("24", v) && v == V{24});
42    CHECK(parse_cpu_list("26,24", v) && v == (V{24, 26}));
43    CHECK(parse_cpu_list("24-27,30,25", v) && v == (V{24, 25, 26, 27, 30}));
44    CHECK(parse_cpu_list("0", v) && v == V{0});
45    for (const char* bad : {"", ",", "1,", ",1", "a", "1-", "-1", "3-1", "1,,2", "1 2", "1;2", "99999999"}) {
46        CHECK(!parse_cpu_list(bad, v));
47        CHECK(v.empty());
48    }
49    Config c;
50    std::string err;
51    CHECK(parse_spec("", c, err) && !c.on);
52    CHECK(parse_spec("off", c, err) && !c.on);
53    CHECK(parse_spec("auto", c, err) && c.on && !c.use_list);
54    CHECK(parse_spec("24,26", c, err) && c.on && c.use_list && c.list == (V{24, 26}));
55    CHECK(!parse_spec("fast", c, err) && !err.empty());
56}
57
58void test_derive() {
59    // the production box's shape: 16 workers on 16 physical cores, the host on core 2, 24 allowed CPUs - CPUs 0-15
60    // are cores 0-15 and 22-29 the second threads of cores 0-7: the host's sibling (CPU 24) is the only spare one
61    {
62        std::vector<LogicalCpu> a;
63        for (int c = 0; c < 16; ++c) a.push_back({c, 0, c});
64        for (int c = 22; c < 30; ++c) a.push_back({c, 0, c - 22});
65        V workers;
66        for (int c = 0; c < 16; ++c)
67            if (c != 2) workers.push_back(c);
68        CHECK(derive(a, 2, workers) == V{24});
69        // an explicit list is taken as given, minus the host's CPU and the workers' CPUs
70        CHECK(restrict_list({24, 26, 2, 5, 99}, a, 2, workers) == (V{24, 26}));
71    }
72    // 12 cores x 2 threads, host on CPU 0, workers on CPUs 1-7: the host's sibling first (12), then both threads of
73    // the four worker-free cores 8-11; the siblings of the workers (13-19) are never offered
74    {
75        const auto a = smt(12, 24);
76        CHECK(derive(a, 0, V{1, 2, 3, 4, 5, 6, 7}) == (V{12, 8, 9, 10, 11, 20, 21, 22, 23}));
77    }
78    // no SMT, every other core a worker: nothing spare
79    {
80        const auto a = cpus({{0, 0, 0}, {1, 0, 1}, {2, 0, 2}, {3, 0, 3}});
81        CHECK(derive(a, 0, V{1, 2, 3}).empty());
82    }
83    // no SMT, a core without a worker: that core
84    {
85        const auto a = cpus({{0, 0, 0}, {1, 0, 1}, {2, 0, 2}, {3, 0, 3}});
86        CHECK(derive(a, 0, V{1, 2}) == V{3});
87    }
88    // every CPU is a worker or the host's: nothing, with SMT too
89    {
90        const auto a = smt(4, 8);
91        CHECK(derive(a, 0, V{1, 2, 3}) == V{4});          // only the host's sibling
92        CHECK(derive(a, 0, V{1, 2, 3, 4}).empty());       // CPU 4 is a worker: the host core's sibling is taken
93    }
94    // two packages: core 1 of package 1 is not core 1 of package 0
95    {
96        const auto a = cpus({{0, 0, 0}, {1, 0, 1}, {2, 1, 0}, {3, 1, 1}});
97        CHECK(derive(a, 0, V{1, 2}) == V{3});
98    }
99    // unknown cores (no sysfs): a CPU of unknown core could be a worker's sibling - none offered
100    {
101        const auto a = cpus({{0, -1, -1}, {1, -1, -1}, {2, -1, -1}, {3, -1, -1}});
102        CHECK(derive(a, 0, V{1}).empty());
103    }
104    // the host's CPU unknown: the free cores only
105    {
106        const auto a = smt(4, 8);
107        CHECK(derive(a, -1, V{0, 1}) == (V{2, 3, 6, 7}));
108    }
109    // CPUs the process may not use are not offered (a taskset): 5 and 6 are outside `a`
110    {
111        const auto a = cpus({{0, 0, 0}, {1, 0, 1}, {4, 0, 0}});
112        CHECK(derive(a, 0, V{1}) == V{4});
113    }
114    CHECK(derive(std::vector<LogicalCpu>{}, 0, V{1}).empty());
115}
116
117#if defined(__linux__)
118int tid_of_self() { return (int) syscall(SYS_gettid); }
119
120std::vector<int> mask_of(int tid) {
121    cpu_set_t m;
122    CPU_ZERO(&m);
123    std::vector<int> out;
124    if (sched_getaffinity(tid, sizeof m, &m) != 0) return out;
125    for (int c = 0; c < CPU_SETSIZE; ++c)
126        if (CPU_ISSET(c, &m)) out.push_back(c);
127    return out;
128}
129
130void test_live() {
131    const std::vector<int> allowed = mask_of(0);
132    if (allowed.size() < 4) {
133        std::printf("aux_cpus_test: live placement skipped (needs 4 allowed CPUs, have %zu)\n", allowed.size());
134        return;
135    }
136    const int host = allowed[0], worker = allowed[1], aux_a = allowed[2], aux_b = allowed[3];
137    std::string err;
138    CHECK(configure(std::to_string(aux_a) + "," + std::to_string(aux_b), err));
139    // before the plan exists nothing is moved
140    CHECK(sweep() == 0);
141    const std::string line = set_topology(host, V{worker});
142    std::printf("%s\n", line.c_str());
143    CHECK(plan() == (V{aux_a, aux_b}));
144    CHECK(enabled());
145
146    // threads that stay alive until told: `floater` is created before the sweep (a CUDA driver thread's case), `owned`
147    // is a pool worker (registered), `late` pins itself at creation
148    std::atomic<int> tid_floater{0}, tid_owned{0}, tid_late{0};
149    std::atomic<bool> stop{false};
150    auto body = [&](std::atomic<int>& tid, bool owned, bool pin) {
151        if (owned) note_owned_thread();
152        if (pin) pin_current_thread();
153        tid = tid_of_self();
154        while (!stop.load()) std::this_thread::sleep_for(std::chrono::milliseconds(2));
155    };
156    set_enabled(false);                                   // earlier threads are born unmoved
157    CHECK(!enabled());
158    std::thread floater([&] { body(tid_floater, false, false); });
159    std::thread owned([&] { body(tid_owned, true, false); });
160    for (int i = 0; i < 500 && (!tid_floater || !tid_owned); ++i) std::this_thread::sleep_for(std::chrono::milliseconds(2));
161    CHECK(mask_of(tid_floater) == allowed);
162
163    set_enabled(true);                                    // the sweep
164    const V aux{aux_a, aux_b};
165    CHECK(mask_of(tid_floater) == aux);
166    CHECK(mask_of(tid_owned) == allowed);                 // owned threads are left alone
167    CHECK(mask_of(tid_of_self()) == allowed);             // so is the main thread (configure registered it)
168
169    std::thread late([&] { body(tid_late, false, true); });
170    for (int i = 0; i < 500 && !tid_late; ++i) std::this_thread::sleep_for(std::chrono::milliseconds(2));
171    CHECK(mask_of(tid_late) == aux);                      // pinned at creation
172
173    set_enabled(false);                                   // back to the placements they had
174    CHECK(mask_of(tid_floater) == allowed);
175    CHECK(mask_of(tid_late) == allowed);
176    CHECK(mask_of(tid_owned) == allowed);
177    CHECK(sweep() == 0);                                  // off: nothing moves
178
179    // a thread pinned to ONE worker CPU by someone else is not ours even when unregistered
180    std::atomic<int> tid_pinned{0};
181    std::thread pinned([&] {
182        cpu_set_t m;
183        CPU_ZERO(&m);
184        CPU_SET(worker, &m);
185        sched_setaffinity(0, sizeof m, &m);
186        tid_pinned = tid_of_self();
187        while (!stop.load()) std::this_thread::sleep_for(std::chrono::milliseconds(2));
188    });
189    for (int i = 0; i < 500 && !tid_pinned; ++i) std::this_thread::sleep_for(std::chrono::milliseconds(2));
190    set_enabled(true);
191    CHECK(mask_of(tid_pinned) == V{worker});
192    CHECK(mask_of(tid_floater) == aux);
193    set_enabled(false);
194
195    stop = true;
196    floater.join();
197    owned.join();
198    late.join();
199    pinned.join();
200}
201#else
202void test_live() { std::printf("aux_cpus_test: live placement is Linux only\n"); }
203#endif
204
205}  // namespace
206
207int main() {
208    test_parse();
209    test_derive();
210    test_live();
211    if (failures) {
212        std::fprintf(stderr, "aux_cpus_test: %d check(s) failed\n", failures);
213        return 1;
214    }
215    std::printf("aux_cpus_test: ok\n");
216    return 0;
217}