-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmemory.go
35 lines (29 loc) · 986 Bytes
/
memory.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package osutil
import (
"io/ioutil"
"strconv"
"strings"
"github.com/pbnjay/memory"
)
const (
// This is the default value for cgroup's limit_in_bytes. This is not a
// valid value and indicates that the memory is not restricted.
// See https://unix.stackexchange.com/questions/420906/what-is-the-value-for-the-cgroups-limit-in-bytes-if-the-memory-is-not-restricte
unrestrictedMemoryLimit = 9223372036854771712
)
const (
dockerMemoryLimitLocation = "/sys/fs/cgroup/memory/memory.limit_in_bytes"
)
// GetTotalMemory returns the total available memory size. The call is
// container-aware.
func GetTotalMemory() uint64 {
totalMemory := memory.TotalMemory()
cgroupLimit, err := ioutil.ReadFile(dockerMemoryLimitLocation)
if err == nil {
dockerMemoryLimit, err := strconv.ParseUint(strings.Replace(string(cgroupLimit), "\n", "", 1), 10, 64)
if err == nil && dockerMemoryLimit != unrestrictedMemoryLimit {
totalMemory = dockerMemoryLimit
}
}
return totalMemory
}