okami.mom/strata

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

Niko1221rc1 merge split (1ba231217)fe57a0cb5

main
16.0 KiB280 linesraw
1// src/card_check.hpp and src/peer_probe.hpp's CPU side: a card's compute capability against the build's arch;
2// CUDA's and nvidia-smi's PCI addresses in sysfs's spelling, BAR1's size from real cards'
3// /sys/bus/pci/devices/<bdf>/resource text, the text it refuses, and the BAR1-against-memory check the engine makes as
4// it loads: prod's V100s pass, an RTX 3090 with a pre-ReBAR VBIOS (256 MiB) is refused, one at full ReBAR passes; and
5// the start-up link probe's report and failure message. Exits 1 after reporting every difference.
6//   bin/card_check_test
7#include <cstdio>
8#include <stdexcept>
9#include <string>
10#include <utility>
11#include <vector>
12
13#include "../src/card_check.hpp"
14#include "../src/model.hpp"
15#include "../src/peer_probe.hpp"
16
17namespace {
18
19int fails = 0;
20void fail(const std::string& what) {
21    std::printf("FAIL: %s\n", what.c_str());
22    ++fails;
23}
24
25// prod's card 0 (0000:3b:00.0), a Tesla V100-PCIE-32GB: the first four lines of its resource file
26const char* V100 =
27    "0x00000000ab000000 0x00000000abffffff 0x0000000000040200\n"
28    "0x00000cb000000000 0x00000cb7ffffffff 0x000000000014220c\n"
29    "0x0000000000000000 0x0000000000000000 0x0000000000000000\n"
30    "0x00000cb800000000 0x00000cb801ffffff 0x000000000014220c\n";
31// read from an RTX 3090 in a desktop of this box's owner (0000:01:00.0, driver 580), whose VBIOS offers no Resizable
32// BAR: BAR1 256 MiB. Not the tester's card, a look-alike of it: theirs had the same 256 MiB BAR1 under a pre-ReBAR
33// VBIOS and hung their ring. The whole file
34const char* RTX3090_PRE_REBAR =
35    "0x00000000de000000 0x00000000deffffff 0x0000000000040200\n"
36    "0x00000000c0000000 0x00000000cfffffff 0x000000000014220c\n"
37    "0x0000000000000000 0x0000000000000000 0x0000000000000000\n"
38    "0x00000000d0000000 0x00000000d1ffffff 0x000000000014220c\n"
39    "0x0000000000000000 0x0000000000000000 0x0000000000000000\n"
40    "0x000000000000e000 0x000000000000e07f 0x0000000000040101\n"
41    "0x00000000000c0000 0x00000000000dffff 0x0000000000000212\n"
42    "0x0000000000000000 0x0000000000000000 0x0000000000000000\n"
43    "0x0000000000000000 0x0000000000000000 0x0000000000000000\n"
44    "0x0000000000000000 0x0000000000000000 0x0000000000000000\n"
45    "0x0000000000000000 0x0000000000000000 0x0000000000000000\n"
46    "0x0000000000000000 0x0000000000000000 0x0000000000000000\n"
47    "0x0000000000000000 0x0000000000000000 0x0000000000000000\n";
48// the same card with BAR1 at 32 GiB, as the tester's cards were after a ReBAR VBIOS: made from the lines above, not
49// read from a card
50const char* RTX3090_REBAR =
51    "0x00000000de000000 0x00000000deffffff 0x0000000000040200\n"
52    "0x0000007800000000 0x0000007fffffffff 0x000000000014220c\n"
53    "0x0000000000000000 0x0000000000000000 0x0000000000000000\n";
54
55constexpr unsigned long long MiB = 1ull << 20, GiB = 1ull << 30;
56// nvidia-smi's memory.total on these cards: at least CUDA's totalGlobalMem, so the hardest case for a pass
57constexpr unsigned long long V100_VRAM = 32768 * MiB, RTX3090_VRAM = 24576 * MiB;
58
59void bdf(const std::string& in, const std::string& want) {
60    try {
61        const std::string got = fnx::sysfs_bdf(in);
62        if (got != want) fail("sysfs_bdf('" + in + "') = '" + got + "', want '" + want + "'");
63    } catch (const std::exception& e) {
64        fail("sysfs_bdf('" + in + "') threw: " + e.what());
65    }
66}
67void bdf_refused(const std::string& in) {
68    try {
69        fail("sysfs_bdf('" + in + "') = '" + fnx::sysfs_bdf(in) + "', want it refused");
70    } catch (const std::runtime_error&) {
71    }
72}
73void bar1(const char* name, const std::string& text, unsigned long long want) {
74    try {
75        const unsigned long long got = fnx::bar1_bytes(text, name);
76        if (got != want) fail(std::string(name) + ": BAR1 " + std::to_string(got) + " bytes, want " + std::to_string(want));
77    } catch (const std::exception& e) {
78        fail(std::string(name) + " threw: " + e.what());
79    }
80}
81// text bar1_bytes must refuse, with an error that says `says`
82void bar1_refused(const char* name, const std::string& text, const std::string& says) {
83    try {
84        fail(std::string(name) + ": BAR1 " + std::to_string(fnx::bar1_bytes(text, name)) + " bytes, want it refused");
85    } catch (const std::runtime_error& e) {
86        if (std::string(e.what()).find(says) == std::string::npos)
87            fail(std::string(name) + ": refused with '" + e.what() + "', want it to say '" + says + "'");
88    }
89}
90void covers(const char* name, unsigned long long bar, unsigned long long vram, bool ok) {
91    try {
92        fnx::require_bar1_covers(0, "0000:01:00.0", name, bar, vram, 3);
93        if (!ok) fail(std::string(name) + ": BAR1 " + std::to_string(bar >> 20) + " MiB for " + std::to_string(vram >> 20) + " MiB passed");
94    } catch (const std::runtime_error& e) {
95        const std::string msg = e.what();
96        if (ok) fail(std::string(name) + ": refused: " + msg);
97        else if (msg.find(std::to_string(bar >> 20) + " MiB BAR1") == std::string::npos || msg.find("FNX_SKIP_BAR1_CHECK") == std::string::npos)
98            fail(std::string(name) + ": the refusal does not name the BAR1 size and the skip: " + msg);
99    }
100}
101
102// require_card_arch on a card (major.minor) against a build (arch); refused when `says` is not empty, saying it
103void arch(const char* name, int major, int minor, int build, const std::string& says) {
104    try {
105        fnx::require_card_arch(0, name, major, minor, build);
106        if (!says.empty()) fail(std::string(name) + " on an sm_" + std::to_string(build) + " build passed, want it refused");
107    } catch (const std::runtime_error& e) {
108        if (says.empty()) fail(std::string(name) + " on an sm_" + std::to_string(build) + " build refused: " + e.what());
109        else if (std::string(e.what()).find(says) == std::string::npos)
110            fail(std::string(name) + ": refused with '" + e.what() + "', want it to say '" + says + "'");
111    }
112}
113
114// peer_report on rates (from, to, us) for ncard cards: want lines in all, each `says` found in the line it indexes
115void report(const char* name, const std::vector<fnx::PeerRate>& r, int ncard, size_t lines,
116            const std::vector<std::pair<size_t, std::string>>& says) {
117    try {
118        const std::vector<std::string> got = fnx::peer_report(r, ncard);
119        if (got.size() != lines) fail(std::string(name) + ": " + std::to_string(got.size()) + " lines, want " + std::to_string(lines));
120        for (const auto& [i, text] : says)
121            if (i >= got.size() || got[i].find(text) == std::string::npos)
122                fail(std::string(name) + ": line " + std::to_string(i) + " does not say '" + text + "'" + (i < got.size() ? ": " + got[i] : ""));
123    } catch (const std::exception& e) {
124        fail(std::string(name) + " threw: " + e.what());
125    }
126}
127// peer_report must refuse, with an error that says `says`
128void report_refused(const char* name, const std::vector<fnx::PeerRate>& r, int ncard, const std::string& says) {
129    try {
130        fnx::peer_report(r, ncard);
131        fail(std::string(name) + ": reported, want it refused");
132    } catch (const std::logic_error& e) {
133        if (std::string(e.what()).find(says) == std::string::npos)
134            fail(std::string(name) + ": refused with '" + e.what() + "', want it to say '" + says + "'");
135    }
136}
137// a failure to report, its fields by name so no two numbers can trade places
138fnx::PeerFailure failure(int from, int to, int ncard, bool copy, unsigned missing, size_t words, unsigned long long timeout_ms) {
139    fnx::PeerFailure f{};
140    f.from = from;
141    f.to = to;
142    f.ncard = ncard;
143    f.copy = copy;
144    f.missing = missing;
145    f.words = words;
146    f.timeout_ms = timeout_ms;
147    return f;
148}
149
150}  // namespace
151
152int main() {
153    arch("Tesla V100-PCIE-32GB", 7, 0, 70, "");
154    arch("Tesla PG500-216", 7, 0, 70, "");                     // 72 SMs: the hc read walks its 80 blocks of work on them
155    arch("NVIDIA GeForce RTX 3090", 8, 6, 86, "");
156    arch("NVIDIA GeForce RTX 3090", 8, 6, 70, "make ARCH=86");    // prod's build on a 3090: the PTX compiled at load
157    arch("Tesla V100-PCIE-32GB", 7, 0, 86, "make ARCH=70");
158    arch("NVIDIA GeForce RTX 4090", 8, 9, 86, "make ARCH=89");
159    arch("NVIDIA A100-PCIE-40GB", 8, 0, 86, "make ARCH=80");
160    arch("NVIDIA GeForce RTX 4090", 8, 9, 89, "");             // a build for its arch: refused only by what else it lacks
161    arch("NVIDIA GeForce RTX 3080", 8, 6, 86, "");             // 68 SMs
162
163    bdf("0000:3B:00.0", "0000:3b:00.0");        // cudaDeviceGetPCIBusId
164    bdf("00000000:3B:00.0", "0000:3b:00.0");    // nvidia-smi
165    bdf("0000:d8:00.0", "0000:d8:00.0");
166    bdf("0001:01:1f.7", "0001:01:1f.7");
167    bdf_refused("");
168    bdf_refused("3B:00.0");
169    bdf_refused("0000:3B:00");
170    bdf_refused("0000:3B:00.0x");
171    bdf_refused("10000:3B:00.0");
172    bdf_refused("0000:3B:20.0");   // device numbers end at 0x1f
173    bdf_refused("0000:3B:00.8");   // functions at 7
174    // what sscanf's %x took: a sign, a value too wide for the field, a 0x, a space
175    bdf_refused("0000:-ffffffffffffffff:00.0");
176    bdf_refused("100000001:3b:00.0");
177    bdf_refused("0000:0x3b:00.0");
178    bdf_refused("0000: 3b:00.0");
179    bdf_refused("0000:+3b:00.0");
180    bdf_refused("0000:100:00.0");   // a bus number is two digits
181    {
182        unsigned long long v = 0;
183        if (fnx::parse_hex("1", 17, true, v)) fail("parse_hex took a width of 17 digits, past what 64 bits hold");
184    }
185
186    bar1("V100", V100, 32 * GiB);
187    bar1("RTX 3090, pre-ReBAR", RTX3090_PRE_REBAR, 256 * MiB);
188    bar1("RTX 3090, ReBAR", RTX3090_REBAR, 32 * GiB);
189    bar1("no 0x, upper case", "0 ff 40200\nC0000000 CFFFFFFF 14220C\n", 256 * MiB);
190    bar1("no newline at the end", "0x0 0xff 0x40200\n0x0000000100000000 0x00000001ffffffff 0x14220c", 4 * GiB);
191
192    bar1_refused("empty", "", "0 lines");
193    bar1_refused("BAR0 alone", "0x00000000de000000 0x00000000deffffff 0x0000000000040200\n", "1 lines");
194    bar1_refused("BAR1 unassigned", "0x00000000de000000 0x00000000deffffff 0x0000000000040200\n"
195                                    "0x0000000000000000 0x0000000000000000 0x0000000000000000\n", "no address");
196    bar1_refused("BAR1 I/O ports", "0x0 0xff 0x40200\n0x000000000000e000 0x000000000000e07f 0x0000000000040101\n", "not a memory range");
197    bar1_refused("BAR1 backwards", "0x0 0xff 0x40200\n0x00000000d0000000 0x00000000c0000000 0x000000000014220c\n", "not a memory range");
198    bar1_refused("two fields", "0x0 0xff 0x40200\n0x00000000c0000000 0x00000000cfffffff\n", "cannot read BAR1");
199    bar1_refused("four fields", "0x0 0xff 0x40200\n0x00000000c0000000 0x00000000cfffffff 0x14220c 0x1\n", "cannot read BAR1");
200    bar1_refused("not hex", "0x0 0xff 0x40200\nBAR1 at c0000000\n", "cannot read BAR1");
201    // what sscanf's %llx took: signs (this one read as exactly 24 GiB and passed), 17 digits, a range from 0 that
202    // wraps the size to 0
203    bar1_refused("signed", "0x0 0xff 0x40200\n-0x600000000 -0x1 0x14220c\n", "cannot read BAR1");
204    // without 0x a sign-permissive parser reads this as a plausible 4 GiB BAR1
205    bar1_refused("signed, no 0x", "0x0 0xff 0x40200\n-1 -ffffffff 14220c\n", "cannot read BAR1");
206    bar1_refused("17 digits", "0x0 0xff 0x40200\n0x10000000000000000 0x1ffffffffffffffff 0x14220c\n", "cannot read BAR1");
207    bar1_refused("all of memory from 0", "0x0 0xff 0x40200\n0x0 0xffffffffffffffff 0x14220c\n", "no address");
208
209    covers("Tesla V100-PCIE-32GB", 32 * GiB, V100_VRAM, true);
210    covers("NVIDIA GeForce RTX 3090, pre-ReBAR", 256 * MiB, RTX3090_VRAM, false);
211    covers("NVIDIA GeForce RTX 3090, ReBAR", 32 * GiB, RTX3090_VRAM, true);
212    covers("a byte short", 24 * GiB - 1, 24 * GiB, false);
213    covers("exactly", 24 * GiB, 24 * GiB, true);
214
215    // the start-up link probe's report: ~47 us per 512 KiB as a tester's 3090s measured (tools/p2p_store.cu); prod's
216    // V100s take 68-97 us
217    const std::vector<fnx::PeerRate> three = {{0, 1, 47}, {0, 2, 47}, {1, 0, 47}, {1, 2, 47}, {2, 0, 47}, {2, 1, 47}};
218    report("three cards, all ~11 GB/s", three, 3, 1, {{0, "kernel stores between the cards: 6 pairs, 11.2 to 11.2 GB/s (512 KiB a pair)"}});
219    std::vector<fnx::PeerRate> one_slow = three;
220    one_slow[3].us = 1310;   // 0.40 GB/s, as a link far down its lanes might
221    report("one slow pair", one_slow, 3, 2, {{0, "6 pairs, 0.4 to 11.2 GB/s"}, {1, "card 1 -> card 2 stores at 0.40 GB/s"}, {1, "the engine runs"}});
222    std::vector<fnx::PeerRate> at_floor = three;
223    at_floor[0].us = (double) fnx::PEER_PROBE_BYTES / (fnx::PEER_SLOW_GBS * 1e3);   // exactly the floor: not slow
224    report("a pair at the floor", at_floor, 3, 1, {});
225    report("two cards", {{0, 1, 47}, {1, 0, 47}}, 2, 1, {{0, "2 pairs"}});
226    report_refused("no pairs", {}, 3, "0 pairs probed for 3 cards");
227    report_refused("a pair left out", {three.begin(), three.end() - 1}, 3, "5 pairs probed for 3 cards");
228    report_refused("one card", {}, 1, "for 1 cards");
229    report_refused("a count no card number makes", three, 65536, "6 pairs probed for 65536 cards");
230    std::vector<fnx::PeerRate> no_time = three;
231    no_time[2].us = 0;
232    report_refused("a pair that took no time", no_time, 3, "took no time");
233    // the pair checks, each wrong pair in the last slot so no pair after it can be what refuses
234    const auto last_is = [&](fnx::PeerRate p) {
235        std::vector<fnx::PeerRate> r = three;
236        r.back() = p;
237        return r;
238    };
239    report_refused("a pair twice, another missing", last_is({0, 1, 47}), 3, "card 0 -> 1 is not a new pair");
240    report_refused("a card to itself", last_is({1, 1, 47}), 3, "card 1 -> 1 is not a new pair");
241    report_refused("a card past the count", last_is({3, 0, 47}), 3, "card 3 -> 0 is not a new pair");
242    report_refused("a card past the count, receiving", last_is({0, 3, 47}), 3, "card 0 -> 3 is not a new pair");
243    report_refused("a negative card", last_is({-1, 0, 47}), 3, "card -1 -> 0 is not a new pair");
244    report_refused("a negative card, receiving", last_is({2, -1, 47}), 3, "card 2 -> -1 is not a new pair");
245    for (const fnx::PeerFailure& bad : {failure(1, 2, 3, false, 4096, 65536, 0), failure(1, 2, 3, false, 0, 65536, 1000),
246                                        failure(1, 2, 3, false, 65537, 65536, 1000)})
247        try {
248            fail("peer_probe_failure reported " + std::to_string(bad.missing) + " of " + std::to_string(bad.words) + " in " +
249                 std::to_string(bad.timeout_ms) + " ms: " + fnx::peer_probe_failure(bad));
250        } catch (const std::logic_error& e) {
251            if (std::string(e.what()).find("nothing to report") == std::string::npos) fail(std::string("peer_probe_failure refused with: ") + e.what());
252        }
253    {
254        const std::string stores = fnx::peer_probe_failure(failure(1, 2, 3, false, 4096, 65536, 1000));
255        const std::string copy = fnx::peer_probe_failure(failure(0, 2, 3, true, 1, 65536, 1000));
256        for (const char* want : {"card 1 -> card 2", "4096 of 65536 words stored by a kernel", "within 1000 ms", "under 0.5 MB/s",
257                                 "leave card 1 or card 2 out of the config's \"gpu\"", "build for 2 (make FNX_NCARD=2)"})
258            if (stores.find(want) == std::string::npos) fail("peer_probe_failure does not say '" + std::string(want) + "': " + stores);
259        if (copy.find("1 of 65536 words copied by the copy engines from card 0") == std::string::npos)
260            fail("peer_probe_failure for a copy does not name it: " + copy);
261    }
262    // the remedy on every count: from 2 there is no smaller build; from 6 and 8 the next one leaves out two cards
263    const std::pair<int, const char*> remedies[] = {
264        {2, "replace card 1: a two-card build has no smaller count"},
265        {3, "build for 2 (make FNX_NCARD=2)"},
266        {4, "build for 3 (make FNX_NCARD=3)"},
267        {6, "build for 4 (make FNX_NCARD=4): 4 is the next count below 6, so leave out 2 cards in all"},
268        {8, "build for 6 (make FNX_NCARD=6): 6 is the next count below 8, so leave out 2 cards in all"}};
269    for (const auto& [n, want] : remedies) {
270        const std::string got = fnx::leave_card_out("card 1", n);
271        if (got.find(want) == std::string::npos) fail("leave_card_out on " + std::to_string(n) + " cards: '" + got + "', want '" + want + "'");
272    }
273
274    if (fails) {
275        std::printf("card_check_test: %d failed\n", fails);
276        return 1;
277    }
278    std::printf("card_check_test: ok\n");
279    return 0;
280}