okami.mom/strata

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

Niko1221Merge f-gfx11-reset 5450e4344: the unregistered expert arena only on the Radeon 780M classe1a17f116

main
16.1 KiB313 linesraw
1// keeping the KFD quiet on an integrated gfx11 GPU under Linux (include/strata/core/kfd_quiet.hpp).  No GPU: the switch rule and the
2// load order's rule.  (The device rule itself is igpu_kfd_settle_default, checked in src/core/arch_defaults_test.cpp.)
3#include "strata/core/kfd_quiet.hpp"
4
5#include <cstdio>
6#include <string>
7#include <utility>
8#include <vector>
9
10#if defined(__linux__)
11#include <sys/stat.h>
12#include <unistd.h>
13#include <cstdlib>
14#endif
15
16using namespace strata::core::kfd_quiet;
17
18namespace {
19int failed = 0;
20#define CHECK(x) do { if (!(x)) { std::fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #x); ++failed; } } while (0)
21
22void switches() {
23    CHECK(switch_on(nullptr, nullptr));
24    CHECK(switch_on("1", nullptr));
25    CHECK(switch_on("", ""));            // empty = unset
26    CHECK(!switch_on("0", nullptr));     // the kill switch turns everything off
27    CHECK(!switch_on("0", "1"));         // ... also when the fix's own switch says 1
28    CHECK(!switch_on(nullptr, "0"));     // one fix off
29    CHECK(!switch_on("1", "0"));
30    CHECK(switch_on("yes", "on"));
31    CHECK(std::string(fix_env(Fix::Load)) == "STRATA_KFD_QUIET_LOAD");
32    CHECK(std::string(fix_env(Fix::Exit)) == "STRATA_KFD_QUIET_EXIT");
33    CHECK(std::string(fix_env(Fix::Xfer)) == "STRATA_KFD_QUIET_XFER");
34}
35
36void load_order() {
37    // not the class: none of the three moves, whatever the switches say (every other card keeps the old order)
38    for (const char* all : {(const char*) nullptr, "1", "0"})
39        for (const char* one : {(const char*) nullptr, "1", "0"}) {
40            const LoadMoves m = load_moves(false, all, one);
41            CHECK(!m.hc_check_early && !m.settle_after_cache && !m.first_prompt_buffers && !m.cpu_share_buffers && !m.staged_copy_warm);
42        }
43    // the class, nothing set: all of them
44    {
45        const LoadMoves m = load_moves(true, nullptr, nullptr);
46        CHECK(m.hc_check_early && m.settle_after_cache && m.first_prompt_buffers && m.cpu_share_buffers && m.staged_copy_warm);
47    }
48    CHECK(kStagedWarmBytes > (1u << 20));   // above the runtime's own staging threshold
49    // the class, STRATA_KFD_QUIET=0 or STRATA_KFD_QUIET_LOAD=0: the old order (the A/B arm of the same binary)
50    using P = std::pair<const char*, const char*>;
51    for (const P& p : std::vector<P>{P{"0", nullptr}, P{nullptr, "0"}, P{"0", "0"}, P{"0", "1"}}) {
52        const LoadMoves m = load_moves(true, p.first, p.second);
53        CHECK(!m.hc_check_early && !m.settle_after_cache && !m.first_prompt_buffers && !m.cpu_share_buffers && !m.staged_copy_warm);
54    }
55    // the flag as the engine sets it: off until the card is named
56    CHECK(!class_flag().load());
57    CHECK(!on(Fix::Load) && !on(Fix::Exit));
58}
59// the class from the topology text, as /sys/class/kfd/kfd/topology/nodes/N/properties reads (shortened)
60const char* kCpuNode = "cpu_cores_count 16\nsimd_count 0\nmem_banks_count 1\ngfx_target_version 0\nvendor_id 0\n";
61const char* k780M = "cpu_cores_count 0\nsimd_count 24\nmem_banks_count 1\ngfx_target_version 110003\nvendor_id 4098\ndevice_id 5567\n";
62const char* k890M = "simd_count 32\ngfx_target_version 110500\n";
63const char* k8060S = "simd_count 80\r\ngfx_target_version 110501\r\n";   // \r tolerated: the number ends at the first non-digit
64const char* k7900 = "simd_count 192\ngfx_target_version 110000\n";        // a discrete RX 7900 XTX
65const char* kR9700 = "simd_count 128\ngfx_target_version 120001\n";
66
67void topology() {
68    {
69        const Node n = parse_node_properties(k780M);
70        CHECK(n.gfx == 110003 && n.simds == 24);
71        const Node c = parse_node_properties(kCpuNode);
72        CHECK(c.gfx == 0 && c.simds == 0);
73        CHECK(parse_node_properties(k8060S).gfx == 110501);
74        CHECK(parse_node_properties("").gfx == 0);
75        CHECK(parse_node_properties("gfx_target_version\nsimd_count x\n").simds == 0);   // no number: not read
76        CHECK(parse_node_properties("xgfx_target_version 110003\n").gfx == 0);            // another key
77    }
78    auto cls = [](std::vector<const char*> nodes) {
79        std::vector<Node> v;
80        for (const char* t : nodes) v.push_back(parse_node_properties(t));
81        return nodes_in_class(v);
82    };
83    CHECK(cls({kCpuNode, k780M}));
84    CHECK(cls({kCpuNode, k890M}));
85    CHECK(cls({kCpuNode, k8060S}));
86    CHECK(!cls({}));                        // no topology (no KFD, another OS, a container without /sys/class/kfd)
87    CHECK(!cls({kCpuNode}));                // no GPU node
88    CHECK(!cls({kCpuNode, k7900}));         // a discrete gfx11
89    CHECK(!cls({kCpuNode, kR9700}));
90    CHECK(!cls({kCpuNode, k780M, k7900}));  // a 780M beside a discrete card: not the class (the card keeps its path)
91    CHECK(!cls({kCpuNode, k890M, kR9700}));
92    // the value: only in the class, only with the switches on, never over the caller's own value
93    CHECK(xfer_value(true, nullptr, nullptr, nullptr) != nullptr && std::string(xfer_value(true, nullptr, nullptr, nullptr)) == "1000000");
94    CHECK(xfer_value(false, nullptr, nullptr, nullptr) == nullptr);
95    CHECK(xfer_value(true, "0", nullptr, nullptr) == nullptr);
96    CHECK(xfer_value(true, nullptr, "0", nullptr) == nullptr);
97    CHECK(xfer_value(true, nullptr, nullptr, "4") == nullptr);
98    CHECK(xfer_value(true, nullptr, nullptr, "") == nullptr);   // set, even empty: the caller's
99#if defined(__linux__)
100    // the folder reader on a temporary tree: two nodes, a stray file, a node without properties
101    char tmpl[] = "/tmp/kfdqXXXXXX";
102    if (mkdtemp(tmpl) != nullptr) {
103        const std::string root = tmpl;
104        auto put = [](const std::string& path, const char* text) {
105            if (std::FILE* f = std::fopen(path.c_str(), "w")) { std::fputs(text, f); std::fclose(f); }
106        };
107        mkdir((root + "/0").c_str(), 0700);
108        mkdir((root + "/1").c_str(), 0700);
109        mkdir((root + "/2").c_str(), 0700);
110        put(root + "/0/properties", kCpuNode);
111        put(root + "/1/properties", k780M);
112        put(root + "/README", "not a node");
113        const std::vector<Node> v = read_topology(root);
114        CHECK(v.size() == 2);
115        CHECK(nodes_in_class(v));
116        CHECK(nodes_in_noreg_class(v));
117        put(root + "/2/properties", k7900);
118        CHECK(!nodes_in_class(read_topology(root)));
119        CHECK(!nodes_in_noreg_class(read_topology(root)));
120        CHECK(read_topology(root + "/none").empty());
121        for (const char* p : {"/0/properties", "/1/properties", "/2/properties", "/README"}) std::remove((root + p).c_str());
122        for (const char* p : {"/0", "/1", "/2", ""}) rmdir((root + p).c_str());
123    } else {
124        std::fprintf(stderr, "FAIL %s:%d: mkdtemp failed\n", __FILE__, __LINE__);
125        ++failed;
126    }
127#endif
128}
129
130// a fake GPU layer for the stop: a clock in seconds, a GPU that finishes after `busy_s` once released (never when `hung`)
131struct FakeGpu {
132    double t = 0.0;
133    double busy_s = 0.5;
134    bool hung = false;
135    bool stopped = false, unregistered = false;
136    std::string order;
137    TeardownResult run(double bound) {
138        auto stop = [&] { stopped = true; order += "S"; };
139        auto wait_for = [&](int ms) {   // waits until done or ms passed; true when done
140            const double lim = ms / 1000.0;
141            if (!hung && busy_s <= lim) { t += busy_s; busy_s = 0.0; return true; }
142            t += lim;
143            if (!hung) busy_s -= lim;
144            return false;
145        };
146        auto release = [&](int ms) { order += "R"; return wait_for(ms); };
147        auto drain = [&](int ms) { order += "D"; return wait_for(ms); };
148        auto unreg = [&] { unregistered = true; order += "U"; };
149        return teardown(stop, release, drain, unreg, [&] { return t; }, bound);
150    }
151};
152
153void stop_order() {
154    // a GPU that finishes in 0.5 s: stop, release, drain, unregister; inside the bound
155    {
156        FakeGpu g;
157        const TeardownResult r = g.run(kExitBoundS);
158        CHECK(g.order == "SRDU");
159        CHECK(r.released && r.drained && r.unregistered && g.unregistered);
160        CHECK(g.t <= kExitBoundS + 1e-9);
161    }
162    // a GPU that takes 2.5 s: the release uses most of the bound, the drain the rest; still drained
163    {
164        FakeGpu g;
165        g.busy_s = 2.5;
166        const TeardownResult r = g.run(kExitBoundS);
167        CHECK(r.released && r.drained && r.unregistered);
168        CHECK(g.t <= kExitBoundS + 1e-9);
169    }
170    // a GPU that takes 5 s: not drained within 3 s: the registration stays (unregistering under a running copy is worse), and
171    // the whole stop took no more than the bound
172    {
173        FakeGpu g;
174        g.busy_s = 5.0;
175        const TeardownResult r = g.run(kExitBoundS);
176        CHECK(!r.released && !r.drained && !r.unregistered && !g.unregistered);
177        CHECK(g.order == "SRD");
178        CHECK(g.t <= kExitBoundS + 0.002);
179    }
180    // a hung GPU: the same, bounded
181    {
182        FakeGpu g;
183        g.hung = true;
184        const TeardownResult r = g.run(kExitBoundS);
185        CHECK(!r.drained && !g.unregistered && g.stopped);
186        CHECK(g.t <= kExitBoundS + 0.002);
187    }
188    // the bound stays inside the server's 5 s between SIGTERM and SIGKILL (serve/server.py ENGINE_TERM_WAIT_S)
189    CHECK(kExitBoundS > 0.0 && kExitBoundS < 5.0);
190    // the first signal starts the stop, a second one takes the default action at once
191    CHECK(on_signal(0) == OnSignal::StartTeardown);
192    CHECK(on_signal(1) == OnSignal::DefaultAction);
193    CHECK(on_signal(2) == OnSignal::DefaultAction);
194}
195
196void noreg() {
197    CHECK(std::string(fix_env(Fix::NoReg)) == "STRATA_KFD_QUIET_NOREG");
198    // the class default: unregistered, unless a switch says 0 or STRATA_UMA_REGISTER=1 asks for the registration
199    CHECK(noreg_default(true, nullptr, nullptr, nullptr));
200    CHECK(noreg_default(true, "1", "1", nullptr));
201    CHECK(noreg_default(true, nullptr, nullptr, "0"));
202    CHECK(!noreg_default(false, nullptr, nullptr, nullptr));   // every other card keeps its registered arena
203    CHECK(!noreg_default(false, "1", "1", nullptr));
204    CHECK(!noreg_default(true, "0", nullptr, nullptr));        // STRATA_KFD_QUIET=0
205    CHECK(!noreg_default(true, nullptr, "0", nullptr));        // STRATA_KFD_QUIET_NOREG=0
206    CHECK(!noreg_default(true, nullptr, nullptr, "1"));        // STRATA_UMA_REGISTER=1
207    // NOREG's own class: the Radeon 780M class only (gfx1103 = 110003); Strix Point, Strix Halo and discrete cards stay registered
208    auto noreg_cls = [](const std::vector<const char*>& texts) {
209        std::vector<Node> v;
210        for (const char* t : texts) v.push_back(parse_node_properties(t));
211        return nodes_in_noreg_class(v);
212    };
213    auto noreg_on = [&](const std::vector<const char*>& texts) { return noreg_default(noreg_cls(texts), nullptr, nullptr, nullptr); };
214    CHECK(gfx_in_noreg_class(110003));
215    CHECK(!gfx_in_noreg_class(110500));
216    CHECK(!gfx_in_noreg_class(110501));
217    CHECK(!gfx_in_noreg_class(110000));
218    CHECK(!gfx_in_noreg_class(120001));
219    CHECK(noreg_on({kCpuNode, k780M}));             // 110003: unregistered
220    CHECK(!noreg_on({kCpuNode, k890M}));            // 110500: registered
221    CHECK(!noreg_on({kCpuNode, k8060S}));           // 110501: registered
222    CHECK(!noreg_on({kCpuNode, k7900}));            // a discrete card: unchanged (registered)
223    CHECK(!noreg_on({kCpuNode, kR9700}));
224    CHECK(!noreg_on({kCpuNode, k780M, k7900}));     // a 780M beside a discrete card: registered, as before
225    CHECK(!noreg_on({kCpuNode, k780M, k890M}));     // every GPU node must be a gfx1103
226    CHECK(!noreg_on({}));
227    CHECK(!noreg_on({kCpuNode}));
228    // the switches still win on the 780M: STRATA_KFD_QUIET_NOREG=0 / STRATA_KFD_QUIET=0 / STRATA_UMA_REGISTER=1 register
229    CHECK(!noreg_default(noreg_cls({kCpuNode, k780M}), nullptr, "0", nullptr));
230    CHECK(!noreg_default(noreg_cls({kCpuNode, k780M}), "0", nullptr, nullptr));
231    CHECK(!noreg_default(noreg_cls({kCpuNode, k780M}), nullptr, nullptr, "1"));
232    // the user's STRATA_ARENA_PIN_GIB wins over the class default both ways
233    CHECK(arena_unregistered(nullptr, noreg_on({kCpuNode, k780M})));
234    CHECK(!arena_unregistered("0", noreg_on({kCpuNode, k780M})));
235    CHECK(!arena_unregistered(nullptr, noreg_on({kCpuNode, k8060S})));
236    CHECK(arena_unregistered("none", noreg_on({kCpuNode, k8060S})));
237    // LOAD / EXIT / XFER keep the whole integrated gfx11 class
238    auto cls = [](const std::vector<const char*>& texts) {
239        std::vector<Node> v;
240        for (const char* t : texts) v.push_back(parse_node_properties(t));
241        return nodes_in_class(v);
242    };
243    CHECK(cls({kCpuNode, k780M}) && cls({kCpuNode, k890M}) && cls({kCpuNode, k8060S}));
244    // the arena's mode: the user's STRATA_ARENA_PIN_GIB wins both ways; unset or empty, the engine's flag
245    CHECK(arena_unregistered(nullptr, true));
246    CHECK(arena_unregistered("", true));
247    CHECK(!arena_unregistered(nullptr, false));
248    CHECK(arena_unregistered("none", false));
249    CHECK(arena_unregistered("none", true));
250    CHECK(!arena_unregistered("0", true));      // the user registers the whole arena
251    CHECK(!arena_unregistered("8", true));      // ... or a capped part of it
252    CHECK(!arena_unregistered("auto", true));
253    // the lock: never tried in the class default; a STRATA_ARENA_LOCK the user set decides; outside NOREG as before (lock unless "0")
254    CHECK(!arena_lock_wanted(true, nullptr));
255    CHECK(arena_lock_wanted(true, "1"));
256    CHECK(!arena_lock_wanted(true, "0"));
257    CHECK(arena_lock_wanted(false, nullptr));
258    CHECK(arena_lock_wanted(false, "1"));
259    CHECK(arena_lock_wanted(false, ""));
260    CHECK(!arena_lock_wanted(false, "0"));
261}
262
263void lend() {
264    CHECK(std::string(fix_env(Fix::Lend)) == "STRATA_KFD_QUIET_LEND");
265    // the switch: written off by default; _LEND=1 on, =0 / STRATA_KFD_QUIET=0 / STRATA_PREFILL_BORROW=1 off; never outside the class
266    CHECK(!kNoLendDefaultOn);
267    CHECK(lend_rule_on(true, nullptr, nullptr, nullptr) == kNoLendDefaultOn);
268    CHECK(lend_rule_on(true, nullptr, nullptr, nullptr, true));    // the constant flipped: the class default
269    CHECK(!lend_rule_on(true, nullptr, nullptr, nullptr, false));
270    CHECK(lend_rule_on(true, nullptr, "1", nullptr));
271    CHECK(lend_rule_on(true, "1", "1", nullptr));
272    CHECK(!lend_rule_on(true, nullptr, "0", nullptr, true));
273    CHECK(!lend_rule_on(true, "0", "1", nullptr, true));
274    CHECK(!lend_rule_on(true, nullptr, "1", "1", true));          // STRATA_PREFILL_BORROW=1 keeps the loan
275    CHECK(lend_rule_on(true, nullptr, "1", "0"));
276    CHECK(!lend_rule_on(false, nullptr, "1", nullptr, true));     // not the class
277    // the pick: the chunk always comes back as it went in (8192 stays 8192: no drop to 2048)
278    const int64_t own8k = 2560;   // about the 780M's own buffers at 8192 (2.5 GiB)
279    for (int64_t c : {(int64_t) 512, (int64_t) 2048, (int64_t) 8192, (int64_t) 16384, (int64_t) 32768}) {
280        CHECK(no_lend_pick(true, true, true, c, 100000, own8k).chunk == c);
281        CHECK(no_lend_pick(true, true, true, c, 0, own8k).chunk == c);
282        CHECK(no_lend_pick(false, true, true, c, 100000, own8k).chunk == c);
283    }
284    // the fit check decides: room >= own + kNoLendKeepMib
285    CHECK(no_lend_pick(true, true, true, 8192, own8k + kNoLendKeepMib, own8k).no_lend);
286    CHECK(!no_lend_pick(true, true, true, 8192, own8k + kNoLendKeepMib - 1, own8k).no_lend);
287    CHECK(!no_lend_pick(true, true, true, 8192, own8k + kNoLendKeepMib - 1, own8k).fits);
288    CHECK(no_lend_pick(true, true, true, 8192, 18000, own8k).no_lend);   // p2006: ~18 GiB free beside a 7.72 GiB cache (G)
289    CHECK(!no_lend_pick(true, true, true, 8192, 0, own8k).no_lend);
290    // only where the path would lend, on one GPU, a chunk of 1 .. kNoLendMaxChunk, with the rule on
291    CHECK(!no_lend_pick(false, true, true, 8192, 100000, own8k).no_lend);
292    CHECK(!no_lend_pick(true, false, true, 8192, 100000, own8k).no_lend);
293    CHECK(!no_lend_pick(true, true, false, 8192, 100000, own8k).no_lend);
294    CHECK(!no_lend_pick(true, true, true, 0, 100000, own8k).no_lend);
295    CHECK(no_lend_pick(true, true, true, kNoLendMaxChunk, 100000, own8k).no_lend);
296    CHECK(!no_lend_pick(true, true, true, kNoLendMaxChunk + 1, 100000, own8k).no_lend);
297}
298}  // namespace
299
300int main() {
301    switches();
302    noreg();
303    lend();
304    load_order();
305    topology();
306    stop_order();
307    if (failed) {
308        std::fprintf(stderr, "kfd_quiet_test: %d check(s) failed\n", failed);
309        return 1;
310    }
311    std::printf("kfd_quiet_test: ok\n");
312    return 0;
313}