fix(scripts/cpu_info): ignore I/O wait time
dracula/tmux reports my CPU usage as ~20% when my computer is mostly idle, whereas other tools, such as eww and waybar, report it as only ~2%. This is because we are counting I/O wait time as CPU usage. I propose ignoring it and deducting it from the total percentage. After this, the results are coherent across tools.
๐ Walkthrough
Walkthrough
Linux branch of get_percent in scripts/cpu_info.sh now parses both idle (id) and I/O wait (wa) fields and computes CPU usage as 100 - id - wa instead of 100 - id.
Changes
| Cohort / File(s) | Summary |
|---|---|
CPU Usage Calculation scripts/cpu_info.sh |
Linux branch of get_percent updated: sed/awk pipeline extracts id and wa fields; final arithmetic computes 100 - id - wa; output still passed to normalize_percent_len. No other OS branches changed. |
Estimated code review effort
๐ฏ 2 (Simple) | โฑ๏ธ ~10 minutes
Areas to check:
- Correctness of the sed/awk field extraction across different
topoutput formats/versions. - Proper handling of edge cases (non-numeric fields, missing
wa).
Pre-merge checks and finishing touches
โ Failed checks (1 warning)
| Check name | Status | Explanation | Resolution |
|---|---|---|---|
| Docstring Coverage | โ ๏ธ Warning | Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. | You can run @coderabbitai generate docstrings to improve docstring coverage. |
โ Passed checks (2 passed)
| Check name | Status | Explanation |
|---|---|---|
| Title Check | โ Passed | The title "fix(scripts/cpu_info): ignore I/O wait time" directly aligns with the main change in the changeset. The raw summary confirms that the Linux branch of get_percent was modified to capture I/O wait time (wa) and deduct it from the total CPU calculation (100 - id - wa), which is exactly what the title describes. The title is concise, specific, and clearly communicates the primary objective without vague language or noise. |
| Description Check | โ Passed | The description is directly related to the changeset and provides clear context for the changes. It explains the problem (dracula/tmux reporting ~20% CPU usage when idle while other tools report ~2%), identifies the root cause (I/O wait time being counted as CPU usage), and describes the proposed solution (ignoring and deducting I/O wait from the total percentage). This matches the technical changes documented in the raw summary and demonstrates a good understanding of what was modified and why. |
โจ Finishing touches
- [ ] ๐ Generate docstrings
๐งช Generate unit tests (beta)
- [ ] Create PR with unit tests
- [ ] Post copyable unit tests in a comment
๐ Recent review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
๐ฅ Commits
Reviewing files that changed from the base of the PR and between 485a2bba47ec013fd246d8a94ce9c0564696f3cd and ddecbcc089d1586c5f0aabb4ce5766dc22c5faaf.
๐ Files selected for processing (1)
scripts/cpu_info.sh(1 hunks)
๐งฐ Additional context used
๐งฌ Code graph analysis (1)
scripts/cpu_info.sh (1)
scripts/utils.sh (1)
normalize_percent_len(26-35)
๐ Additional comments (1)
scripts/cpu_info.sh (1)
12-12: Logic is correct; verify sed pattern robustness across systems.The calculation
100 - idle - iowaitis correct. Iowait is the portion of idle time during which there was at least one outstanding block I/O request, so excluding both idle and iowait from 100% properly isolates active CPU time, aligning with standard tools.The sed pattern structure is reasonable (
..., X%* id, Y%* wa...extraction), but empirical verification across different Linux distributions andtopversions is essential. Silent pattern failures (mismatches due to format variations) would produce incorrect CPU percentages without warning. Manually test the change on your systems to confirm the pattern reliably extracts idle and iowait values across your target environments.
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Comment @coderabbitai help to get the list of available commands and usage tips.
I'm a bit hesitant to merge this:
#current
$ LC_NUMERIC=en_US.UTF-8 top -bn2 -d 0.01 | grep "[C]pu(s)" | tail -1 | sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | awk '{print 100 - $1"%"}'
1.3%
#PR
$ LC_NUMERIC=en_US.UTF-8 top -bn2 -d 0.01 | grep "[C]pu(s)" | tail -1 | sed "s/.*, *\([0-9.]*\)%* id, *\([0-9.]*\)%* wa.*/\1 \2/" | awk '{print 100 - $1 - $2"%"}'
1.9%
On an x86_64-linux Arch Linux machine. I'll test on a few more machines and see.
A Fedora machine:
$ LC_NUMERIC=en_US.UTF-8 top -bn2 -d 0.01 | grep "[C]pu(s)" | tail -1 | sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | awk '{print 100 - $1"%"}'
57.1%
$ LC_NUMERIC=en_US.UTF-8 top -bn2 -d 0.01 | grep "[C]pu(s)" | tail -1 | sed "s/.*, *\([0-9.]*\)%* id, *\([0-9.]*\)%* wa.*/\1 \2/" | awk '{print 100 - $1 - $2"%"}'
57.1%
What kind of system are you testing this on?
@ethancedwards8 I've tested this on multiple x86_64-linux (NixOS) machines. To simulate I/O wait time, you can use the following tool:
stress --io 16
https://linux.die.net/man/1/stress https://archlinux.org/packages/extra/x86_64/stress/
Then you should have a significant difference between the two commands.