okami.mom/strata

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

Niko1221Merge main 6c642df75 into f-own-int (round 16: network waits, robustness, Windows kill-on-close job start, GitHub and transfer fixes)cd9745050

main
2.9 KiB68 linesraw
1// #5718 follow-up: the stall report names the GPU streams a host may be waiting on and says, from a non-blocking query,
2// whether their work is finished.  No GPU: the query is faked per stream; this checks the list (RAII, slots), the line
3// for each state, and that nothing is printed when no stream is watched.
4#include "strata/core/stream_watch.hpp"
5
6#include <cstdint>
7#include <cstdio>
8#include <cstring>
9#include <string>
10
11using namespace strata::core;
12
13namespace {
14int failed = 0;
15#define CHECK(x) do { if (!(x)) { std::fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #x); ++failed; } } while (0)
16
17std::string report_text() {
18    std::FILE* f = std::tmpfile();
19    if (!f) return "<no tmpfile>";
20    stream_watch().report(f, [](void* s, int, std::string& why) {
21        const auto id = (std::uintptr_t) s;
22        if (id == 1) return StreamState::finished;
23        if (id == 2) return StreamState::working;
24        why = "invalid resource handle";
25        return StreamState::failed;
26    });
27    std::string out;
28    std::rewind(f);
29    char buf[512];
30    while (std::fgets(buf, sizeof buf, f)) out += buf;
31    std::fclose(f);
32    return out;
33}
34}  // namespace
35
36int main() {
37    CHECK(report_text().empty());                                        // nothing watched: no line
38    {
39        WatchedStream a("the prompt's GPU work", (void*) 1, 0);
40        CHECK(stream_watch().count() == 1);
41        {
42            WatchedStream b("the prompt's GPU work", (void*) 2, 1);
43            WatchedStream c("the prompt's GPU work", (void*) 3, 0);
44            const std::string t = report_text();
45            std::printf("%s", t.c_str());
46            CHECK(t.find("  the prompt's GPU work (GPU 0) had finished, but the engine was still waiting for it\n")
47                  != std::string::npos);
48            CHECK(t.find("  the prompt's GPU work (GPU 1) was still running\n") != std::string::npos);
49            CHECK(t.find("  the state of the prompt's GPU work could not be read: invalid resource handle\n")
50                  != std::string::npos);            CHECK(stream_watch().count() == 3);
51        }
52        CHECK(stream_watch().count() == 1);                              // b and c left with their scopes
53        const std::string t = report_text();
54        CHECK(t.find("(GPU 1)") == std::string::npos);
55    }
56    CHECK(stream_watch().count() == 0);
57    CHECK(report_text().empty());
58    {   // all slots taken: the extra one is simply not listed, and nothing breaks when it goes
59        WatchedStream* many[StreamWatch::kSlots + 2];
60        for (auto& w : many) w = new WatchedStream("s", (void*) 1, 0);
61        CHECK(stream_watch().count() == StreamWatch::kSlots);
62        for (auto& w : many) delete w;
63        CHECK(stream_watch().count() == 0);
64    }
65    if (failed) { std::fprintf(stderr, "stream_watch_test: %d failed\n", failed); return 1; }
66    std::printf("stream_watch_test: ok\n");
67    return 0;
68}