2021-12-23 05:54:41 +08:00
|
|
|
//go:build solaris
|
2021-08-18 21:52:13 +08:00
|
|
|
// +build solaris
|
|
|
|
|
2017-03-15 03:40:30 +08:00
|
|
|
package mem
|
|
|
|
|
|
|
|
import (
|
2017-12-31 14:25:49 +08:00
|
|
|
"context"
|
2022-03-05 00:18:03 +08:00
|
|
|
"errors"
|
2017-03-15 03:40:30 +08:00
|
|
|
"fmt"
|
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2021-11-06 17:53:56 +08:00
|
|
|
"github.com/shirou/gopsutil/v3/internal/common"
|
The current codes miss below statistic data under solaris/illumos:
1. the disk io statistic data as: nread, nwritten, reads, writes, rtime, wtime;
2. the free memory under global zone;
3. the net io statistic data as: rbytes64, ipackets64, idrops64, ierrors, obytes64, opackets64, odrops64, oerrors.
The new feature branch adds the above missing statistic data based on the psutil project (https://psutil.readthedocs.io/), it has been tested under solaris ( Oracle Solaris 11.4 X86) and illumos (OmniOS v11 r151044).
2022-11-22 12:28:36 +08:00
|
|
|
"github.com/tklauser/go-sysconf"
|
2017-03-15 03:40:30 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// VirtualMemory for Solaris is a minimal implementation which only returns
|
|
|
|
// what Nomad needs. It does take into account global vs zone, however.
|
|
|
|
func VirtualMemory() (*VirtualMemoryStat, error) {
|
2017-12-31 14:25:49 +08:00
|
|
|
return VirtualMemoryWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func VirtualMemoryWithContext(ctx context.Context) (*VirtualMemoryStat, error) {
|
2017-03-15 03:40:30 +08:00
|
|
|
result := &VirtualMemoryStat{}
|
|
|
|
|
|
|
|
zoneName, err := zoneName()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if zoneName == "global" {
|
|
|
|
cap, err := globalZoneMemoryCapacity()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
result.Total = cap
|
2022-11-29 13:30:09 +08:00
|
|
|
freemem, err := globalZoneFreeMemory(ctx)
|
The current codes miss below statistic data under solaris/illumos:
1. the disk io statistic data as: nread, nwritten, reads, writes, rtime, wtime;
2. the free memory under global zone;
3. the net io statistic data as: rbytes64, ipackets64, idrops64, ierrors, obytes64, opackets64, odrops64, oerrors.
The new feature branch adds the above missing statistic data based on the psutil project (https://psutil.readthedocs.io/), it has been tested under solaris ( Oracle Solaris 11.4 X86) and illumos (OmniOS v11 r151044).
2022-11-22 12:28:36 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
result.Available = freemem
|
|
|
|
result.Free = freemem
|
|
|
|
result.Used = result.Total - result.Free
|
2017-03-15 03:40:30 +08:00
|
|
|
} else {
|
|
|
|
cap, err := nonGlobalZoneMemoryCapacity()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
result.Total = cap
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func SwapMemory() (*SwapMemoryStat, error) {
|
2017-12-31 14:25:49 +08:00
|
|
|
return SwapMemoryWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func SwapMemoryWithContext(ctx context.Context) (*SwapMemoryStat, error) {
|
2017-03-15 03:40:30 +08:00
|
|
|
return nil, common.ErrNotImplementedError
|
|
|
|
}
|
|
|
|
|
|
|
|
func zoneName() (string, error) {
|
2018-03-31 20:35:53 +08:00
|
|
|
ctx := context.Background()
|
2022-03-05 00:18:03 +08:00
|
|
|
out, err := invoke.CommandWithContext(ctx, "zonename")
|
2017-03-15 03:40:30 +08:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.TrimSpace(string(out)), nil
|
|
|
|
}
|
|
|
|
|
2021-10-22 23:28:52 +08:00
|
|
|
var globalZoneMemoryCapacityMatch = regexp.MustCompile(`[Mm]emory size: (\d+) Megabytes`)
|
2017-03-15 03:40:30 +08:00
|
|
|
|
|
|
|
func globalZoneMemoryCapacity() (uint64, error) {
|
2018-03-31 20:35:53 +08:00
|
|
|
ctx := context.Background()
|
2022-03-05 00:18:03 +08:00
|
|
|
out, err := invoke.CommandWithContext(ctx, "prtconf")
|
2017-03-15 03:40:30 +08:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
match := globalZoneMemoryCapacityMatch.FindAllStringSubmatch(string(out), -1)
|
|
|
|
if len(match) != 1 {
|
2022-03-05 00:18:03 +08:00
|
|
|
return 0, errors.New("memory size not contained in output of prtconf")
|
2017-03-15 03:40:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
totalMB, err := strconv.ParseUint(match[0][1], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return totalMB * 1024 * 1024, nil
|
|
|
|
}
|
|
|
|
|
2022-11-29 13:30:09 +08:00
|
|
|
func globalZoneFreeMemory(ctx context.Context) (uint64, error) {
|
The current codes miss below statistic data under solaris/illumos:
1. the disk io statistic data as: nread, nwritten, reads, writes, rtime, wtime;
2. the free memory under global zone;
3. the net io statistic data as: rbytes64, ipackets64, idrops64, ierrors, obytes64, opackets64, odrops64, oerrors.
The new feature branch adds the above missing statistic data based on the psutil project (https://psutil.readthedocs.io/), it has been tested under solaris ( Oracle Solaris 11.4 X86) and illumos (OmniOS v11 r151044).
2022-11-22 12:28:36 +08:00
|
|
|
output, err := invoke.CommandWithContext(ctx, "pagesize")
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
pagesize, err := strconv.ParseUint(strings.TrimSpace(string(output)), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
free, err := sysconf.Sysconf(sysconf.SC_AVPHYS_PAGES)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return uint64(free) * pagesize, nil
|
|
|
|
}
|
|
|
|
|
2021-10-22 23:28:52 +08:00
|
|
|
var kstatMatch = regexp.MustCompile(`(\S+)\s+(\S*)`)
|
2017-03-15 03:40:30 +08:00
|
|
|
|
|
|
|
func nonGlobalZoneMemoryCapacity() (uint64, error) {
|
2018-03-31 20:35:53 +08:00
|
|
|
ctx := context.Background()
|
2022-03-05 00:18:03 +08:00
|
|
|
out, err := invoke.CommandWithContext(ctx, "kstat", "-p", "-c", "zone_memory_cap", "memory_cap:*:*:physcap")
|
2017-03-15 03:40:30 +08:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
kstats := kstatMatch.FindAllStringSubmatch(string(out), -1)
|
|
|
|
if len(kstats) != 1 {
|
2017-03-15 21:43:20 +08:00
|
|
|
return 0, fmt.Errorf("expected 1 kstat, found %d", len(kstats))
|
2017-03-15 03:40:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
memSizeBytes, err := strconv.ParseUint(kstats[0][2], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return memSizeBytes, nil
|
|
|
|
}
|
2021-08-18 21:52:13 +08:00
|
|
|
|
2021-10-09 22:17:22 +08:00
|
|
|
const swapCommand = "swap"
|
2021-08-18 21:52:13 +08:00
|
|
|
|
|
|
|
// The blockSize as reported by `swap -l`. See https://docs.oracle.com/cd/E23824_01/html/821-1459/fsswap-52195.html
|
|
|
|
const blockSize = 512
|
|
|
|
|
|
|
|
// swapctl column indexes
|
|
|
|
const (
|
|
|
|
nameCol = 0
|
|
|
|
// devCol = 1
|
|
|
|
// swaploCol = 2
|
|
|
|
totalBlocksCol = 3
|
|
|
|
freeBlocksCol = 4
|
|
|
|
)
|
|
|
|
|
|
|
|
func SwapDevices() ([]*SwapDevice, error) {
|
|
|
|
return SwapDevicesWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func SwapDevicesWithContext(ctx context.Context) ([]*SwapDevice, error) {
|
2022-03-05 00:18:03 +08:00
|
|
|
output, err := invoke.CommandWithContext(ctx, swapCommand, "-l")
|
2021-08-18 21:52:13 +08:00
|
|
|
if err != nil {
|
2021-10-09 22:17:22 +08:00
|
|
|
return nil, fmt.Errorf("could not execute %q: %w", swapCommand, err)
|
2021-08-18 21:52:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return parseSwapsCommandOutput(string(output))
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseSwapsCommandOutput(output string) ([]*SwapDevice, error) {
|
|
|
|
lines := strings.Split(output, "\n")
|
|
|
|
if len(lines) == 0 {
|
2021-10-09 22:17:22 +08:00
|
|
|
return nil, fmt.Errorf("could not parse output of %q: no lines in %q", swapCommand, output)
|
2021-08-18 21:52:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check header headerFields are as expected.
|
|
|
|
headerFields := strings.Fields(lines[0])
|
|
|
|
if len(headerFields) < freeBlocksCol {
|
2021-10-09 22:17:22 +08:00
|
|
|
return nil, fmt.Errorf("couldn't parse %q: too few fields in header %q", swapCommand, lines[0])
|
2021-08-18 21:52:13 +08:00
|
|
|
}
|
|
|
|
if headerFields[nameCol] != "swapfile" {
|
2021-10-09 22:17:22 +08:00
|
|
|
return nil, fmt.Errorf("couldn't parse %q: expected %q to be %q", swapCommand, headerFields[nameCol], "swapfile")
|
2021-08-18 21:52:13 +08:00
|
|
|
}
|
|
|
|
if headerFields[totalBlocksCol] != "blocks" {
|
2021-10-09 22:17:22 +08:00
|
|
|
return nil, fmt.Errorf("couldn't parse %q: expected %q to be %q", swapCommand, headerFields[totalBlocksCol], "blocks")
|
2021-08-18 21:52:13 +08:00
|
|
|
}
|
|
|
|
if headerFields[freeBlocksCol] != "free" {
|
2021-10-09 22:17:22 +08:00
|
|
|
return nil, fmt.Errorf("couldn't parse %q: expected %q to be %q", swapCommand, headerFields[freeBlocksCol], "free")
|
2021-08-18 21:52:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var swapDevices []*SwapDevice
|
|
|
|
for _, line := range lines[1:] {
|
|
|
|
if line == "" {
|
|
|
|
continue // the terminal line is typically empty
|
|
|
|
}
|
|
|
|
fields := strings.Fields(line)
|
|
|
|
if len(fields) < freeBlocksCol {
|
2021-10-09 22:17:22 +08:00
|
|
|
return nil, fmt.Errorf("couldn't parse %q: too few fields", swapCommand)
|
2021-08-18 21:52:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
totalBlocks, err := strconv.ParseUint(fields[totalBlocksCol], 10, 64)
|
|
|
|
if err != nil {
|
2021-10-09 22:17:22 +08:00
|
|
|
return nil, fmt.Errorf("couldn't parse 'Size' column in %q: %w", swapCommand, err)
|
2021-08-18 21:52:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
freeBlocks, err := strconv.ParseUint(fields[freeBlocksCol], 10, 64)
|
|
|
|
if err != nil {
|
2021-10-09 22:17:22 +08:00
|
|
|
return nil, fmt.Errorf("couldn't parse 'Used' column in %q: %w", swapCommand, err)
|
2021-08-18 21:52:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
swapDevices = append(swapDevices, &SwapDevice{
|
|
|
|
Name: fields[nameCol],
|
|
|
|
UsedBytes: (totalBlocks - freeBlocks) * blockSize,
|
|
|
|
FreeBytes: freeBlocks * blockSize,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return swapDevices, nil
|
|
|
|
}
|