Skip to content

Commit

Permalink
server: catch incomplete available memory detection by gosigar
Browse files Browse the repository at this point in the history
If gosigar is told by a syscall there is 0 available memory, consider
this information as false even if there was no error.
  • Loading branch information
knz committed May 10, 2017
1 parent 47d79cc commit 37e7024
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions pkg/server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,12 @@ func GetTotalMemory() (int64, error) {
humanize.IBytes(mem.Total), humanize.Bytes(math.MaxInt64))
}
totalMem := int64(mem.Total)
checkTotal := func(x int64) (int64, error) {
if x <= 0 {
return 0, fmt.Errorf("inferred memory size %d is suspicious, considering invalid", x)
}
return x, nil
}
if runtime.GOOS == "linux" {
var err error
var buf []byte
Expand All @@ -245,35 +251,34 @@ func GetTotalMemory() (int64, error) {
log.Infof(context.TODO(), "can't read available memory from cgroups (%s), using system memory %s instead", err,
humanizeutil.IBytes(totalMem))
}
return totalMem, nil
return checkTotal(totalMem)
}
var cgAvlMem uint64
if cgAvlMem, err = strconv.ParseUint(strings.TrimSpace(string(buf)), 10, 64); err != nil {
if log.V(1) {
log.Infof(context.TODO(), "can't parse available memory from cgroups (%s), using system memory %s instead", err,
humanizeutil.IBytes(totalMem))
}
return totalMem, nil
return checkTotal(totalMem)
}
if cgAvlMem > math.MaxInt64 {
if log.V(1) {
log.Infof(context.TODO(), "available memory from cgroups is too large and unsupported %s using system memory %s instead",
humanize.IBytes(cgAvlMem), humanizeutil.IBytes(totalMem))

}
return totalMem, nil
return checkTotal(totalMem)
}
if cgAvlMem > mem.Total {
if mem.Total > 0 && cgAvlMem > mem.Total {
if log.V(1) {
log.Infof(context.TODO(), "available memory from cgroups %s exceeds system memory %s, using system memory",
humanize.IBytes(cgAvlMem), humanizeutil.IBytes(totalMem))
}
return totalMem, nil
return checkTotal(totalMem)
}

return int64(cgAvlMem), nil
totalMem = int64(cgAvlMem)
}
return totalMem, nil
return checkTotal(totalMem)
}

// setOpenFileLimit sets the soft limit for open file descriptors to the hard
Expand Down

0 comments on commit 37e7024

Please sign in to comment.