1
0
mirror of https://github.com/shirou/gopsutil.git synced 2025-04-24 13:48:56 +08:00
shirou_gopsutil/mem/mem_darwin.go

131 lines
3.3 KiB
Go
Raw Normal View History

// SPDX-License-Identifier: BSD-3-Clause
2021-12-22 21:54:41 +00:00
//go:build darwin
2014-12-30 22:09:05 +09:00
package mem
import (
2017-12-31 15:25:49 +09:00
"context"
"fmt"
"unsafe"
"golang.org/x/sys/unix"
2023-06-03 14:17:16 -07:00
"github.com/shirou/gopsutil/v4/internal/common"
)
2016-02-22 07:24:07 +01:00
func getHwMemsize() (uint64, error) {
total, err := unix.SysctlUint64("hw.memsize")
2016-02-22 07:24:07 +01:00
if err != nil {
return 0, err
}
return total, nil
}
// xsw_usage in sys/sysctl.h
type swapUsage struct {
Total uint64
Avail uint64
Used uint64
Pagesize int32
Encrypted bool
}
// 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) {
// https://github.com/yanllearnn/go-osstat/blob/ae8a279d26f52ec946a03698c7f50a26cfb427e3/memory/memory_darwin.go
var ret *SwapMemoryStat
value, err := unix.SysctlRaw("vm.swapusage")
2014-09-20 09:30:14 +09:00
if err != nil {
return ret, err
}
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
}
swap := (*swapUsage)(unsafe.Pointer(&value[0]))
2015-02-26 16:23:35 +09:00
u := float64(0)
if swap.Total != 0 {
u = ((float64(swap.Total) - float64(swap.Avail)) / float64(swap.Total)) * 100.0
2015-02-26 16:23:35 +09:00
}
ret = &SwapMemoryStat{
Total: swap.Total,
Used: swap.Used,
Free: swap.Avail,
2014-09-20 09:30:14 +09:00
UsedPercent: u,
}
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
}
type vmStatisticsData struct {
freeCount uint32
activeCount uint32
inactiveCount uint32
wireCount uint32
_ [44]byte // Not used here
}
// VirtualMemory returns VirtualmemoryStat.
func VirtualMemory() (*VirtualMemoryStat, error) {
return VirtualMemoryWithContext(context.Background())
}
func VirtualMemoryWithContext(ctx context.Context) (*VirtualMemoryStat, error) {
2024-09-17 17:04:23 +08:00
machLib, err := common.NewLibrary(common.System)
if err != nil {
return nil, err
}
defer machLib.Close()
hostStatistics := common.GetFunc[common.HostStatisticsFunc](machLib, common.HostStatisticsSym)
machHostSelf := common.GetFunc[common.MachHostSelfFunc](machLib, common.MachHostSelfSym)
count := uint32(common.HOST_VM_INFO_COUNT)
var vmstat vmStatisticsData
status := hostStatistics(machHostSelf(), common.HOST_VM_INFO,
uintptr(unsafe.Pointer(&vmstat)), &count)
if status != common.KERN_SUCCESS {
return nil, fmt.Errorf("host_statistics error=%d", status)
}
pageSizeAddr, _ := machLib.Dlsym("vm_kernel_page_size")
pageSize := **(**uint64)(unsafe.Pointer(&pageSizeAddr))
total, err := getHwMemsize()
if err != nil {
return nil, err
}
totalCount := uint32(total / pageSize)
availableCount := vmstat.inactiveCount + vmstat.freeCount
usedPercent := 100 * float64(totalCount-availableCount) / float64(totalCount)
usedCount := totalCount - availableCount
return &VirtualMemoryStat{
Total: total,
Available: pageSize * uint64(availableCount),
Used: pageSize * uint64(usedCount),
UsedPercent: usedPercent,
Free: pageSize * uint64(vmstat.freeCount),
Active: pageSize * uint64(vmstat.activeCount),
Inactive: pageSize * uint64(vmstat.inactiveCount),
Wired: pageSize * uint64(vmstat.wireCount),
}, nil
}