diff options
| author | Tauwni <102470988+Tauwni@users.noreply.github.com> | 2026-11-26 12:19:12 +0100 |
|---|---|---|
| committer | Tauwni <102470988+Tauwni@users.noreply.github.com> | 2026-11-26 12:19:12 +0100 |
| commit | beffaa9a476ca1b3dfd9cfa55e39eafbb1f5eec6 (patch) | |
| tree | 32877805bf4eb46d9a89d5753db6b5a2b06dc845 /runtime/test/compile_helper_test.cpp | |
| parent | f29ca7bacae9507f83e8c56abdb604bd4efb4cf1 (diff) | |
| parent | a73a57e1339be072baf5522351c8ec7f1482b56b (diff) | |
Merge main into fix/null-descriptor-generated-dir
Diffstat (limited to 'runtime/test/compile_helper_test.cpp')
| -rw-r--r-- | runtime/test/compile_helper_test.cpp | 176 |
1 files changed, 176 insertions, 0 deletions
diff --git a/runtime/test/compile_helper_test.cpp b/runtime/test/compile_helper_test.cpp new file mode 100644 index 000000000..7ef8d176e --- /dev/null +++ b/runtime/test/compile_helper_test.cpp | |||
| @@ -0,0 +1,176 @@ | |||
| 1 | // The compile helper's file protocol (shims/d3d12_compile_helper.h): request lines, their order, and the markers that | ||
| 2 | // keep two processes from compiling one entry at once. No Vulkan, no helper process. | ||
| 3 | #include "../shims/d3d12_compile_helper.h" | ||
| 4 | |||
| 5 | #include <chrono> | ||
| 6 | #include <cstdio> | ||
| 7 | #include <filesystem> | ||
| 8 | #include <fstream> | ||
| 9 | #include <string> | ||
| 10 | #include <thread> | ||
| 11 | |||
| 12 | namespace fs = std::filesystem; | ||
| 13 | namespace helper = rt::d3d12::compile_helper; | ||
| 14 | |||
| 15 | #define CHECK(condition) do { \ | ||
| 16 | if (!(condition)) { \ | ||
| 17 | std::fprintf(stderr, "line %d: %s\n", __LINE__, #condition); \ | ||
| 18 | return 1; \ | ||
| 19 | } \ | ||
| 20 | } while (0) | ||
| 21 | |||
| 22 | static int test_lines() { | ||
| 23 | const auto line = helper::parse_request(helper::format_request("fspx_compute/lib_cs_00001_ab", 4096)); | ||
| 24 | CHECK(line.name == "fspx_compute/lib_cs_00001_ab" && line.priority == 4096); | ||
| 25 | const auto bare = helper::parse_request("name only"); | ||
| 26 | CHECK(bare.name == "name only" && bare.priority == 0); // an older writer's request is urgent | ||
| 27 | return 0; | ||
| 28 | } | ||
| 29 | |||
| 30 | static int test_paths() { | ||
| 31 | const fs::path file = fs::path("J") / "cs-0123456789abcdef.bin"; | ||
| 32 | CHECK(helper::request_path(file) == fs::path("J") / "requests" / "cs-0123456789abcdef.req"); | ||
| 33 | CHECK(helper::failed_path(file) == fs::path("J") / "requests" / "cs-0123456789abcdef.failed"); | ||
| 34 | CHECK(helper::building_path(file) == fs::path("J") / "requests" / "cs-0123456789abcdef.building"); | ||
| 35 | return 0; | ||
| 36 | } | ||
| 37 | |||
| 38 | static int test_order(const fs::path& dir) { | ||
| 39 | const auto requests = dir / "requests"; | ||
| 40 | fs::create_directories(requests); | ||
| 41 | const auto now = fs::file_time_type::clock::now(); | ||
| 42 | const auto put = [&](const char* stem, const std::string& line, std::chrono::seconds age) { | ||
| 43 | const auto path = requests / (std::string(stem) + ".req"); | ||
| 44 | std::ofstream(path, std::ios::binary) << line << "\n"; | ||
| 45 | fs::last_write_time(path, now - age); | ||
| 46 | }; | ||
| 47 | put("cs-0000000000000001", helper::format_request("warm-old", 500000), std::chrono::seconds(30)); | ||
| 48 | put("cs-0000000000000002", helper::format_request("dispatch-new", 0), std::chrono::seconds(1)); | ||
| 49 | put("cs-0000000000000003", helper::format_request("dispatch-old", 0), std::chrono::seconds(20)); | ||
| 50 | put("cs-0000000000000004", helper::format_request("warm-small", 1000), std::chrono::seconds(5)); | ||
| 51 | std::ofstream(requests / "cs-0000000000000005.failed") << "not a request\n"; | ||
| 52 | const auto list = helper::list_requests(requests); | ||
| 53 | CHECK(list.size() == 4); | ||
| 54 | // Urgent (dispatch) first, oldest first among equals, then warm-ups by size. | ||
| 55 | CHECK(list[0].name == "dispatch-old" && list[1].name == "dispatch-new"); | ||
| 56 | CHECK(list[2].name == "warm-small" && list[3].name == "warm-old"); | ||
| 57 | CHECK(list[0].stem == "cs-0000000000000003"); | ||
| 58 | return 0; | ||
| 59 | } | ||
| 60 | |||
| 61 | static int test_markers(const fs::path& dir) { | ||
| 62 | const auto file = dir / "cs-00000000000000aa.bin"; | ||
| 63 | CHECK(helper::mark_building(file)); | ||
| 64 | CHECK(!helper::mark_building(file)); // this process has it already | ||
| 65 | helper::unmark_building(file); | ||
| 66 | CHECK(!fs::exists(helper::building_path(file))); // the held handle's close removes the lock file | ||
| 67 | CHECK(helper::mark_building(file)); | ||
| 68 | helper::unmark_building(file); | ||
| 69 | // A marker left behind names a process that no longer holds it (a killed helper) and is taken over: it is named | ||
| 70 | // after this very process, which is the reused process id that used to make it look busy forever. | ||
| 71 | std::ofstream(helper::building_path(file), std::ios::binary | std::ios::trunc) | ||
| 72 | << std::to_string(GetCurrentProcessId()) << "\n"; | ||
| 73 | CHECK(fs::exists(helper::building_path(file))); | ||
| 74 | CHECK(helper::mark_building(file)); | ||
| 75 | helper::unmark_building(file); | ||
| 76 | CHECK(!fs::exists(helper::building_path(file))); | ||
| 77 | // A lock file another opener holds is a live build: no claim, and nothing removes it. | ||
| 78 | const HANDLE held = CreateFileW(helper::building_path(file).c_str(), GENERIC_WRITE, 0, nullptr, CREATE_NEW, | ||
| 79 | FILE_ATTRIBUTE_NORMAL, nullptr); | ||
| 80 | CHECK(held != INVALID_HANDLE_VALUE); | ||
| 81 | CHECK(!helper::mark_building(file)); | ||
| 82 | CHECK(fs::exists(helper::building_path(file))); | ||
| 83 | CloseHandle(held); | ||
| 84 | fs::remove(helper::building_path(file)); | ||
| 85 | CHECK(helper::mark_building(file)); | ||
| 86 | helper::unmark_building(file); | ||
| 87 | return 0; | ||
| 88 | } | ||
| 89 | |||
| 90 | // What state() answers for a request the helper has taken: no .req and no .failed means it is not coming (the helper | ||
| 91 | // removes .req before it writes .failed and before the build that writes the journal file), so it is failed rather | ||
| 92 | // than pending for as long as the helper lives. | ||
| 93 | static int test_state(const fs::path& dir) { | ||
| 94 | auto& client = helper::Client::instance(); | ||
| 95 | client.pretend_helper_running_for_test(); // no helper process is started | ||
| 96 | const leonida::cached_pso::Identity identity; | ||
| 97 | const auto file = dir / "cs-00000000000000bb.bin"; | ||
| 98 | std::string why; | ||
| 99 | CHECK(client.request(file, "fspx_compute/lib_cs_00001", 4000, identity, &why)); | ||
| 100 | CHECK(fs::exists(helper::request_path(file))); | ||
| 101 | CHECK(client.state(file, identity, &why) == helper::Client::State::pending); | ||
| 102 | // A bump of the priority keeps a .failed: it is the answer to this same request. | ||
| 103 | helper::write_line(helper::failed_path(file), "no compute entry of that name"); | ||
| 104 | CHECK(client.request(file, "fspx_compute/lib_cs_00001", 0, identity, &why)); | ||
| 105 | CHECK(fs::exists(helper::failed_path(file))); | ||
| 106 | CHECK(helper::read_line(helper::failed_path(file)) == "no compute entry of that name"); | ||
| 107 | why.clear(); | ||
| 108 | CHECK(client.state(file, identity, &why) == helper::Client::State::failed); | ||
| 109 | CHECK(why.find("no compute entry of that name") != std::string::npos); | ||
| 110 | // A fresh request drops an older failure, so asking again can retry. | ||
| 111 | CHECK(client.request(file, "fspx_compute/lib_cs_00001", 0, identity, &why)); | ||
| 112 | CHECK(!fs::exists(helper::failed_path(file))); | ||
| 113 | // .req gone, no .failed, no journal file: failed. | ||
| 114 | const auto gone = dir / "cs-00000000000000cc.bin"; | ||
| 115 | CHECK(client.request(gone, "fspx_compute/lib_cs_00002", 0, identity, &why)); | ||
| 116 | CHECK(client.state(gone, identity, &why) == helper::Client::State::pending); | ||
| 117 | fs::remove(helper::request_path(gone)); | ||
| 118 | why.clear(); | ||
| 119 | CHECK(client.state(gone, identity, &why) == helper::Client::State::failed); | ||
| 120 | CHECK(!why.empty()); | ||
| 121 | // .req gone because the helper has started building it (it holds the lock file): pending, also past the pending | ||
| 122 | // bound, since giving up would only start the same build in the game. Failed once the lock is let go with no file. | ||
| 123 | const auto building = dir / "cs-00000000000000ee.bin"; | ||
| 124 | SetEnvironmentVariableA("LEONIDA_COMPILE_HELPER_PENDING_MS", "100"); | ||
| 125 | CHECK(client.request(building, "fspx_compute/lib_cs_00004", 0, identity, &why)); | ||
| 126 | const HANDLE lock = CreateFileW(helper::building_path(building).c_str(), GENERIC_WRITE, 0, nullptr, CREATE_NEW, | ||
| 127 | FILE_ATTRIBUTE_NORMAL | FILE_FLAG_DELETE_ON_CLOSE, nullptr); | ||
| 128 | CHECK(lock != INVALID_HANDLE_VALUE); | ||
| 129 | fs::remove(helper::request_path(building)); | ||
| 130 | CHECK(helper::building_held(building)); | ||
| 131 | CHECK(client.state(building, identity, &why) == helper::Client::State::pending); | ||
| 132 | std::this_thread::sleep_for(std::chrono::milliseconds(250)); | ||
| 133 | CHECK(client.state(building, identity, &why) == helper::Client::State::pending); | ||
| 134 | CloseHandle(lock); | ||
| 135 | CHECK(!helper::building_held(building)); | ||
| 136 | why.clear(); | ||
| 137 | CHECK(client.state(building, identity, &why) == helper::Client::State::failed); | ||
| 138 | SetEnvironmentVariableA("LEONIDA_COMPILE_HELPER_PENDING_MS", nullptr); | ||
| 139 | return 0; | ||
| 140 | } | ||
| 141 | |||
| 142 | // A pending request the helper never answers is failed after LEONIDA_COMPILE_HELPER_PENDING_MS, so a skipped dispatch | ||
| 143 | // becomes a build here instead of never running. | ||
| 144 | static int test_pending_bound(const fs::path& dir) { | ||
| 145 | auto& client = helper::Client::instance(); | ||
| 146 | client.pretend_helper_running_for_test(); | ||
| 147 | const leonida::cached_pso::Identity identity; | ||
| 148 | const auto file = dir / "cs-00000000000000dd.bin"; | ||
| 149 | SetEnvironmentVariableA("LEONIDA_COMPILE_HELPER_PENDING_MS", "150"); | ||
| 150 | std::string why; | ||
| 151 | CHECK(client.request(file, "fspx_compute/lib_cs_00003", 0, identity, &why)); | ||
| 152 | CHECK(client.state(file, identity, &why) == helper::Client::State::pending); | ||
| 153 | std::this_thread::sleep_for(std::chrono::milliseconds(300)); | ||
| 154 | why.clear(); | ||
| 155 | CHECK(client.state(file, identity, &why) == helper::Client::State::failed); | ||
| 156 | CHECK(why.find("150 ms") != std::string::npos); | ||
| 157 | SetEnvironmentVariableA("LEONIDA_COMPILE_HELPER_PENDING_MS", nullptr); | ||
| 158 | return 0; | ||
| 159 | } | ||
| 160 | |||
| 161 | int main() { | ||
| 162 | const auto dir = fs::temp_directory_path() / | ||
| 163 | ("leonida_compile_helper_test_" + std::to_string(std::chrono::steady_clock::now().time_since_epoch().count())); | ||
| 164 | std::error_code ec; | ||
| 165 | fs::create_directories(dir, ec); | ||
| 166 | int failed = test_lines(); | ||
| 167 | if (!failed) failed = test_paths(); | ||
| 168 | if (!failed) failed = test_order(dir); | ||
| 169 | if (!failed) failed = test_markers(dir); | ||
| 170 | if (!failed) failed = test_state(dir); | ||
| 171 | if (!failed) failed = test_pending_bound(dir); | ||
| 172 | fs::remove_all(dir, ec); | ||
| 173 | if (failed) return failed; | ||
| 174 | std::printf("compile_helper_test: ok\n"); | ||
| 175 | return 0; | ||
| 176 | } | ||