mirror of
https://github.com/criyle/go-sandbox.git
synced 2025-11-04 14:49:53 +08:00
pkg/cgroup: add enabled controler check for v2
This commit is contained in:
parent
144d408a6d
commit
3fb1ccd890
@ -130,7 +130,8 @@ func newV1(prefix string, ct *Controllers) (cg Cgroup, err error) {
|
|||||||
|
|
||||||
func newV2(prefix string, ct *Controllers) (cg Cgroup, err error) {
|
func newV2(prefix string, ct *Controllers) (cg Cgroup, err error) {
|
||||||
v2 := &V2{
|
v2 := &V2{
|
||||||
path: path.Join(basePath, prefix),
|
path: path.Join(basePath, prefix),
|
||||||
|
control: ct,
|
||||||
}
|
}
|
||||||
if _, err := os.Stat(v2.path); err == nil {
|
if _, err := os.Stat(v2.path); err == nil {
|
||||||
v2.existing = true
|
v2.existing = true
|
||||||
@ -221,6 +222,7 @@ func openExistingV2(prefix string, ct *Controllers) (cg Cgroup, err error) {
|
|||||||
}
|
}
|
||||||
return &V2{
|
return &V2{
|
||||||
path: path.Join(basePath, prefix),
|
path: path.Join(basePath, prefix),
|
||||||
|
control: ect,
|
||||||
existing: true,
|
existing: true,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@ -13,6 +13,7 @@ import (
|
|||||||
// V2 provides cgroup interface for v2
|
// V2 provides cgroup interface for v2
|
||||||
type V2 struct {
|
type V2 struct {
|
||||||
path string
|
path string
|
||||||
|
control *Controllers
|
||||||
subtreeOnce sync.Once
|
subtreeOnce sync.Once
|
||||||
subtreeErr error
|
subtreeErr error
|
||||||
existing bool
|
existing bool
|
||||||
@ -41,7 +42,8 @@ func (c *V2) New(name string) (Cgroup, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
v2 := &V2{
|
v2 := &V2{
|
||||||
path: path.Join(c.path, name),
|
path: path.Join(c.path, name),
|
||||||
|
control: c.control,
|
||||||
}
|
}
|
||||||
if err := os.Mkdir(v2.path, dirPerm); err != nil {
|
if err := os.Mkdir(v2.path, dirPerm); err != nil {
|
||||||
if !os.IsExist(err) {
|
if !os.IsExist(err) {
|
||||||
@ -55,7 +57,8 @@ func (c *V2) New(name string) (Cgroup, error) {
|
|||||||
// Nest creates a sub-cgroup, moves current process into that cgroup
|
// Nest creates a sub-cgroup, moves current process into that cgroup
|
||||||
func (c *V2) Nest(name string) (Cgroup, error) {
|
func (c *V2) Nest(name string) (Cgroup, error) {
|
||||||
v2 := &V2{
|
v2 := &V2{
|
||||||
path: path.Join(c.path, name),
|
path: path.Join(c.path, name),
|
||||||
|
control: c.control,
|
||||||
}
|
}
|
||||||
if err := os.Mkdir(v2.path, dirPerm); err != nil {
|
if err := os.Mkdir(v2.path, dirPerm); err != nil {
|
||||||
if !os.IsExist(err) {
|
if !os.IsExist(err) {
|
||||||
@ -138,32 +141,50 @@ func (c *V2) CPUUsage() (uint64, error) {
|
|||||||
|
|
||||||
// MemoryUsage reads memory.current
|
// MemoryUsage reads memory.current
|
||||||
func (c *V2) MemoryUsage() (uint64, error) {
|
func (c *V2) MemoryUsage() (uint64, error) {
|
||||||
|
if !c.control.Memory {
|
||||||
|
return 0, ErrNotInitialized
|
||||||
|
}
|
||||||
return c.ReadUint("memory.current")
|
return c.ReadUint("memory.current")
|
||||||
}
|
}
|
||||||
|
|
||||||
// MemoryMaxUsage reads memory.peak
|
// MemoryMaxUsage reads memory.peak
|
||||||
func (c *V2) MemoryMaxUsage() (uint64, error) {
|
func (c *V2) MemoryMaxUsage() (uint64, error) {
|
||||||
|
if !c.control.Memory {
|
||||||
|
return 0, ErrNotInitialized
|
||||||
|
}
|
||||||
return c.ReadUint("memory.peak")
|
return c.ReadUint("memory.peak")
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetCPUBandwidth set cpu.max quota period
|
// SetCPUBandwidth set cpu.max quota period
|
||||||
func (c *V2) SetCPUBandwidth(quota, period uint64) error {
|
func (c *V2) SetCPUBandwidth(quota, period uint64) error {
|
||||||
|
if !c.control.CPU {
|
||||||
|
return ErrNotInitialized
|
||||||
|
}
|
||||||
content := strconv.FormatUint(quota, 10) + " " + strconv.FormatUint(period, 10)
|
content := strconv.FormatUint(quota, 10) + " " + strconv.FormatUint(period, 10)
|
||||||
return c.WriteFile("cpu.max", []byte(content))
|
return c.WriteFile("cpu.max", []byte(content))
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetCPUSet sets cpuset.cpus
|
// SetCPUSet sets cpuset.cpus
|
||||||
func (c *V2) SetCPUSet(content []byte) error {
|
func (c *V2) SetCPUSet(content []byte) error {
|
||||||
|
if !c.control.CPUSet {
|
||||||
|
return ErrNotInitialized
|
||||||
|
}
|
||||||
return c.WriteFile("cpuset.cpus", content)
|
return c.WriteFile("cpuset.cpus", content)
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetMemoryLimit memory.max
|
// SetMemoryLimit memory.max
|
||||||
func (c *V2) SetMemoryLimit(l uint64) error {
|
func (c *V2) SetMemoryLimit(l uint64) error {
|
||||||
|
if !c.control.Memory {
|
||||||
|
return ErrNotInitialized
|
||||||
|
}
|
||||||
return c.WriteUint("memory.max", l)
|
return c.WriteUint("memory.max", l)
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetProcLimit pids.max
|
// SetProcLimit pids.max
|
||||||
func (c *V2) SetProcLimit(l uint64) error {
|
func (c *V2) SetProcLimit(l uint64) error {
|
||||||
|
if !c.control.Pids {
|
||||||
|
return ErrNotInitialized
|
||||||
|
}
|
||||||
return c.WriteUint("pids.max", l)
|
return c.WriteUint("pids.max", l)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user