Skip to content

Commit

Permalink
feat(linux): add procPeak measurement for peak thread count in the co…
Browse files Browse the repository at this point in the history
…ntainer

Linux kernel >= 6.1 && cgroup v2 only
  • Loading branch information
criyle committed Feb 20, 2025
1 parent 9dc3380 commit efa629a
Show file tree
Hide file tree
Showing 13 changed files with 28 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ interface Result {
exitStatus: number; // 程序返回值
time: number; // 程序运行 CPU 时间,单位纳秒
memory: number; // 程序运行内存,单位 byte
procPeak?: number; // 程序运行最大线程数量(需要内核版本>=6.1,且开启 cgroup v2)
runTime: number; // 程序运行现实时间,单位纳秒
// copyOut 和 pipeCollector 指定的文件内容
files?: {[name:string]:string};
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ interface Result {
time: number; // ns (cgroup recorded time)
memory: number; // byte
runTime: number; // ns (wall clock time)
procPeak?: number; // peak number of process (cgroup v2, kernel >= 6.1)
// copyFile name -> content
files?: {[name:string]:string};
// copyFileCached name -> fileId
Expand Down
2 changes: 2 additions & 0 deletions cmd/go-judge/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,7 @@ func generateHandleVersion(conf *config.Config, builderParam map[string]any) fun
"symlink": true,
"addressSpaceLimit": true,
"stream": true,
"procPeak": true,
})
}
}
Expand All @@ -563,6 +564,7 @@ func generateHandleConfig(conf *config.Config, builderParam map[string]any) func
"symlink": true,
"addressSpaceLimit": true,
"stream": true,
"procPeak": true,
"fileStorePath": conf.Dir,
"runnerConfig": builderParam,
})
Expand Down
4 changes: 4 additions & 0 deletions cmd/go-judge/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ type Result struct {
Time uint64 `json:"time"`
Memory uint64 `json:"memory"`
RunTime uint64 `json:"runTime"`
ProcPeak uint64 `json:"procPeak,omitempty"`
Files map[string]string `json:"files,omitempty"`
FileIDs map[string]string `json:"fileIds,omitempty"`
FileError []FileError `json:"fileError,omitempty"`
Expand All @@ -128,6 +129,7 @@ func (r Result) String() string {
Error string
Time time.Duration
RunTime time.Duration
ProcPeak uint64
Memory envexec.Size
Files map[string]string
FileIDs map[string]string
Expand All @@ -140,6 +142,7 @@ func (r Result) String() string {
Time: time.Duration(r.Time),
RunTime: time.Duration(r.RunTime),
Memory: envexec.Size(r.Memory),
ProcPeak: r.ProcPeak,
Files: make(map[string]string),
FileIDs: r.FileIDs,
FileError: r.FileError,
Expand Down Expand Up @@ -250,6 +253,7 @@ func convertResult(r worker.Result, mmap bool) (Result, error) {
Time: uint64(r.Time),
RunTime: uint64(r.RunTime),
Memory: uint64(r.Memory),
ProcPeak: r.ProcPeak,
FileIDs: r.FileIDs,
FileError: r.FileError,
}
Expand Down
4 changes: 4 additions & 0 deletions env/linuxcontainer/cgroup_wrapper_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ func (c *wCgroup) MaxMemory() (envexec.Size, error) {
return envexec.Size(s), err
}

func (c *wCgroup) ProcPeak() (uint64, error) {
return c.cg.ProcessPeak()
}

func (c *wCgroup) AddProc(pid int) error {
return c.cg.AddProc(pid)
}
Expand Down
1 change: 1 addition & 0 deletions env/linuxcontainer/cgrouppool_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type Cgroup interface {
CPUUsage() (time.Duration, error)
CurrentMemory() (envexec.Size, error)
MaxMemory() (envexec.Size, error)
ProcPeak() (uint64, error)

AddProc(int) error
Reset() error
Expand Down
3 changes: 3 additions & 0 deletions env/linuxcontainer/envprocess_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ func (p *process) collectUsage() {
if m, err := p.cg.MaxMemory(); err == nil && m > 0 {
p.rt.Memory = m
}
if pp, err := p.cg.ProcPeak(); err == nil && pp > 0 {
p.rt.ProcPeak = pp
}
}

func (p *process) Done() <-chan struct{} {
Expand Down
7 changes: 4 additions & 3 deletions envexec/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,10 @@ type Result struct {

Error string // error

Time time.Duration
RunTime time.Duration
Memory Size // byte
Time time.Duration
RunTime time.Duration
Memory Size // byte
ProcPeak uint64 // maximum processes ever running

// Files stores copy out files
Files map[string]*os.File
Expand Down
1 change: 1 addition & 0 deletions envexec/run_single.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func runSingle(pc context.Context, c *Cmd, fds []*os.File, ptc []pipeCollector,
Time: rt.Time,
RunTime: rt.RunningTime,
Memory: rt.Memory,
ProcPeak: rt.ProcPeak,
Files: files,
FileError: fe,
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.23
require (
github.com/coreos/go-systemd/v22 v22.5.0
github.com/creack/pty v1.1.24
github.com/criyle/go-sandbox v0.10.7
github.com/criyle/go-sandbox v0.10.8
github.com/elastic/go-seccomp-bpf v1.5.0
github.com/elastic/go-ucfg v0.8.8
github.com/gin-contrib/zap v1.1.4
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ github.com/creack/pty v1.1.24 h1:bJrF4RRfyJnbTJqzRLHzcGaZK1NeM5kTC9jGgovnR1s=
github.com/creack/pty v1.1.24/go.mod h1:08sCNb52WyoAwi2QDyzUCTgcvVFhUzewun7wtTfvcwE=
github.com/criyle/go-sandbox v0.10.7 h1:KTBzdzkCCu0mV+JE4fb00WUmvV0qMo6zG91wLDWb9Ew=
github.com/criyle/go-sandbox v0.10.7/go.mod h1:9IZSv7cxcDkVaPSRufhMPLUg+7M7lTPAt8hjd/iMKFo=
github.com/criyle/go-sandbox v0.10.8 h1:A99ywIJfgeiW5byzRR2kXB4a3YbwV7Wkc4lLTfNP33o=
github.com/criyle/go-sandbox v0.10.8/go.mod h1:9IZSv7cxcDkVaPSRufhMPLUg+7M7lTPAt8hjd/iMKFo=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down
3 changes: 3 additions & 0 deletions worker/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ type Result struct {
Time time.Duration
RunTime time.Duration
Memory Size
ProcPeak uint64
Files map[string]*os.File
FileIDs map[string]string
FileError []FileError
Expand All @@ -79,6 +80,7 @@ func (r Result) String() string {
Time time.Duration
RunTime time.Duration
Memory Size
ProcPeak uint64
Files map[string]string
FileIDs map[string]string
FileError []FileError
Expand All @@ -90,6 +92,7 @@ func (r Result) String() string {
Time: r.Time,
RunTime: r.RunTime,
Memory: r.Memory,
ProcPeak: r.ProcPeak,
Files: make(map[string]string),
FileIDs: r.FileIDs,
FileError: r.FileError,
Expand Down
1 change: 1 addition & 0 deletions worker/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ func (w *worker) convertResult(result envexec.Result, cmd Cmd) (res Result) {
res.Time = result.Time
res.RunTime = result.RunTime
res.Memory = result.Memory
res.ProcPeak = result.ProcPeak
res.FileError = result.FileError
res.Files = make(map[string]*os.File)
res.FileIDs = make(map[string]string)
Expand Down

0 comments on commit efa629a

Please sign in to comment.