2021-12-22 21:54:41 +00:00
|
|
|
//go:build darwin
|
2014-08-08 23:09:28 +09:00
|
|
|
// +build darwin
|
|
|
|
|
2014-12-30 22:09:05 +09:00
|
|
|
package mem
|
2014-08-08 23:09:28 +09:00
|
|
|
|
|
|
|
import (
|
2017-12-31 15:25:49 +09:00
|
|
|
"context"
|
2019-03-02 20:43:24 +01:00
|
|
|
"fmt"
|
|
|
|
"unsafe"
|
2014-11-27 10:18:15 +09:00
|
|
|
|
2017-06-02 13:51:00 -07:00
|
|
|
"golang.org/x/sys/unix"
|
2023-06-03 14:17:16 -07:00
|
|
|
|
|
|
|
"github.com/shirou/gopsutil/v3/internal/common"
|
2014-08-08 23:09:28 +09:00
|
|
|
)
|
|
|
|
|
2016-02-22 07:24:07 +01:00
|
|
|
func getHwMemsize() (uint64, error) {
|
2021-11-15 10:26:08 +01:00
|
|
|
total, err := unix.SysctlUint64("hw.memsize")
|
2016-02-22 07:24:07 +01:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return total, nil
|
|
|
|
}
|
|
|
|
|
2019-03-02 20:43:24 +01:00
|
|
|
// xsw_usage in sys/sysctl.h
|
|
|
|
type swapUsage struct {
|
|
|
|
Total uint64
|
|
|
|
Avail uint64
|
|
|
|
Used uint64
|
|
|
|
Pagesize int32
|
|
|
|
Encrypted bool
|
|
|
|
}
|
|
|
|
|
2014-08-08 23:09:28 +09:00
|
|
|
// SwapMemory returns swapinfo.
|
|
|
|
func SwapMemory() (*SwapMemoryStat, error) {
|
2017-12-31 15:25:49 +09:00
|
|
|
return SwapMemoryWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func SwapMemoryWithContext(ctx context.Context) (*SwapMemoryStat, error) {
|
2019-03-02 20:43:24 +01:00
|
|
|
// https://github.com/yanllearnn/go-osstat/blob/ae8a279d26f52ec946a03698c7f50a26cfb427e3/memory/memory_darwin.go
|
2014-08-08 23:09:28 +09:00
|
|
|
var ret *SwapMemoryStat
|
|
|
|
|
2019-03-02 20:43:24 +01:00
|
|
|
value, err := unix.SysctlRaw("vm.swapusage")
|
2014-09-20 09:30:14 +09:00
|
|
|
if err != nil {
|
|
|
|
return ret, err
|
|
|
|
}
|
2019-03-02 20:43:24 +01:00
|
|
|
if len(value) != 32 {
|
|
|
|
return ret, fmt.Errorf("unexpected output of sysctl vm.swapusage: %v (len: %d)", value, len(value))
|
2014-09-20 09:30:14 +09:00
|
|
|
}
|
2019-03-02 20:43:24 +01:00
|
|
|
swap := (*swapUsage)(unsafe.Pointer(&value[0]))
|
2014-08-08 23:09:28 +09:00
|
|
|
|
2015-02-26 16:23:35 +09:00
|
|
|
u := float64(0)
|
2019-03-02 20:43:24 +01:00
|
|
|
if swap.Total != 0 {
|
|
|
|
u = ((float64(swap.Total) - float64(swap.Avail)) / float64(swap.Total)) * 100.0
|
2015-02-26 16:23:35 +09:00
|
|
|
}
|
2014-08-08 23:09:28 +09:00
|
|
|
|
|
|
|
ret = &SwapMemoryStat{
|
2019-03-02 20:43:24 +01:00
|
|
|
Total: swap.Total,
|
|
|
|
Used: swap.Used,
|
|
|
|
Free: swap.Avail,
|
2014-09-20 09:30:14 +09:00
|
|
|
UsedPercent: u,
|
2014-08-08 23:09:28 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|
2021-08-18 09:52:13 -04:00
|
|
|
|
|
|
|
func SwapDevices() ([]*SwapDevice, error) {
|
|
|
|
return SwapDevicesWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func SwapDevicesWithContext(ctx context.Context) ([]*SwapDevice, error) {
|
|
|
|
return nil, common.ErrNotImplementedError
|
|
|
|
}
|