fix(libkernel): answer vm.loadavg with the host's load averages #12436
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/12436"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-28T13:31:41Z.
What
ProsperoCraft (PPSA99874 01.000.117, the catalog's Minecraft Java launcher with a Java 25 runtime) aborts 3 s after start:
MIB {2, 2} is FreeBSD's
vm.loadavg(CTL_VM,VM_LOADAVG). The runtime'slibjvm.sohas its owngetloadavg, whichos::loadavgjumps to. It is FreeBSD libc's:sysctl({2, 2}, &loadavg, &size)withsize24, then eachldavg[i] / fscaleas a double, or -1 ifsysctlfails.sysctlhere throws for any MIB it doesn't know under a known top-level node, so the title ends there.How
sysctl{2, 2} now answers like FreeBSD 11.4's read-only node (sysctl_vm_loadavg): the 24-bytestruct loadavg, three 32-bit fixed-point averages and an 8-bytefscale.fscaleis 2048, FreeBSD'sFSCALE(1 << FSHIFT,FSHIFT11). The averages are the host's 1, 5 and 15 minute loads fromgetloadavg, rounded to that scale. Windows has no load average, so there they are 0.EPERM, as FreeBSD'ssysctl_rootdoes for a node withoutCTLFLAG_WR. A short buffer getsENOMEMthrough the existingCopyOut.{2, 2, n}returnsENOTDIR, like the other leaves (IsLeafMib).sysctlbyname("vm.loadavg")andsysctlnametomib("vm.loadavg")map to {2, 2}.What the console returns was not measured, so
docs/dev/debt/libkernel.mdgets avm.loadavgentry under Unknown function info that says so, next tokern.boottime.Tested
Linux (Ubuntu 26.04.1, GCC 15.2), on
main750cf6cf48:guest_system_configuration: the size query by MIB and by name gives 24; a read givesfscale2048 and, off Windows, averages within 1.0 of the host'sgetloadavg; the by-name read matches;sysctlnametomibwrites {2, 2} and nothing past it; a write returnsEPERM; {2, 2, 0} returnsENOTDIR.errnois kept on success. It passes 3 of 3 (ef9c7a06a5). The test alone onmain(60da3ec100) fails:sysctl_nid_postfix: unsupported MIB 2.2.ea0b9c1272, from ebosido444-ai's review): the answer's 4 padding bytes afterldavgare now zero, and each scaled average is clamped toUINT32_MAXbefore the conversion. The test checks that the padding is 0, and that everyldavgvalue is exactlyhost * 2048 + 0.5for the host averages read just before or just after the call. That replaces the earlier ±1.0 bound. It passes 3 of 3, and all 942^guest_tests pass atea0b9c1272. The exact check catches a wrong scale, but not a lost+ 0.5on glibc:getloadavgthere divides the kernel's 11-bit fixed-point loads, so every host load × 2048 is a whole number, and rounding and truncation agree (XairRrR's mutation run).^guest_tests pass atea0b9c1272. The head20d8e0c35dadds only a debt entry line: the averages are also 0 whengetloadavgreturns fewer than 3 (XairRrR's review).sysctlcall. The JVM then stops on its own a moment later:Native memory allocation (mprotect) failed to protect 49152 bytes. Error detail: memory to guard stack pages(stackOverflow.cpp:113). Its primordial thread runs on the host process's main stack (stack(0x00007ffe107c1000,0x00007ffe10fbf000)in itshs_errfile), which guestmprotectdoesn't cover. That is a separate stop. With this PR, #7532 (hugodearaujo, the guest entry on a main stack at the console's address) and #9684 (NevesHenry, 16 KiB-aligned thread stacks) merged together (27e6cd782a), ProsperoCraft runs the full 120 s: its Instances screen, new-instance form and on-screen keyboard follow the input (#7532, #9684).guest_system_configurationpasses 3 of 3 at this head (ea0b9c1272) and onmain750cf6cf48.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-28T13:51:18Z.
Review: COMMENTED
I reviewed the exact head
ef9c7a06a5and compared the newvm.loadavgpath with the documented FreeBSDstruct loadavglayout: three 32-bit fixed-point values followed by 64-bitfscale, with MIB/name conversion and read-only handling routed through existing sysctl helpers. The added test covers size query, host values, by-name parity, errno preservation, trailing MIB components, and writes. I found no concrete issue in the changed path. Hosted conventions and Windows checks pass; the PR body reports the Linux 942-test suite and Windows focused suite, which I could not independently rerun here because this checkout's full CMake configuration is missing SDL2/XRandR support.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-28T14:15:25Z.
Review: APPROVED
Approved at exact head
ef9c7a06a5. I reviewed thevm.loadavgpath against the documented FreeBSD 11.4 layout (three 32-bit fixed-point values plus 64-bitfscale), verified the{2,2}andvm.loadavgmappings, read-onlyEPERMhandling, and the trailing-component leaf behavior. The added tests cover size query, by-name parity, fscale/host values, errno preservation, name-to-MIB output, writes, and trailing components. Hosted Windows, conventions, eligibility, intake, labels, and report checks pass. The PR reports the Linux 942-test suite and ProsperoCraft run; I did not independently rerun those locally. Console loadavg behavior remains explicitly documented as unmeasured.Imported from boykopovar/AnyPS5. Originally posted by XairRrR on 2026-10-29T03:45:56Z.
Review: APPROVED
Reviewed — approved
The
vm.loadavganswer merges cleanly, builds, andguest_system_configurationpasses on the merged tree and fails onmain's implementation as it should. One non-blocking point: on current glibc the new exact-value check cannot detect a lost+ 0.5.What I ran
Head
ea0b9c12728948765792fd62433a846bfe4b0dc0, merged with--no-ffontomainb433c40f2f46aa2e59e19806010a839ac8572d64in a clean worktree. Arch Linux 7.2.11-zen, GCC 16.2.1, glibc 2.44, Intel Core i9-14900KF. The build and tests ran in a sandbox with no network.Results
maindocs/dev/debt/libkernel.mdauto-merged)git diff --checkcheck_conventions.pyguest_system_configuration_tests,libsguest_system_configurationguest_getrlimit(the other test that uses sysctl)main'sSystemConfiguration.cpp)sysctl_nid_postfix: unsupported MIB 2.2+ 0.5inScaledLoadNegative control output
Why the rounding mutation survives: glibc 2.44
getloadavggetloadavgmakes onesysinfocall and returnsloads[i] / 65536.0. Linux keeps the load averages with 11 fractional bits and shifts them left by 5 forsysinfo, so every load × 2048 is an exact integer (1321376 / 32 = 41293). In six separate runs of the program, every printed load × 2048 was a whole number, for example41427.0000 26281.0000 23170.0000.Negative control
I restored
core/libs/prx/libkernel/System/src/SystemConfiguration.cppfromorigin/main, kept the PR's test, and rebuilt.guest_system_configurationaborted withunsupported MIB 2.2, the same failure the description reports. After I restored the merged file, it passed again.Findings
Blocking
None.
Non-blocking
+ 0.5. The reply to ebosido444-ai's review says the exact comparison atGuestSystemConfiguration.cpp:543-544"catches a lost+ 0.5". On this host it does not. I removed+ 0.5fromScaledLoadand the test still passed. glibc 2.44 buildsgetloadavgfromsysinfo, which carries the kernel's 11-bit fixed-point loads (details above). Soaverage * 2048is always a whole number, and rounding and truncation give the same result. The check still catches a wrong scale, and the conversion itself is correct. The test just cannot observe the rounding through the host. If you want rounding coverage, one option is to test the conversion on fixed inputs such as 1.0003 → 2049 (2048 without the rounding). Another is to drop the claim from the description.getloadavggives zeros that look like an idle machine. AtSystemConfiguration.cpp:131-132, ifgetloadavgreturns anything but 3, the averages stay 0 and the call succeeds. With glibc'ssysinfopath this practically never happens, so it is minor. Throwing there, or listing the case next to the Windows zeros in the debt entry, would match the rule against silent made-up success.ebosido444-ai's two earlier findings (unclamped conversion, uninitialized padding) are fixed at this head.
ScaledLoadclamps to[0, UINT32_MAX]and the struct is zeroed withmemsetbefore it is filled. I agree with MarcZX10's reading of the layout: 3 ×uint32_t, 4 padding bytes, then an 8-bytefscale, 24 bytes in total, which matches FreeBSD 11.4'sstruct loadavgon amd64. The write path returnsEPERMbefore anything is copied out, likesysctl_rootfor a node withoutCTLFLAG_WR.{2, 2, n}returnsENOTDIRthroughIsLeafMib.Not verified
vm.loadavg. The debt entry already records it as unmeasured.Imported from boykopovar/AnyPS5. Originally posted by XairRrR on 2026-10-29T03:50:57Z.
Review: APPROVED
Re-checked — approved
The only new commit,
20d8e0c35d, adds the failed-getloadavgcase to thevm.loadavgdebt entry, and the description no longer claims that the exact check catches a lost+ 0.5. Both of my earlier non-blocking points are addressed.What changed since my last review
20d8e0c35d(docs(libkernel): ...). It changes onlydocs/dev/debt/libkernel.md.git diff ea0b9c1272..20d8e0c35dshows one line edited in thevm.loadavgentry, and no code, test or build files changed.+ 0.5on glibc. It also mentions the new head.What I ran
Head
20d8e0c35d4088cfed7cd1163b4dee1bc6847eed, merged with--no-ffontomainb433c40f2f46aa2e59e19806010a839ac8572d64in a clean worktree. No code changed, so I did not rebuild or rerun tests. The build, test, negative control and mutation results from my previous review onea0b9c1272still apply to the same code.Previous findings
+ 0.5on glibc: Resolved. The author agreed and removed the claim from the description instead of changing the test. That settles the point, because the conversion itself was already correct.getloadavggives zeros that look like an idle machine: Resolved. The debt entry now says the averages are 0 whengetloadavgreturns fewer than 3 and that the call still succeeds. This matchesHostLoadAverageatcore/libs/prx/libkernel/System/src/SystemConfiguration.cpp:125-135: the struct is zeroed and the averages are filled only whengetloadavg(averages, 3) == 3. "Fewer than 3" also covers the-1error return.New findings
Blocking
None.
Non-blocking
None.
Not verified
ea0b9c1272carry over.vm.loadavgis still unmeasured, as the debt entry records.