Repository navigation
fix(libkernel): answer vm.loadavg with the host's load averages - #12436
Conversation
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
@KernelPanicNT a question for your firmware 10.01 kernel data (#9199), if you have a moment. Is FreeBSD's AI-assisted: yes (Claude Code) |
|
I reviewed head Local |
MarcZX10
left a comment
There was a problem hiding this comment.
I reviewed the exact head ef9c7a06a5 and compared the new vm.loadavg path with the documented FreeBSD struct loadavg layout: three 32-bit fixed-point values followed by 64-bit fscale, 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.
|
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.
|
MarcZX10
left a comment
There was a problem hiding this comment.
Approved at exact head ef9c7a06a5. I reviewed the vm.loadavg path against the documented FreeBSD 11.4 layout (three 32-bit fixed-point values plus 64-bit fscale), verified the {2,2} and vm.loadavg mappings, read-only EPERM handling, 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.
|
The conversion arithmetic is right (fscale 2048,
Neither is observable through the loose test bound at |
GuestLoadAverage's braced initializer left its four bytes of padding after ldavg indeterminate, and the sysctl copies all 24 bytes to the guest. The scaled value of a load over UINT32_MAX / 2048 was converted out of range. The test now checks the padding and the exact fscale 2048 scaling against the host averages read just before and after the call. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
@ebosido444-ai thanks, both are right.
The test now checks both:
The clamp isn't reachable through the sysctl on a real host, so no test covers it. Tested at AI-assisted: yes (Claude Code) |
XairRrR
left a comment
There was a problem hiding this comment.
Reviewed — approved
The vm.loadavg answer merges cleanly, builds, and guest_system_configuration passes on the merged tree and fails on main'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-ff onto main b433c40f2f46aa2e59e19806010a839ac8572d64 in 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.
git merge --no-ff refs/review/12436 # onto origin/main, no conflicts
git diff --check origin/main...refs/review/12436
python3 tools/check_conventions.py --base origin/main
cmake --build build -j10 --target guest_system_configuration_tests
cmake --build build -j10 --target libs
cmake --build build -j10 --target guest_getrlimit_tests
ctest --test-dir build -R '^guest_system_configuration$' --output-on-failure # 3 times
ctest --test-dir build -R '^guest_(system_configuration|getrlimit)' --output-on-failure
# negative control: SystemConfiguration.cpp restored from origin/main, test kept from the PR
# mutation: "+ 0.5" removed from ScaledLoad, rebuilt, test rerun
strace ./la # a 3-line program that calls getloadavg and prints each value * 2048Results
| Check | Result |
|---|---|
Merge onto main |
Clean (docs/dev/debt/libkernel.md auto-merged) |
git diff --check |
Clean |
check_conventions.py |
All conventions checks passed |
Build guest_system_configuration_tests, libs |
OK |
guest_system_configuration |
Passed, 3 of 3 runs (0.03 s each) |
guest_getrlimit (the other test that uses sysctl) |
Passed |
Negative control (main's SystemConfiguration.cpp) |
Fails as expected: sysctl_nid_postfix: unsupported MIB 2.2 |
Mutation: drop + 0.5 in ScaledLoad |
Test still passes (see the non-blocking finding below) |
Negative control output
terminate called after throwing an instance of 'std::runtime_error'
what(): sysctl_nid_postfix: unsupported MIB 2.2
Why the rounding mutation survives: glibc 2.44 getloadavg
sysinfo({uptime=224727, loads=[1321376, 871456, 753664], ...}) = 0
getloadavg makes one sysinfo call and returns loads[i] / 65536.0. Linux keeps the load averages with 11 fractional bits and shifts them left by 5 for sysinfo, 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 example 41427.0000 26281.0000 23170.0000.
Negative control
I restored core/libs/prx/libkernel/System/src/SystemConfiguration.cpp from origin/main, kept the PR's test, and rebuilt. guest_system_configuration aborted with unsupported MIB 2.2, the same failure the description reports. After I restored the merged file, it passed again.
Findings
Blocking
None.
Non-blocking
- On Linux, the exact-value check does not detect a lost
+ 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. - A failing
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. ScaledLoad clamps to [0, UINT32_MAX] and the struct is zeroed with memset before it is filled. I agree with MarcZX10's reading of the layout: 3 × uint32_t, 4 padding bytes, then an 8-byte fscale, 24 bytes in total, which matches FreeBSD 11.4's struct loadavg on amd64. The write path returns EPERM before anything is copied out, like sysctl_root for a node without CTLFLAG_WR. {2, 2, n} returns ENOTDIR through IsLeafMib.
Not verified
- Windows build and test (no Windows host here; the PR reports a Windows 11 VM run).
- ProsperoCraft (PPSA99874): I did not launch any title.
- The padding check against the old brace-initialised code. As the author notes, it depends on leftover stack contents, so I did not try to reproduce it.
- What the console returns for
vm.loadavg. The debt entry already records it as unmeasured.
…vg's debt entry Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
@XairRrR thanks for the mutation run and the
AI-assisted: yes (Claude Code) |
XairRrR
left a comment
There was a problem hiding this comment.
Re-checked — approved
The only new commit, 20d8e0c35d, adds the failed-getloadavg case to the vm.loadavg debt 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
- One commit,
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. - The description's Tested section now says the exact check catches a wrong scale but not a lost
+ 0.5on glibc. It also mentions the new head. - The author replied to both points from my last review.
What I ran
Head 20d8e0c35d4088cfed7cd1163b4dee1bc6847eed, merged with --no-ff onto main b433c40f2f46aa2e59e19806010a839ac8572d64 in 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 on ea0b9c1272 still apply to the same code.
git log --oneline ea0b9c1272..refs/review/12436
git diff ea0b9c1272 refs/review/12436
git merge-tree --write-tree origin/main refs/review/12436 # exit 0, no conflicts
git merge --no-ff refs/review/12436 # onto origin/main, libkernel.md auto-merged
git diff --check origin/main...refs/review/12436 # clean
python3 tools/check_conventions.py --base origin/main # All conventions checks passed.Previous findings
- Exact-value check cannot see a lost
+ 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. - A failing
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
- I did not rebuild or rerun tests at this head. Only a docs line changed, so the results for
ea0b9c1272carry over. - The Windows run and the ProsperoCraft (PPSA99874) run are author-reported. I did not launch any title.
- What the console returns for
vm.loadavgis still unmeasured, as the debt entry records.
|
Checked the |
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, fix(linux): enter the guest entry on a main stack at the console's address #7532 (hugodearaujo, the guest entry on a main stack at the console's address) and fix(libkernel): start thread stacks on a 16 KiB page boundary on Linux #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