mirror of
https://github.com/lzh-1625/go_process_manager.git
synced 2025-10-05 07:56:50 +08:00
std cgruop support
This commit is contained in:
@@ -26,7 +26,6 @@ type Process interface {
|
|||||||
readInit()
|
readInit()
|
||||||
doOnInit()
|
doOnInit()
|
||||||
doOnKilled()
|
doOnKilled()
|
||||||
initCgroup()
|
|
||||||
Start() error
|
Start() error
|
||||||
Type() constants.TerminalType
|
Type() constants.TerminalType
|
||||||
SetTerminalSize(int, int)
|
SetTerminalSize(int, int)
|
87
internal/app/logic/process_base_cgroup_linux.go
Normal file
87
internal/app/logic/process_base_cgroup_linux.go
Normal file
@@ -0,0 +1,87 @@
|
|||||||
|
package logic
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/containerd/cgroups/v3"
|
||||||
|
"github.com/containerd/cgroups/v3/cgroup1"
|
||||||
|
"github.com/containerd/cgroups/v3/cgroup2"
|
||||||
|
"github.com/lzh-1625/go_process_manager/config"
|
||||||
|
"github.com/lzh-1625/go_process_manager/log"
|
||||||
|
"github.com/opencontainers/runtime-spec/specs-go"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (p *ProcessBase) initCgroup() {
|
||||||
|
if !p.Config.cgroupEnable {
|
||||||
|
log.Logger.Debugw("不启用cgroup")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
switch cgroups.Mode() {
|
||||||
|
case cgroups.Unavailable:
|
||||||
|
log.Logger.Warnw("当前系统不支持cgroup")
|
||||||
|
case cgroups.Legacy, cgroups.Hybrid:
|
||||||
|
log.Logger.Debugw("启用cgroupv1")
|
||||||
|
p.initCgroupV1()
|
||||||
|
case cgroups.Unified:
|
||||||
|
log.Logger.Debugw("启用cgroupv2")
|
||||||
|
p.initCgroupV2()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *ProcessBase) initCgroupV1() {
|
||||||
|
resources := &specs.LinuxResources{}
|
||||||
|
if p.Config.cpuLimit != nil {
|
||||||
|
period := uint64(config.CF.CgroupPeriod)
|
||||||
|
quota := int64(float32(config.CF.CgroupPeriod) * *p.Config.cpuLimit * 0.01)
|
||||||
|
cpuResources := &specs.LinuxCPU{
|
||||||
|
Period: &period,
|
||||||
|
Quota: "a,
|
||||||
|
}
|
||||||
|
resources.CPU = cpuResources
|
||||||
|
}
|
||||||
|
if p.Config.memoryLimit != nil {
|
||||||
|
limit := int64(*p.Config.memoryLimit * 1024 * 1024)
|
||||||
|
memResources := &specs.LinuxMemory{
|
||||||
|
Limit: &limit,
|
||||||
|
}
|
||||||
|
if config.CF.CgroupSwapLimit {
|
||||||
|
memResources.Swap = &limit
|
||||||
|
}
|
||||||
|
resources.Memory = memResources
|
||||||
|
}
|
||||||
|
control, err := cgroup1.New(cgroup1.StaticPath("/"+p.Name), resources)
|
||||||
|
if err != nil {
|
||||||
|
log.Logger.Errorw("启用cgroup失败", "err", err, "name", p.Name)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
control.AddProc(uint64(p.Pid))
|
||||||
|
p.cgroup.delete = control.Delete
|
||||||
|
p.cgroup.enable = true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *ProcessBase) initCgroupV2() {
|
||||||
|
resources := &cgroup2.Resources{}
|
||||||
|
if p.Config.cpuLimit != nil {
|
||||||
|
period := uint64(config.CF.CgroupPeriod)
|
||||||
|
quota := int64(float32(config.CF.CgroupPeriod) * *p.Config.cpuLimit * 0.01)
|
||||||
|
resources.CPU = &cgroup2.CPU{
|
||||||
|
Max: cgroup2.NewCPUMax("a, &period),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if p.Config.memoryLimit != nil {
|
||||||
|
limit := int64(*p.Config.memoryLimit * 1024 * 1024)
|
||||||
|
memResources := &cgroup2.Memory{
|
||||||
|
Max: &limit,
|
||||||
|
}
|
||||||
|
if config.CF.CgroupSwapLimit {
|
||||||
|
memResources.Swap = &limit
|
||||||
|
}
|
||||||
|
resources.Memory = memResources
|
||||||
|
}
|
||||||
|
control, err := cgroup2.NewSystemd("/", p.Name+".slice", -1, resources)
|
||||||
|
if err != nil {
|
||||||
|
log.Logger.Errorw("启用cgroup失败", "err", err, "name", p.Name)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
control.AddProc(uint64(p.Pid))
|
||||||
|
p.cgroup.delete = control.DeleteSystemd
|
||||||
|
p.cgroup.enable = true
|
||||||
|
}
|
7
internal/app/logic/process_base_cgroup_windows.go
Normal file
7
internal/app/logic/process_base_cgroup_windows.go
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
package logic
|
||||||
|
|
||||||
|
import "github.com/lzh-1625/go_process_manager/log"
|
||||||
|
|
||||||
|
func (p *ProcessBase) initCgroup() {
|
||||||
|
log.Logger.Debugw("不支持cgroup")
|
||||||
|
}
|
@@ -6,15 +6,11 @@ import (
|
|||||||
"os/exec"
|
"os/exec"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/containerd/cgroups/v3"
|
|
||||||
"github.com/containerd/cgroups/v3/cgroup1"
|
|
||||||
"github.com/containerd/cgroups/v3/cgroup2"
|
|
||||||
"github.com/lzh-1625/go_process_manager/config"
|
"github.com/lzh-1625/go_process_manager/config"
|
||||||
"github.com/lzh-1625/go_process_manager/internal/app/constants"
|
"github.com/lzh-1625/go_process_manager/internal/app/constants"
|
||||||
"github.com/lzh-1625/go_process_manager/internal/app/model"
|
"github.com/lzh-1625/go_process_manager/internal/app/model"
|
||||||
"github.com/lzh-1625/go_process_manager/log"
|
"github.com/lzh-1625/go_process_manager/log"
|
||||||
"github.com/lzh-1625/go_process_manager/utils"
|
"github.com/lzh-1625/go_process_manager/utils"
|
||||||
"github.com/opencontainers/runtime-spec/specs-go"
|
|
||||||
|
|
||||||
"github.com/creack/pty"
|
"github.com/creack/pty"
|
||||||
)
|
)
|
||||||
@@ -118,83 +114,6 @@ func (p *ProcessPty) readInit() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *ProcessPty) initCgroup() {
|
|
||||||
if !p.Config.cgroupEnable {
|
|
||||||
log.Logger.Debugw("不启用cgroup")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
switch cgroups.Mode() {
|
|
||||||
case cgroups.Unavailable:
|
|
||||||
log.Logger.Warnw("当前系统不支持cgroup")
|
|
||||||
case cgroups.Legacy, cgroups.Hybrid:
|
|
||||||
log.Logger.Debugw("启用cgroupv1")
|
|
||||||
p.initCgroupV1()
|
|
||||||
case cgroups.Unified:
|
|
||||||
log.Logger.Debugw("启用cgroupv2")
|
|
||||||
p.initCgroupV2()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *ProcessPty) initCgroupV1() {
|
|
||||||
resources := &specs.LinuxResources{}
|
|
||||||
if p.Config.cpuLimit != nil {
|
|
||||||
period := uint64(config.CF.CgroupPeriod)
|
|
||||||
quota := int64(float32(config.CF.CgroupPeriod) * *p.Config.cpuLimit * 0.01)
|
|
||||||
cpuResources := &specs.LinuxCPU{
|
|
||||||
Period: &period,
|
|
||||||
Quota: "a,
|
|
||||||
}
|
|
||||||
resources.CPU = cpuResources
|
|
||||||
}
|
|
||||||
if p.Config.memoryLimit != nil {
|
|
||||||
limit := int64(*p.Config.memoryLimit * 1024 * 1024)
|
|
||||||
memResources := &specs.LinuxMemory{
|
|
||||||
Limit: &limit,
|
|
||||||
}
|
|
||||||
if config.CF.CgroupSwapLimit {
|
|
||||||
memResources.Swap = &limit
|
|
||||||
}
|
|
||||||
resources.Memory = memResources
|
|
||||||
}
|
|
||||||
control, err := cgroup1.New(cgroup1.StaticPath("/"+p.Name), resources)
|
|
||||||
if err != nil {
|
|
||||||
log.Logger.Errorw("启用cgroup失败", "err", err, "name", p.Name)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
control.AddProc(uint64(p.Pid))
|
|
||||||
p.cgroup.delete = control.Delete
|
|
||||||
p.cgroup.enable = true
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *ProcessPty) initCgroupV2() {
|
|
||||||
resources := &cgroup2.Resources{}
|
|
||||||
if p.Config.cpuLimit != nil {
|
|
||||||
period := uint64(config.CF.CgroupPeriod)
|
|
||||||
quota := int64(float32(config.CF.CgroupPeriod) * *p.Config.cpuLimit * 0.01)
|
|
||||||
resources.CPU = &cgroup2.CPU{
|
|
||||||
Max: cgroup2.NewCPUMax("a, &period),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if p.Config.memoryLimit != nil {
|
|
||||||
limit := int64(*p.Config.memoryLimit * 1024 * 1024)
|
|
||||||
memResources := &cgroup2.Memory{
|
|
||||||
Max: &limit,
|
|
||||||
}
|
|
||||||
if config.CF.CgroupSwapLimit {
|
|
||||||
memResources.Swap = &limit
|
|
||||||
}
|
|
||||||
resources.Memory = memResources
|
|
||||||
}
|
|
||||||
control, err := cgroup2.NewSystemd("/", p.Name+".slice", -1, resources)
|
|
||||||
if err != nil {
|
|
||||||
log.Logger.Errorw("启用cgroup失败", "err", err, "name", p.Name)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
control.AddProc(uint64(p.Pid))
|
|
||||||
p.cgroup.delete = control.DeleteSystemd
|
|
||||||
p.cgroup.enable = true
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *ProcessPty) ReadCache(ws ConnectInstance) {
|
func (p *ProcessPty) ReadCache(ws ConnectInstance) {
|
||||||
ws.Write(p.cacheBytesBuf.Bytes())
|
ws.Write(p.cacheBytesBuf.Bytes())
|
||||||
}
|
}
|
||||||
|
@@ -117,10 +117,6 @@ func (p *ProcessPty) readInit() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *ProcessPty) initCgroup() {
|
|
||||||
log.Logger.Debugw("不支持cgroup")
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *ProcessPty) ReadCache(ws ConnectInstance) {
|
func (p *ProcessPty) ReadCache(ws ConnectInstance) {
|
||||||
ws.Write(p.cacheBytesBuf.Bytes())
|
ws.Write(p.cacheBytesBuf.Bytes())
|
||||||
}
|
}
|
||||||
|
@@ -82,10 +82,6 @@ func (p *ProcessStd) doOnInit() {
|
|||||||
p.cacheLine = make([]string, config.CF.ProcessMsgCacheLinesLimit)
|
p.cacheLine = make([]string, config.CF.ProcessMsgCacheLinesLimit)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *ProcessStd) initCgroup() {
|
|
||||||
log.Logger.Debugw("不支持cgroup")
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *ProcessStd) ReadCache(ws ConnectInstance) {
|
func (p *ProcessStd) ReadCache(ws ConnectInstance) {
|
||||||
for _, line := range p.cacheLine {
|
for _, line := range p.cacheLine {
|
||||||
ws.WriteString(line)
|
ws.WriteString(line)
|
||||||
|
Reference in New Issue
Block a user