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