diff options
| author | Aslan2142 <jan.trencansky@protonmail.com> | 2026-11-27 14:19:01 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-11-27 14:19:01 +0100 |
| commit | ec91d695ca5ffebbf327c2127ea49d89547a4e81 (patch) | |
| tree | b41c80bee71fcd8a24327c387588f6f0483ed7e7 /runtime/src | |
| parent | 17d3af0b652c50d1c787e6341739144251dbaa12 (diff) | |
| parent | d4830779134edfc079f0a7419303ce9615c01269 (diff) | |
Merge pull request #2260 from Tyrannys/claude/pipeline-prewarm-order
runtime: prewarm graphics pipelines in recorded order on the compile pool (RT-PIPELINE-PREWARM-ORDER phase 2)
Diffstat (limited to 'runtime/src')
| -rw-r--r-- | runtime/src/pipeline_prewarm.h | 171 |
1 files changed, 171 insertions, 0 deletions
diff --git a/runtime/src/pipeline_prewarm.h b/runtime/src/pipeline_prewarm.h new file mode 100644 index 000000000..394164aa9 --- /dev/null +++ b/runtime/src/pipeline_prewarm.h | |||
| @@ -0,0 +1,171 @@ | |||
| 1 | // RT-PIPELINE-PREWARM-ORDER, phase 2 (in-game): build at boot, ahead of their first draw, the pipelines an earlier run | ||
| 2 | // asked for first, in the order it asked for them. Off unless LEONIDA_PIPELINE_PREWARM_ORDER names an order log | ||
| 3 | // (pipeline_order.h, written with LEONIDA_PIPELINE_ORDER_LOG=1); "1" names pipeline_order.log beside the session cache. | ||
| 4 | // | ||
| 5 | // The plan is read once, when the device starts. A pipeline the title creates whose description key is in it gets its | ||
| 6 | // recorded variants queued on the compile workers (d3d12_pipeline_compile.h) as warm-ups, at a priority that keeps the | ||
| 7 | // recorded order: below every demanded build, and one warm-up at a time, none while a demanded build runs. | ||
| 8 | // | ||
| 9 | // Which run: of the sessions recorded on this GPU (vendor and device id), the one with the most records, the newest of | ||
| 10 | // those on a tie. A file from another GPU, a missing or unreadable file and a file with no usable record are ignored | ||
| 11 | // with one line on stderr; a damaged line is skipped (pipeline_order::parse), as are line types a v1 reader does not | ||
| 12 | // know (MoonEyes' `E` lines for the offline precompiler). | ||
| 13 | // | ||
| 14 | // Graphics and mesh records only: compute pipelines already have their own warm-up by module size, and the offline | ||
| 15 | // precompiler takes their recorded order (gta6rt --precompile-pipelines --order-file). | ||
| 16 | // | ||
| 17 | // The budget is the build time the earlier run recorded: records are taken in order until their build times add up to | ||
| 18 | // LEONIDA_PIPELINE_PREWARM_BUDGET_MS (default 300000), and at most LEONIDA_PIPELINE_PREWARM_MAX_ENTRIES (default 4096). | ||
| 19 | // A record with no build time counts as unknown_build_ms. | ||
| 20 | #pragma once | ||
| 21 | |||
| 22 | #include "pipeline_order.h" | ||
| 23 | |||
| 24 | #include <cstdint> | ||
| 25 | #include <filesystem> | ||
| 26 | #include <fstream> | ||
| 27 | #include <sstream> | ||
| 28 | #include <string> | ||
| 29 | #include <string_view> | ||
| 30 | #include <unordered_map> | ||
| 31 | #include <unordered_set> | ||
| 32 | #include <vector> | ||
| 33 | |||
| 34 | namespace leonida::pipeline_prewarm { | ||
| 35 | |||
| 36 | inline constexpr std::uint64_t default_budget_ms = 300000; | ||
| 37 | inline constexpr std::size_t default_max_entries = 4096; | ||
| 38 | inline constexpr std::uint32_t unknown_build_ms = 1000; // what a record with no build time counts against the budget | ||
| 39 | |||
| 40 | struct Options { | ||
| 41 | std::uint64_t budget_ms = default_budget_ms; | ||
| 42 | std::size_t max_entries = default_max_entries; | ||
| 43 | }; | ||
| 44 | |||
| 45 | // One variant of one pipeline to build, and where it came in the recorded order (0 first). | ||
| 46 | struct Entry { | ||
| 47 | std::uint32_t rank = 0; | ||
| 48 | std::uint32_t variant = 0; | ||
| 49 | char kind = 'g'; | ||
| 50 | }; | ||
| 51 | |||
| 52 | // The compile-worker priority of a prewarm: after demanded builds (0), in recorded order. Compute warm-ups use their | ||
| 53 | // module size (some hundred KiB), so prewarms mostly go first; the checkpoint (SIZE_MAX) stays last. | ||
| 54 | inline std::size_t priority(std::uint32_t rank) { return std::size_t{1} + rank; } | ||
| 55 | |||
| 56 | struct Plan { | ||
| 57 | std::unordered_map<std::string, std::vector<Entry>> by_key; // description key -> its variants, in recorded order | ||
| 58 | std::size_t entries = 0; // variants planned | ||
| 59 | std::size_t session_records = 0; // records of the session read | ||
| 60 | std::size_t compute_skipped = 0; // compute records left to the size-ordered warm-up | ||
| 61 | std::size_t over_budget = 0; // graphics and mesh records past the budget or the entry limit | ||
| 62 | std::uint64_t budget_used_ms = 0; // recorded build time of the planned ones | ||
| 63 | std::size_t bad_lines = 0; // damaged or unknown lines skipped | ||
| 64 | std::string message; // the one line for stderr: what was read, or why nothing is prewarmed | ||
| 65 | |||
| 66 | explicit operator bool() const { return entries != 0; } | ||
| 67 | const std::vector<Entry>* find(std::string_view key) const { | ||
| 68 | const auto it = by_key.find(std::string(key)); | ||
| 69 | return it == by_key.end() ? nullptr : &it->second; | ||
| 70 | } | ||
| 71 | }; | ||
| 72 | |||
| 73 | // Drops the lines a v1 reader does not know but that are not damage: `E` lines (the library entry of a record, for the | ||
| 74 | // offline precompiler). pipeline_order::parse would count them as bad lines. | ||
| 75 | inline std::string without_unknown_lines(std::string_view text) { | ||
| 76 | std::string out; | ||
| 77 | out.reserve(text.size()); | ||
| 78 | std::size_t at = 0; | ||
| 79 | while (at < text.size()) { | ||
| 80 | std::size_t end = text.find('\n', at); | ||
| 81 | const bool last = end == std::string_view::npos; | ||
| 82 | if (last) end = text.size(); | ||
| 83 | const std::string_view line = text.substr(at, end - at); | ||
| 84 | if (!(line.size() >= 2 && line[0] == 'E' && line[1] == ' ')) { | ||
| 85 | out += line; | ||
| 86 | if (!last) out += '\n'; | ||
| 87 | } | ||
| 88 | at = last ? end : end + 1; | ||
| 89 | } | ||
| 90 | return out; | ||
| 91 | } | ||
| 92 | |||
| 93 | // The plan from the text of an order log, for the GPU with this vendor and device id. `source` names the file in the | ||
| 94 | // message. Never throws on any text. | ||
| 95 | inline Plan make_plan(std::string_view text, std::uint32_t vendor, std::uint32_t device, const Options& options = {}, | ||
| 96 | std::string_view source = "the order log") { | ||
| 97 | Plan plan; | ||
| 98 | const auto read = pipeline_order::parse(without_unknown_lines(text)); | ||
| 99 | plan.bad_lines = read.bad_lines; | ||
| 100 | const pipeline_order::Session* chosen = nullptr; | ||
| 101 | std::size_t other_gpu = 0; | ||
| 102 | for (const auto& session : read.sessions) { | ||
| 103 | if (session.records.empty()) continue; | ||
| 104 | if (session.vendor != vendor || session.device != device) { | ||
| 105 | ++other_gpu; | ||
| 106 | continue; | ||
| 107 | } | ||
| 108 | if (!chosen || session.records.size() >= chosen->records.size()) chosen = &session; | ||
| 109 | } | ||
| 110 | const std::string head = "[rt] pipeline-prewarm: ", where(source); | ||
| 111 | if (!chosen) { | ||
| 112 | plan.message = head + "nothing to prewarm from " + where + ": " + | ||
| 113 | (other_gpu ? std::to_string(other_gpu) + " recorded run(s), none on this GPU" | ||
| 114 | : std::string("no readable record")) + | ||
| 115 | (plan.bad_lines ? " (" + std::to_string(plan.bad_lines) + " damaged line(s) skipped)" : ""); | ||
| 116 | return plan; | ||
| 117 | } | ||
| 118 | plan.session_records = chosen->records.size(); | ||
| 119 | std::unordered_set<std::string> seen; // key + variant: a record repeated in a session is planned once | ||
| 120 | std::uint32_t rank = 0; | ||
| 121 | for (const auto& record : chosen->records) { | ||
| 122 | if (record.kind == 'c') { | ||
| 123 | ++plan.compute_skipped; | ||
| 124 | continue; | ||
| 125 | } | ||
| 126 | if (!seen.insert(record.key + '/' + std::to_string(record.variant)).second) continue; | ||
| 127 | const std::uint64_t cost = record.build_ms == pipeline_order::unknown_ms ? unknown_build_ms : record.build_ms; | ||
| 128 | if (plan.entries >= options.max_entries || plan.budget_used_ms + cost > options.budget_ms) { | ||
| 129 | ++plan.over_budget; | ||
| 130 | continue; | ||
| 131 | } | ||
| 132 | plan.budget_used_ms += cost; | ||
| 133 | plan.by_key[record.key].push_back({rank++, record.variant, record.kind}); | ||
| 134 | ++plan.entries; | ||
| 135 | } | ||
| 136 | plan.message = head + std::to_string(plan.entries) + " graphics variant(s) of " + | ||
| 137 | std::to_string(plan.by_key.size()) + " pipeline(s) to prewarm from " + where + " (a run of " + | ||
| 138 | std::to_string(plan.session_records) + " record(s); " + std::to_string(plan.budget_used_ms) + | ||
| 139 | " ms recorded build time; " + std::to_string(plan.over_budget) + " over the budget; " + | ||
| 140 | std::to_string(plan.compute_skipped) + " compute left to the warm-up"; | ||
| 141 | if (plan.bad_lines) plan.message += "; " + std::to_string(plan.bad_lines) + " damaged line(s) skipped"; | ||
| 142 | plan.message += ")"; | ||
| 143 | return plan; | ||
| 144 | } | ||
| 145 | |||
| 146 | // Reads the file and makes the plan. A file that cannot be read gives an empty plan with the reason. | ||
| 147 | inline Plan load_plan(const std::filesystem::path& path, std::uint32_t vendor, std::uint32_t device, | ||
| 148 | const Options& options = {}) { | ||
| 149 | std::ifstream in(path, std::ios::binary); | ||
| 150 | if (!in) { | ||
| 151 | Plan plan; | ||
| 152 | plan.message = "[rt] pipeline-prewarm: cannot read " + path.string() + "; nothing is prewarmed"; | ||
| 153 | return plan; | ||
| 154 | } | ||
| 155 | std::ostringstream text; | ||
| 156 | text << in.rdbuf(); | ||
| 157 | return make_plan(text.str(), vendor, device, options, path.string()); | ||
| 158 | } | ||
| 159 | |||
| 160 | // A positive whole number, or `fallback` for unset, empty, zero, or anything else. | ||
| 161 | inline std::uint64_t parse_count(const char* text, std::uint64_t fallback) { | ||
| 162 | if (!text || !*text) return fallback; | ||
| 163 | std::uint64_t v = 0; | ||
| 164 | for (const char* p = text; *p; ++p) { | ||
| 165 | if (*p < '0' || *p > '9' || v > (UINT64_MAX - 9) / 10) return fallback; | ||
| 166 | v = v * 10 + std::uint64_t(*p - '0'); | ||
| 167 | } | ||
| 168 | return v ? v : fallback; | ||
| 169 | } | ||
| 170 | |||
| 171 | } // namespace leonida::pipeline_prewarm | ||