mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
e0c9d7b654
MemInfo provides a simple API to get memory information from the system. Signed-off-by: Andrea Luzzardi <aluzzardi@gmail.com>
67 lines
1.2 KiB
Go
67 lines
1.2 KiB
Go
package system
|
|
|
|
import (
|
|
"bufio"
|
|
"errors"
|
|
"io"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/docker/docker/pkg/units"
|
|
)
|
|
|
|
var (
|
|
ErrMalformed = errors.New("malformed file")
|
|
)
|
|
|
|
// Retrieve memory statistics of the host system and parse them into a MemInfo
|
|
// type.
|
|
func ReadMemInfo() (*MemInfo, error) {
|
|
file, err := os.Open("/proc/meminfo")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer file.Close()
|
|
return parseMemInfo(file)
|
|
}
|
|
|
|
func parseMemInfo(reader io.Reader) (*MemInfo, error) {
|
|
meminfo := &MemInfo{}
|
|
scanner := bufio.NewScanner(reader)
|
|
for scanner.Scan() {
|
|
// Expected format: ["MemTotal:", "1234", "kB"]
|
|
parts := strings.Fields(scanner.Text())
|
|
|
|
// Sanity checks: Skip malformed entries.
|
|
if len(parts) < 3 || parts[2] != "kB" {
|
|
continue
|
|
}
|
|
|
|
// Convert to bytes.
|
|
size, err := strconv.Atoi(parts[1])
|
|
if err != nil {
|
|
continue
|
|
}
|
|
bytes := int64(size) * units.KiB
|
|
|
|
switch parts[0] {
|
|
case "MemTotal:":
|
|
meminfo.MemTotal = bytes
|
|
case "MemFree:":
|
|
meminfo.MemFree = bytes
|
|
case "SwapTotal:":
|
|
meminfo.SwapTotal = bytes
|
|
case "SwapFree:":
|
|
meminfo.SwapFree = bytes
|
|
}
|
|
|
|
}
|
|
|
|
// Handle errors that may have occurred during the reading of the file.
|
|
if err := scanner.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return meminfo, nil
|
|
}
|