Skip to content

Fix potential swap undercount on FreeBSD - #2123

Merged
BenBE merged 1 commit into
htop-dev:mainfrom
jheanlee:freebsd-swap-undercount
Sep 27, 2026
Merged

BenBE merged 1 commit into
htop-dev:mainfrom
jheanlee:freebsd-swap-undercount

Conversation

@jheanlee

Copy link
Copy Markdown
Contributor

Fixes potential totalSwap and usedSwap undercount on FreeBSD.

The original implementation only reads ARRAYSIZE(swap) - 1 (15) swap devices into the buffer and sum them as totalSwap and usedSwap. If a system has more than 16 devices, the original implementation would not count the remaining devices beyond 16 (maxswap = LEN = 16; kvm_getswapinfo() reads up to maxswap - 1 devices.).

kvm_getswapinfo() reserves the last structure for a total sum of all devices, including those that are not written into the array:

A grand total of all swap devices (including any devices that go beyond maxswap - 1) is returned in one additional array entry.

The grand total is calculated from all available swap devices whether or not you made room for them all in the array.

When maxswap = 1, we get the grand total of all swap devices in the supplied kvm_swap structure.

For proof and more information, please check GuillaumeGomez/sysinfo#1746.

@coderabbitai

coderabbitai Bot commented Sep 26, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

Navigate logical layers of code changes, visualize relationships, and explore their blast radius.

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: ASSERTIVE

Plan: Advanced

Run ID: 57351d50-3c1c-4160-88bd-58c57b277d62

📥 Commits

Reviewing files that changed from the base of the PR and between 4ab4dfc and 1ec61ed.

📒 Files selected for processing (1)
  • freebsd/FreeBSDMachine.c

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.


📝 Walkthrough

Walkthrough

FreeBSD swap reporting now uses one kvm_getswapinfo() call to obtain the grand total. If the call returns a negative count, the code sets total and used swap to zero. Otherwise, it converts the returned totals from pages to kilobytes.

Suggested reviewers: prownd

Priority: ⬇️ Low

Change: Bug fix

Merge Risk: ⚪ Minimal · up to 1ec61

The change targets accurate FreeBSD swap totals, and the supplied evidence identifies no remaining actionable merge risk.


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.

❤️ Share

One call gathers swap totals
A grand total replaces the sum
Negative counts set values to zero
Page totals become kilobytes
FreeBSD reports the result in turn

Comment @coderabbitai help to get the list of available commands.

@BenBE BenBE added enhancement Extension or improvement to existing feature FreeBSD 👹 FreeBSD related issues labels Sep 27, 2026
@jheanlee

Copy link
Copy Markdown
Contributor Author

Would the bug label fit this PR more than enhancement here? This PR fixes the issue that totalSwap and usedSwap are undercounted when there are more than 15 swap devices.

@BenBE
BenBE force-pushed the freebsd-swap-undercount branch from 4ab4dfc to 1ec61ed Compare September 27, 2026 14:43
@BenBE BenBE added this to the 3.6.0 milestone Sep 27, 2026
@BenBE
BenBE merged commit bb3ee0a into htop-dev:main Sep 27, 2026
21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement Extension or improvement to existing feature FreeBSD 👹 FreeBSD related issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants