go-judge/env/linuxcontainer/cgrouppool_linux.go
criyle ffdf3e3925
Some checks are pending
Build / Goreleaser (push) Waiting to run
Build / Upload artifacts-${{ matrix.os }}-${{ matrix.arch }} (amd64_v3, darwin) (push) Blocked by required conditions
Build / Upload artifacts-${{ matrix.os }}-${{ matrix.arch }} (amd64_v3, linux) (push) Blocked by required conditions
Build / Upload artifacts-${{ matrix.os }}-${{ matrix.arch }} (amd64_v3, windows) (push) Blocked by required conditions
Build / Upload artifacts-${{ matrix.os }}-${{ matrix.arch }} (arm64_v8.0, darwin) (push) Blocked by required conditions
Build / Upload artifacts-${{ matrix.os }}-${{ matrix.arch }} (arm64_v8.0, linux) (push) Blocked by required conditions
Build / Upload artifacts-${{ matrix.os }}-${{ matrix.arch }} (arm64_v8.0, windows) (push) Blocked by required conditions
linuxc: try support faster new clone3(CLONE_INTO_CGROUP) syscall
criyle/go-sandbox#13
2025-02-20 22:42:24 +00:00

86 lines
1.6 KiB
Go

package linuxcontainer
import (
"os"
"sync"
"time"
"github.com/criyle/go-judge/envexec"
)
// Cgroup defines interface to limit and monitor resources consumption of a process
type Cgroup interface {
SetCpuset(string) error
SetMemoryLimit(envexec.Size) error
SetProcLimit(uint64) error
SetCPURate(uint64) error // 1000 as 1
CPUUsage() (time.Duration, error)
CurrentMemory() (envexec.Size, error)
MaxMemory() (envexec.Size, error)
ProcPeak() (uint64, error)
AddProc(int) error
Reset() error
Destroy() error
Open() (*os.File, error)
}
// CgroupPool implements pool of Cgroup
type CgroupPool interface {
Get() (Cgroup, error)
Put(Cgroup)
}
// CgroupListPool implements cgroup pool
type CgroupListPool struct {
builder CgroupBuilder
cfsPeriod time.Duration
cgs []Cgroup
mu sync.Mutex
}
// NewCgroupListPool creates new cgroup pool
func NewCgroupListPool(builder CgroupBuilder, cfsPeriod time.Duration) CgroupPool {
return &CgroupListPool{builder: builder, cfsPeriod: cfsPeriod}
}
// Get gets cgroup from pool, if pool is empty, creates new one
func (w *CgroupListPool) Get() (Cgroup, error) {
w.mu.Lock()
defer w.mu.Unlock()
if len(w.cgs) > 0 {
rt := w.cgs[len(w.cgs)-1]
w.cgs = w.cgs[:len(w.cgs)-1]
return rt, nil
}
cg, err := w.builder.Random("")
if err != nil {
return nil, err
}
return &wCgroup{cg: cg, cfsPeriod: w.cfsPeriod}, nil
}
// Put puts cgroup into the pool
func (w *CgroupListPool) Put(c Cgroup) {
w.mu.Lock()
defer w.mu.Unlock()
c.Reset()
w.cgs = append(w.cgs, c)
}
// Shutdown destroy all cgroup
func (w *CgroupListPool) Shutdown() {
w.mu.Lock()
defer w.mu.Unlock()
for _, c := range w.cgs {
c.Destroy()
}
}