diff options
| author | OreoFR <29048345+OreoFR@users.noreply.github.com> | 2026-11-27 02:53:48 +0100 |
|---|---|---|
| committer | OreoFR <29048345+OreoFR@users.noreply.github.com> | 2026-11-27 02:53:48 +0100 |
| commit | b2cccb953a4282b8b82e7f5266a0b9dc22fab68b (patch) | |
| tree | 2f2632afb67e26c9c256d72e9a5ed779adeed0be /runtime/test/submit_mutex_test.cpp | |
| parent | e5d1a14681fa65de2d8b49b2efeeec62370ec8ee (diff) | |
compile gate: probes from submissions only, a ring of recent leaves, and a held limit
Review of #2412 (nicoco23, ecox982745):
- A later create leaving hid the earlier one a held probe needs: last_leave_seq_ kept one leave per
kind, so when A (there when the submission entered) and then B (entered after it) left just before
the submission came back, B replaced A and the probe was not counted. The recent leaves are now a
ring of 64, and any leave of the kind, of a create the submission entered beside, within 500 ms
before it came back counts.
- Presents and idle or semaphore waits passed the gate as probes: they can take seconds on their own
while the GPU is busy, so a slow present could mark a kind as holding, and quick ones could turn a
kind off. SubmitTurn takes `probe` (true by default); the presents, vkQueueWaitIdle,
vkDeviceWaitIdle and vkWaitSemaphores turns pass false. vkQueueSubmit and vkQueueBindSparse probe.
- A kind that holds submissions was waited for with no limit, so a false verdict brought back the
unbounded gate for the rest of the run. It is now waited for up to LEONIDA_COMPILE_GATE_HELD_WAIT_MS
(60 s; 0: no limit), after which the creates in the driver are gone past and not waited for again.
The header comment that said a false alarm "only keeps the bounded gate" is rewritten.
Tests: submit_mutex_test gains both finish orders of A and B, B alone with A still there, a 2.1 s
present that judges nothing, and a holding kind gone past at a 400 ms held limit. All pass (MSVC,
Windows 11). The docs tests pass.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/test/submit_mutex_test.cpp')
| -rw-r--r-- | runtime/test/submit_mutex_test.cpp | 146 |
1 files changed, 145 insertions, 1 deletions
diff --git a/runtime/test/submit_mutex_test.cpp b/runtime/test/submit_mutex_test.cpp index 69b7c8e1e..6b5663d74 100644 --- a/runtime/test/submit_mutex_test.cpp +++ b/runtime/test/submit_mutex_test.cpp | |||
| @@ -10,6 +10,7 @@ | |||
| 10 | #include <chrono> | 10 | #include <chrono> |
| 11 | #include <cstdio> | 11 | #include <cstdio> |
| 12 | #include <cstdlib> | 12 | #include <cstdlib> |
| 13 | #include <memory> | ||
| 13 | #include <mutex> | 14 | #include <mutex> |
| 14 | #include <thread> | 15 | #include <thread> |
| 15 | 16 | ||
| @@ -617,13 +618,156 @@ int gate_per_kind() { | |||
| 617 | return 0; | 618 | return 0; |
| 618 | } | 619 | } |
| 619 | 620 | ||
| 621 | // A create that stays in the "driver" until `release` is set; `in` is set once it is there, `left_at` when it leaves. | ||
| 622 | struct HeldCreate { | ||
| 623 | std::atomic<bool> in{false}, release{false}; | ||
| 624 | std::atomic<std::int64_t> left_at{0}; | ||
| 625 | std::thread thread; | ||
| 626 | HeldCreate(SubmitMutex& m, const SubmitMutex::CreateInfo& info) { | ||
| 627 | thread = std::thread([&m, info, this] { | ||
| 628 | const SubmitMutex::CompileTurn turn(m, info); | ||
| 629 | in = true; | ||
| 630 | while (!release) std::this_thread::sleep_for(1ms); | ||
| 631 | left_at = now_ms(); | ||
| 632 | }); | ||
| 633 | } | ||
| 634 | void let_go() { | ||
| 635 | release = true; | ||
| 636 | if (thread.joinable()) thread.join(); | ||
| 637 | } | ||
| 638 | ~HeldCreate() { let_go(); } | ||
| 639 | }; | ||
| 640 | |||
| 641 | // The cases raised in review of #2412: a later create leaving does not hide an earlier one, presents and waits do | ||
| 642 | // not probe, and a kind that holds submissions is waited for up to the held limit only. | ||
| 643 | int gate_review_cases() { | ||
| 644 | if (!SubmitMutex::compile_gate_enabled()) { | ||
| 645 | std::printf("compile gate off (LEONIDA_COMPILE_GATE=0): review cases not checked\n"); | ||
| 646 | return 0; | ||
| 647 | } | ||
| 648 | using Verdict = SubmitMutex::GateVerdict; | ||
| 649 | using Kind = SubmitMutex::CreateKind; | ||
| 650 | const SubmitMutex::CreateInfo compute{Kind::compute, "test compute", "test", 1000}; | ||
| 651 | const auto turn_compute_off = [&](SubmitMutex& m, HeldCreate& c) { | ||
| 652 | { const SubmitMutex::SubmitTurn turn(m); } | ||
| 653 | std::this_thread::sleep_for(1100ms); | ||
| 654 | { const SubmitMutex::SubmitTurn turn(m); } | ||
| 655 | return m.gate_verdict(Kind::compute) == Verdict::driver_does_not_hold && c.in.load(); | ||
| 656 | }; | ||
| 657 | // A, there when the submission entered, leaves; then B, which entered after it, leaves; then the submission comes | ||
| 658 | // back. A's leave still counts: re-armed. | ||
| 659 | for (const bool a_first : {true, false}) { | ||
| 660 | SubmitMutex m; | ||
| 661 | m.set_gate_adaptive(true); | ||
| 662 | m.set_gate_wait_limit_ms(100); | ||
| 663 | m.set_gate_probe_budget_ms(0); | ||
| 664 | HeldCreate a(m, compute); | ||
| 665 | const bool off = eventually([&] { return a.in.load(); }) && turn_compute_off(m, a); | ||
| 666 | std::unique_ptr<HeldCreate> b; | ||
| 667 | { | ||
| 668 | const SubmitMutex::SubmitTurn turn(m); | ||
| 669 | b = std::make_unique<HeldCreate>(m, compute); // enters after the submission did | ||
| 670 | EXPECT(eventually([&] { return b->in.load(); })); | ||
| 671 | std::this_thread::sleep_for(2000ms); | ||
| 672 | if (a_first) { | ||
| 673 | a.let_go(); | ||
| 674 | std::this_thread::sleep_for(30ms); | ||
| 675 | b->let_go(); | ||
| 676 | } else { | ||
| 677 | b->let_go(); | ||
| 678 | std::this_thread::sleep_for(30ms); | ||
| 679 | a.let_go(); | ||
| 680 | } | ||
| 681 | std::this_thread::sleep_for(50ms); | ||
| 682 | } | ||
| 683 | EXPECT(off); | ||
| 684 | EXPECT(m.gate_verdict(Kind::compute) == Verdict::driver_holds); | ||
| 685 | } | ||
| 686 | std::printf("compile gate: A then B and B then A leaving just before a 2.1 s submission both re-arm compute\n"); | ||
| 687 | // B alone leaves (it entered after the submission), A is still there: not held. | ||
| 688 | { | ||
| 689 | SubmitMutex m; | ||
| 690 | m.set_gate_adaptive(true); | ||
| 691 | m.set_gate_wait_limit_ms(100); | ||
| 692 | m.set_gate_probe_budget_ms(0); | ||
| 693 | HeldCreate a(m, compute); | ||
| 694 | const bool off = eventually([&] { return a.in.load(); }) && turn_compute_off(m, a); | ||
| 695 | { | ||
| 696 | const SubmitMutex::SubmitTurn turn(m); | ||
| 697 | HeldCreate b(m, compute); | ||
| 698 | EXPECT(eventually([&] { return b.in.load(); })); | ||
| 699 | std::this_thread::sleep_for(2000ms); | ||
| 700 | b.let_go(); | ||
| 701 | std::this_thread::sleep_for(50ms); | ||
| 702 | } | ||
| 703 | EXPECT(off); | ||
| 704 | EXPECT(m.gate_verdict(Kind::compute) == Verdict::driver_does_not_hold); | ||
| 705 | } | ||
| 706 | std::printf("compile gate: a create that entered after the submission leaving does not re-arm compute\n"); | ||
| 707 | // A present or wait (probe false) that comes back 2.1 s later, just after the create left, judges nothing. | ||
| 708 | { | ||
| 709 | SubmitMutex m; | ||
| 710 | m.set_gate_adaptive(true); | ||
| 711 | m.set_gate_wait_limit_ms(100); | ||
| 712 | m.set_gate_probe_budget_ms(0); | ||
| 713 | HeldCreate a(m, compute); | ||
| 714 | EXPECT(eventually([&] { return a.in.load(); })); | ||
| 715 | { | ||
| 716 | const SubmitMutex::SubmitTurn turn(m, false); | ||
| 717 | std::this_thread::sleep_for(2000ms); | ||
| 718 | a.let_go(); | ||
| 719 | std::this_thread::sleep_for(50ms); | ||
| 720 | } | ||
| 721 | EXPECT(m.gate_verdict(Kind::compute) == Verdict::pending); | ||
| 722 | } | ||
| 723 | std::printf("compile gate: a 2.1 s present or wait judges nothing\n"); | ||
| 724 | // A kind that holds is waited for up to the held limit, then gone past; the next submission does not wait for it. | ||
| 725 | { | ||
| 726 | SubmitMutex m; | ||
| 727 | m.set_gate_adaptive(true); | ||
| 728 | m.set_gate_wait_limit_ms(100); | ||
| 729 | m.set_gate_probe_budget_ms(0); | ||
| 730 | m.set_gate_held_limit_ms(400); | ||
| 731 | { | ||
| 732 | HeldCreate first(m, compute); | ||
| 733 | EXPECT(eventually([&] { return first.in.load(); })); | ||
| 734 | const SubmitMutex::SubmitTurn turn(m); | ||
| 735 | std::this_thread::sleep_for(2000ms); | ||
| 736 | first.let_go(); | ||
| 737 | std::this_thread::sleep_for(50ms); | ||
| 738 | } | ||
| 739 | const Verdict held = m.gate_verdict(Kind::compute); | ||
| 740 | HeldCreate stays(m, compute); | ||
| 741 | EXPECT(eventually([&] { return stays.in.load(); })); | ||
| 742 | std::int64_t waited = 0, next_waited = 0; | ||
| 743 | bool ahead = false; | ||
| 744 | { | ||
| 745 | const std::int64_t start = now_ms(); | ||
| 746 | const SubmitMutex::SubmitTurn turn(m); | ||
| 747 | waited = now_ms() - start; | ||
| 748 | ahead = turn.went_ahead(); | ||
| 749 | } | ||
| 750 | { | ||
| 751 | const std::int64_t start = now_ms(); | ||
| 752 | const SubmitMutex::SubmitTurn turn(m); | ||
| 753 | next_waited = now_ms() - start; | ||
| 754 | } | ||
| 755 | EXPECT(held == Verdict::driver_holds); | ||
| 756 | EXPECT(ahead && waited >= 390 && waited < 2000); | ||
| 757 | EXPECT(next_waited < 50); | ||
| 758 | std::printf("compile gate: with a holding kind a submission waited %lld ms (held limit 400 ms), the next %lld ms\n", | ||
| 759 | static_cast<long long>(waited), static_cast<long long>(next_waited)); | ||
| 760 | } | ||
| 761 | return 0; | ||
| 762 | } | ||
| 763 | |||
| 620 | } // namespace | 764 | } // namespace |
| 621 | 765 | ||
| 622 | int main() { | 766 | int main() { |
| 623 | std::setvbuf(stdout, nullptr, _IONBF, 0); | 767 | std::setvbuf(stdout, nullptr, _IONBF, 0); |
| 624 | if (owner_tracking() || waits_for_holder() || waits_for_queued_submitter() || handoff_is_capped() || | 768 | if (owner_tracking() || waits_for_holder() || waits_for_queued_submitter() || handoff_is_capped() || |
| 625 | compile_gate() || gate_wait_limit() || gate_adapts() || | 769 | compile_gate() || gate_wait_limit() || gate_adapts() || |
| 626 | gate_probe_budget() || gate_per_kind()) | 770 | gate_probe_budget() || gate_per_kind() || gate_review_cases()) |
| 627 | return 1; | 771 | return 1; |
| 628 | std::printf("submit_mutex_test: all passed\n"); | 772 | std::printf("submit_mutex_test: all passed\n"); |
| 629 | return 0; | 773 | return 0; |