1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Use MemAvailable instead of MemFree to estimate actual available memory

Signed-off-by: Kenta Tada <Kenta.Tada@sony.com>
This commit is contained in:
Kenta Tada 2019-07-10 17:43:03 +09:00
parent f550cb5792
commit 65177a72c5
2 changed files with 12 additions and 5 deletions

View file

@ -27,6 +27,7 @@ func ReadMemInfo() (*MemInfo, error) {
func parseMemInfo(reader io.Reader) (*MemInfo, error) {
meminfo := &MemInfo{}
scanner := bufio.NewScanner(reader)
memAvailable := int64(-1)
for scanner.Scan() {
// Expected format: ["MemTotal:", "1234", "kB"]
parts := strings.Fields(scanner.Text())
@ -48,6 +49,8 @@ func parseMemInfo(reader io.Reader) (*MemInfo, error) {
meminfo.MemTotal = bytes
case "MemFree:":
meminfo.MemFree = bytes
case "MemAvailable:":
memAvailable = bytes
case "SwapTotal:":
meminfo.SwapTotal = bytes
case "SwapFree:":
@ -55,6 +58,9 @@ func parseMemInfo(reader io.Reader) (*MemInfo, error) {
}
}
if memAvailable != -1 {
meminfo.MemFree = memAvailable
}
// Handle errors that may have occurred during the reading of the file.
if err := scanner.Err(); err != nil {

View file

@ -14,8 +14,9 @@ func TestMemInfo(t *testing.T) {
const input = `
MemTotal: 1 kB
MemFree: 2 kB
SwapTotal: 3 kB
SwapFree: 4 kB
MemAvailable: 3 kB
SwapTotal: 4 kB
SwapFree: 5 kB
Malformed1:
Malformed2: 1
Malformed3: 2 MB
@ -28,13 +29,13 @@ func TestMemInfo(t *testing.T) {
if meminfo.MemTotal != 1*units.KiB {
t.Fatalf("Unexpected MemTotal: %d", meminfo.MemTotal)
}
if meminfo.MemFree != 2*units.KiB {
if meminfo.MemFree != 3*units.KiB {
t.Fatalf("Unexpected MemFree: %d", meminfo.MemFree)
}
if meminfo.SwapTotal != 3*units.KiB {
if meminfo.SwapTotal != 4*units.KiB {
t.Fatalf("Unexpected SwapTotal: %d", meminfo.SwapTotal)
}
if meminfo.SwapFree != 4*units.KiB {
if meminfo.SwapFree != 5*units.KiB {
t.Fatalf("Unexpected SwapFree: %d", meminfo.SwapFree)
}
}