|
|
|
@ -1,3 +1,5 @@
|
|
|
|
|
// +build solaris
|
|
|
|
|
|
|
|
|
|
package mem
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
@ -119,3 +121,85 @@ func nonGlobalZoneMemoryCapacity() (uint64, error) {
|
|
|
|
|
|
|
|
|
|
return memSizeBytes, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const swapsCommand = "swap"
|
|
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
|
swapsCommandPath, err := exec.LookPath(swapsCommand)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("could not find command %q: %w", swapCommand, err)
|
|
|
|
|
}
|
|
|
|
|
output, err := invoke.CommandWithContext(swapsCommandPath, "-l")
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("could not execute %q: %w", swapsCommand, err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return parseSwapsCommandOutput(string(output))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func parseSwapsCommandOutput(output string) ([]*SwapDevice, error) {
|
|
|
|
|
lines := strings.Split(output, "\n")
|
|
|
|
|
if len(lines) == 0 {
|
|
|
|
|
return nil, fmt.Errorf("could not parse output of %q: no lines in %q", swapsCommand, output)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check header headerFields are as expected.
|
|
|
|
|
headerFields := strings.Fields(lines[0])
|
|
|
|
|
if len(headerFields) < freeBlocksCol {
|
|
|
|
|
return nil, fmt.Errorf("couldn't parse %q: too few fields in header %q", swapsCommand, lines[0])
|
|
|
|
|
}
|
|
|
|
|
if headerFields[nameCol] != "swapfile" {
|
|
|
|
|
return nil, fmt.Errorf("couldn't parse %q: expected %q to be %q", swapsCommand, headerFields[nameCol], "swapfile")
|
|
|
|
|
}
|
|
|
|
|
if headerFields[totalBlocksCol] != "blocks" {
|
|
|
|
|
return nil, fmt.Errorf("couldn't parse %q: expected %q to be %q", swapsCommand, headerFields[totalBlocksCol], "blocks")
|
|
|
|
|
}
|
|
|
|
|
if headerFields[freeBlocksCol] != "free" {
|
|
|
|
|
return nil, fmt.Errorf("couldn't parse %q: expected %q to be %q", swapsCommand, headerFields[freeBlocksCol], "free")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 {
|
|
|
|
|
return nil, fmt.Errorf("couldn't parse %q: too few fields", swapsCommand)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
totalBlocks, err := strconv.ParseUint(fields[totalBlocksCol], 10, 64)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("couldn't parse 'Size' column in %q: %w", swapsCommand, err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
freeBlocks, err := strconv.ParseUint(fields[freeBlocksCol], 10, 64)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("couldn't parse 'Used' column in %q: %w", swapsCommand, err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
swapDevices = append(swapDevices, &SwapDevice{
|
|
|
|
|
Name: fields[nameCol],
|
|
|
|
|
UsedBytes: (totalBlocks - freeBlocks) * blockSize,
|
|
|
|
|
FreeBytes: freeBlocks * blockSize,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return swapDevices, nil
|
|
|
|
|
}
|
|
|
|
|