test(libkernel): give the positioned close test a real 300 us head start #7669

Merged
thuanquang merged 1 commit from upstream-pr/7669 into main 2026-10-19 10:53:27 +00:00
thuanquang commented 2026-10-18 21:25:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by thuanquang on 2026-10-18T21:25:04Z.


What

guest_positioned_close (from #5938) gives the reader thread a 300 us head start before sceKernelClose, so that close lands while the 32 MiB sceKernelPread holds the positioned-transfer lock, and it requires at least one of its 60 rounds to overlap that way. On Windows the head start is not there: with the WinLibs toolchain, std::this_thread::sleep_for of anything below 1 ms returns at once. libstdc++ calls winpthreads' nanosleep, which converts the request to whole milliseconds, so 300 us becomes 0 and no wait happens (#1471 tracks the same thing for production code). Close then races the reader's first instructions in every round, and a run where all 60 rounds lose the race aborts with Positioned close check failed at line 69. That is the intermittent failure on main reported in #7532 (5 of 30 runs on a VM) and #6972.

The test now waits with PreciseSleepUs(300), the sub-millisecond wait production code uses since #1551.

Tested

Windows 11 26200, Ryzen 5 5600X, WinLibs GCC 15.2.0 r7, Release, BUILD_TESTING=ON, on main 48d61f53b.

  • The wait itself, a probe built with the same toolchain: sleep_for of 50, 300 and 999 us took 0.0 us (median of 200 calls each); 1000 us took 15.6 ms.
  • Rounds that overlap, from a local build that prints overlapped (not part of this PR), 30 runs each on an idle machine: main 56 to 60 of 60 per run (median 59), this change 60 of 60 in every run. With 24 busy threads both overlapped 60 of 60 in 10 runs each: on this machine a loaded main thread is slow to close, so the reader wins. The failing runs in #7532 come from a host where close wins every round, which the real 300 us head start rules out.
  • guest_positioned_close passes with the change. I did not reproduce the abort on this machine: 45 runs of main under load passed.
  • python tools/check_conventions.py --base origin/main with this title and body passes.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes (Claude Code)

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4912942201 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7669). Originally posted by [thuanquang](https://github.com/thuanquang) on 2026-10-18T21:25:04Z. --- ### What `guest_positioned_close` (from #5938) gives the reader thread a 300 us head start before `sceKernelClose`, so that close lands while the 32 MiB `sceKernelPread` holds the positioned-transfer lock, and it requires at least one of its 60 rounds to overlap that way. On Windows the head start is not there: with the WinLibs toolchain, `std::this_thread::sleep_for` of anything below 1 ms returns at once. libstdc++ calls winpthreads' `nanosleep`, which converts the request to whole milliseconds, so 300 us becomes 0 and no wait happens (#1471 tracks the same thing for production code). Close then races the reader's first instructions in every round, and a run where all 60 rounds lose the race aborts with `Positioned close check failed at line 69`. That is the intermittent failure on `main` reported in #7532 (5 of 30 runs on a VM) and #6972. The test now waits with `PreciseSleepUs(300)`, the sub-millisecond wait production code uses since #1551. ### Tested Windows 11 26200, Ryzen 5 5600X, WinLibs GCC 15.2.0 r7, Release, `BUILD_TESTING=ON`, on `main` `48d61f53b`. - The wait itself, a probe built with the same toolchain: `sleep_for` of 50, 300 and 999 us took 0.0 us (median of 200 calls each); 1000 us took 15.6 ms. - Rounds that overlap, from a local build that prints `overlapped` (not part of this PR), 30 runs each on an idle machine: `main` 56 to 60 of 60 per run (median 59), this change 60 of 60 in every run. With 24 busy threads both overlapped 60 of 60 in 10 runs each: on this machine a loaded main thread is slow to close, so the reader wins. The failing runs in #7532 come from a host where close wins every round, which the real 300 us head start rules out. - `guest_positioned_close` passes with the change. I did not reproduce the abort on this machine: 45 runs of `main` under load passed. - `python tools/check_conventions.py --base origin/main` with this title and body passes. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes (Claude Code) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
karimibrahim07 commented 2026-10-19 04:32:55 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by karimibrahim07 on 2026-10-19T04:32:55Z.

Review: COMMENTED


Windows check of head 2c75b6c5b, merged onto main ccbd40240 together with #7670, #7709, #7717 and #7680 (each merge clean, built once). Windows 11 Pro (26300), Ryzen 5 3600, WinLibs GCC 15.2.0 r7, Ninja Release, BUILD_TESTING=ON.

  • The wait, same toolchain (a standalone probe, not part of the PR, 100 calls each): std::this_thread::sleep_for of 50, 300 and 999 us took a median of 0.1 us (max 2.2 us), and 1000 us took 15.6 ms. That matches your numbers, so on main the test's 300 us head start isn't there. PreciseSleepUs(300) from PreciseWait.hpp, 200 calls on a loaded machine: 368 to 634 us, median 500 us. It always waits at least what was asked, so the head start is real now.
  • guest_positioned_close passes, --repeat until-fail:3.
  • Under load (8 copies at once plus 12 busy processes, 80 runs each), main's test and this one both passed. Like you, I didn't reproduce the #7532 abort on this machine. There was one abort, in this PR's binary, at line 35 (create_directory). That one comes from my harness: 8 copies in the same working directory, two of which picked the same steady_clock directory name. It isn't the overlap check, and ctest never runs two copies at once.

Code: PreciseSleepUs is what production code uses for sub-millisecond waits since #1551, and on Windows it takes the PreciseSleepHundredNanos path, not winpthreads' nanosleep. The include is the only other change. I have no requests.

AI-assisted: yes. I ran the build, the probe and the runs above.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5566953057 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7669#pullrequestreview-5566953057). Originally posted by [karimibrahim07](https://github.com/karimibrahim07) on 2026-10-19T04:32:55Z. **Review:** COMMENTED --- Windows check of head `2c75b6c5b`, merged onto `main` `ccbd40240` together with #7670, #7709, #7717 and #7680 (each merge clean, built once). Windows 11 Pro (26300), Ryzen 5 3600, WinLibs GCC 15.2.0 r7, Ninja Release, `BUILD_TESTING=ON`. - **The wait, same toolchain** (a standalone probe, not part of the PR, 100 calls each): `std::this_thread::sleep_for` of 50, 300 and 999 us took a median of 0.1 us (max 2.2 us), and 1000 us took 15.6 ms. That matches your numbers, so on `main` the test's 300 us head start isn't there. `PreciseSleepUs(300)` from `PreciseWait.hpp`, 200 calls on a loaded machine: 368 to 634 us, median 500 us. It always waits at least what was asked, so the head start is real now. - `guest_positioned_close` passes, `--repeat until-fail:3`. - Under load (8 copies at once plus 12 busy processes, 80 runs each), `main`'s test and this one both passed. Like you, I didn't reproduce the #7532 abort on this machine. There was one abort, in this PR's binary, at line 35 (`create_directory`). That one comes from my harness: 8 copies in the same working directory, two of which picked the same `steady_clock` directory name. It isn't the overlap check, and ctest never runs two copies at once. **Code:** `PreciseSleepUs` is what production code uses for sub-millisecond waits since #1551, and on Windows it takes the `PreciseSleepHundredNanos` path, not winpthreads' `nanosleep`. The include is the only other change. I have no requests. AI-assisted: yes. I ran the build, the probe and the runs above.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!7669
No description provided.