Swap usage on darwin from sysctl vm.swapusage (#1608)

Signed-off-by: jonas <jonas.lindmark@denacode.se>
This commit is contained in:
jonas-lindmark 2020-02-19 15:51:30 +01:00 committed by GitHub
parent 8faa843fc4
commit 9828533697
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -16,6 +16,8 @@
package collector
// #include <mach/mach_host.h>
// #include <sys/sysctl.h>
// typedef struct xsw_usage xsw_usage_t;
import "C"
import (
@ -43,6 +45,13 @@ func (c *meminfoCollector) getMemInfo() (map[string]float64, error) {
if err != nil {
return nil, err
}
swapraw, err := unix.SysctlRaw("vm.swapusage")
if err != nil {
return nil, err
}
swap := (*C.xsw_usage_t)(unsafe.Pointer(&swapraw[0]))
// Syscall removes terminating NUL which we need to cast to uint64
total := binary.LittleEndian.Uint64([]byte(totalb + "\x00"))
@ -59,5 +68,8 @@ func (c *meminfoCollector) getMemInfo() (map[string]float64, error) {
"swapped_in_bytes_total": ps * float64(vmstat.pageins),
"swapped_out_bytes_total": ps * float64(vmstat.pageouts),
"total_bytes": float64(total),
"swap_used_bytes": float64(swap.xsu_used),
"swap_free_bytes": float64(swap.xsu_avail),
"swap_total_bytes": float64(swap.xsu_total),
}, nil
}