cpu.go 668 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 (
	DefaultCPULimit = float64(2)
)

func GetSelfCPULimit(defaultLimit float64) float64 {
	return GetCPULimit(-1, defaultLimit)
}

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

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