Re: Can free swap size be determined from the sysctl information ?

From: Konstantin Belousov <kostikbel_at_gmail.com>
Date: Sun, 18 Aug 2024 09:41:09 UTC
On Sun, Aug 18, 2024 at 02:08:17AM -0700, Yuri wrote:
> It seems that it should be possible to find free swap size from the sysctl
> information, but it isn't obvious how to do this.
> 
> 
> Here are all *swap* entries in the sysctl information:
> $ sysctl -a | grep swap
> 0 MD md98 68719441408 512 u 98 s 512 f 0 fs 0 l 68719441408 t vnode file
> /disk-large1/swap-64GB label
> z0xfffff8004d862400 [shape=box,label="SWAP\nswap\nr#2"];
>      <name>swap</name>
>            <file>/disk-large1/swap-64GB</file>
> vm.swap_enabled: 1
> vm.domain.0.stats.unswappable: 0
> vm.swap_idle_threshold2: 10
> vm.swap_idle_threshold1: 2
> vm.swap_idle_enabled: 0
> vm.disable_swapspace_pageouts: 0
> vm.stats.vm.v_swappgsout: 433264
> vm.stats.vm.v_swappgsin: 94457
> vm.stats.vm.v_swapout: 65078
> vm.stats.vm.v_swapin: 11121
> vm.stats.swap.free_completed: 65205
> vm.stats.swap.free_deferred: 86103
> vm.nswapdev: 1
> vm.swap_fragmentation:
> vm.swap_async_max: 4
> vm.swap_maxpages: 48890752
> vm.swap_total: 68719439872
> vm.swap_reserved: 16267402461184
> 
> 
> top(1) shows 775M of free swap out of 64G total.
> top(1) gets swap information from kvm_getswapinfo(3).
> 
> 
> Motivation: one Golang-based port needs free swap size information, and I am
> trying to figure out how to get this information in Golang.
> 
> Golang has the "sysctl" module.

Look at kvm_getswapinfo(3).
A good example of its use is usr.bin/top/machine.c:swapmode()