diff options
| author | Aslan2142 <jan.trencansky@protonmail.com> | 2026-11-27 13:02:22 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-11-27 13:02:22 +0100 |
| commit | f75b2b4258cf25e360bd6d59f554ca4ebdf30bf4 (patch) | |
| tree | aed044faed97a033c8dc4a3b37b1c09fcb1f9162 /runtime/src | |
| parent | a3bafd05d02b1628796a24d851693adb0d9bd136 (diff) | |
| parent | ebdc9916278981721453db22a533659a4e971efa (diff) | |
Merge pull request #2412 from OreoFR/fix/compile-gate-bounded
compile gate: adaptive per kind of create, re-armed by a held submission (RT-COMPILE-GATE-ADAPTIVE)
Diffstat (limited to 'runtime/src')
| -rw-r--r-- | runtime/src/vulkan_backend.h | 15 | ||||
| -rw-r--r-- | runtime/src/vulkan_device_fault.h | 614 | ||||
| -rw-r--r-- | runtime/src/vulkan_present.h | 13 |
3 files changed, 589 insertions, 53 deletions
diff --git a/runtime/src/vulkan_backend.h b/runtime/src/vulkan_backend.h index bf970aa28..18bcc07ba 100644 --- a/runtime/src/vulkan_backend.h +++ b/runtime/src/vulkan_backend.h | |||
| @@ -1700,7 +1700,7 @@ public: | |||
| 1700 | ++host_texture_stats_.waits; | 1700 | ++host_texture_stats_.waits; |
| 1701 | // A device wait is a queue operation: it passes the compile gate, so it never waits in the driver behind a | 1701 | // A device wait is a queue operation: it passes the compile gate, so it never waits in the driver behind a |
| 1702 | // long pipeline create (the starved wait that loses the device after 10 s). | 1702 | // long pipeline create (the starved wait that loses the device after 10 s). |
| 1703 | const SubmitMutex::SubmitTurn turn(submit_mutex_); | 1703 | const SubmitMutex::SubmitTurn turn(submit_mutex_, false); |
| 1704 | const auto result = wait_submitted_locked(kTitleWriteWaitNs, "vkWaitSemaphores (title texture upload)"); | 1704 | const auto result = wait_submitted_locked(kTitleWriteWaitNs, "vkWaitSemaphores (title texture upload)"); |
| 1705 | // Not done in time: VK_NOT_READY, so the caller takes its staged path (ordered on the queue) or tries | 1705 | // Not done in time: VK_NOT_READY, so the caller takes its staged path (ordered on the queue) or tries |
| 1706 | // again later, and the skip is not counted as a failed fill. | 1706 | // again later, and the skip is not counted as a failed fill. |
| @@ -1722,7 +1722,7 @@ public: | |||
| 1722 | LayoutPieces pieces; | 1722 | LayoutPieces pieces; |
| 1723 | if (!tracker_.pieces(id, range, pieces) || pieces.empty()) return VK_ERROR_UNKNOWN; | 1723 | if (!tracker_.pieces(id, range, pieces) || pieces.empty()) return VK_ERROR_UNKNOWN; |
| 1724 | if (image_work_pending_) { | 1724 | if (image_work_pending_) { |
| 1725 | const SubmitMutex::SubmitTurn turn(submit_mutex_); // a queue operation: the compile gate (as above) | 1725 | const SubmitMutex::SubmitTurn turn(submit_mutex_, false); // a queue operation: the compile gate (as above) |
| 1726 | const auto r = wait_submitted_locked(kTitleWriteWaitNs, "vkWaitSemaphores (title staging)"); | 1726 | const auto r = wait_submitted_locked(kTitleWriteWaitNs, "vkWaitSemaphores (title staging)"); |
| 1727 | if (r != VK_SUCCESS) return r; | 1727 | if (r != VK_SUCCESS) return r; |
| 1728 | image_work_pending_ = false; | 1728 | image_work_pending_ = false; |
| @@ -2073,7 +2073,7 @@ public: | |||
| 2073 | } | 2073 | } |
| 2074 | 2074 | ||
| 2075 | void mark_device_lost(const char* where = "a Vulkan call returned VK_ERROR_DEVICE_LOST") noexcept { | 2075 | void mark_device_lost(const char* where = "a Vulkan call returned VK_ERROR_DEVICE_LOST") noexcept { |
| 2076 | set_device_lost(device_lost_, where); | 2076 | if (set_device_lost(device_lost_, where)) submit_mutex_.report_creates_on_loss(); // which creates were in it |
| 2077 | run_device_lost_hook(); // LEONIDA_AFTERMATH: the GPU crash dump is written before waiters wake | 2077 | run_device_lost_hook(); // LEONIDA_AFTERMATH: the GPU crash dump is written before waiters wake |
| 2078 | if (fence_dispatcher_) { | 2078 | if (fence_dispatcher_) { |
| 2079 | try { | 2079 | try { |
| @@ -2308,10 +2308,11 @@ private: | |||
| 2308 | if (gate.submits != gate_logged.submits || gate.creates != gate_logged.creates) { | 2308 | if (gate.submits != gate_logged.submits || gate.creates != gate_logged.creates) { |
| 2309 | gate_logged = gate; | 2309 | gate_logged = gate; |
| 2310 | std::fprintf(stderr, "[rt] compile-gate: t=%.0f s: %llu submissions waited for pipeline creates " | 2310 | std::fprintf(stderr, "[rt] compile-gate: t=%.0f s: %llu submissions waited for pipeline creates " |
| 2311 | "(%lld ms in all, longest %lld ms); %llu creates waited behind a submission " | 2311 | "(%lld ms in all, longest %lld ms, %llu went ahead at the limit); %llu creates " |
| 2312 | "(%lld ms in all, longest %lld ms)\n", | 2312 | "waited behind a submission (%lld ms in all, longest %lld ms)\n", |
| 2313 | double(diagnostic_ms()) / 1000.0, static_cast<unsigned long long>(gate.submits), | 2313 | double(diagnostic_ms()) / 1000.0, static_cast<unsigned long long>(gate.submits), |
| 2314 | static_cast<long long>(gate.submit_ms), static_cast<long long>(gate.submit_max_ms), | 2314 | static_cast<long long>(gate.submit_ms), static_cast<long long>(gate.submit_max_ms), |
| 2315 | static_cast<unsigned long long>(gate.ahead), | ||
| 2315 | static_cast<unsigned long long>(gate.creates), static_cast<long long>(gate.create_ms), | 2316 | static_cast<unsigned long long>(gate.creates), static_cast<long long>(gate.create_ms), |
| 2316 | static_cast<long long>(gate.create_max_ms)); | 2317 | static_cast<long long>(gate.create_max_ms)); |
| 2317 | } | 2318 | } |
| @@ -2614,7 +2615,7 @@ public: | |||
| 2614 | context_.holds().pump_ready(); // presents queued behind what was just dropped can run | 2615 | context_.holds().pump_ready(); // presents queued behind what was just dropped can run |
| 2615 | if (queue_) { | 2616 | if (queue_) { |
| 2616 | std::lock_guard lock(context_.submit_mutex()); | 2617 | std::lock_guard lock(context_.submit_mutex()); |
| 2617 | const SubmitMutex::SubmitTurn turn(context_.submit_mutex()); // a queue operation: the compile gate | 2618 | const SubmitMutex::SubmitTurn turn(context_.submit_mutex(), false); // a queue operation: the compile gate |
| 2618 | for (; open_labels_; --open_labels_) context_.labels_.queue_end(queue_); | 2619 | for (; open_labels_; --open_labels_) context_.labels_.queue_end(queue_); |
| 2619 | const VkResult idle_result = context_.observe_result(vk.vkQueueWaitIdle(queue_), "vkQueueWaitIdle"); | 2620 | const VkResult idle_result = context_.observe_result(vk.vkQueueWaitIdle(queue_), "vkQueueWaitIdle"); |
| 2620 | if (idle_result == VK_SUCCESS) { | 2621 | if (idle_result == VK_SUCCESS) { |
| @@ -2903,7 +2904,7 @@ public: | |||
| 2903 | bool idle() { | 2904 | bool idle() { |
| 2904 | flush_async(); | 2905 | flush_async(); |
| 2905 | std::lock_guard lock(context_.submit_mutex()); | 2906 | std::lock_guard lock(context_.submit_mutex()); |
| 2906 | const SubmitMutex::SubmitTurn turn(context_.submit_mutex()); // a queue operation: the compile gate | 2907 | const SubmitMutex::SubmitTurn turn(context_.submit_mutex(), false); // a queue operation: the compile gate |
| 2907 | return context_.observe_result(context_.vk.vkQueueWaitIdle(queue_), "vkQueueWaitIdle") == VK_SUCCESS; | 2908 | return context_.observe_result(context_.vk.vkQueueWaitIdle(queue_), "vkQueueWaitIdle") == VK_SUCCESS; |
| 2908 | } | 2909 | } |
| 2909 | 2910 | ||
diff --git a/runtime/src/vulkan_device_fault.h b/runtime/src/vulkan_device_fault.h index b3af188d1..17f420934 100644 --- a/runtime/src/vulkan_device_fault.h +++ b/runtime/src/vulkan_device_fault.h | |||
| @@ -309,6 +309,27 @@ struct DriverCompile { | |||
| 309 | }; | 309 | }; |
| 310 | inline DriverCompile driver_compile; | 310 | inline DriverCompile driver_compile; |
| 311 | 311 | ||
| 312 | // The compile gate's types, for SubmitMutex (which names them CreateKind, CreateInfo and GateAhead). At namespace | ||
| 313 | // scope, not nested: a default argument `= {}` of a nested class with default member initializers is rejected by | ||
| 314 | // clang and GCC inside the enclosing class (the member initializers are not parsed until that class is complete). | ||
| 315 | enum class CompileGateKind : std::uint8_t { compute, graphics }; // graphics: mesh pipelines too | ||
| 316 | // What a create is, for the verdicts and the reports. Names are ShaderLibrary strings, valid until device teardown | ||
| 317 | // (or literals). | ||
| 318 | struct CompileGateCreate { | ||
| 319 | CompileGateKind kind = CompileGateKind::compute; | ||
| 320 | const char* name = nullptr; | ||
| 321 | const char* trigger = nullptr; | ||
| 322 | std::size_t bytes = 0; // SPIR-V | ||
| 323 | }; | ||
| 324 | struct CompileGateAhead { // a submission that entered the driver alongside creates not waited for | ||
| 325 | std::int64_t since_ms = -1; // when it entered, or -1: no such create was in the driver | ||
| 326 | std::uint64_t last_create = 0; // the number of the last create that had entered then | ||
| 327 | std::uint32_t creates = 0; // such creates in the driver then | ||
| 328 | std::uint8_t kinds = 0; // their kinds, bit per CompileGateKind | ||
| 329 | bool went_ahead = false; // it waited at the gate up to the limit or the probe budget | ||
| 330 | bool probe = true; // its time in the driver is a probe (a submission, not a present or a wait) | ||
| 331 | }; | ||
| 332 | |||
| 312 | // VulkanContext's submit lock, which also remembers who holds it, since when and how many threads wait for it. A | 333 | // VulkanContext's submit lock, which also remembers who holds it, since when and how many threads wait for it. A |
| 313 | // submit or an idle wait blocked inside the driver holds it for as long as it blocks; the stall watchdog and the loss | 334 | // submit or an idle wait blocked inside the driver holds it for as long as it blocks; the stall watchdog and the loss |
| 314 | // report name that thread, and a hold of a second or more is logged when it ends. | 335 | // report name that thread, and a hold of a second or more is logged when it ends. |
| @@ -398,36 +419,243 @@ public: | |||
| 398 | // that arrives while a submission waits queues behind it, so a submission waits only for the creates already in | 419 | // that arrives while a submission waits queues behind it, so a submission waits only for the creates already in |
| 399 | // the driver. A long compile then stalls the submitting thread here, in our code, instead of in the driver. | 420 | // the driver. A long compile then stalls the submitting thread here, in our code, instead of in the driver. |
| 400 | // No deadlock: a create holds the gate only across its vkCreate*Pipelines and a submission only across its | 421 | // No deadlock: a create holds the gate only across its vkCreate*Pipelines and a submission only across its |
| 401 | // vkQueueSubmit, and neither driver call waits for anything of ours. LEONIDA_COMPILE_GATE=0 turns it off. | 422 | // vkQueueSubmit, and neither driver call waits for anything of ours. |
| 423 | // | ||
| 424 | // By default (classic) only compute creates pass the gate and a submission waits for them however long they take, | ||
| 425 | // as before the adaptive gate; LEONIDA_COMPILE_GATE_WAIT_MS bounds that too. With the adaptive gate | ||
| 426 | // (LEONIDA_COMPILE_GATE=adaptive) the wait is bounded (gate_wait_limit_ms, LEONIDA_COMPILE_GATE_WAIT_MS): a | ||
| 427 | // submission waiting at the gate holds the submit lock, so every queue stops behind it, and the title's hang | ||
| 428 | // watchdog ends the process once its main loop has not reported for 30 s. One create stayed in the driver for more | ||
| 429 | // than 54 s (RTX 4070 Laptop, NVIDIA 621.40), and the gate turned it into that exit. Past the limit the submission | ||
| 430 | // goes ahead, and the creates that were in the driver then no longer hold any submission back; creates that enter | ||
| 431 | // later are waited for again. The default limit, 15 s, keeps the protection measured above for every create of up | ||
| 432 | // to 25 s: 630.20 gave the submission 10 s in the driver before the loss, and a stop much longer than 25 s ends at | ||
| 433 | // the watchdog anyway. The same with the watchdog kept kicked (LEONIDA_TITLE_WATCHDOG=0 with | ||
| 434 | // LEONIDA_TITLE_WATCHDOG_KICK): nothing ends the run then, but the stops still cost the frames (an RTX 4060 run | ||
| 435 | // that way had 459 waits, 155 s in all, by t=202 s). Only LEONIDA_COMPILE_GATE_WAIT_MS=0 waits for the creates | ||
| 436 | // however long they take. | ||
| 437 | // | ||
| 438 | // Adaptive (LEONIDA_COMPILE_GATE=adaptive, opt-in): each submission that enters the driver alongside a create the | ||
| 439 | // gate no longer waits for is a probe of whether this driver holds a submission behind a create. Back while that | ||
| 440 | // create is still in the driver: under kProbeQuickMs is quick, kProbeHeldMs or more is held, between says nothing. | ||
| 441 | // On 621.40 the three that went ahead came back in 0-11 ms, the create still there; the one 630.20 held stayed 10 | ||
| 442 | // s, until the loss. Two quick probes at least kProbeSpanMs apart turn the gate off for the rest of the run (the | ||
| 443 | // device's, which is the process's), logged once. Two because on 630.20 back-to-back creates take and release the | ||
| 444 | // driver's lock (see let_submit_finish), so one submission could slip in between and come back quickly from a | ||
| 445 | // driver that does hold submissions; a second one a second later must be as lucky. It costs no extra wait: once one | ||
| 446 | // has gone ahead, the next ones enter at once while that create is still in the driver. One held probe keeps the | ||
| 447 | // gate on for the rest of the run. Only submissions (vkQueueSubmit, vkQueueBindSparse) probe: presents and idle or | ||
| 448 | // semaphore waits pass the gate as submissions do, but take seconds on their own while the GPU is busy, so their | ||
| 449 | // time in the driver judges nothing. LEONIDA_COMPILE_GATE=1 gates without probing, LEONIDA_COMPILE_GATE=0 turns the | ||
| 450 | // gate off. let_submit_finish is unaffected by all of this. | ||
| 451 | // | ||
| 452 | // The first probes come early: while no verdict is in, a submission also goes ahead once the submissions' waits at | ||
| 453 | // the gate in the last kProbeWindowMs, its own included, add up to the probe budget (gate_probe_budget_ms, | ||
| 454 | // LEONIDA_COMPILE_GATE_PROBE_MS, 2500 ms). On 621.40 no single wait reached 15 s, but 80 waits of up to 6.7 s, | ||
| 455 | // back to back, kept the main loop from reporting for 30 s. A budget over a window rather than a shorter limit | ||
| 456 | // per wait, because it also counts many short waits, and a single wait reaches it as soon as a 2.5 s limit | ||
| 457 | // would. Before the verdict the gate then stops the queues for at most a quarter of any 10 s; after a quick | ||
| 458 | // verdict not at all. The price is on a driver that does hold submissions (630.20): the first probe enters the | ||
| 459 | // driver after 2.5 s of waiting instead of 15, and loses the device if the create stays in the driver 10 s more. | ||
| 460 | // That is one probe per kind and run: the probe holds the submit lock while it is in the driver, so no other | ||
| 461 | // submission enters until it comes back, and then (within 500 ms of the create leaving, 2 s or more) it gives the | ||
| 462 | // held verdict, after which no submission enters next to a create of that kind early again. This is why the | ||
| 463 | // adaptive gate is opt-in until it has run on such a driver. The alternative there is the same run starved | ||
| 464 | // by waits like the ones above. LEONIDA_COMPILE_GATE_WAIT_MS=0 without LEONIDA_COMPILE_GATE_PROBE_MS also makes the | ||
| 465 | // budget 0: the gate then waits however long the creates take and never probes, as LEONIDA_COMPILE_GATE=1 does. | ||
| 466 | // | ||
| 467 | // A verdict per kind of create (CreateKind: compute, or graphics with mesh): a submission that came back quickly | ||
| 468 | // beside a compute create says nothing about a graphics one, and a run on 621.40 lost the device beside a mesh | ||
| 469 | // pipeline create 9 s after compute probes had turned the gate off. A probe counts for each kind that had a | ||
| 470 | // create in the driver from before the submission entered until it came back: a quick one for all of them, a | ||
| 471 | // held one against all of them, since which create held it cannot be told. A submission waits for the creates of | ||
| 472 | // every kind whose gate is not off; the probe budget applies only while all of those are of kinds still pending. | ||
| 473 | // A kind that is off is still timed: a submission that stays kProbeHeldMs or more beside one of its creates turns | ||
| 474 | // that kind's gate back on for the rest of the run (logged once), so the submissions after it wait. The one that | ||
| 475 | // stayed cannot be helped. A held probe counts only if the submission came back within kHeldReturnMs after a | ||
| 476 | // create of that kind, there when it entered, left the driver: a submission held behind a create comes back when | ||
| 477 | // the create does. One that comes back slow with its creates still there was slow for another reason (on 621.40, | ||
| 478 | // a 36 s submission came back 18 s before the compute create beside it, and the 4070 Laptop's 8 GB were full); | ||
| 479 | // counting it re-armed the gate and starved the title for 78 s behind creates that were not holding anything. A submission never goes ahead (at the limit or the budget) while a create of a kind | ||
| 480 | // that holds submissions is in the driver, and such creates are waited for even if one was gone past before the | ||
| 481 | // verdict: on 621.40, with the gate off, a vkQueueSubmit sat 54 s in the driver beside a 56 s mesh create and the | ||
| 482 | // device was lost; waiting here starves the title as long, but keeps the device. Graphics creates pass the gate | ||
| 483 | // since this split (adaptive and LEONIDA_COMPILE_GATE=1; the classic gate, as before, only compute ones). On a | ||
| 484 | // device loss, report_creates_on_loss names the creates in the driver and the verdicts. | ||
| 485 | // classic (the default): the gate as on main before the adaptive one, compute creates only, no limit, no probes. | ||
| 486 | enum class GateMode { off, classic, adaptive, always }; | ||
| 487 | enum class GateVerdict { pending, driver_does_not_hold, driver_holds }; | ||
| 488 | static constexpr std::int64_t kProbeQuickMs = 250; | ||
| 489 | static constexpr std::int64_t kProbeHeldMs = 2000; | ||
| 490 | static constexpr std::int64_t kHeldReturnMs = 500; | ||
| 491 | static constexpr std::int64_t kProbeSpanMs = 1000; | ||
| 492 | static constexpr unsigned kQuickProbes = 2; | ||
| 493 | static constexpr std::int64_t kProbeWindowMs = 10000; | ||
| 494 | static constexpr std::int64_t kDefaultProbeBudgetMs = 2500; | ||
| 495 | using CreateKind = CompileGateKind; // graphics: mesh pipelines too | ||
| 496 | static constexpr std::size_t kKinds = 2; | ||
| 497 | static const char* kind_name(CreateKind kind) { return kind == CreateKind::compute ? "compute" : "graphics"; } | ||
| 498 | using CreateInfo = CompileGateCreate; | ||
| 499 | using GateAhead = CompileGateAhead; | ||
| 402 | class CompileTurn { // around vkCreate*Pipelines | 500 | class CompileTurn { // around vkCreate*Pipelines |
| 403 | public: | 501 | public: |
| 404 | explicit CompileTurn(SubmitMutex& gate) : gate_(gate), waited_(gate.enter_compile()) {} | 502 | explicit CompileTurn(SubmitMutex& gate, const CreateInfo& info = {}) |
| 405 | ~CompileTurn() { gate_.leave_compile(); } | 503 | : gate_(gate), waited_(gate.enter_compile(seq_, info)) {} |
| 504 | ~CompileTurn() { gate_.leave_compile(seq_); } | ||
| 406 | CompileTurn(const CompileTurn&) = delete; | 505 | CompileTurn(const CompileTurn&) = delete; |
| 407 | CompileTurn& operator=(const CompileTurn&) = delete; | 506 | CompileTurn& operator=(const CompileTurn&) = delete; |
| 408 | std::int64_t waited_ms() const { return waited_; } | 507 | std::int64_t waited_ms() const { return waited_; } |
| 409 | 508 | ||
| 410 | private: | 509 | private: |
| 411 | SubmitMutex& gate_; | 510 | SubmitMutex& gate_; |
| 511 | std::uint64_t seq_ = 0; // declared before waited_: enter_compile sets it | ||
| 412 | std::int64_t waited_; | 512 | std::int64_t waited_; |
| 413 | }; | 513 | }; |
| 414 | class SubmitTurn { // around vkQueueSubmit, with the submit lock held | 514 | class SubmitTurn { // around vkQueueSubmit, with the submit lock held |
| 415 | public: | 515 | public: |
| 416 | explicit SubmitTurn(SubmitMutex& gate) : gate_(gate) { gate.enter_submit(); } | 516 | // `probe` false for a present or an idle or semaphore wait: it waits at the gate like a submission, but its |
| 417 | ~SubmitTurn() { gate_.leave_submit(); } | 517 | // time in the driver judges nothing (it can take seconds on its own while the GPU is busy). |
| 518 | explicit SubmitTurn(SubmitMutex& gate, bool probe = true) : gate_(gate) { | ||
| 519 | gate.enter_submit(&ahead_, probe); | ||
| 520 | ahead_.probe = probe; | ||
| 521 | } | ||
| 522 | ~SubmitTurn() { gate_.leave_submit(ahead_); } | ||
| 418 | SubmitTurn(const SubmitTurn&) = delete; | 523 | SubmitTurn(const SubmitTurn&) = delete; |
| 419 | SubmitTurn& operator=(const SubmitTurn&) = delete; | 524 | SubmitTurn& operator=(const SubmitTurn&) = delete; |
| 525 | // It went ahead at the wait limit with creates still in the driver. | ||
| 526 | bool went_ahead() const { return ahead_.went_ahead; } | ||
| 420 | 527 | ||
| 421 | private: | 528 | private: |
| 422 | SubmitMutex& gate_; | 529 | SubmitMutex& gate_; |
| 530 | GateAhead ahead_; | ||
| 423 | }; | 531 | }; |
| 424 | 532 | ||
| 425 | static bool compile_gate_enabled() { | 533 | // LEONIDA_COMPILE_GATE: 0 off, 1 always (both kinds, and compute builds hold the submit lock, compute_driver_turn |
| 426 | static const bool on = [] { | 534 | // in shims/d3d12_vulkan_device.cpp), `adaptive` the adaptive gate, anything else (unset) classic. Classic stays |
| 535 | // the default until the adaptive gate has run on a driver that holds submissions (630.20): there its first probe | ||
| 536 | // of a kind enters the driver after the probe budget and loses the device if the create stays 10 s more. | ||
| 537 | static GateMode compile_gate_mode() { | ||
| 538 | static const GateMode mode = [] { | ||
| 427 | const char* value = std::getenv("LEONIDA_COMPILE_GATE"); | 539 | const char* value = std::getenv("LEONIDA_COMPILE_GATE"); |
| 428 | return !(value && !std::strcmp(value, "0")); | 540 | if (value && !std::strcmp(value, "0")) return GateMode::off; |
| 541 | if (value && !std::strcmp(value, "1")) return GateMode::always; | ||
| 542 | if (value && !std::strcmp(value, "adaptive")) return GateMode::adaptive; | ||
| 543 | return GateMode::classic; | ||
| 544 | }(); | ||
| 545 | return mode; | ||
| 546 | } | ||
| 547 | static bool compile_gate_enabled() { return compile_gate_mode() != GateMode::off; } | ||
| 548 | // Whether graphics (and mesh) creates pass the gate: not in the classic gate, where only compute ones did. | ||
| 549 | static bool compile_gate_graphics() { | ||
| 550 | return compile_gate_mode() == GateMode::adaptive || compile_gate_mode() == GateMode::always; | ||
| 551 | } | ||
| 552 | |||
| 553 | // Read once. How long one submission waits at the gate before it goes ahead (0: until the creates leave): | ||
| 554 | // LEONIDA_COMPILE_GATE_WAIT_MS=<ms>. The adaptive gate's probe budget over kProbeWindowMs (0: no early probes): | ||
| 555 | // LEONIDA_COMPILE_GATE_PROBE_MS=<ms>. Unset, kDefaultGateWaitMs and kDefaultProbeBudgetMs, whatever the title's | ||
| 556 | // hang watchdog does; the budget is 0 when the limit is, and the limit 0 with LEONIDA_COMPILE_GATE=1. | ||
| 557 | static constexpr std::int64_t kDefaultGateWaitMs = 15000; | ||
| 558 | struct GateDefaults { | ||
| 559 | std::int64_t wait_ms; | ||
| 560 | std::int64_t probe_ms; | ||
| 561 | }; | ||
| 562 | static GateDefaults gate_defaults() { | ||
| 563 | static const GateDefaults defaults = [] { | ||
| 564 | const auto read = [](const char* name, std::int64_t fallback) -> std::int64_t { | ||
| 565 | const char* value = std::getenv(name); | ||
| 566 | if (!value || !*value) return fallback; | ||
| 567 | char* end = nullptr; | ||
| 568 | const long long n = std::strtoll(value, &end, 10); | ||
| 569 | if (end && !*end && n >= 0) return n; | ||
| 570 | std::fprintf(stderr, "[rt] compile-gate: %s=%s is not a number of milliseconds; ignored\n", name, value); | ||
| 571 | return fallback; | ||
| 572 | }; | ||
| 573 | const GateMode mode = compile_gate_mode(); | ||
| 574 | const bool always = mode == GateMode::always; | ||
| 575 | const bool adaptive = mode == GateMode::adaptive; // classic and always: no limit by default | ||
| 576 | const std::int64_t wait_ms = read("LEONIDA_COMPILE_GATE_WAIT_MS", adaptive ? kDefaultGateWaitMs : 0); | ||
| 577 | const GateDefaults d{wait_ms, read("LEONIDA_COMPILE_GATE_PROBE_MS", wait_ms ? kDefaultProbeBudgetMs : 0)}; | ||
| 578 | if (mode == GateMode::classic && d.wait_ms) | ||
| 579 | std::fprintf(stderr, "[rt] compile-gate: classic (compute creates); a submission waits at most %lld ms " | ||
| 580 | "for them to leave the driver (LEONIDA_COMPILE_GATE_WAIT_MS; " | ||
| 581 | "LEONIDA_COMPILE_GATE=adaptive for the adaptive gate)\n", | ||
| 582 | static_cast<long long>(d.wait_ms)); | ||
| 583 | else if (always && d.wait_ms) | ||
| 584 | std::fprintf(stderr, "[rt] compile-gate: always (LEONIDA_COMPILE_GATE=1); a submission waits at most " | ||
| 585 | "%lld ms for pipeline creates to leave the driver (LEONIDA_COMPILE_GATE_WAIT_MS)\n", | ||
| 586 | static_cast<long long>(d.wait_ms)); | ||
| 587 | else if (always) | ||
| 588 | std::fprintf(stderr, "[rt] compile-gate: always (LEONIDA_COMPILE_GATE=1); a submission waits for " | ||
| 589 | "pipeline creates to leave the driver, however long they take\n"); | ||
| 590 | else if (adaptive) | ||
| 591 | std::fprintf(stderr, "[rt] compile-gate: adaptive (LEONIDA_COMPILE_GATE=adaptive), off once " | ||
| 592 | "submissions come back quickly with a create in the driver; a submission goes " | ||
| 593 | "ahead after %lld ms of waiting (0: no limit, LEONIDA_COMPILE_GATE_WAIT_MS) or, " | ||
| 594 | "until that is known, once the waits of the last " | ||
| 595 | "%lld s add up to %lld ms (0: never, LEONIDA_COMPILE_GATE_PROBE_MS)\n", | ||
| 596 | static_cast<long long>(d.wait_ms), static_cast<long long>(kProbeWindowMs / 1000), | ||
| 597 | static_cast<long long>(d.probe_ms)); | ||
| 598 | return d; | ||
| 599 | }(); | ||
| 600 | return defaults; | ||
| 601 | } | ||
| 602 | static std::int64_t default_gate_wait_limit_ms() { return gate_defaults().wait_ms; } | ||
| 603 | static std::int64_t default_gate_probe_budget_ms() { return gate_defaults().probe_ms; } | ||
| 604 | // How long a submission waits for creates of a kind that holds submissions before it goes ahead anyway | ||
| 605 | // (LEONIDA_COMPILE_GATE_HELD_WAIT_MS, 0: until they leave). Longer than the plain limit: going ahead there means | ||
| 606 | // waiting in the driver, where the device was lost once (a 54 s submission beside a 56 s mesh create on 621.40, | ||
| 607 | // the gate off); a false "holds" verdict costs at most this per create instead of the rest of the run. | ||
| 608 | static constexpr std::int64_t kDefaultHeldWaitMs = 60000; | ||
| 609 | static std::int64_t default_gate_held_limit_ms() { | ||
| 610 | static const std::int64_t ms = [] { | ||
| 611 | const char* value = std::getenv("LEONIDA_COMPILE_GATE_HELD_WAIT_MS"); | ||
| 612 | if (!value || !*value) return kDefaultHeldWaitMs; | ||
| 613 | char* end = nullptr; | ||
| 614 | const long long n = std::strtoll(value, &end, 10); | ||
| 615 | if (end && !*end && n >= 0) return std::int64_t{n}; | ||
| 616 | std::fprintf(stderr, "[rt] compile-gate: LEONIDA_COMPILE_GATE_HELD_WAIT_MS=%s is not a number of " | ||
| 617 | "milliseconds; ignored\n", value); | ||
| 618 | return kDefaultHeldWaitMs; | ||
| 429 | }(); | 619 | }(); |
| 430 | return on; | 620 | return ms; |
| 621 | } | ||
| 622 | void set_gate_held_limit_ms(std::int64_t ms) { // for tests | ||
| 623 | std::lock_guard lock(gate_mutex_); | ||
| 624 | held_limit_ms_ = (std::max)(ms, std::int64_t{0}); | ||
| 625 | } | ||
| 626 | std::int64_t gate_wait_limit_ms() { | ||
| 627 | std::lock_guard lock(gate_mutex_); | ||
| 628 | return wait_limit_ms_; | ||
| 629 | } | ||
| 630 | void set_gate_wait_limit_ms(std::int64_t ms) { // for tests | ||
| 631 | std::lock_guard lock(gate_mutex_); | ||
| 632 | wait_limit_ms_ = (std::max)(ms, std::int64_t{0}); | ||
| 633 | } | ||
| 634 | std::int64_t gate_probe_budget_ms() { | ||
| 635 | std::lock_guard lock(gate_mutex_); | ||
| 636 | return probe_budget_ms_; | ||
| 637 | } | ||
| 638 | void set_gate_probe_budget_ms(std::int64_t ms) { // for tests | ||
| 639 | std::lock_guard lock(gate_mutex_); | ||
| 640 | probe_budget_ms_ = (std::max)(ms, std::int64_t{0}); | ||
| 641 | } | ||
| 642 | void set_gate_adaptive(bool on) { // for tests | ||
| 643 | std::lock_guard lock(gate_mutex_); | ||
| 644 | adaptive_ = on; | ||
| 645 | } | ||
| 646 | void set_gate_graphics(bool on) { // for tests: whether graphics creates pass the gate | ||
| 647 | std::lock_guard lock(gate_mutex_); | ||
| 648 | graphics_ = on; | ||
| 649 | } | ||
| 650 | GateVerdict gate_verdict(CreateKind kind = CreateKind::compute) { | ||
| 651 | std::lock_guard lock(gate_mutex_); | ||
| 652 | return verdict_[std::size_t(kind)]; | ||
| 653 | } | ||
| 654 | |||
| 655 | // How many "reached the limit, but a create of a kind that holds ..." notices were due (for tests: one per wait). | ||
| 656 | unsigned gate_held_notices() { | ||
| 657 | std::lock_guard lock(gate_mutex_); | ||
| 658 | return logged_held_waits_; | ||
| 431 | } | 659 | } |
| 432 | 660 | ||
| 433 | // The thread waiting in enter_submit for creates to leave the driver, or 0. | 661 | // The thread waiting in enter_submit for creates to leave the driver, or 0. |
| @@ -436,6 +664,7 @@ public: | |||
| 436 | struct GateStats { | 664 | struct GateStats { |
| 437 | std::uint64_t submits = 0; // submissions that had to wait for a create | 665 | std::uint64_t submits = 0; // submissions that had to wait for a create |
| 438 | std::int64_t submit_ms = 0, submit_max_ms = 0; | 666 | std::int64_t submit_ms = 0, submit_max_ms = 0; |
| 667 | std::uint64_t ahead = 0; // of those, the ones that went ahead at the wait limit | ||
| 439 | std::uint64_t creates = 0; // creates that waited behind a submission | 668 | std::uint64_t creates = 0; // creates that waited behind a submission |
| 440 | std::int64_t create_ms = 0, create_max_ms = 0; | 669 | std::int64_t create_ms = 0, create_max_ms = 0; |
| 441 | }; | 670 | }; |
| @@ -444,64 +673,342 @@ public: | |||
| 444 | return stats_; | 673 | return stats_; |
| 445 | } | 674 | } |
| 446 | 675 | ||
| 447 | std::int64_t enter_compile() { | 676 | // `seq` receives the create's number, which leave_compile takes back. |
| 677 | std::int64_t enter_compile(std::uint64_t& seq, const CreateInfo& info = {}) { | ||
| 448 | if (!compile_gate_enabled()) return 0; | 678 | if (!compile_gate_enabled()) return 0; |
| 449 | std::unique_lock lock(gate_mutex_); | 679 | std::unique_lock lock(gate_mutex_); |
| 450 | std::int64_t waited = 0; | 680 | std::int64_t waited = 0; |
| 451 | if (submits_) { | 681 | if (info.kind == CreateKind::graphics && !graphics_) { // the classic gate: compute creates only |
| 682 | seq = 0; | ||
| 683 | return 0; | ||
| 684 | } | ||
| 685 | // Not behind this thread's own submission (it holds the submit lock: none of its submissions is in the | ||
| 686 | // driver now, and waiting for them would never end). | ||
| 687 | const bool own = owner_.load(std::memory_order_relaxed) == diagnostic_thread_id(); | ||
| 688 | if (submits_ && !own && !kind_off(info.kind)) { | ||
| 452 | const std::int64_t start = diagnostic_ms(); | 689 | const std::int64_t start = diagnostic_ms(); |
| 453 | gate_changed_.wait(lock, [&] { return submits_ == 0; }); | 690 | gate_changed_.wait(lock, [&] { return submits_ == 0 || kind_off(info.kind); }); |
| 454 | waited = diagnostic_ms() - start; | 691 | waited = diagnostic_ms() - start; |
| 455 | ++stats_.creates; | 692 | ++stats_.creates; |
| 456 | stats_.create_ms += waited; | 693 | stats_.create_ms += waited; |
| 457 | stats_.create_max_ms = (std::max)(stats_.create_max_ms, waited); | 694 | stats_.create_max_ms = (std::max)(stats_.create_max_ms, waited); |
| 458 | } | 695 | } |
| 459 | ++compiles_; | 696 | seq = ++last_create_; |
| 697 | in_driver_.push_back(InDriver{seq, info, diagnostic_ms(), diagnostic_thread_id()}); | ||
| 460 | return waited; | 698 | return waited; |
| 461 | } | 699 | } |
| 462 | void leave_compile() { | 700 | void leave_compile(std::uint64_t seq) { |
| 463 | if (!compile_gate_enabled()) return; | 701 | if (!compile_gate_enabled()) return; |
| 464 | std::lock_guard lock(gate_mutex_); | 702 | std::lock_guard lock(gate_mutex_); |
| 465 | if (--compiles_ == 0 && submits_) gate_changed_.notify_all(); | 703 | for (auto it = in_driver_.begin(); it != in_driver_.end(); ++it) |
| 704 | if (it->seq == seq) { | ||
| 705 | recent_leaves_[leave_count_++ % kRecentLeaves] = Leave{seq, diagnostic_ms(), it->info.kind}; | ||
| 706 | in_driver_.erase(it); | ||
| 707 | break; | ||
| 708 | } | ||
| 709 | if (submits_) gate_changed_.notify_all(); | ||
| 466 | } | 710 | } |
| 467 | 711 | ||
| 468 | std::int64_t enter_submit() { | 712 | // `ahead` (optional) is told whether the submission entered the driver alongside creates the gate does not wait |
| 713 | // for, and whether it went ahead at the wait limit or the probe budget, for leave_submit. | ||
| 714 | // `probe` false (a present or an idle or semaphore wait): it never goes ahead at the probe budget, only at the | ||
| 715 | // wait limit, since its time in the driver judges nothing. | ||
| 716 | std::int64_t enter_submit(GateAhead* ahead = nullptr, bool probe = true) { | ||
| 469 | if (!compile_gate_enabled()) return 0; | 717 | if (!compile_gate_enabled()) return 0; |
| 470 | std::unique_lock lock(gate_mutex_); | 718 | std::unique_lock lock(gate_mutex_); |
| 471 | ++submits_; | 719 | ++submits_; |
| 472 | if (!compiles_) return 0; | 720 | const InDriver* first = first_blocking(); |
| 721 | if (!first) { | ||
| 722 | if (ahead) *ahead = beside(diagnostic_ms(), false); | ||
| 723 | return 0; | ||
| 724 | } | ||
| 473 | const std::uint32_t self = diagnostic_thread_id(); | 725 | const std::uint32_t self = diagnostic_thread_id(); |
| 474 | gate_waiter_.store(self, std::memory_order_relaxed); | 726 | gate_waiter_.store(self, std::memory_order_relaxed); |
| 475 | const std::int64_t start = diagnostic_ms(); | 727 | const std::int64_t start = diagnostic_ms(); |
| 476 | const GateWait what{self, compiles_, driver_compile.name.load(std::memory_order_acquire), | 728 | const GateWait what{self, blocking(), first->info}; |
| 477 | driver_compile.trigger.load(std::memory_order_relaxed), | ||
| 478 | driver_compile.bytes.load(std::memory_order_relaxed)}; | ||
| 479 | std::int64_t next_note = 2000; | 729 | std::int64_t next_note = 2000; |
| 480 | while (!gate_changed_.wait_for(lock, std::chrono::milliseconds(250), [&] { return compiles_ == 0; })) { | 730 | const char* why = nullptr; // set: it goes ahead |
| 481 | const std::int64_t waited = diagnostic_ms() - start; | 731 | std::int64_t limit = 0, budget = 0; // in force at the last check |
| 482 | if (waited < next_note) continue; | 732 | std::int64_t window_ms = 0; // with the probe budget: the waits of the window, this one's included |
| 483 | next_note = waited + 5000; | 733 | bool held_noted = false; // the "reached the limit, but ..." notice, once per wait |
| 484 | note_gate_wait("has waited", what, waited); | 734 | for (;;) { |
| 735 | const std::int64_t now = diagnostic_ms(); | ||
| 736 | const std::int64_t waited = now - start; | ||
| 737 | // Checked each time: a create of a kind that holds submissions, not gone past, may have left (or entered) | ||
| 738 | // since, while creates of other kinds stay. | ||
| 739 | const bool held = held_blocking(); | ||
| 740 | // The plain limit, and the probe budget only for a probe while every kind it waits for has no verdict. | ||
| 741 | const std::int64_t plain_budget = probe && adaptive_ && blocking_kinds_pending() ? probe_budget_ms_ : 0; | ||
| 742 | const char* plain = nullptr; | ||
| 743 | if (plain_budget > 0) window_ms = recent_waits_ms(now) + waited; | ||
| 744 | if (wait_limit_ms_ > 0 && waited >= wait_limit_ms_) { | ||
| 745 | plain = "limit"; | ||
| 746 | } else if (plain_budget > 0 && window_ms >= plain_budget) { | ||
| 747 | plain = "probe budget"; | ||
| 748 | } | ||
| 749 | if (!held) { | ||
| 750 | limit = wait_limit_ms_; | ||
| 751 | budget = plain_budget; | ||
| 752 | why = plain; | ||
| 753 | } else { | ||
| 754 | // Not into the driver beside a create of a kind that holds submissions: there it would wait for the | ||
| 755 | // create anyway, and the driver lost the device under such waits (621.40, a mesh create). It waits | ||
| 756 | // here for those, up to the held limit (LEONIDA_COMPILE_GATE_HELD_WAIT_MS; 0: however long), which | ||
| 757 | // applies on its own, also when it is shorter than the plain limit. | ||
| 758 | limit = held_limit_ms_; | ||
| 759 | budget = 0; | ||
| 760 | if (limit > 0 && waited >= limit) why = "held limit"; | ||
| 761 | if (plain && !why && !held_noted) { | ||
| 762 | held_noted = true; | ||
| 763 | if (logged_held_waits_++ < 8) | ||
| 764 | std::fprintf(stderr, "[rt] compile-gate: vkQueueSubmit on thread %u reached the %s after %lld " | ||
| 765 | "ms, but a create of a kind that holds submissions is in the driver: it " | ||
| 766 | "waits until that create leaves or %lld ms have passed\n", | ||
| 767 | self, plain, static_cast<long long>(waited), | ||
| 768 | static_cast<long long>(held_limit_ms_)); | ||
| 769 | } | ||
| 770 | } | ||
| 771 | if (why) { | ||
| 772 | // Every create in the driver now stops holding submissions back; later ones are waited for. | ||
| 773 | overdue_through_ = last_create_; | ||
| 774 | if (why[0] == 'h') held_overdue_through_ = last_create_; | ||
| 775 | gate_changed_.notify_all(); | ||
| 776 | break; | ||
| 777 | } | ||
| 778 | if (waited >= next_note) { | ||
| 779 | next_note = waited + 5000; | ||
| 780 | note_gate_wait("has waited", what, waited); | ||
| 781 | } | ||
| 782 | std::int64_t step = 250; | ||
| 783 | if (limit > 0) step = (std::min)(step, limit - waited); | ||
| 784 | if (budget > 0) step = (std::min)(step, budget - window_ms); | ||
| 785 | step = (std::max)(step, std::int64_t{1}); | ||
| 786 | // Woken when no create holds it back any more, or when whether a holding create does has changed. | ||
| 787 | if (gate_changed_.wait_for(lock, std::chrono::milliseconds(step), | ||
| 788 | [&] { return !first_blocking() || held_blocking() != held; }) && | ||
| 789 | !first_blocking()) | ||
| 790 | break; | ||
| 485 | } | 791 | } |
| 486 | gate_waiter_.store(0, std::memory_order_relaxed); | 792 | gate_waiter_.store(0, std::memory_order_relaxed); |
| 487 | const std::int64_t waited = diagnostic_ms() - start; | 793 | const std::int64_t end = diagnostic_ms(); |
| 794 | const std::int64_t waited = end - start; | ||
| 795 | remember_wait(start, end); | ||
| 488 | ++stats_.submits; | 796 | ++stats_.submits; |
| 489 | stats_.submit_ms += waited; | 797 | stats_.submit_ms += waited; |
| 490 | stats_.submit_max_ms = (std::max)(stats_.submit_max_ms, waited); | 798 | stats_.submit_max_ms = (std::max)(stats_.submit_max_ms, waited); |
| 491 | // Every wait of 500 ms or more, and the first 32 of 50 ms or more. | 799 | const auto creates = static_cast<unsigned>(in_driver_.size()); |
| 492 | if (waited >= 500 || (waited >= 50 && logged_waits_ < 32)) { | 800 | if (why && logged_go_ahead_ >= 16) { |
| 801 | ++stats_.ahead; // the stats line counts the rest | ||
| 802 | } else if (why) { | ||
| 803 | ++stats_.ahead; | ||
| 804 | ++logged_go_ahead_; | ||
| 805 | note_gate_wait("goes ahead after", what, waited); | ||
| 806 | if (why[0] == 'l') | ||
| 807 | std::fprintf(stderr, "[rt] compile-gate: at the %lld ms limit (LEONIDA_COMPILE_GATE_WAIT_MS): the %u " | ||
| 808 | "create(s) still in the driver hold no submission back from now on (%llu " | ||
| 809 | "submissions went ahead so far)\n", | ||
| 810 | static_cast<long long>(limit), creates, static_cast<unsigned long long>(stats_.ahead)); | ||
| 811 | else if (why[0] == 'h') | ||
| 812 | std::fprintf(stderr, "[rt] compile-gate: at the %lld ms held limit (LEONIDA_COMPILE_GATE_HELD_WAIT_MS): " | ||
| 813 | "the %u create(s) still in the driver hold no submission back from now on, those " | ||
| 814 | "of a kind that holds submissions included (%llu submissions went ahead so far)\n", | ||
| 815 | static_cast<long long>(limit), creates, static_cast<unsigned long long>(stats_.ahead)); | ||
| 816 | else | ||
| 817 | std::fprintf(stderr, "[rt] compile-gate: the waits of the last %lld s add up to %lld ms, the probe " | ||
| 818 | "budget is %lld ms (LEONIDA_COMPILE_GATE_PROBE_MS): the %u create(s) still in the " | ||
| 819 | "driver hold no submission back from now on (%llu submissions went ahead so far)\n", | ||
| 820 | static_cast<long long>(kProbeWindowMs / 1000), static_cast<long long>(window_ms), | ||
| 821 | static_cast<long long>(budget), creates, static_cast<unsigned long long>(stats_.ahead)); | ||
| 822 | } else if (waited >= 500 || (waited >= 50 && logged_waits_ < 32)) { | ||
| 823 | // Every wait of 500 ms or more, and the first 32 of 50 ms or more. | ||
| 493 | ++logged_waits_; | 824 | ++logged_waits_; |
| 494 | note_gate_wait("waited", what, waited); | 825 | note_gate_wait("waited", what, waited); |
| 495 | } | 826 | } |
| 827 | if (ahead) *ahead = beside(end, why != nullptr); | ||
| 496 | return waited; | 828 | return waited; |
| 497 | } | 829 | } |
| 498 | void leave_submit() { | 830 | void leave_submit(const GateAhead& ahead = {}) { |
| 499 | if (!compile_gate_enabled()) return; | 831 | if (!compile_gate_enabled()) return; |
| 500 | std::lock_guard lock(gate_mutex_); | 832 | std::lock_guard lock(gate_mutex_); |
| 833 | if (ahead.since_ms >= 0 && ahead.probe) probe(ahead); | ||
| 501 | if (--submits_ == 0) gate_changed_.notify_all(); | 834 | if (--submits_ == 0) gate_changed_.notify_all(); |
| 502 | } | 835 | } |
| 503 | 836 | ||
| 837 | // On a device loss (VulkanContext::mark_device_lost, once): the creates in the driver, how long each has been | ||
| 838 | // there and whether submissions wait for it, and the verdict of each kind. | ||
| 839 | void report_creates_on_loss() noexcept { | ||
| 840 | if (!compile_gate_enabled()) return; | ||
| 841 | try { | ||
| 842 | std::lock_guard lock(gate_mutex_); | ||
| 843 | const std::int64_t now = diagnostic_ms(); | ||
| 844 | std::fprintf(stderr, "[rt] compile-gate: at the device loss (t=%.3f s): compute gate %s, graphics gate %s; " | ||
| 845 | "%zu pipeline create(s) in the driver\n", | ||
| 846 | double(now) / 1000.0, verdict_name(CreateKind::compute), verdict_name(CreateKind::graphics), | ||
| 847 | in_driver_.size()); | ||
| 848 | for (const auto& c : in_driver_) | ||
| 849 | std::fprintf(stderr, "[rt] compile-gate: %s create %s (%zu B SPIR-V, %s) on thread %u, in the driver " | ||
| 850 | "for %lld ms, %s\n", | ||
| 851 | kind_name(c.info.kind), c.info.name ? c.info.name : "-", c.info.bytes, | ||
| 852 | c.info.trigger ? c.info.trigger : "-", c.thread, static_cast<long long>(now - c.since_ms), | ||
| 853 | is_blocking(c) ? "submissions wait for it" : "submissions do not wait for it"); | ||
| 854 | } catch (...) { | ||
| 855 | } | ||
| 856 | } | ||
| 857 | |||
| 504 | private: | 858 | private: |
| 859 | struct InDriver { | ||
| 860 | std::uint64_t seq; | ||
| 861 | CreateInfo info; | ||
| 862 | std::int64_t since_ms; | ||
| 863 | std::uint32_t thread; | ||
| 864 | }; | ||
| 865 | |||
| 866 | // With gate_mutex_ held. | ||
| 867 | bool kind_off(CreateKind kind) const { | ||
| 868 | return verdict_[std::size_t(kind)] == GateVerdict::driver_does_not_hold; | ||
| 869 | } | ||
| 870 | // A create a submission waits for: of a kind whose gate is on, and either of a kind that holds submissions or not | ||
| 871 | // gone past at the limit or the budget. | ||
| 872 | bool is_blocking(const InDriver& c) const { | ||
| 873 | if (kind_off(c.info.kind)) return false; | ||
| 874 | if (verdict_[std::size_t(c.info.kind)] == GateVerdict::driver_holds) return c.seq > held_overdue_through_; | ||
| 875 | return c.seq > overdue_through_; | ||
| 876 | } | ||
| 877 | bool held_blocking() const { | ||
| 878 | for (const auto& c : in_driver_) | ||
| 879 | if (verdict_[std::size_t(c.info.kind)] == GateVerdict::driver_holds && c.seq > held_overdue_through_) | ||
| 880 | return true; | ||
| 881 | return false; | ||
| 882 | } | ||
| 883 | const InDriver* first_blocking() const { | ||
| 884 | for (const auto& c : in_driver_) | ||
| 885 | if (is_blocking(c)) return &c; | ||
| 886 | return nullptr; | ||
| 887 | } | ||
| 888 | std::uint32_t blocking() const { | ||
| 889 | std::uint32_t n = 0; | ||
| 890 | for (const auto& c : in_driver_) n += is_blocking(c); | ||
| 891 | return n; | ||
| 892 | } | ||
| 893 | bool blocking_kinds_pending() const { | ||
| 894 | for (const auto& c : in_driver_) | ||
| 895 | if (is_blocking(c) && verdict_[std::size_t(c.info.kind)] != GateVerdict::pending) return false; | ||
| 896 | return true; | ||
| 897 | } | ||
| 898 | // The creates a submission entering the driver at `now` does not wait for (all of those in it: it got past the | ||
| 899 | // gate). | ||
| 900 | GateAhead beside(std::int64_t now, bool went_ahead) const { | ||
| 901 | GateAhead a; | ||
| 902 | if (in_driver_.empty()) return a; | ||
| 903 | a.since_ms = now; | ||
| 904 | a.last_create = last_create_; | ||
| 905 | a.creates = static_cast<std::uint32_t>(in_driver_.size()); | ||
| 906 | for (const auto& c : in_driver_) a.kinds |= std::uint8_t(1u << unsigned(c.info.kind)); | ||
| 907 | a.went_ahead = went_ahead; | ||
| 908 | return a; | ||
| 909 | } | ||
| 910 | const char* verdict_name(CreateKind kind) const { | ||
| 911 | switch (verdict_[std::size_t(kind)]) { | ||
| 912 | case GateVerdict::driver_does_not_hold: return "off (the driver does not hold submissions)"; | ||
| 913 | case GateVerdict::driver_holds: return rearmed_[std::size_t(kind)] ? "on again (re-armed)" : "on (held probe)"; | ||
| 914 | default: return adaptive_ ? "on (no verdict yet)" : "on"; | ||
| 915 | } | ||
| 916 | } | ||
| 917 | |||
| 918 | // With gate_mutex_ held: the submissions' past waits at the gate, for the probe budget. The last kRecentWaits | ||
| 919 | // are kept; more in one window are short ones, so dropping the oldest of them costs little. | ||
| 920 | static constexpr std::size_t kRecentWaits = 64; | ||
| 921 | void remember_wait(std::int64_t start, std::int64_t end) { | ||
| 922 | if (end <= start) return; | ||
| 923 | recent_waits_[recent_count_++ % kRecentWaits] = {start, end}; | ||
| 924 | } | ||
| 925 | // How much of the last kProbeWindowMs before `now` they took. | ||
| 926 | std::int64_t recent_waits_ms(std::int64_t now) const { | ||
| 927 | const std::int64_t from = now - kProbeWindowMs; | ||
| 928 | std::int64_t sum = 0; | ||
| 929 | for (std::size_t i = 0; i < (std::min)(recent_count_, kRecentWaits); ++i) { | ||
| 930 | const auto& [start, end] = recent_waits_[i]; | ||
| 931 | sum += (std::max)(std::int64_t{0}, (std::min)(end, now) - (std::max)(start, from)); | ||
| 932 | } | ||
| 933 | return sum; | ||
| 934 | } | ||
| 935 | |||
| 936 | // With gate_mutex_ held, as a submission leaves the driver: what its time there says about the driver, for each | ||
| 937 | // kind with a create in the driver from before it entered until now (see the gate's comment). The ones that went | ||
| 938 | // ahead are logged (the first 8), each verdict once. | ||
| 939 | void probe(const GateAhead& ahead) { | ||
| 940 | const std::int64_t now = diagnostic_ms(); | ||
| 941 | const std::int64_t took = now - ahead.since_ms; | ||
| 942 | std::uint32_t still = 0; | ||
| 943 | std::uint8_t kinds = 0; // of the creates it entered beside, the kinds still in the driver | ||
| 944 | for (const auto& c : in_driver_) | ||
| 945 | if (c.seq <= ahead.last_create) { | ||
| 946 | ++still; | ||
| 947 | kinds |= std::uint8_t(1u << unsigned(c.info.kind)); | ||
| 948 | } | ||
| 949 | kinds &= ahead.kinds; | ||
| 950 | if (ahead.went_ahead && logged_ahead_ < 8) { | ||
| 951 | ++logged_ahead_; | ||
| 952 | std::fprintf(stderr, "[rt] compile-gate: the submission that went ahead on thread %u was in the driver " | ||
| 953 | "for %lld ms; %u of the %u create(s) it went past are still there\n", | ||
| 954 | diagnostic_thread_id(), static_cast<long long>(took), still, ahead.creates); | ||
| 955 | } | ||
| 956 | if (!adaptive_) return; | ||
| 957 | for (std::size_t k = 0; k < kKinds; ++k) { | ||
| 958 | if (!(ahead.kinds & (1u << k))) continue; | ||
| 959 | if (took >= kProbeHeldMs) { | ||
| 960 | // Held behind a create of this kind only if one it entered beside has just left. | ||
| 961 | bool left_just_before = false; | ||
| 962 | for (std::size_t i = 0; i < (std::min)(leave_count_, kRecentLeaves) && !left_just_before; ++i) { | ||
| 963 | const Leave& l = recent_leaves_[i]; | ||
| 964 | left_just_before = std::size_t(l.kind) == k && l.seq <= ahead.last_create && | ||
| 965 | l.ms >= ahead.since_ms && now - l.ms <= kHeldReturnMs; | ||
| 966 | } | ||
| 967 | if (left_just_before) { | ||
| 968 | judge(CreateKind(k), took, now); | ||
| 969 | } else if (logged_not_held_++ < 8) { | ||
| 970 | std::fprintf(stderr, "[rt] compile-gate: a submission stayed %lld ms in the driver beside a %s " | ||
| 971 | "create but came back %s: not counted as held by it\n", | ||
| 972 | static_cast<long long>(took), kind_name(CreateKind(k)), | ||
| 973 | (kinds & (1u << k)) ? "with the create still there" : "long after the create left"); | ||
| 974 | } | ||
| 975 | } else if (kinds & (1u << k)) { | ||
| 976 | judge(CreateKind(k), took, now); | ||
| 977 | } | ||
| 978 | } | ||
| 979 | } | ||
| 980 | void judge(CreateKind kind, std::int64_t took, std::int64_t now) { | ||
| 981 | const std::size_t k = std::size_t(kind); | ||
| 982 | GateVerdict& verdict = verdict_[k]; | ||
| 983 | if (verdict == GateVerdict::driver_holds) return; | ||
| 984 | if (took >= kProbeHeldMs) { | ||
| 985 | const bool was_off = verdict == GateVerdict::driver_does_not_hold; | ||
| 986 | verdict = GateVerdict::driver_holds; | ||
| 987 | rearmed_[k] = was_off; | ||
| 988 | std::fprintf(stderr, "[rt] compile-gate: a submission stayed %lld ms in the driver beside a %s create at " | ||
| 989 | "t=%.3f s: this driver may hold submissions behind %s creates, so the %s gate is %s " | ||
| 990 | "for the rest of the run\n", | ||
| 991 | static_cast<long long>(took), kind_name(kind), double(now) / 1000.0, kind_name(kind), | ||
| 992 | kind_name(kind), was_off ? "back on" : "on"); | ||
| 993 | return; | ||
| 994 | } | ||
| 995 | if (verdict != GateVerdict::pending || took >= kProbeQuickMs) return; // off already, or says nothing | ||
| 996 | if (quick_probes_[k]++ == 0) first_quick_ms_[k] = now; | ||
| 997 | longest_quick_ms_[k] = (std::max)(longest_quick_ms_[k], took); | ||
| 998 | if (quick_probes_[k] < kQuickProbes || now - first_quick_ms_[k] < kProbeSpanMs) return; | ||
| 999 | verdict = GateVerdict::driver_does_not_hold; | ||
| 1000 | gate_changed_.notify_all(); // the submissions and creates waiting now go | ||
| 1001 | std::fprintf(stderr, "[rt] compile-gate: %s gate off at t=%.3f s: %u submissions entered the driver beside a " | ||
| 1002 | "%s create and came back in at most %lld ms (under %lld ms), the first and last %lld ms " | ||
| 1003 | "apart, the create still there: this driver does not hold a submission behind a %s " | ||
| 1004 | "create. Still timed: one that stays %lld ms turns it back on (LEONIDA_COMPILE_GATE=1 " | ||
| 1005 | "keeps the gate)\n", | ||
| 1006 | kind_name(kind), double(now) / 1000.0, quick_probes_[k], kind_name(kind), | ||
| 1007 | static_cast<long long>(longest_quick_ms_[k]), static_cast<long long>(kProbeQuickMs), | ||
| 1008 | static_cast<long long>(now - first_quick_ms_[k]), kind_name(kind), | ||
| 1009 | static_cast<long long>(kProbeHeldMs)); | ||
| 1010 | } | ||
| 1011 | |||
| 505 | void taken() { | 1012 | void taken() { |
| 506 | since_.store(diagnostic_ms(), std::memory_order_relaxed); | 1013 | since_.store(diagnostic_ms(), std::memory_order_relaxed); |
| 507 | owner_.store(diagnostic_thread_id(), std::memory_order_relaxed); | 1014 | owner_.store(diagnostic_thread_id(), std::memory_order_relaxed); |
| @@ -518,23 +1025,21 @@ private: | |||
| 518 | } | 1025 | } |
| 519 | 1026 | ||
| 520 | // With gate_mutex_ held. | 1027 | // With gate_mutex_ held. |
| 521 | // The creates a submission found in the driver: how many, and the compute one named last (graphics creates are | 1028 | // The creates a submission found in the driver: how many it waits for, and the first of them. |
| 522 | // not named). Names are ShaderLibrary strings, valid until device teardown. | ||
| 523 | struct GateWait { | 1029 | struct GateWait { |
| 524 | std::uint32_t thread; | 1030 | std::uint32_t thread; |
| 525 | std::uint32_t creates; | 1031 | std::uint32_t creates; |
| 526 | const char* name; | 1032 | CreateInfo first; |
| 527 | const char* trigger; | ||
| 528 | std::size_t bytes; | ||
| 529 | }; | 1033 | }; |
| 530 | void note_gate_wait(const char* how, const GateWait& what, std::int64_t waited) const { | 1034 | void note_gate_wait(const char* how, const GateWait& what, std::int64_t waited) const { |
| 531 | std::fprintf(stderr, "[rt] compile-gate: vkQueueSubmit on thread %u %s %lld ms for %u pipeline create(s) to " | 1035 | std::fprintf(stderr, "[rt] compile-gate: vkQueueSubmit on thread %u %s %lld ms for %u pipeline create(s) to " |
| 532 | "leave the driver (compute %s, %zu B SPIR-V, %s) at t=%.3f s; %llu such waits so far, " | 1036 | "leave the driver (%s %s, %zu B SPIR-V, %s) at t=%.3f s; %llu such waits so far, " |
| 533 | "%lld ms in all, longest %lld ms\n", | 1037 | "%lld ms in all, longest %lld ms\n", |
| 534 | what.thread, how, static_cast<long long>(waited), what.creates, what.name ? what.name : "-", | 1038 | what.thread, how, static_cast<long long>(waited), what.creates, kind_name(what.first.kind), |
| 535 | what.name ? what.bytes : std::size_t{0}, what.name && what.trigger ? what.trigger : "-", | 1039 | what.first.name ? what.first.name : "-", what.first.bytes, |
| 536 | double(diagnostic_ms()) / 1000.0, static_cast<unsigned long long>(stats_.submits), | 1040 | what.first.trigger ? what.first.trigger : "-", double(diagnostic_ms()) / 1000.0, |
| 537 | static_cast<long long>(stats_.submit_ms), static_cast<long long>(stats_.submit_max_ms)); | 1041 | static_cast<unsigned long long>(stats_.submits), static_cast<long long>(stats_.submit_ms), |
| 1042 | static_cast<long long>(stats_.submit_max_ms)); | ||
| 538 | } | 1043 | } |
| 539 | 1044 | ||
| 540 | std::mutex mutex_; | 1045 | std::mutex mutex_; |
| @@ -548,11 +1053,40 @@ private: | |||
| 548 | 1053 | ||
| 549 | std::mutex gate_mutex_; // leaf: guards the gate counts and stats | 1054 | std::mutex gate_mutex_; // leaf: guards the gate counts and stats |
| 550 | std::condition_variable gate_changed_; | 1055 | std::condition_variable gate_changed_; |
| 551 | std::uint32_t compiles_ = 0; // creates in the driver | 1056 | std::vector<InDriver> in_driver_; // the creates in the driver now, oldest first |
| 1057 | std::uint64_t last_create_ = 0; // the number of the last create that entered | ||
| 1058 | std::uint64_t overdue_through_ = 0; // creates up to this number were in the driver when a submission went ahead | ||
| 1059 | std::uint64_t held_overdue_through_ = 0; // the same, for creates of a kind that holds (at the held limit) | ||
| 1060 | std::int64_t held_limit_ms_ = default_gate_held_limit_ms(); // 0: no limit | ||
| 552 | std::uint32_t submits_ = 0; // submissions waiting for them or in the driver | 1061 | std::uint32_t submits_ = 0; // submissions waiting for them or in the driver |
| 1062 | std::int64_t wait_limit_ms_ = default_gate_wait_limit_ms(); // 0: no limit | ||
| 1063 | std::int64_t probe_budget_ms_ = default_gate_probe_budget_ms(); // 0: no early probes | ||
| 1064 | bool adaptive_ = compile_gate_mode() == GateMode::adaptive; | ||
| 1065 | bool graphics_ = compile_gate_graphics(); // graphics creates pass the gate | ||
| 1066 | std::array<std::pair<std::int64_t, std::int64_t>, kRecentWaits> recent_waits_{}; // [start, end) ms | ||
| 1067 | std::size_t recent_count_ = 0; | ||
| 1068 | // Per CreateKind. | ||
| 1069 | std::array<GateVerdict, kKinds> verdict_{GateVerdict::pending, GateVerdict::pending}; | ||
| 1070 | std::array<bool, kKinds> rearmed_{}; | ||
| 1071 | // The last creates to leave the driver: a held probe needs one it entered beside to have just left. A ring, so a | ||
| 1072 | // later create leaving does not hide an earlier one that did. | ||
| 1073 | struct Leave { | ||
| 1074 | std::uint64_t seq = 0; | ||
| 1075 | std::int64_t ms = 0; | ||
| 1076 | CreateKind kind{}; | ||
| 1077 | }; | ||
| 1078 | static constexpr std::size_t kRecentLeaves = 64; | ||
| 1079 | std::array<Leave, kRecentLeaves> recent_leaves_{}; | ||
| 1080 | std::size_t leave_count_ = 0; | ||
| 1081 | unsigned logged_not_held_ = 0; | ||
| 1082 | std::array<unsigned, kKinds> quick_probes_{}; | ||
| 1083 | std::array<std::int64_t, kKinds> first_quick_ms_{}, longest_quick_ms_{}; | ||
| 1084 | unsigned logged_held_waits_ = 0; | ||
| 553 | std::atomic<std::uint32_t> gate_waiter_{0}; | 1085 | std::atomic<std::uint32_t> gate_waiter_{0}; |
| 554 | GateStats stats_; | 1086 | GateStats stats_; |
| 555 | unsigned logged_waits_ = 0; | 1087 | unsigned logged_waits_ = 0; |
| 1088 | unsigned logged_ahead_ = 0; | ||
| 1089 | unsigned logged_go_ahead_ = 0; | ||
| 556 | }; | 1090 | }; |
| 557 | 1091 | ||
| 558 | // One vkQueueSubmit of a VulkanQueue: its waits, its signals and how long the call took (-1: not returned yet). | 1092 | // One vkQueueSubmit of a VulkanQueue: its waits, its signals and how long the call took (-1: not returned yet). |
diff --git a/runtime/src/vulkan_present.h b/runtime/src/vulkan_present.h index 81f3b57e1..da9376e54 100644 --- a/runtime/src/vulkan_present.h +++ b/runtime/src/vulkan_present.h | |||
| @@ -212,7 +212,7 @@ public: | |||
| 212 | const VkDevice device = context_.device(); | 212 | const VkDevice device = context_.device(); |
| 213 | { | 213 | { |
| 214 | std::lock_guard lock(context_.submit_mutex()); | 214 | std::lock_guard lock(context_.submit_mutex()); |
| 215 | const SubmitMutex::SubmitTurn turn(context_.submit_mutex()); // a queue operation: the compile gate | 215 | const SubmitMutex::SubmitTurn turn(context_.submit_mutex(), false); // a queue operation: the compile gate |
| 216 | context_.observe_result(vk.vkQueueWaitIdle(queue_), "vkQueueWaitIdle (present)"); | 216 | context_.observe_result(vk.vkQueueWaitIdle(queue_), "vkQueueWaitIdle (present)"); |
| 217 | // A WSI layer may perform a device-wide idle while destroying the swapchain. | 217 | // A WSI layer may perform a device-wide idle while destroying the swapchain. |
| 218 | // Keep that implicit idle serialized with submits from every context queue. | 218 | // Keep that implicit idle serialized with submits from every context queue. |
| @@ -650,8 +650,9 @@ private: | |||
| 650 | { | 650 | { |
| 651 | std::lock_guard lock(context_.submit_mutex()); | 651 | std::lock_guard lock(context_.submit_mutex()); |
| 652 | // One turn of the compile gate for the submit and the present after it: no pipeline create enters the | 652 | // One turn of the compile gate for the submit and the present after it: no pipeline create enters the |
| 653 | // driver between the two (queue_submit's own turn nests inside this one). | 653 | // driver between the two (queue_submit's own turn nests inside this one). Not a probe: it also covers |
| 654 | const SubmitMutex::SubmitTurn turn(context_.submit_mutex()); // a queue operation: the compile gate | 654 | // finish_startup_frame's fence wait and the present; the nested turn probes the vkQueueSubmit alone. |
| 655 | const SubmitMutex::SubmitTurn turn(context_.submit_mutex(), false); // a queue operation: the compile gate | ||
| 655 | submitted = context_.observe_result(context_.queue_submit(queue_, 1, &submit_info, frame.done), | 656 | submitted = context_.observe_result(context_.queue_submit(queue_, 1, &submit_info, frame.done), |
| 656 | "vkQueueSubmit (present)"); | 657 | "vkQueueSubmit (present)"); |
| 657 | if (submitted == VK_SUCCESS) context_.note_image_work_locked(); | 658 | if (submitted == VK_SUCCESS) context_.note_image_work_locked(); |
| @@ -1083,7 +1084,7 @@ private: | |||
| 1083 | const VkDevice device = context_.device(); | 1084 | const VkDevice device = context_.device(); |
| 1084 | { | 1085 | { |
| 1085 | std::lock_guard lock(context_.submit_mutex()); | 1086 | std::lock_guard lock(context_.submit_mutex()); |
| 1086 | const SubmitMutex::SubmitTurn turn(context_.submit_mutex()); // a queue operation: the compile gate | 1087 | const SubmitMutex::SubmitTurn turn(context_.submit_mutex(), false); // a queue operation: the compile gate |
| 1087 | // The old swapchain's images and semaphores may still be in use. | 1088 | // The old swapchain's images and semaphores may still be in use. |
| 1088 | context_.observe_result(vk.vkQueueWaitIdle(queue_), "vkQueueWaitIdle (present)"); | 1089 | context_.observe_result(vk.vkQueueWaitIdle(queue_), "vkQueueWaitIdle (present)"); |
| 1089 | release(swapchain_); | 1090 | release(swapchain_); |
| @@ -1174,7 +1175,7 @@ private: | |||
| 1174 | return true; | 1175 | return true; |
| 1175 | { | 1176 | { |
| 1176 | std::lock_guard lock(context_.submit_mutex()); | 1177 | std::lock_guard lock(context_.submit_mutex()); |
| 1177 | const SubmitMutex::SubmitTurn turn(context_.submit_mutex()); // a queue operation: the compile gate | 1178 | const SubmitMutex::SubmitTurn turn(context_.submit_mutex(), false); // a queue operation: the compile gate |
| 1178 | // The old target may still be in use. | 1179 | // The old target may still be in use. |
| 1179 | context_.observe_result(context_.vk.vkQueueWaitIdle(queue_), "vkQueueWaitIdle (present)"); | 1180 | context_.observe_result(context_.vk.vkQueueWaitIdle(queue_), "vkQueueWaitIdle (present)"); |
| 1180 | } | 1181 | } |
| @@ -1531,7 +1532,7 @@ private: | |||
| 1531 | const VkDevice device = context_.device(); | 1532 | const VkDevice device = context_.device(); |
| 1532 | { | 1533 | { |
| 1533 | std::lock_guard lock(context_.submit_mutex()); | 1534 | std::lock_guard lock(context_.submit_mutex()); |
| 1534 | const SubmitMutex::SubmitTurn turn(context_.submit_mutex()); // a queue operation: the compile gate | 1535 | const SubmitMutex::SubmitTurn turn(context_.submit_mutex(), false); // a queue operation: the compile gate |
| 1535 | // The old swapchain's images and semaphores may still be in use. | 1536 | // The old swapchain's images and semaphores may still be in use. |
| 1536 | context_.observe_result(vk.vkQueueWaitIdle(queue_), "vkQueueWaitIdle (present)"); | 1537 | context_.observe_result(vk.vkQueueWaitIdle(queue_), "vkQueueWaitIdle (present)"); |
| 1537 | } | 1538 | } |