diff options
Diffstat (limited to 'runtime/test/dev_overlay_test.cpp')
| -rw-r--r-- | runtime/test/dev_overlay_test.cpp | 166 |
1 files changed, 166 insertions, 0 deletions
diff --git a/runtime/test/dev_overlay_test.cpp b/runtime/test/dev_overlay_test.cpp new file mode 100644 index 000000000..55a5a31cc --- /dev/null +++ b/runtime/test/dev_overlay_test.cpp | |||
| @@ -0,0 +1,166 @@ | |||
| 1 | // src/dev_overlay.h: the text of a sample, the counter differences, the phase bookkeeping behind its "now" line | ||
| 2 | // (submit_phases.h), the pipeline builds in flight (dev_status.h) and the memory figures it shares with the title HUD | ||
| 3 | // (memory_figures.h). No window, GPU or game file. | ||
| 4 | #include "dev_overlay.h" | ||
| 5 | |||
| 6 | #include <chrono> | ||
| 7 | #include <cstdint> | ||
| 8 | #include <cstdio> | ||
| 9 | #include <string> | ||
| 10 | #include <thread> | ||
| 11 | #include <vector> | ||
| 12 | |||
| 13 | #define CHECK(condition) do { \ | ||
| 14 | if (!(condition)) { \ | ||
| 15 | std::fprintf(stderr, "line %d: %s\n", __LINE__, #condition); \ | ||
| 16 | return 1; \ | ||
| 17 | } \ | ||
| 18 | } while (0) | ||
| 19 | |||
| 20 | namespace sp = leonida::submit_phases; | ||
| 21 | namespace dov = leonida::dev_overlay; | ||
| 22 | |||
| 23 | static bool has(const std::vector<std::string>& lines, const char* part) { | ||
| 24 | for (const auto& line : lines) | ||
| 25 | if (line.find(part) != std::string::npos) return true; | ||
| 26 | return false; | ||
| 27 | } | ||
| 28 | |||
| 29 | // memory_figures.h, which the overlay and the title HUD share: the private bytes Wine reports, the text of both | ||
| 30 | // figures with and without one, and the device's published GPU memory, read at most once per refresh. | ||
| 31 | static int check_memory_figures() { | ||
| 32 | namespace mf = leonida::memory_figures; | ||
| 33 | constexpr std::uint64_t kGiB = 1ull << 30; | ||
| 34 | |||
| 35 | // Wine fills PagefileUsage and leaves PrivateUsage 0; Windows fills both with the same value. | ||
| 36 | mf::ProcessMemory ram; | ||
| 37 | CHECK(mf::ram_text(ram) == "RAM n/a"); // GetProcessMemoryInfo failed | ||
| 38 | ram.read = true; | ||
| 39 | ram.working_set = kGiB; | ||
| 40 | CHECK(mf::ram_text(ram) == "RAM n/a (ws 1.0)"); | ||
| 41 | ram.pagefile_usage = 5 * kGiB / 2; | ||
| 42 | CHECK(mf::private_bytes(ram) == 5 * kGiB / 2 && mf::ram_text(ram) == "RAM 2.5 GB (ws 1.0)"); | ||
| 43 | ram.private_usage = 5 * kGiB / 2; | ||
| 44 | CHECK(mf::private_bytes(ram) == 5 * kGiB / 2 && mf::ram_text(ram) == "RAM 2.5 GB (ws 1.0)"); | ||
| 45 | |||
| 46 | leonida::VramBudget vram; | ||
| 47 | CHECK(mf::vram_text(vram) == "VRAM n/a"); | ||
| 48 | vram.available = true; | ||
| 49 | vram.local_usage = 3 * kGiB + kGiB / 2; | ||
| 50 | vram.local_budget = 16 * kGiB; | ||
| 51 | vram.shared_usage = kGiB / 2; | ||
| 52 | CHECK(mf::vram_text(vram) == "VRAM 3.5/16.0 GB +0.5 shared"); | ||
| 53 | |||
| 54 | // The overlay's line with both figures. | ||
| 55 | dov::Sample s; | ||
| 56 | s.ram = ram; | ||
| 57 | s.vram = vram; | ||
| 58 | CHECK(has(dov::lines(s, 1), "RAM 2.5 GB (ws 1.0) VRAM 3.5/16.0 GB +0.5 shared")); | ||
| 59 | |||
| 60 | // The published figure: none until a device publishes a reader; then read at most once per refresh_ms; another | ||
| 61 | // device's withdrawal leaves it; its own takes it away. | ||
| 62 | struct Fake { | ||
| 63 | int reads = 0; | ||
| 64 | leonida::VramBudget figure; | ||
| 65 | }; | ||
| 66 | static Fake device; | ||
| 67 | static const int other = 0; | ||
| 68 | device.figure = vram; | ||
| 69 | auto& publisher = mf::vram_publisher(); | ||
| 70 | using clock = std::chrono::steady_clock; | ||
| 71 | const clock::time_point t0 = clock::now(); | ||
| 72 | CHECK(!publisher.latest(t0).available); | ||
| 73 | mf::publish_vram_reader( | ||
| 74 | [](const void* context) { | ||
| 75 | auto* fake = static_cast<Fake*>(const_cast<void*>(context)); | ||
| 76 | ++fake->reads; | ||
| 77 | return fake->figure; | ||
| 78 | }, | ||
| 79 | &device); | ||
| 80 | CHECK(publisher.latest(t0).local_usage == 3 * kGiB + kGiB / 2 && device.reads == 1); | ||
| 81 | device.figure.local_usage = 4 * kGiB; | ||
| 82 | CHECK(publisher.latest(t0 + mf::refresh_ms / 2).local_usage == 3 * kGiB + kGiB / 2 && device.reads == 1); | ||
| 83 | CHECK(publisher.latest(t0 + mf::refresh_ms).local_usage == 4 * kGiB && device.reads == 2); | ||
| 84 | mf::withdraw_vram_reader(&other); | ||
| 85 | CHECK(mf::latest_vram().available); | ||
| 86 | mf::withdraw_vram_reader(&device); | ||
| 87 | CHECK(!mf::latest_vram().available); | ||
| 88 | return 0; | ||
| 89 | } | ||
| 90 | |||
| 91 | int main() { | ||
| 92 | // A total that went down was reset by its log line: what it holds is the new part. | ||
| 93 | std::uint64_t before = 0; | ||
| 94 | CHECK(dov::grown(10, before) == 10 && before == 10); | ||
| 95 | CHECK(dov::grown(25, before) == 15); | ||
| 96 | CHECK(dov::grown(4, before) == 4 && before == 4); | ||
| 97 | |||
| 98 | // Nested scopes: the inner one is running, then the outer one again, then none. | ||
| 99 | CHECK(sp::running().phase == -1); | ||
| 100 | { | ||
| 101 | const sp::Scope replay(sp::replay); | ||
| 102 | CHECK(sp::running().phase == sp::replay); | ||
| 103 | { | ||
| 104 | const sp::Scope textures(sp::textures); | ||
| 105 | CHECK(sp::running().phase == sp::textures); | ||
| 106 | } | ||
| 107 | CHECK(sp::running().phase == sp::replay); | ||
| 108 | } | ||
| 109 | CHECK(sp::running().phase == -1); | ||
| 110 | |||
| 111 | // Two threads whose scopes overlap and end in the order they began: no phase stays behind. | ||
| 112 | { | ||
| 113 | std::atomic<int> step{0}; | ||
| 114 | std::thread a([&] { | ||
| 115 | const sp::Scope heaps(sp::heaps); | ||
| 116 | step = 1; | ||
| 117 | while (step.load() < 2) std::this_thread::yield(); | ||
| 118 | }); | ||
| 119 | std::thread b([&] { | ||
| 120 | while (step.load() < 1) std::this_thread::yield(); | ||
| 121 | const sp::Scope shadows(sp::shadows); | ||
| 122 | step = 2; | ||
| 123 | while (step.load() < 3) std::this_thread::yield(); | ||
| 124 | }); | ||
| 125 | a.join(); | ||
| 126 | CHECK(sp::running().phase == sp::shadows); | ||
| 127 | step = 3; | ||
| 128 | b.join(); | ||
| 129 | CHECK(sp::running().phase == -1); | ||
| 130 | } | ||
| 131 | |||
| 132 | // The builds in flight and the last one's name, cut to its buffer. | ||
| 133 | auto& builds = leonida::dev_status::builds(); | ||
| 134 | const auto done = builds.done.load(); | ||
| 135 | { | ||
| 136 | const leonida::dev_status::Build one("one"); | ||
| 137 | const leonida::dev_status::Build two(std::string(500, 'x').c_str()); | ||
| 138 | CHECK(builds.active.load() == 2 && builds.last() == std::string(95, 'x')); | ||
| 139 | } | ||
| 140 | CHECK(builds.active.load() == 0 && builds.done.load() == done + 2); | ||
| 141 | |||
| 142 | dov::Sample s; | ||
| 143 | auto text = dov::lines(s, 12); | ||
| 144 | CHECK(text.size() == 7); | ||
| 145 | // No counters and no device figure: "n/a", never a false 0. | ||
| 146 | CHECK(has(text, "RAM n/a VRAM n/a")); | ||
| 147 | CHECK(has(text, "12 s") && has(text, "now: the title's own code") && has(text, "pipelines: 0 built")); | ||
| 148 | CHECK(!has(text, "building")); | ||
| 149 | s.frames = 8.44; | ||
| 150 | s.phase = sp::textures; | ||
| 151 | s.phase_for = 3.9; | ||
| 152 | s.phase_ms[sp::shadows] = 2; | ||
| 153 | s.phase_ms[sp::geometry] = 1.5; | ||
| 154 | s.building = 3; | ||
| 155 | s.built = 853; | ||
| 156 | s.build = std::string(200, 'n'); // longer than the line's part for it | ||
| 157 | s.drawn = 3029; | ||
| 158 | s.skipped = 38363; | ||
| 159 | text = dov::lines(s, 147); | ||
| 160 | CHECK(has(text, "8.4 fps") && has(text, "now: refreshing textures for 3 s") && has(text, "copies 3.5")); | ||
| 161 | CHECK(has(text, "853 built, 3 building (nnn") && has(text, "3029 drawn, 38363 skipped")); | ||
| 162 | for (const auto& line : text) CHECK(line.size() < 160); | ||
| 163 | if (const int failed = check_memory_figures()) return failed; | ||
| 164 | std::puts("dev_overlay_test: ok"); | ||
| 165 | return 0; | ||
| 166 | } | ||