File: mem_plan9.go

package info (click to toggle)
golang-github-shirou-gopsutil 4.25.2-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid, trixie
  • size: 1,824 kB
  • sloc: makefile: 76; ansic: 19; sh: 11
file content (69 lines) | stat: -rw-r--r-- 1,837 bytes parent folder | download
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(ctx context.Context) ([]*SwapDevice, error) {
	return nil, common.ErrNotImplementedError
}