Skip to content

perf(windows): resume wxc-exec without scanning every thread - #127

Open
ltsabadz wants to merge 1 commit into
ROCm:mainfrom
ltsabadz:fix/windows-resume-main-thread
Open

ltsabadz wants to merge 1 commit into
ROCm:mainfrom
ltsabadz:fix/windows-resume-main-thread

Conversation

@ltsabadz

Copy link
Copy Markdown
Collaborator

Motivation

On Windows, axis run starts wxc-exec.exe suspended, puts it in the Job Object, and then resumes it. To find its thread, AXIS took a snapshot of every thread on the machine. That costs about 63 ms per launch which this PR cuts down.

Technical Details

Resume the process with one NtResumeProcess call on the handle std::process::Child already holds. It resumes the same threads as before. NtResumeProcess isn't documented, but ntdll has exported it since Windows XP. The windows crate's ToolHelp feature is no longer used, so it's removed.

Security impact: none. The order is the same (suspended, in the Job Object, then resumed), and if the resume fails, the launch is still refused before the command runs.

Test Plan

cargo fmt, cargo clippy and the axis-sandbox tests on Windows, including a new test that resumes a suspended process. Ran e2e/windows/test_mxc_processcontainer.ps1 on a host with BaseContainer. Benchmarked axis run --policy minimal with axis-bench launch from feature/launch-timing, with and without this commit, 30 rounds each, alternating.

Test Result

  • All pass.
  • The resume step takes 63 → 0.01 ms.
  • Windows only; Linux and macOS are unchanged.

MxcWindowsSandbox::launch() creates wxc-exec.exe suspended, assigns it to
the limited Job Object, and then resumes it. resume_process found the
executor's suspended thread with CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD),
which lists every thread on the system. On a Windows 11 laptop running
CrowdStrike that took a median of 61 ms per launch (spawn.resume span,
40-87 ms across runs), and more when the host was busy.

Resume through the process handle that std's Child already holds, with
NtResumeProcess. Like the old loop, it resumes every thread of the process
once, but in one system call. Keeping the main thread handle instead would
mean calling CreateProcessW ourselves, because Command does not expose it.
NtResumeProcess is not documented by Microsoft, but ntdll has exported it
since Windows XP and the Windows SDK's ntdll.lib provides it.

The suspended, assign, resume order is unchanged, and so is the failure
path: if the resume fails, for example because the executor is already
exiting, launch() kills it and returns an error before any user code runs.
A new test covers both the resume and the refusal for an exited process.
The windows crate's ToolHelp feature is no longer used and is dropped.

Co-authored-by: Cursor <[email protected]>

@johayang-amd johayang-amd 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 Luka, replacing the system-wide thread snapshot with one call on the handle we already hold is a nice simplification, and the test covers both the resume and the refusal for an exited process.

A few requests, none blocking:

  • The description has numbers for the resume step only. Could you add end-to-end axis run medians for main and this change as a short table in Test Result? DEVELOPMENT.md asks for that on launch-path changes. Ours are below if they help.
  • NtResumeProcess is undocumented. A one-line comment on the extern block saying so, and that ntdll has exported it since XP, would help the next reader; right now that's only in the commit message.
  • Optional: assign_process_to_job still reopens the process by PID; it could take the same BorrowedHandle.

The two failing checks aren't from this PR: Dependency audit is the new url advisory (RUSTSEC-2026-0592), and Test (Linux) is an intermittent failure in a tmpdir test from #107, which we'll fix separately.

Tested on Windows 11 26100: built main 8040824 and this PR on top of it, then ran axis --socket 0.0.0.0:9 run --policy <mxc, block> -- cmd /c exit 0 alternately, 60 rounds each, twice. The median went from 591 to 519 ms and from 578 to 514 ms. axis.exe no longer imports CreateToolhelp32Snapshot.

@johayang-amd

Copy link
Copy Markdown
Collaborator

Our Windows numbers as a table, in case you want to reuse them in Test Result. Windows 11 26100, axis --socket 0.0.0.0:9 run --policy <mxc, block> -- cmd /c exit 0, main 8040824 and this PR built on top of it, run alternately, median:

Run main This PR
60 rounds, first 591 ms 519 ms
60 rounds, second 578 ms 514 ms

@johayang-amd johayang-amd 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.

Approving d7df925. The comment and table requests above are optional; the failing Dependency audit and Test (Linux) checks are fixed by #128.

This branch has not been deployed

No deployments
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.

2 participants