Skip to content

test(libkernel): measure the remapped direct memory charge in the test process - #9645

Merged
adriadam10 merged 1 commit into
boykopovar:mainfrom
dantdmdarealguy:test/guest-memory-process-commit
Oct 25, 2026
Merged

adriadam10 merged 1 commit into
boykopovar:mainfrom
dantdmdarealguy:test/guest-memory-process-commit

Conversation

@dantdmdarealguy

Copy link
Copy Markdown
Contributor

What

guest_memory fails intermittently on Windows at CheckRemappedDirectMemoryIsChargedOnce (core/libs/tests/GuestMemory.cpp:2164, Require(once)):

1/1 Test #1675: guest_memory .....................Exit code 0xc0000409***Exception:   4.14 sec
Guest memory check failed at .../core/libs/tests/GuestMemory.cpp:2164
FATAL: abort() (SIGABRT) on thread 52888

The check, added in #5596, maps 256 MiB of direct memory, writes it, unmaps it and maps it again. It then requires the commit charge to have grown by less than 384 MiB, so the resident run is charged once and not twice. It read that charge from K32GetPerformanceInfo's CommitTotal, the commit total of the whole system. Every other process changes that total. When a parallel test or a compiler commits and frees more than 128 MiB during each of the three attempts, the check fails although the library charged the memory once. It is a test bug: the library is unchanged and no title reaches it.

The check now reads the commit of the test process itself, PrivateUsage + SharedCommitUsage from K32GetProcessMemoryInfo with a PROCESS_MEMORY_COUNTERS_EX2 layout. MinGW's psapi.h doesn't declare that struct, so the test lays it out as PROCESS_MEMORY_COUNTERS_EX followed by the two added fields. PrivateUsage covers the private MEM_WRITE_WATCH resident run and the private reservation that keeps unmapped pages. SharedCommitUsage covers commit in the SEC_RESERVE direct memory section, where a demoted run goes. A standalone probe on this machine showed both:

  • Committing 256 MiB of a SEC_RESERVE section raises SharedCommitUsage by 256 MiB.
  • 256 MiB of private MEM_WRITE_WATCH memory raises PrivateUsage by 256 MiB.
  • Each moves the system total by the same amount.

In the test, the remap grows the process figure by 265 MiB on every run. A second charge would add at least another 256 MiB and still fail the check. Other processes can't move the figure, so the three-attempt loop that absorbed their noise is gone, and the check runs once. The resident-mapping requirement, the data checks and the threshold are unchanged.

Tested

Windows 11 Pro, Intel Core i9-14900K, 128 GB RAM, NVIDIA GeForce RTX 5080 (driver 32.0.16.1742), WinLibs GCC 15.2.0, Ninja, BUILD_TESTING=ON, every AGC execution test built, SDL_AUDIODRIVER=dummy.

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, some AI assistance was used

@github-actions

github-actions Bot commented Oct 22, 2026 •

Copy link
Copy Markdown

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 4449 core/libs/tests/GuestMemory.cpp
PR 5021 core/libs/tests/GuestMemory.cpp
PR 6321 core/libs/tests/GuestMemory.cpp
PR 7105 core/libs/tests/GuestMemory.cpp
PR 10550 core/libs/tests/GuestMemory.cpp

@fogydes

fogydes commented Oct 22, 2026

Copy link
Copy Markdown
Contributor

Reviewed; the metric switch checks out. Replacing the system-wide K32GetPerformanceInfo commit math with K32GetProcessMemoryInfo's PrivateUsage matches what this test actually needs to catch: a double charge for a re-mapped direct allocation shows up as new commit inside this process, while the old system-wide counter moved with unrelated processes' work, which is what the removed 3-attempt retry loop compensated for. Headroom (bytes + bytes/2) is still generous for allocations from the run itself now that other processes are out of the picture.

One nit: sharedCommitUsage in the anonymous struct is never populated — K32GetProcessMemoryInfo writes up to PrivateUsage and the field stays at its zero value from info{}, so it only pads the cb passed to the call. If the shared-commit query is meant to come later, fine to keep as scaffold; otherwise drop it. Same for the struct shape: padding the counters struct to over-read its own fields is legal but undocumented — the two trailing fields' purpose deserves the one-line comment the comment rules would normally forbid, so consider whether the struct can just be PROCESS_MEMORY_COUNTERS_EX plus a separate variable sized by sizeof.

@dantdmdarealguy

Copy link
Copy Markdown
Contributor Author

Thanks for the review. On the nit: sharedCommitUsage is populated. The two trailing fields are the documented PROCESS_MEMORY_COUNTERS_EX2 layout (PrivateWorkingSetSize, then SharedCommitUsage), which GetProcessMemoryInfo fills when cb is the EX2 size on Windows 10/11 22H2 with the September 2023 update or later. MinGW's psapi.h doesn't declare EX2 yet, which is the only reason the test spells it out.

It is needed, not scaffolding: a run demoted into the SEC_RESERVE direct memory section is charged as shared commit, not PrivateUsage. A standalone probe on Windows 11 showed committing 256 MiB of a SEC_RESERVE section raises SharedCommitUsage by 256 MiB and leaves PrivateUsage unchanged, while 256 MiB of private MEM_WRITE_WATCH memory raises PrivateUsage by 256 MiB. With PrivateUsage alone, a second charge that landed in the section would be missed. In the test, PrivateUsage + SharedCommitUsage grows by 265 MiB per run.

On an older Windows that doesn't fill the field it stays 0, which only weakens the check (it can't fail falsely); if it rejected the EX2 size the Require on the call would fail loudly.

@MarcZX10 MarcZX10 left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The test change directly addresses the flake source: the previous system-wide commit total could move due to unrelated processes, while the new measurement is scoped to the test process. The PrivateUsage + SharedCommitUsage sum is also necessary for both private write-watch pages and commit in the shared direct-memory section; the author supplied Windows probe measurements for each. The reviewer question about the second field has been answered with the documented EX2 layout and supported OS range. I independently ran current-main conventions and git diff --check; both pass. Product CI jobs were skipped by path filters, and this Linux workspace cannot run the Windows-specific measurement. The PR reports substantial Windows A/B stress runs (6/100 baseline failures vs 0/100 with the test change, plus Debug and current-main comparisons). The test is generic and Unblocks: none.

I’ve noticed that holding this PR because of #5239 isn’t necessary. It’s fine for PRs to keep coming in; they’ll be handled in due course. Please disregard my earlier request to hold or defer it.

@MarcZX10 MarcZX10 left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The process-scoped measurement and the reported Windows A/B stress results address the original flake convincingly. One repository requirement remains: this PR currently targets base b27924d, while main is 8b3a6ba; CONTRIBUTING.md requires each PR to be based on current main. Please rebase/update the PR to current main and rerun the focused Windows guest_memory check on the resulting merge base. The path-filtered hosted product jobs are skipped, so their current green status does not cover this test-only change. I cannot run this Windows-specific test from the Linux workspace.

The current request is solely for the repository's current-main requirement and a test run against the rebased result.

I’ve noticed that holding this PR because of #5239 isn’t necessary. It’s fine for PRs to keep coming in; they’ll be handled in due course. Please disregard my earlier request to hold or defer it.

@MarcZX10

Copy link
Copy Markdown
Contributor

I’ve noticed that my earlier request to hold this PR based on #5239 was unnecessary. It’s fine to keep submitting PRs; maintainers will handle them in due course. Please disregard that request.

@BKlaasWerkman

Copy link
Copy Markdown
Contributor

A Windows run that makes the failure deterministic, and the before/after.

Windows 11 Pro 10.0.26300, Core Ultra 9 185H, 64 GB, WinLibs GCC 15.2.0 r7, Release. main 812ed15 against main with head c43cb10 merged (clean merge).

In the wild. One of six full ctest --parallel 8 runs of main 812ed15 on this machine failed exactly as in the description:

Test #1782: guest_memory ....Exit code 0xc0000409***Exception:   4.73 sec
Guest memory check failed at C:/Users/Berrick/AnyPS5/core/libs/tests/GuestMemory.cpp:2164

On demand. A second process grows its own commit during the test: it commits 16 MiB with VirtualAlloc(MEM_RESERVE | MEM_COMMIT) every 2 ms, without touching the pages, up to 6 GiB, then releases everything and starts again. That raises the system CommitTotal by more than 128 MiB within most attempts, like a parallel test ramping up. A process that commits and frees 512 MiB in a square wave did not break main in 10 runs; the commit has to grow during the attempt.

ctest -R '^guest_memory$', 10 runs each:

main this PR
machine otherwise idle 10/10 10/10
with the growing-commit process 0/10, all at GuestMemory.cpp:2164 10/10

Not covered: whether the process figure still catches a real double charge. Showing that would take a library change that charges the remap twice.

AI-assisted: yes (Claude Code)

@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Oct 24, 2026
…t process

CheckRemappedDirectMemoryIsChargedOnce compared the system-wide commit
total (K32GetPerformanceInfo) before and after mapping, unmapping and
remapping 256 MiB of direct memory, and required the growth to stay under
384 MiB in one of three attempts. Every other process on the host changes
that total, so guest_memory failed at GuestMemory.cpp:2164 whenever a
parallel test or a compiler committed and freed more than 128 MiB during
each attempt.

Measure the private and shared commit of the test process instead
(PrivateUsage and SharedCommitUsage of PROCESS_MEMORY_COUNTERS_EX2), which
covers both the private resident run and the direct memory section. The
remap grows it by 265 MiB on every run, so the attempts are dropped.
@dantdmdarealguy
dantdmdarealguy force-pushed the test/guest-memory-process-commit branch from c43cb10 to dc31bde Compare October 24, 2026 23:33
@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 24, 2026

@THEMANnt THEMANnt left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Reviewed dc31bdea09e5ffe8f01ffc15b7a76abdf314e773 against f26434afa36e58778a9ff4b948aea52dff19eb0c. No actionable findings in this focused review.

Source review: Inspected the complete measurement change and surrounding allocation/remap/data-retention/cleanup checks. The test now measures the current process's private plus shared commit rather than machine-wide CommitTotal, removing unrelated processes from the comparison. The 1.5-allocation threshold, remapped bytes and release checks are retained; retries are removed with the global-noise source.

Validation: Head and baseline GuestMemory.cpp pass GCC 15.2 C++23 syntax checks with the two CMake-required module-path macros supplied as placeholder strings. Initial missing-macro errors were harness configuration, not PR defects. A separate Windows probe using the exact new counter lambda created, unmapped and remapped a 16 MiB section; its measured charge was 16,809,984 bytes, below the 1.5-times threshold, with retained data. That is a native counter/section probe, not the full guest direct-memory test. Conventions, whitespace and registration passed. Hosted native/relinker jobs were skipped.

Scope: Bounded measurement correction and local Windows API behavior; no full guest-memory suite, pinned library rebuild, other-Windows-version sweep or game execution.

@adriadam10 adriadam10 self-assigned this Oct 25, 2026
@adriadam10
adriadam10 dismissed MarcZX10’s stale review October 25, 2026 18:27

Dismissing a stale review: the requested changes were addressed after it (new head) and the merge queue blocks on it. Please request changes again if anything is still open.

@adriadam10
adriadam10 merged commit 90f918f into boykopovar:main Oct 25, 2026
9 checks passed
@adriadam10

Copy link
Copy Markdown
Collaborator

Checked the diff against the description: the counter layout matches PROCESS_MEMORY_COUNTERS_EX2, a second charge would still exceed the unchanged 1.5x threshold, and the data and resident-mapping checks are intact. The Windows A/B results from the author and @BKlaasWerkman cover what we can't run here; merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants