Skip to content

Commit

Permalink
Add support for cgroups
Browse files Browse the repository at this point in the history
  • Loading branch information
atoulme committed Nov 16, 2023
1 parent fad5e68 commit 6af6091
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 0 deletions.
6 changes: 6 additions & 0 deletions process/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type Process struct {
Pid int32 `json:"pid"`
name string
status string
cgroup string
parent int32
parentMutex sync.RWMutex // for windows ppid cache
numCtxSwitches *NumCtxSwitchesStat
Expand Down Expand Up @@ -375,6 +376,11 @@ func (p *Process) Name() (string, error) {
return p.NameWithContext(context.Background())
}

// Cgroup returns cgroup of the process.
func (p *Process) Cgroup() (string, error) {
return p.CgroupWithContext(context.Background())
}

// Exe returns executable path of the process.
func (p *Process) Exe() (string, error) {
return p.ExeWithContext(context.Background())
Expand Down
4 changes: 4 additions & 0 deletions process/process_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ func (p *Process) NameWithContext(ctx context.Context) (string, error) {
return name, nil
}

func (p *Process) CgroupWithContext(ctx context.Context) (string, error) {
return "", common.ErrNotImplementedError
}

func (p *Process) createTimeWithContext(ctx context.Context) (int64, error) {
k, err := p.getKProc()
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions process/process_fallback.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ func (p *Process) NameWithContext(ctx context.Context) (string, error) {
return "", common.ErrNotImplementedError
}

func (p *Process) CgroupWithContext(ctx context.Context) (string, error) {
return "", common.ErrNotImplementedError
}

func (p *Process) TgidWithContext(ctx context.Context) (int32, error) {
return 0, common.ErrNotImplementedError
}
Expand Down
4 changes: 4 additions & 0 deletions process/process_freebsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ func (p *Process) NameWithContext(ctx context.Context) (string, error) {
return name, nil
}

func (p *Process) CgroupWithContext(ctx context.Context) (string, error) {
return "", common.ErrNotImplementedError
}

func (p *Process) CwdWithContext(ctx context.Context) (string, error) {
return "", common.ErrNotImplementedError
}
Expand Down
22 changes: 22 additions & 0 deletions process/process_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,15 @@ func (p *Process) NameWithContext(ctx context.Context) (string, error) {
return p.name, nil
}

func (p *Process) CgroupWithContext(ctx context.Context) (string, error) {
if p.cgroup == "" {
if err := p.fillCgroupWithContext(ctx); err != nil {
return "", err
}
}
return p.cgroup, nil
}

func (p *Process) TgidWithContext(ctx context.Context) (int32, error) {
if p.tgid == 0 {
if err := p.fillFromStatusWithContext(ctx); err != nil {
Expand Down Expand Up @@ -810,6 +819,19 @@ func (p *Process) fillFromCommWithContext(ctx context.Context) error {
return nil
}

// Get cgroup from /proc/(pid)/cgroup
func (p *Process) fillCgroupWithContext(ctx context.Context) error {
pid := p.Pid
statPath := common.HostProcWithContext(ctx, strconv.Itoa(int(pid)), "cgroup")
contents, err := os.ReadFile(statPath)
if err != nil {
return err
}

p.cgroup = strings.TrimSuffix(string(contents), "\n")
return nil
}

// Get various status from /proc/(pid)/status
func (p *Process) fillFromStatus() error {
return p.fillFromStatusWithContext(context.Background())
Expand Down
4 changes: 4 additions & 0 deletions process/process_openbsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ func (p *Process) NameWithContext(ctx context.Context) (string, error) {
return name, nil
}

func (p *Process) CgroupWithContext(ctx context.Context) (string, error) {
return "", nil
}

func (p *Process) CwdWithContext(ctx context.Context) (string, error) {
return "", common.ErrNotImplementedError
}
Expand Down
4 changes: 4 additions & 0 deletions process/process_plan9.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ func (p *Process) NameWithContext(ctx context.Context) (string, error) {
return "", common.ErrNotImplementedError
}

func (p *Process) CgroupWithContext(ctx context.Context) (string, error) {
return "", common.ErrNotImplementedError
}

func (p *Process) TgidWithContext(ctx context.Context) (int32, error) {
return 0, common.ErrNotImplementedError
}
Expand Down
4 changes: 4 additions & 0 deletions process/process_solaris.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ func (p *Process) NameWithContext(ctx context.Context) (string, error) {
return "", common.ErrNotImplementedError
}

func (p *Process) CgroupWithContext(ctx context.Context) (string, error) {
return "", common.ErrNotImplementedError
}

func (p *Process) TgidWithContext(ctx context.Context) (int32, error) {
return 0, common.ErrNotImplementedError
}
Expand Down
17 changes: 17 additions & 0 deletions process/process_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,16 @@ func Test_Process_Exe(t *testing.T) {
}
}

func Test_Process_Cgroup(t *testing.T) {
p := testGetProcess()

_, err := p.Cgroup()
skipIfNotImplementedErr(t, err)
if err != nil {
t.Errorf("getting cgroup error %v", err)
}
}

func Test_Process_CpuPercent(t *testing.T) {
p := testGetProcess()
_, err := p.Percent(0)
Expand Down Expand Up @@ -862,6 +872,13 @@ func BenchmarkProcessName(b *testing.B) {
}
}

func BenchmarkProcessCgroup(b *testing.B) {
p := testGetProcess()
for i := 0; i < b.N; i++ {
p.Cgroup()
}
}

func BenchmarkProcessPpid(b *testing.B) {
p := testGetProcess()
for i := 0; i < b.N; i++ {
Expand Down
4 changes: 4 additions & 0 deletions process/process_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,10 @@ func (p *Process) NameWithContext(ctx context.Context) (string, error) {
return filepath.Base(exe), nil
}

func (p *Process) CgroupWithContext(ctx context.Context) (string, error) {
return "", common.ErrNotImplementedError
}

func (p *Process) TgidWithContext(ctx context.Context) (int32, error) {
return 0, common.ErrNotImplementedError
}
Expand Down

0 comments on commit 6af6091

Please sign in to comment.