mirror of https://github.com/shirou/gopsutil
[v3][mem] add plan9 support
parent
3a63fa472d
commit
0eae055018
@ -0,0 +1,67 @@
|
||||
// +build plan9
|
||||
|
||||
package mem
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
|
||||
stats "github.com/lufia/plan9stats"
|
||||
"github.com/shirou/gopsutil/v3/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(ctx context.Context) ([]*SwapDevice, error) {
|
||||
return nil, common.ErrNotImplementedError
|
||||
}
|
@ -0,0 +1,79 @@
|
||||
// +build plan9
|
||||
|
||||
package mem
|
||||
|
||||
import (
|
||||
"os"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var virtualMemoryTests = []struct {
|
||||
mockedRootFS string
|
||||
stat *VirtualMemoryStat
|
||||
}{
|
||||
{"swap", &VirtualMemoryStat{
|
||||
Total: 1071185920,
|
||||
Available: 808370176,
|
||||
Used: 11436032,
|
||||
UsedPercent: 1.3949677238843257,
|
||||
Free: 808370176,
|
||||
SwapTotal: 655360000,
|
||||
SwapFree: 655360000},
|
||||
},
|
||||
}
|
||||
|
||||
func TestVirtualMemoryPlan9(t *testing.T) {
|
||||
origProc := os.Getenv("HOST_ROOT")
|
||||
t.Cleanup(func() {
|
||||
os.Setenv("HOST_ROOT", origProc)
|
||||
})
|
||||
|
||||
for _, tt := range virtualMemoryTests {
|
||||
t.Run(tt.mockedRootFS, func(t *testing.T) {
|
||||
os.Setenv("HOST_ROOT", "testdata/plan9/virtualmemory/")
|
||||
|
||||
stat, err := VirtualMemory()
|
||||
skipIfNotImplementedErr(t, err)
|
||||
if err != nil {
|
||||
t.Errorf("error %v", err)
|
||||
}
|
||||
if !reflect.DeepEqual(stat, tt.stat) {
|
||||
t.Errorf("got: %+v\nwant: %+v", stat, tt.stat)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
var swapMemoryTests = []struct {
|
||||
mockedRootFS string
|
||||
swap *SwapMemoryStat
|
||||
}{
|
||||
{"swap", &SwapMemoryStat{
|
||||
Total: 655360000,
|
||||
Used: 0,
|
||||
Free: 655360000},
|
||||
},
|
||||
}
|
||||
|
||||
func TestSwapMemoryPlan9(t *testing.T) {
|
||||
origProc := os.Getenv("HOST_ROOT")
|
||||
t.Cleanup(func() {
|
||||
os.Setenv("HOST_ROOT", origProc)
|
||||
})
|
||||
|
||||
for _, tt := range swapMemoryTests {
|
||||
t.Run(tt.mockedRootFS, func(t *testing.T) {
|
||||
os.Setenv("HOST_ROOT", "testdata/plan9/virtualmemory/")
|
||||
|
||||
swap, err := SwapMemory()
|
||||
skipIfNotImplementedErr(t, err)
|
||||
if err != nil {
|
||||
t.Errorf("error %v", err)
|
||||
}
|
||||
if !reflect.DeepEqual(swap, tt.swap) {
|
||||
t.Errorf("got: %+v\nwant: %+v", swap, tt.swap)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
1071185920 memory
|
||||
4096 pagesize
|
||||
61372 kernel
|
||||
2792/200148 user
|
||||
0/160000 swap
|
||||
9046176/219352384 kernel malloc
|
||||
0/16777216 kernel draw
|
@ -0,0 +1,9 @@
|
||||
// +build plan9
|
||||
|
||||
package process
|
||||
|
||||
import (
|
||||
"syscall"
|
||||
)
|
||||
|
||||
type Signal = syscall.Note
|
Loading…
Reference in New Issue