okami.mom/strata

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

Niko1221Merge f-int-tidy 4e9dde87e into f-p4-int (conversation tests in the CPU check preset, packer probe without bytecode, a per-architecture spill-guard baseline, a log line for a driver GPU pause between requests on AMD under Linux)466626cbb

main
9.3 KiB178 linesraw
1// The GPU pause counter (strata/core/gpu_pause_time.hpp): the reader against a fake KFD tree (present, missing,
2// unreadable, garbage, several GPUs) and the two lines.  No GPU, no driver.  Off Linux the reader is always "not
3// available", which is checked instead.
4#include "strata/core/gpu_pause_time.hpp"
5
6#include <chrono>
7#include <cstdio>
8#include <cstdlib>
9#include <string>
10
11#if defined(__linux__)
12#include <sys/stat.h>
13#include <unistd.h>
14#endif
15
16using namespace strata::core;
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
22#if defined(__linux__)
23void put(const std::string& path, const char* text) {
24    std::FILE* f = std::fopen(path.c_str(), "w");
25    if (f == nullptr) { std::fprintf(stderr, "cannot write %s\n", path.c_str()); ++failed; return; }
26    std::fputs(text, f);
27    std::fclose(f);
28}
29void gpu(const std::string& proc, const char* id, const char* text) {
30    const std::string d = proc + "/stats_" + id;
31    mkdir(d.c_str(), 0755);
32    if (text != nullptr) put(d + "/evicted_ms", text);
33}
34#endif
35}  // namespace
36
37int main() {
38#if defined(__linux__)
39    char tmpl[] = "/tmp/gpu_pause_test_XXXXXX";
40    const char* root = mkdtemp(tmpl);
41    CHECK(root != nullptr);
42    if (root == nullptr) return 1;
43    const std::string r = root;
44    auto proc = [&](long pid) {
45        const std::string p = r + "/" + std::to_string(pid);
46        mkdir(p.c_str(), 0755);
47        return p;
48    };
49
50    CHECK(gpu_pause_ms(r.c_str(), 1) == -1);                        // no folder for the process: not available
51    proc(2);
52    CHECK(gpu_pause_ms(r.c_str(), 2) == -1);                        // a folder without GPUs
53    put(proc(2) + "/pasid", "32770\n");                              // other entries are ignored
54    CHECK(gpu_pause_ms(r.c_str(), 2) == -1);
55
56    gpu(proc(3), "41945", "954\n");                                  // one GPU, as the driver writes it
57    CHECK(gpu_pause_ms(r.c_str(), 3) == 954);
58    gpu(proc(3), "17288", "0\n");                                    // a second GPU: summed
59    CHECK(gpu_pause_ms(r.c_str(), 3) == 954);
60    put(proc(3) + "/stats_17288/evicted_ms", "15\n");
61    CHECK(gpu_pause_ms(r.c_str(), 3) == 969);
62    put(proc(3) + "/stats_17288/cu_occupancy", "12\n");              // other counters beside it are not read
63    CHECK(gpu_pause_ms(r.c_str(), 3) == 969);
64
65    gpu(proc(4), "1", nullptr);                                      // a GPU folder without the counter: not available
66    CHECK(gpu_pause_ms(r.c_str(), 4) == -1);
67    gpu(proc(5), "1", "12");                                         // no newline is fine
68    CHECK(gpu_pause_ms(r.c_str(), 5) == 12);
69    const char* garbage[] = {"", "\n", "abc\n", "-5\n", "12 ms\n", "1.5\n", "99999999999999999999999\n"};
70    long pid = 10;
71    for (const char* g : garbage) {
72        gpu(proc(pid), "1", g);
73        CHECK(gpu_pause_ms(r.c_str(), pid) == -1);
74        ++pid;
75    }
76    gpu(proc(30), "1", "100\n");                                     // one good GPU and one bad: not available
77    gpu(proc(30), "2", "x\n");
78    CHECK(gpu_pause_ms(r.c_str(), 30) == -1);
79
80    gpu(proc(40), "1", "77\n");                                      // unreadable (skipped when run as root)
81    const std::string locked = r + "/40/stats_1/evicted_ms";
82    chmod(locked.c_str(), 0);
83    if (geteuid() != 0) CHECK(gpu_pause_ms(r.c_str(), 40) == -1);
84    chmod(locked.c_str(), 0644);
85    CHECK(gpu_pause_ms(r.c_str(), 40) == 77);
86
87    // the real tree for this process: whatever it is, it is a number or "not available", and it does not throw
88    const int64_t self = gpu_pause_ms_self();
89    CHECK(self >= -1);
90    std::printf("this process on this machine: %lld\n", (long long) self);
91
92    // no KFD tree (a CUDA box, a container without sysfs): not available, no line, and it costs one failed opendir.
93    // 10,000 reads: the mean is printed; the bound (50 us each) only catches something gone badly wrong
94    {
95        const std::string none = r + "/no-kfd-here";
96        const auto t0 = std::chrono::steady_clock::now();
97        int64_t v = 0;
98        for (int i = 0; i < 10000; ++i) v += gpu_pause_ms(none.c_str(), 1234);
99        const double us = std::chrono::duration<double, std::micro>(std::chrono::steady_clock::now() - t0).count() / 10000;
100        CHECK(v == -10000);
101        CHECK(gpu_pause_request_line(gpu_pause_ms(none.c_str(), 1234), gpu_pause_ms(none.c_str(), 1234)).empty());
102        CHECK(gpu_pause_stall_line(gpu_pause_ms(none.c_str(), 1234), -1, -1).empty());
103        CHECK(us < 50.0);
104        std::printf("no KFD tree: %.2f us per read\n", us);
105        // the present tree with two GPUs, for comparison (the real sysfs read costs more: measured on p620)
106        const auto t1 = std::chrono::steady_clock::now();
107        for (int i = 0; i < 10000; ++i) v += gpu_pause_ms(r.c_str(), 3);
108        std::printf("fake tree, 2 GPUs: %.2f us per read\n",
109                    std::chrono::duration<double, std::micro>(std::chrono::steady_clock::now() - t1).count() / 10000);
110    }
111
112    const std::string rm = "rm -rf '" + r + "'";
113    if (std::system(rm.c_str()) != 0) std::fprintf(stderr, "could not remove %s\n", r.c_str());
114#else
115    CHECK(gpu_pause_ms("C:/nowhere", 1) == -1);
116    CHECK(gpu_pause_ms_self() == -1);
117#endif
118
119    // the stall report's line
120    CHECK(gpu_pause_stall_line(-1, 5, -1).empty());                  // not available: no line
121    CHECK(gpu_pause_stall_line(-1, -1, 7).empty());
122    CHECK(gpu_pause_stall_line(6123, 5169, -1) ==
123          "  GPU work paused by the graphics driver: 6123 ms since the engine started, 954 ms in this request (the AMD "
124          "driver pauses all of the engine's GPU work while Linux moves memory the GPU uses)");
125    CHECK(gpu_pause_stall_line(8073, 5169, 6123) ==
126          "  GPU work paused by the graphics driver: 8073 ms since the engine started, 2904 ms in this request, 1950 ms "
127          "in the last 2 s");
128    CHECK(gpu_pause_stall_line(40, -1, -1) ==                        // between requests: no request part
129          "  GPU work paused by the graphics driver: 40 ms since the engine started (the AMD driver pauses all of the "
130          "engine's GPU work while Linux moves memory the GPU uses)");
131    CHECK(gpu_pause_stall_line(40, -1, 40) ==
132          "  GPU work paused by the graphics driver: 40 ms since the engine started, 0 ms in the last 2 s");
133
134    // the request line: only at or above the threshold, and never from an unread or a reset counter
135    CHECK(kGpuPauseReportMs == 100);
136    CHECK(gpu_pause_request_line(-1, 900).empty());
137    CHECK(gpu_pause_request_line(100, -1).empty());
138    CHECK(gpu_pause_request_line(100, 50).empty());
139    CHECK(gpu_pause_request_line(1000, 1015).empty());               // the usual 15 ms: no line
140    CHECK(gpu_pause_request_line(1000, 1099).empty());
141    CHECK(gpu_pause_request_line(1000, 1100) ==
142          "strata serve: the graphics driver paused the GPU for 100 ms during this request while Linux moved memory "
143          "the GPU uses; the request was slower by up to that much");
144    CHECK(gpu_pause_request_line(15, 969) ==
145          "strata serve: the graphics driver paused the GPU for 954 ms during this request while Linux moved memory "
146          "the GPU uses; the request was slower by up to that much");
147
148    // the gap line: the same bar, from READY or the end of the previous request to the next request's begin
149    CHECK(gpu_pause_between_line(-1, 900).empty());                  // never idle yet / the read failed
150    CHECK(gpu_pause_between_line(100, -1).empty());
151    CHECK(gpu_pause_between_line(100, 50).empty());                  // a counter that went back (a new process): no line
152    CHECK(gpu_pause_between_line(1000, 1099).empty());
153    CHECK(gpu_pause_between_line(1000, 1100) ==
154          "strata serve: the graphics driver paused the GPU for 100 ms between requests while Linux moved memory the GPU uses");
155
156    // the bookkeeping over a sequence, as the request loop calls it
157    {
158        GpuPauseIdle idle;
159        CHECK(idle.begin(500).empty());                              // before READY was read: nothing to compare with
160        idle.idle(500);                                              // READY
161        CHECK(idle.begin(560).empty());                              // 60 ms before the first request: below the bar
162        idle.idle(900);                                              // the first request ended (it paused 340 ms inside: its own line)
163        CHECK(idle.begin(900).empty());                              // the next one began at once: no gap
164        idle.idle(950);                                              // it ended
165        CHECK(idle.begin(1450) ==                                    // 500 ms of driver pause while idle
166              "strata serve: the graphics driver paused the GPU for 500 ms between requests while Linux moved memory the GPU uses");
167        CHECK(idle.begin(1450) == idle.begin(1450));                 // begin() does not move the mark: only idle() does
168        idle.idle(-1);                                               // a failed read at an end: no gap is made from unrelated counters
169        CHECK(idle.begin(5000).empty());
170        idle.idle(5000);
171        CHECK(idle.begin(5099).empty());
172        CHECK(!idle.begin(5100).empty());
173    }
174
175    if (failed) { std::fprintf(stderr, "gpu_pause_time_test: %d failed\n", failed); return 1; }
176    std::printf("gpu_pause_time_test: ok\n");
177    return 0;
178}