1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
// SPDX-License-Identifier: BSD-3-Clause
//go:build plan9
package mem
import (
"context"
"os"
stats "github.com/lufia/plan9stats"
"github.com/shirou/gopsutil/v4/internal/common"
)
func SwapMemory() (*SwapMemoryStat, error) {
return SwapMemoryWithContext(context.Background())
}
func SwapMemoryWithContext(ctx context.Context) (*SwapMemoryStat, error) {
root := os.Getenv("HOST_ROOT")
m, err := stats.ReadMemStats(ctx, stats.WithRootDir(root))
if err != nil {
return nil, err
}
u := 0.0
if m.SwapPages.Avail != 0 {
u = float64(m.SwapPages.Used) / float64(m.SwapPages.Avail) * 100.0
}
return &SwapMemoryStat{
Total: uint64(m.SwapPages.Avail * m.PageSize),
Used: uint64(m.SwapPages.Used * m.PageSize),
Free: uint64(m.SwapPages.Free() * m.PageSize),
UsedPercent: u,
}, nil
}
func VirtualMemory() (*VirtualMemoryStat, error) {
return VirtualMemoryWithContext(context.Background())
}
func VirtualMemoryWithContext(ctx context.Context) (*VirtualMemoryStat, error) {
root := os.Getenv("HOST_ROOT")
m, err := stats.ReadMemStats(ctx, stats.WithRootDir(root))
if err != nil {
return nil, err
}
u := 0.0
if m.UserPages.Avail != 0 {
u = float64(m.UserPages.Used) / float64(m.UserPages.Avail) * 100.0
}
return &VirtualMemoryStat{
Total: uint64(m.Total),
Available: uint64(m.UserPages.Free() * m.PageSize),
Used: uint64(m.UserPages.Used * m.PageSize),
UsedPercent: u,
Free: uint64(m.UserPages.Free() * m.PageSize),
SwapTotal: uint64(m.SwapPages.Avail * m.PageSize),
SwapFree: uint64(m.SwapPages.Free() * m.PageSize),
}, nil
}
func SwapDevices() ([]*SwapDevice, error) {
return SwapDevicesWithContext(context.Background())
}
func SwapDevicesWithContext(_ context.Context) ([]*SwapDevice, error) {
return nil, common.ErrNotImplementedError
}
|