memory.go 706 Bytes
Newer Older
“李磊”'s avatar
“李磊” committed
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
package limit

import (
	"linkfog.com/public/lib/cgroup"
	"linkfog.com/public/lib/cgroup/fs"
	"linkfog.com/public/lib/cgroup/fs2"
	"linkfog.com/public/lib/l"
)

var (
	DefaultMemLimit = float64(3 * 1024 * 1024 * 1024)
)

func GetSelfMemoryLimit(defaultLimit float64) float64 {
	return GetMemoryLimit(-1, defaultLimit)
}

func GetMemoryLimit(pid int, defaultLimit float64) float64 {
	cgroupDir, err := cgroup.GetContainerMemoryCgroupPath(pid)
	if err != nil {
		l.Errorf("read self cgroup memory path error:%v, use default value", err)
		return defaultLimit
	}

	if cgroup.IsCgroup2() {
		return fs2.GetMemoryLimit(cgroupDir, defaultLimit)
	} else {
		return fs.GetMemoryLimit(cgroupDir, defaultLimit)
	}
}