Skip to content

fix(windows): use precise waits in sub-millisecond polling loops - #1551

Merged
adriadam10 merged 2 commits into
boykopovar:mainfrom
MarcZX10:fix/precise-subtick-polls
Oct 10, 2026
Merged

adriadam10 merged 2 commits into
boykopovar:mainfrom
MarcZX10:fix/precise-subtick-polls

Conversation

@MarcZX10

@MarcZX10 MarcZX10 commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

What

Replace sub-millisecond std::this_thread::sleep_for polling with PreciseSleepUs() in three host-side loops. On Windows, winpthreads truncates these nanosleep requests to whole milliseconds, so requests below 1 ms return immediately and spin. This addresses the Windows busy-spin behavior documented in #1471.

  • AGC WAIT_REG_MEM: 50 µs
  • APR counter/address wait: 50 µs
  • Font lock backoff: 30 µs

Keep the focused precise_wait CTest for the helper.

Relationship to #1576

The production call-site changes originated in #1576 and are retained here with Shaan credited as co-author. The maintainer closed #1576 as a duplicate of this PR on 2026-10-10, identifying #1551 as the single carrier. This PR also includes the focused PreciseSleepUs helper regression test.

The AIO substitutions were removed after Windows measurements showed that the AIO wait loops return on their first check and the change had no measurable benefit. The same report found each precise wait takes about 0.5 ms instead of the requested 10–50 µs, so no AIO performance claim is made.

Overlap audit

The automated overlap notice for #1360 is only the insertion point in core/libs/prx/libc/CMakeLists.txt. #1360 registers its mapping-history test; this PR registers the precise-wait helper test. They add separate targets and test behavior, with no production-file overlap or dependency.

Tested

Current PR head is 54a90185147ea49d1913bccfed53fb477e619b9f, rebased linearly onto upstream main at 6cc33583326dd86c35f14710b984d767405d203e. The five-file diff contains three production call sites, the helper test, and its CMake registration.

  • Linux x86-64, GCC 12.2, Release, Unix Makefiles: built libc, libSceAgcDriver, libSceFont, libkernel, precise_wait_tests, agc_driver_pm4_tests, guest_aio_tests, guest_font_tests, and guest_font_writing_line_tests.
  • Focused CTest on this head: 5/5 passed (precise_wait, agc_driver_pm4, guest_aio, guest_font, guest_font_writing_line; 0.08 s total).
  • python3 tools/check_conventions.py --base upstream/main and git diff --check upstream/main...HEAD pass locally.
  • A contributor's Windows 10 / MinGW GCC 15.2 report measured sleep_for(10/50/200 µs) returning immediately and PreciseSleepUs taking about 0.50 ms; see the report. An independent Windows 11 Pro 25H2 report on earlier head d6c7e2f6, cherry-picked onto main d70b8998, used RTX 5060 Ti / NVIDIA 617.42, MinGW-w64 GCC 15.2.0, Release and a full build plus libs: precise_wait passed and full CTest passed (511/511) (report). These Windows runs predate this rebase.
  • The helper test verifies the requested minimum delay; it does not independently detect a call site reverting to sleep_for.
  • GitHub Build run 38038198535 and Conventions run 38038197304 are queued on this head. The fork eligibility gate skips full Linux and Windows builds; eligible register-context, write-watch, and relinker jobs are pending.

Checklist

@MarcZX10

MarcZX10 commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor Author

Rebased directly onto upstream/main (c68c7576); current head is 679549800f5083fffe17d5ca820af7c2e7fcc3af. The five focused Linux tests passed, including five consecutive precise_wait runs; conventions and git diff --check pass. Windows remains untested locally. Could a maintainer approve the current-head workflows, which are action_required with no jobs?

@MarcZX10

MarcZX10 commented Oct 8, 2026

Copy link
Copy Markdown
Contributor Author

Rebased this PR onto current upstream main (4b6ab6bb) and updated the body with the new head and validation. I also moved the precise_wait CMake registration beside guest_time_conversion, away from the common if(BUILD_TESTING) insertion point used by #1806 and #1814. Those PRs remain independent; this one has no dependency on either. The current CMake configure/build and focused CTest pass, and conventions plus git diff --check pass.

@MarcZX10

MarcZX10 commented Oct 8, 2026

Copy link
Copy Markdown
Contributor Author

The current-head Build, Conventions and Progress report runs were created as action_required (no PR checks are reported). The contribution guide says a maintainer must approve fork workflows before they can run. Could a maintainer approve the runs for head 0999ed5a so upstream CI can validate this rebase?

@MarcZX10

MarcZX10 commented Oct 8, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto current upstream main (364a56b; head 0812c0bc). The reviewed six-file diff is unchanged in scope. The focused helper test compiles and passes directly on this head; the prior head passed the CMake configure/build and focused CTest runs. Conventions and git diff --check pass. No checks are reported for the rebased head; Windows remains untested locally.

@MarcZX10

MarcZX10 commented Oct 8, 2026

Copy link
Copy Markdown
Contributor Author

The Build/Conventions workflow runs for current head 0812c0bcec6cfcd55af3902dda6cd2ba561a857f are marked action_required and have no jobs, so upstream CI has not run yet. CONTRIBUTING requires maintainer approval for fork workflows. Could an authorized maintainer approve these current-head runs?

@MarcZX10
MarcZX10 force-pushed the fix/precise-subtick-polls branch from 0812c0b to b439aa2 Compare October 8, 2026 21:50
@MarcZX10

MarcZX10 commented Oct 8, 2026

Copy link
Copy Markdown
Contributor Author

The fork Build on the previous head found a Windows link failure in precise_wait_tests.exe: undefined __gxx_personality_v0. The test target was missing configure_windows_unwind, which the repository applies to other C++ test executables. Added that configuration in head b439aa29, rebased onto current main 94b1eecb; conventions, diff check and the focused helper test pass. I dispatched a new fork Build (37849602930); its Windows result is still pending. Upstream checks remain action_required.

@MarcZX10
MarcZX10 force-pushed the fix/precise-subtick-polls branch from b439aa2 to 1f8a2cc Compare October 8, 2026 22:06
@MarcZX10

MarcZX10 commented Oct 8, 2026

Copy link
Copy Markdown
Contributor Author

The second Windows failure on head b439aa29 confirmed that configure_windows_unwind only renames unwind sections after linking; it does not provide __gxx_personality_v0. The test now links libc (which supplies the project personality symbol) and adds the matching Windows DLL path, while retaining unwind setup. Current head 1f8a2ccc is rebased onto ae91d978; conventions, diff check and the focused helper test pass. New fork Build 37851358263 is queued; its Windows result is pending. Upstream checks remain action_required.

@MarcZX10
MarcZX10 force-pushed the fix/precise-subtick-polls branch from 1f8a2cc to adc6a7e Compare October 8, 2026 22:24
@MarcZX10 MarcZX10 changed the title fix(windows): use precise sleeps for sub-tick polls test(libc): cover precise sleep helper Oct 8, 2026
@MarcZX10

MarcZX10 commented Oct 8, 2026

Copy link
Copy Markdown
Contributor Author

The re-scoped test-only head is adc6a7ed. Its upstream Build and Conventions runs are action_required with no jobs, while the fork Build is running. CONTRIBUTING says a maintainer must approve fork workflows; could an authorized maintainer approve the current-head runs?

@MarcZX10
MarcZX10 force-pushed the fix/precise-subtick-polls branch from adc6a7e to 87b2633 Compare October 8, 2026 22:55
@MarcZX10

MarcZX10 commented Oct 8, 2026

Copy link
Copy Markdown
Contributor Author

The test-only PR is now rebased onto current main f1451396 at head 87b26330; local conventions and the focused CTest pass. The new upstream Build and Conventions runs are action_required with no jobs, per the documented fork policy. Could an authorized maintainer approve those current-head runs? The fork Build is queued.

@MarcZX10
MarcZX10 force-pushed the fix/precise-subtick-polls branch from 87b2633 to ccf07c9 Compare October 9, 2026 08:35
@MarcZX10

MarcZX10 commented Oct 9, 2026

Copy link
Copy Markdown
Contributor Author

Rebased the isolated test-only change onto current upstream main 6cc0503d; the full diff is still just CTest registration and one small regression test. The current Linux precise_wait test passes (1/1, 0.00 s), conventions and diff checks pass, and prior Linux/Windows full CTest both passed on this exact test source. Fork Build 37905909616 is running on the updated head.

@MarcZX10
MarcZX10 force-pushed the fix/precise-subtick-polls branch 4 times, most recently from 822c179 to 46c8f5a Compare October 9, 2026 11:18
@MarcZX10

MarcZX10 commented Oct 9, 2026

Copy link
Copy Markdown
Contributor Author

Rebased PR #1551 linearly onto current main (d70b8998). On head 46c8f5af, the focused Linux CTest passes (1/1, 0.02 s), conventions and diff checks pass, and GitHub reports it mergeable. The previous fork full CTest results remain disclosed in the body and were not rerun after this rebase. Under #2113, could a collaborator with write access run Actions → Build for this PR so the full Linux, Windows and relinker checks are recorded on the new head?

@MarcZX10
MarcZX10 force-pushed the fix/precise-subtick-polls branch from f0a4b00 to d6c7e2f Compare October 9, 2026 12:02
@MarcZX10 MarcZX10 changed the title test(libc): cover precise sleep helper fix(windows): use precise waits in sub-millisecond polling loops Oct 9, 2026
@MarcZX10

MarcZX10 commented Oct 9, 2026

Copy link
Copy Markdown
Contributor Author

I expanded this PR to include the four sub-millisecond polling call-site changes from #1576 and credited Shaan as co-author. On exact head d6c7e2f6, the Linux Release build and focused CTest pass (7/7), conventions pass, and git diff --check passes. The fork Build dispatch is skipped by its eligibility gate because manual full builds require a PR open in the fork; upstream currently reports only Conventions. Under #2113, could a collaborator with upstream write access run/approve Build for this head, including the Windows full CTest?

@rmz-24

rmz-24 commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

Windows run: Windows 11 Pro 25H2 (10.0.26200.9457), RTX 5060 Ti (NVIDIA 617.42), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release, -DANYPS5_ENABLE_SPIRV_TOOLS=ON, full build plus libs. d6c7e2f cherry-picked onto current main d70b899 (applies cleanly).

  • precise_wait passes.
  • Full ctest: 511/511, including guest_aio, guest_ampr_markers, guest_font, guest_font_writing_line and agc_driver_pm4_failure.

AI-assisted: yes

@MarcZX10

MarcZX10 commented Oct 9, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the Windows report: it covers the exact PR head d6c7e2f6 on current main, and the full Windows CTest passes (511/511). That supplies the Windows evidence. The upstream PR still reports only Conventions; could an authorized collaborator run the official Build for this head to record Linux and the relinker matrix as well?

@MarcZX10
MarcZX10 force-pushed the fix/precise-subtick-polls branch from d6c7e2f to a62e5f4 Compare October 9, 2026 19:32
@MarcZX10

MarcZX10 commented Oct 9, 2026

Copy link
Copy Markdown
Contributor Author

Rebased the existing PR linearly onto current main (ae3d5f95) at head a62e5f49. The diff still contains the same four wait-site changes and focused helper test. On this source diff, all affected Linux libraries and test targets build, and the seven focused CTests pass (7/7). Local and GitHub Conventions pass. I updated the description to label the Windows full-suite report as pre-rebase evidence.\n\nCould a collaborator with write access run the required upstream Linux/Windows Build and relinker matrix for this exact head under #2113?

@MarcZX10
MarcZX10 force-pushed the fix/precise-subtick-polls branch from a62e5f4 to 46a77cc Compare October 9, 2026 20:01
@MarcZX10

MarcZX10 commented Oct 9, 2026

Copy link
Copy Markdown
Contributor Author

Validation on the updated head

I rebased the existing branch linearly onto upstream main at 8b393b6e and pushed head 46a77cca. The full six-file diff still contains the same four polling changes and focused helper test. On this source, all affected libraries and seven focused tests build; CTest passes 7/7. Local Conventions and git diff --check pass. The Windows full-suite report remains clearly labeled as pre-rebase evidence.

The matching production call-site work from #1576 is integrated here with Shaan credited as co-author. #1551 remains the fuller candidate because it includes the same production changes plus the helper regression test. The required upstream Build and relinker matrix is still pending for this head.

@MarcZX10
MarcZX10 force-pushed the fix/precise-subtick-polls branch from 46a77cc to e777d26 Compare October 9, 2026 23:01

@adriadam10 adriadam10 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the Windows evidence and the helper test. One blocker:

  • The four production changes (Pm4.cpp:816, Apr.cpp:589, Aio.cpp:256/:281, State.cpp:60) are the same edits as the open #1576, which CONTRIBUTING doesn't allow (no open PR implementing the same change). Co-authoring the commit doesn't change that: two PRs can't both land them.
    • Fix: drop the call-site changes from this PR and keep only core/libs/tests/PreciseWait.cpp + its CMake registration (the scope it had before 2026-10-09), with #1576 in Depends on if you want the test to cover it. Or, if you and the author of #1576 prefer this one to carry the change, agree that there and close the other.
  • Minor: precise_wait passes on main without any of these changes (it only exercises the helper), so it won't catch a call site reverting to sleep_for. That's fine as a helper test, just don't claim it covers the call sites.

Checked: Linux build, precise_wait, agc_driver_pm4, guest_aio, guest_font* pass on this head.

@MarcZX10
MarcZX10 force-pushed the fix/precise-subtick-polls branch from e777d26 to 0bb0b7c Compare October 10, 2026 07:21
@MarcZX10

Copy link
Copy Markdown
Contributor Author

Thanks for the review. I agree that the two PRs cannot both land the same production changes, and that the helper test does not prove the call sites use the helper.

I rebased #1551 onto current main (a291ae0f) and reran the affected Linux builds and focused tests: all five focused CTests pass, and local Conventions passes. I updated the description to state the helper test's limits and to mark the overlap with #1576 explicitly.

I have asked Shaan on #1576 to confirm one carrier. My preference is #1551 because it preserves the four production changes with Shaan credited and includes the helper regression test. If Shaan prefers #1576, I will remove the production changes from #1551. I will not ask for both to merge; the overlap remains unresolved until Shaan replies.

@MarcZX10
MarcZX10 force-pushed the fix/precise-subtick-polls branch from 0bb0b7c to 6c240d5 Compare October 10, 2026 07:25
@MarcZX10

Copy link
Copy Markdown
Contributor Author

Follow-up to the review

  • Duplicate PR: maintainer adriadam10 closed fix(libkernel): use precise sleep for sub-millisecond host waits #1576 as a duplicate of this PR on 2026-10-10. Shaan remains credited as co-author on the production-change commit.
  • AIO: I removed both AIO substitutions. The Windows measurement in fix(libkernel): use precise sleep for sub-millisecond host waits #1576 found the wait loops return on the first check, so that change had no measured benefit; the approximately 0.5 ms wait could also exceed short AIO timeouts.
  • Test scope: the precise_wait test covers the helper's requested minimum delay. It does not prove production call sites use the helper; the PR description now states that limitation.
  • Current diff: three production call sites (AGC, APR, Font), the helper test, and CMake registration. The focused Linux CTests pass 5/5 on head 6c240d55; local Conventions and git diff --check pass.

The current GitHub Build has passed eligibility and Ubuntu register-context jobs. Its Linux and Windows full-build jobs are skipped by the fork eligibility gate; macOS and relinker jobs are still running. The PR description records these limits. Please re-review this updated scope when convenient.

@MarcZX10

Copy link
Copy Markdown
Contributor Author

Current-head CI result

Build run 38034401344 completed successfully on head 6c240d55.

Check group Result
Register-context (Ubuntu Debug/Release, macOS Intel Debug/Release) Passed
Write-watch (macOS and macOS Intel) Passed
Relinker (Ubuntu, macOS, Windows) Passed
Full Linux and Windows Build Skipped by the fork eligibility gate
Conventions Passed in run 38034477114

The local focused Linux CTests pass 5/5 as recorded in the description. The full CI build matrix did not run on this fork PR; Windows reports in the body are from earlier heads. Please re-review this updated scope and CI result when convenient.

@MarcZX10
MarcZX10 force-pushed the fix/precise-subtick-polls branch from 6c240d5 to 54a9018 Compare October 10, 2026 08:31
@MarcZX10

Copy link
Copy Markdown
Contributor Author

Rebased #1551 onto upstream main at 6cc33583 (head 54a90185). The affected Linux Release libraries and tests build; the five focused CTests pass (5/5), and local Conventions plus git diff --check pass.

The maintainer closed #1576 as a duplicate of this PR, so the three production call sites remain consolidated here with Shaan credited as co-author. The current Build and Conventions runs are queued (Build, Conventions); full Linux and Windows jobs are skipped by the fork eligibility gate. Please re-review when the current checks finish.

@MarcZX10

MarcZX10 commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor Author

Follow-up on the review blocker

The duplicate has now been resolved: maintainer adriadam10 closed #1576 as a duplicate of #1551, noting that this PR is the single carrier for the shared call-site changes and includes the helper regression test. Shaan remains credited as co-author.

The current posted head is 54a90185147ea49d1913bccfed53fb477e619b9f. Its eligible GitHub checks have completed:

Check group Result
Conventions Passed
Register-context (Ubuntu and macOS Intel, Debug/Release) Passed
Write-watch (macOS and macOS Intel) Passed
Relinker (Ubuntu, Windows and macOS) Passed
Full Linux and Windows build/test jobs Skipped by the fork eligibility gate

The PR description also clarifies that precise_wait tests the helper's requested minimum delay; it does not prove that production call sites remain wired to the helper. The local focused Linux test results are recorded there as well.

Could you revisit the CHANGES_REQUESTED review with the duplicate closed and these checks complete? I understand the skipped full Linux/Windows jobs still require collaborator-triggered validation under the contribution guide.

@adriadam10 adriadam10 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checked the three call sites (Pm4.cpp WAIT_REG_MEM, Apr.cpp counter/address wait, State.cpp font backoff) against the existing PreciseSleepUs helper on main; they build and precise_wait passes on main + this PR (Linux). Windows evidence from @rmz-24 (full ctest 511/511) and @popcoder565's measurements in #1471 cover the behaviour we can't run here. Note the helper test only pins the minimum delay, not the call sites. Merging.

@adriadam10
adriadam10 merged commit 695e0b9 into boykopovar:main Oct 10, 2026
14 of 16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants