Add option to include child processes in observation

This commit is contained in:
Ingo Oppermann
2025-06-25 16:53:27 +02:00
parent fba9a75d8a
commit d6ce3a5891
4 changed files with 18 additions and 12 deletions

View File

@@ -89,7 +89,7 @@ func (u *MockPSUtil) GPU() ([]psutil.GPUInfo, error) {
return gpu, nil return gpu, nil
} }
func (u *MockPSUtil) Process(pid int32) (psutil.Process, error) { func (u *MockPSUtil) Process(pid int32, children bool) (psutil.Process, error) {
return &mockPSUtilProcess{}, nil return &mockPSUtilProcess{}, nil
} }

View File

@@ -36,6 +36,7 @@ type process struct {
cpuLimit uint64 cpuLimit uint64
ncpu float64 ncpu float64
proc *psprocess.Process proc *psprocess.Process
children bool
procfs Procfs procfs Procfs
stopTicker context.CancelFunc stopTicker context.CancelFunc
@@ -51,13 +52,14 @@ type process struct {
gpu gpu.GPU gpu gpu.GPU
} }
func (u *util) Process(pid int32) (Process, error) { func (u *util) Process(pid int32, children bool) (Process, error) {
p := &process{ p := &process{
pid: pid, pid: pid,
hasCgroup: u.hasCgroup, hasCgroup: u.hasCgroup,
cpuLimit: u.cpuLimit, cpuLimit: u.cpuLimit,
ncpu: u.ncpu, ncpu: u.ncpu,
gpu: u.gpu, gpu: u.gpu,
children: children,
procfs: u.procfs, procfs: u.procfs,
} }
@@ -105,6 +107,7 @@ func (p *process) collectCPU() cpuTimesStat {
} }
} }
if p.children {
cstat := p.collectCPUFromChildren(p.proc) cstat := p.collectCPUFromChildren(p.proc)
stat.total += cstat.total stat.total += cstat.total
@@ -112,6 +115,7 @@ func (p *process) collectCPU() cpuTimesStat {
stat.user += cstat.user stat.user += cstat.user
stat.idle += cstat.idle stat.idle += cstat.idle
stat.other += cstat.other stat.other += cstat.other
}
return *stat return *stat
} }
@@ -163,7 +167,9 @@ func (p *process) collectMemory() uint64 {
rss := info.RSS rss := info.RSS
if p.children {
rss += p.collectMemoryFromChildren(p.proc) rss += p.collectMemoryFromChildren(p.proc)
}
return rss return rss
} }

View File

@@ -113,7 +113,7 @@ type Util interface {
GPU() ([]GPUInfo, error) GPU() ([]GPUInfo, error)
// Process returns a process observer for a process with the given pid. // Process returns a process observer for a process with the given pid.
Process(pid int32) (Process, error) Process(pid int32, children bool) (Process, error)
} }
type util struct { type util struct {

View File

@@ -238,7 +238,7 @@ func New(config Config) (Resources, error) {
"max_gpu_memory": r.maxGPUMemory, "max_gpu_memory": r.maxGPUMemory,
}) })
r.self, err = r.psutil.Process(int32(os.Getpid())) r.self, err = r.psutil.Process(int32(os.Getpid()), false)
if err != nil { if err != nil {
return nil, fmt.Errorf("unable to create process observer for self: %w", err) return nil, fmt.Errorf("unable to create process observer for self: %w", err)
} }
@@ -639,7 +639,7 @@ func (r *resources) Network() ([]NetworkInfo, error) {
} }
func (r *resources) Process(pid int32) (Process, error) { func (r *resources) Process(pid int32) (Process, error) {
proc, err := r.psutil.Process(pid) proc, err := r.psutil.Process(pid, true)
if err != nil { if err != nil {
return nil, err return nil, err
} }