okami.mom/strata

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

Niko1221Merge f-gfx11-arrival 0ee263992 into f-p4-int (integrated AMD graphics under Linux: no huge-page request for the expert arena, a settle before READY, the model files' page cache given back after the expert load)29ebb7926

main
7.1 KiB160 linesraw
1// the wait before READY on an integrated AMD GPU under Linux (include/strata/core/kfd_settle.hpp).  No GPU, no sleeping: the
2// counter reader and the clock are faked; the folder reader is tried on a temporary folder.  Cases: file missing, unreadable, garbage,
3// counter moving, counter quiet, cap reached.  (The device rule is checked in src/core/arch_defaults_test.cpp.)
4#include "strata/core/kfd_settle.hpp"
5
6#include <chrono>
7#include <cstdint>
8#include <cstdio>
9#include <cstdlib>
10#include <functional>
11#include <string>
12
13#if defined(__linux__)
14#include <sys/stat.h>
15#include <unistd.h>
16#endif
17
18using namespace strata::core;
19using namespace strata::core::kfd_settle;
20
21namespace {
22int failed = 0;
23#define CHECK(x) do { if (!(x)) { std::fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #x); ++failed; } } while (0)
24
25struct Fake {
26    double t = 0.0;
27    int sleeps = 0;
28    std::function<Reading(int)> reading;   // the reading at poll number i (0 = the first, before any sleep)
29    Result run(double quiet, double cap, double poll = 0.2) {
30        return settle([&] { return reading(sleeps); }, [&] { return t; }, [&](double s) { t += s; ++sleeps; }, quiet, cap, poll);
31    }
32};
33Reading ok(int64_t ms) { Reading r; r.kind = Reading::Ok; r.ms = ms; return r; }
34Reading kind(Reading::Kind k) { Reading r; r.kind = k; return r; }
35bool near(double a, double b, double tol) { return a > b - tol && a < b + tol; }
36
37#if defined(__linux__)
38void put(const std::string& path, const std::string& text) {
39    if (std::FILE* f = std::fopen(path.c_str(), "w")) { std::fwrite(text.data(), 1, text.size(), f); std::fclose(f); }
40}
41#endif
42}  // namespace
43
44int main() {
45    // 1. the counter file is missing: nothing is waited, nothing is slept
46    {
47        Fake f;
48        f.reading = [](int) { return kind(Reading::Missing); };
49        const Result r = f.run(2.0, 10.0);
50        CHECK(r.why == Result::NoCounter);
51        CHECK(r.waited_s == 0.0);
52        CHECK(f.sleeps == 0);
53    }
54    // 2. unreadable: the same at the first reading; a counter that stops being readable while waiting ends the wait
55    {
56        Fake f;
57        f.reading = [](int) { return kind(Reading::Unreadable); };
58        const Result r = f.run(2.0, 10.0);
59        CHECK(r.why == Result::NoCounter && r.waited_s == 0.0 && f.sleeps == 0);
60        Fake g;
61        g.reading = [](int i) { return i < 3 ? ok(100 + i) : kind(Reading::Unreadable); };   // moving, then gone
62        const Result s = g.run(2.0, 10.0);
63        CHECK(s.why == Result::NoCounter);
64        CHECK(near(s.waited_s, 0.6, 0.05));
65    }
66    // 3. garbage in the file
67    {
68        int64_t ms = -1;
69        CHECK(!parse_evicted_ms("", ms));
70        CHECK(!parse_evicted_ms("   \n", ms));
71        CHECK(!parse_evicted_ms("abc", ms));
72        CHECK(!parse_evicted_ms("12x", ms));
73        CHECK(!parse_evicted_ms("-5", ms));
74        CHECK(!parse_evicted_ms("+5", ms));
75        CHECK(!parse_evicted_ms("1 2", ms));
76        CHECK(!parse_evicted_ms("1.5", ms));
77        CHECK(!parse_evicted_ms("1234567890123456789", ms));   // 19 digits
78        CHECK(ms == -1);                                       // untouched on failure
79        CHECK(parse_evicted_ms("0", ms) && ms == 0);
80        CHECK(parse_evicted_ms("12\n", ms) && ms == 12);
81        CHECK(parse_evicted_ms("  7 \t\n", ms) && ms == 7);
82        CHECK(parse_evicted_ms("123456789012345678", ms) && ms == 123456789012345678LL);   // 18 digits
83    }
84    // 4. the counter moving: every poll for 1 s, then still: the wait ends two quiet seconds after the last move
85    {
86        Fake f;
87        f.reading = [](int i) { return ok(i <= 5 ? 1000 + 10 * i : 1050); };   // moves at polls 1..5 (t = 0.2 .. 1.0)
88        const Result r = f.run(2.0, 10.0);
89        CHECK(r.why == Result::Quiet);
90        CHECK(near(r.waited_s, 3.0, 0.25));    // last move at 1.0 s + 2.0 s quiet
91        CHECK(r.ms == 1050);
92    }
93    // 5. the counter quiet from the start: the least wait is the quiet time
94    {
95        Fake f;
96        f.reading = [](int) { return ok(4242); };
97        const Result r = f.run(2.0, 10.0);
98        CHECK(r.why == Result::Quiet);
99        CHECK(near(r.waited_s, 2.0, 0.21));
100        CHECK(r.ms == 4242);
101        Fake g;
102        g.reading = [](int) { return ok(0); };   // a process that was never evicted
103        CHECK(g.run(2.0, 10.0).why == Result::Quiet);
104    }
105    // 6. the cap: a counter that never stops ends the wait at the cap
106    {
107        Fake f;
108        f.reading = [](int i) { return ok(i * 5); };
109        const Result r = f.run(2.0, 10.0);
110        CHECK(r.why == Result::Cap);
111        CHECK(r.waited_s >= 10.0 && r.waited_s < 10.3);
112        // quiet is judged before the cap: quiet exactly at the cap counts as quiet
113        Fake g;
114        g.reading = [](int i) { return ok(i < 40 ? i : 40); };   // moves until 8.0 s, quiet from then: 10.0 s
115        const Result q = g.run(2.0, 10.0);
116        CHECK(q.why == Result::Quiet);
117    }
118#if defined(__linux__)
119    // the folder reader: sum over stats_*, missing, unreadable
120    {
121        char tmpl[] = "/tmp/kfd_settle_test_XXXXXX";
122        const char* dir = mkdtemp(tmpl);
123        CHECK(dir != nullptr);
124        if (dir != nullptr) {
125            const std::string d = dir;
126            CHECK(read_own_evicted_ms(d + "/no_such_folder").kind == Reading::Missing);
127            CHECK(read_own_evicted_ms(d).kind == Reading::Missing);                          // an empty folder
128            mkdir((d + "/stats_1").c_str(), 0700);
129            CHECK(read_own_evicted_ms(d).kind == Reading::Missing);                          // a GPU folder without the file
130            put(d + "/stats_1/evicted_ms", "12\n");
131            Reading r = read_own_evicted_ms(d);
132            CHECK(r.kind == Reading::Ok && r.ms == 12);
133            mkdir((d + "/stats_2").c_str(), 0700);
134            put(d + "/stats_2/evicted_ms", "30");
135            r = read_own_evicted_ms(d);
136            CHECK(r.kind == Reading::Ok && r.ms == 42);                                      // two GPUs: the sum
137            put(d + "/stats_2/evicted_ms", "not a number\n");
138            CHECK(read_own_evicted_ms(d).kind == Reading::Unreadable);
139            put(d + "/stats_2/evicted_ms", "");
140            CHECK(read_own_evicted_ms(d).kind == Reading::Unreadable);
141            std::remove((d + "/stats_1/evicted_ms").c_str());
142            std::remove((d + "/stats_2/evicted_ms").c_str());
143            rmdir((d + "/stats_1").c_str());
144            rmdir((d + "/stats_2").c_str());
145            rmdir(d.c_str());
146        }
147        // this process's own folder: whatever the box has, the call returns (it is Missing on a box without KFD)
148        (void) read_own_evicted_ms(own_proc_dir());
149    }
150#endif
151    // the engine's call: with the flag down (the default) it returns at once
152    {
153        CHECK(!kfd_settle_flag().load());
154        const auto t0 = std::chrono::steady_clock::now();
155        settle_before_ready();
156        CHECK(std::chrono::duration<double>(std::chrono::steady_clock::now() - t0).count() < 0.5);
157    }
158    if (failed == 0) std::printf("kfd_settle_test: ok\n");
159    return failed == 0 ? 0 : 1;
160}