okami.mom/strata

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

Niko1221Merge f-027-gh-eng: GitHub engine items for 0.2.76ca60e9a0

main
5.1 KiB96 linesraw
1// tests/program/gpu_report_test.cpp - #3797: the out-of-memory report at start (pure text; the NVML query is only
2// checked to be safe to call: it must return, not throw, and leave the list empty when it answers false).
3#include "strata/program/gpu_report.hpp"
4
5#include <cstdio>
6#include <string>
7
8using namespace strata::program::gpu_report;
9
10namespace {
11int g_fail = 0;
12void check(bool ok, const char* what, const std::string& got = {}) {
13    if (ok) return;
14    std::fprintf(stderr, "FAIL: %s%s%s\n", what, got.empty() ? "" : "\n  got: ", got.c_str());
15    ++g_fail;
16}
17bool has(const std::string& s, const char* part) { return s.find(part) != std::string::npos; }
18}  // namespace
19
20int main() {
21    {   // the case of #3797: 320 MiB free of a 22 GB card, another program holds the rest
22        Card c;
23        c.index = 1;
24        c.name = "NVIDIA GeForce RTX 2080 Ti";
25        c.free_mib = 320;
26        c.total_mib = 21998;
27        c.processes_known = true;
28        c.processes = {{4242, "/usr/bin/python3.12", 20100}, {777, "/opt/strata/strata", 900}, {1, "Xorg", 12}, {5, "", 0}};
29        const std::string r = format_gpu_memory_report({c}, 777);
30        check(has(r, "CUDA1 (NVIDIA GeForce RTX 2080 Ti): 320 MiB free of 21998 MiB"), "free and total", r);
31        check(has(r, "python3.12 (pid 4242) 19.6 GiB"), "the largest other program, by file name", r);
32        check(!has(r, "strata (pid 777)"), "this process is not listed as another program", r);
33        check(has(r, "Xorg (pid 1) 12 MiB"), "a small one is still named", r);
34        check(has(r, "1 program whose memory the driver does not report"), "a process of unknown size is counted", r);
35        check(r.find("python3.12") < r.find("Xorg"), "largest first", r);
36    }
37    {   // more than three: the rest is counted
38        Card c;
39        c.index = 0;
40        c.free_mib = 100;
41        c.total_mib = 12288;
42        c.processes_known = true;
43        for (uint32_t i = 1; i <= 6; ++i) c.processes.push_back({i, "p" + std::to_string(i), 100u * i});
44        const std::string r = format_gpu_memory_report({c}, 0);
45        check(has(r, "p6 (pid 6) 600 MiB, p5 (pid 5) 500 MiB, p4 (pid 4) 400 MiB, and 3 more"), "three named, three counted", r);
46        check(!has(r, "p3 (pid 3)"), "the fourth is not named", r);
47    }
48    {   // the driver would not say: no claim about other programs
49        Card c;
50        c.index = 0;
51        c.free_mib = 512;
52        c.total_mib = 8192;
53        c.processes_known = false;
54        const std::string r = format_gpu_memory_report({c}, 1);
55        check(has(r, "512 MiB free of 8192 MiB; the driver does not say which programs use it here"), "unknown processes", r);
56        check(!has(r, "other programs on it"), "no list", r);
57    }
58    {   // the driver answered with nothing but ourselves: said carefully (Windows often lists no program at all)
59        Card c;
60        c.index = 0;
61        c.free_mib = 700;
62        c.total_mib = 12288;
63        c.processes_known = true;
64        c.processes = {{99, "strata.exe", 11000}};
65        const std::string r = format_gpu_memory_report({c}, 99);
66        check(has(r, "the driver lists no other program using it"), "only ourselves", r);
67    }
68    {   // two cards, a pid with no name, a Windows path
69        Card a, b;
70        a.index = 0; a.free_mib = 2000; a.total_mib = 16000; a.processes_known = true;
71        b.index = 1; b.free_mib = 50; b.total_mib = 12000; b.processes_known = true;
72        b.processes = {{31, "", 11000}, {32, "C:\\Program Files\\Obs\\obs64.exe", 300}};
73        const std::string r = format_gpu_memory_report({a, b}, 5);
74        check(has(r, "CUDA0: 2000 MiB free of 16000 MiB; the driver lists no other program using it\n"), "first card line", r);
75        check(has(r, "CUDA1: 50 MiB free of 12000 MiB; other programs on it: pid 31 10.7 GiB, obs64.exe (pid 32) 300 MiB"), "second card line", r);
76    }
77    check(format_gpu_memory_report({}, 1).empty(), "no cards, no text");
78    {   // planned versus free
79        check(planned_vs_free_text(796, 68) ==
80                  "the layer split planned to keep 796 MiB free here after loading; 68 MiB are free, so 728 MiB went to something the plan did not count",
81              "planned versus free", planned_vs_free_text(796, 68));
82        check(planned_vs_free_text(796, 796).empty() && planned_vs_free_text(796, 900).empty(), "enough free: nothing to say");
83        check(planned_vs_free_text(0, 10).empty() && planned_vs_free_text(-5, 10).empty(), "no plan, no text");
84        check(has(planned_vs_free_text(300, -4), "0 MiB are free, so 300 MiB"), "a negative free count is read as 0");
85    }
86    {   // the query never throws and never leaves a half-filled list behind a false
87        std::vector<Process> p{{1, "x", 1}};
88        const bool ok = query_other_processes("", p);
89        check(!ok && p.empty(), "an empty bus id is refused");
90        std::vector<Process> q{{1, "x", 1}};
91        const bool r = query_other_processes("ffff:ff:ff.f", q);   // no such card: false, or true with a list; both are fine
92        check(r || q.empty(), "a failed query leaves the list empty");
93    }
94    std::printf(g_fail ? "FAIL\n" : "PASS\n");
95    return g_fail ? 1 : 0;
96}