...
1
2
3 package mem
4
5 import (
6 "context"
7 "fmt"
8 "unsafe"
9
10 "github.com/shirou/gopsutil/internal/common"
11 "golang.org/x/sys/unix"
12 )
13
14 func getHwMemsize() (uint64, error) {
15 total, err := unix.SysctlUint64("hw.memsize")
16 if err != nil {
17 return 0, err
18 }
19 return total, nil
20 }
21
22
23 type swapUsage struct {
24 Total uint64
25 Avail uint64
26 Used uint64
27 Pagesize int32
28 Encrypted bool
29 }
30
31
32 func SwapMemory() (*SwapMemoryStat, error) {
33 return SwapMemoryWithContext(context.Background())
34 }
35
36 func SwapMemoryWithContext(ctx context.Context) (*SwapMemoryStat, error) {
37
38 var ret *SwapMemoryStat
39
40 value, err := unix.SysctlRaw("vm.swapusage")
41 if err != nil {
42 return ret, err
43 }
44 if len(value) != 32 {
45 return ret, fmt.Errorf("unexpected output of sysctl vm.swapusage: %v (len: %d)", value, len(value))
46 }
47 swap := (*swapUsage)(unsafe.Pointer(&value[0]))
48
49 u := float64(0)
50 if swap.Total != 0 {
51 u = ((float64(swap.Total) - float64(swap.Avail)) / float64(swap.Total)) * 100.0
52 }
53
54 ret = &SwapMemoryStat{
55 Total: swap.Total,
56 Used: swap.Used,
57 Free: swap.Avail,
58 UsedPercent: u,
59 }
60
61 return ret, nil
62 }
63
64 func SwapDevices() ([]*SwapDevice, error) {
65 return SwapDevicesWithContext(context.Background())
66 }
67
68 func SwapDevicesWithContext(ctx context.Context) ([]*SwapDevice, error) {
69 return nil, common.ErrNotImplementedError
70 }
71
View as plain text