Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[process] Add support for cgroups #1552

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading