cgroup.go 784 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
31
32
33
34
35
36
37
38
39
package cgroup

import (
	"os"
	"path/filepath"
	"sync"

	"linkfog.com/public/lib/l"
	"linkfog.com/public/option"
)

var (
	HostCgroup    = option.HostPrefix + "/sys/fs/cgroup/"
	DefaultCgroup = "/sys/fs/cgroup/"

	cgroup2ControllersPaths = []string{
		"/proc/1/root/sys/fs/cgroup/cgroup.controllers",
		filepath.Join(HostCgroup, "cgroup.controllers"),
		filepath.Join(DefaultCgroup, "cgroup.controllers"),
	}
	isCgroup2Once sync.Once
	isCgroup2     bool
)

func IsCgroup2() bool {
	isCgroup2Once.Do(func() {
		for _, path := range cgroup2ControllersPaths {
			if _, err := os.Stat(path); err == nil {
				l.Info(path, "exist, is cgroup v2")
				isCgroup2 = true
				break
			}
		}
		if !isCgroup2 {
			l.Info("cgroup.controllers not exist, is cgroup v1")
		}
	})
	return isCgroup2
}