Repository navigation
Fix memory and CPU readings; add tests and CI - #1
Merged
Merged
Conversation
Memory was read from field 23 of /proc/[pid]/stat (virtual size, in bytes)
instead of field 24 (resident pages), then multiplied by a hard-coded 4 KiB
page size, overstating it by thousands of times. CPU% subtracted the
process's CPU time from system uptime instead of its start time.
Memory now uses field 24 and sysconf(_SC_PAGESIZE). CPU% compares two
readings taken an interval apart, as top does. Names are taken from the
first '(' to the last ')' so names containing parentheses parse correctly.
Adds -i, -n and -s options, and moves parsing into src/proc.c.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The key unit test uses a real /proc/1/stat line and asserts that memory comes from field 24, not field 23. The end-to-end script also compares the reading for PID 1 with ps(1) on a live system. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
CI builds with GCC and Clang with warnings as errors, runs the unit and end-to-end tests, repeats them under AddressSanitizer and UBSan, and runs cppcheck. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What was wrong
stime, the parser skipped 7 fields and read field 23 of/proc/[pid]/stat(vsize, the virtual size in bytes) as the resident set size. It then multiplied that by a hard-coded 4 and labelled it KB. For a shell using under 1 MB of RAM, it would print 6,766,592 KB.top.)broke parsing, because the name was read with%255[^)].What changed
rss, in pages) multiplied bysysconf(_SC_PAGESIZE).utime + stimetaken-iseconds apart, astopdoes.(to the last), and every file read and parse is checked.-i(sampling interval),-n(row limit) and-s cpu|mem(sort order).src/proc.cso they can be unit tested.How it's tested
make test): a real/proc/1/statline, which asserts that memory comes from field 24 and not field 23; names with spaces and parentheses; overlong names; malformed lines; and the CPU% and memory calculations.make integration): runs the program on a fake/proctree, compares its memory figure for PID 1 withpson the live system, and checks that invalid options exit with status 2.🤖 Generated with Claude Code