2017-03-15 03:40:30 +08:00
|
|
|
package mem
|
|
|
|
|
|
|
|
import (
|
2017-12-31 14:25:49 +08:00
|
|
|
"context"
|
2017-03-15 03:40:30 +08:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"os/exec"
|
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/shirou/gopsutil/internal/common"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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
|
|
|
|
} 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) {
|
|
|
|
zonename, err := exec.LookPath("/usr/bin/zonename")
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2018-03-31 20:35:53 +08:00
|
|
|
ctx := context.Background()
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-03-15 21:43:20 +08:00
|
|
|
var globalZoneMemoryCapacityMatch = regexp.MustCompile(`memory size: ([\d]+) Megabytes`)
|
2017-03-15 03:40:30 +08:00
|
|
|
|
|
|
|
func globalZoneMemoryCapacity() (uint64, error) {
|
|
|
|
prtconf, err := exec.LookPath("/usr/sbin/prtconf")
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2018-03-31 20:35:53 +08:00
|
|
|
ctx := context.Background()
|
|
|
|
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 {
|
2017-03-15 21:43:20 +08:00
|
|
|
return 0, errors.New("memory size not contained in output of /usr/sbin/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
|
|
|
|
}
|
|
|
|
|
|
|
|
var kstatMatch = regexp.MustCompile(`([^\s]+)[\s]+([^\s]*)`)
|
|
|
|
|
|
|
|
func nonGlobalZoneMemoryCapacity() (uint64, error) {
|
|
|
|
kstat, err := exec.LookPath("/usr/bin/kstat")
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2018-03-31 20:35:53 +08:00
|
|
|
ctx := context.Background()
|
|
|
|
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
|
|
|
|
}
|