Merge pull request #1112 from xuyang2/process-name-linux

[process][linux] get name from /proc/(pid)/comm
pull/1116/head
shirou 4 years ago committed by GitHub
commit efc3e519be
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -82,7 +82,7 @@ func (p *Process) PpidWithContext(ctx context.Context) (int32, error) {
func (p *Process) NameWithContext(ctx context.Context) (string, error) { func (p *Process) NameWithContext(ctx context.Context) (string, error) {
if p.name == "" { if p.name == "" {
if err := p.fillFromStatusWithContext(ctx); err != nil { if err := p.fillNameWithContext(ctx); err != nil {
return "", err return "", err
} }
} }
@ -800,6 +800,28 @@ func (p *Process) fillFromStatmWithContext(ctx context.Context) (*MemoryInfoStat
return memInfo, memInfoEx, nil return memInfo, memInfoEx, nil
} }
// Get name from /proc/(pid)/comm or /proc/(pid)/status
func (p *Process) fillNameWithContext(ctx context.Context) error {
err := p.fillFromCommWithContext(ctx)
if err == nil && p.name != "" && len(p.name) < 15 {
return nil
}
return p.fillFromStatusWithContext(ctx)
}
// Get name from /proc/(pid)/comm
func (p *Process) fillFromCommWithContext(ctx context.Context) error {
pid := p.Pid
statPath := common.HostProc(strconv.Itoa(int(pid)), "comm")
contents, err := ioutil.ReadFile(statPath)
if err != nil {
return err
}
p.name = strings.TrimSuffix(string(contents), "\n")
return nil
}
// Get various status from /proc/(pid)/status // Get various status from /proc/(pid)/status
func (p *Process) fillFromStatusWithContext(ctx context.Context) error { func (p *Process) fillFromStatusWithContext(ctx context.Context) error {
pid := p.Pid pid := p.Pid

@ -3,10 +3,10 @@
package process package process
import ( import (
"os"
"context" "context"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os"
"strconv" "strconv"
"strings" "strings"
"testing" "testing"
@ -94,6 +94,28 @@ func Test_Process_splitProcStat_fromFile(t *testing.T) {
} }
} }
func Test_fillFromCommWithContext(t *testing.T) {
pids, err := ioutil.ReadDir("testdata/linux/")
if err != nil {
t.Error(err)
}
f := common.MockEnv("HOST_PROC", "testdata/linux")
defer f()
for _, pid := range pids {
pid, err := strconv.ParseInt(pid.Name(), 0, 32)
if err != nil {
continue
}
if _, err := os.Stat(fmt.Sprintf("testdata/linux/%d/status", pid)); err != nil {
continue
}
p, _ := NewProcess(int32(pid))
if err := p.fillFromCommWithContext(context.Background()); err != nil {
t.Error(err)
}
}
}
func Test_fillFromStatusWithContext(t *testing.T) { func Test_fillFromStatusWithContext(t *testing.T) {
pids, err := ioutil.ReadDir("testdata/linux/") pids, err := ioutil.ReadDir("testdata/linux/")
if err != nil { if err != nil {
@ -116,6 +138,26 @@ func Test_fillFromStatusWithContext(t *testing.T) {
} }
} }
func Benchmark_fillFromCommWithContext(b *testing.B) {
f := common.MockEnv("HOST_PROC", "testdata/linux")
defer f()
pid := 1060
p, _ := NewProcess(int32(pid))
for i := 0; i < b.N; i++ {
p.fillFromCommWithContext(context.Background())
}
}
func Benchmark_fillFromStatusWithContext(b *testing.B) {
f := common.MockEnv("HOST_PROC", "testdata/linux")
defer f()
pid := 1060
p, _ := NewProcess(int32(pid))
for i := 0; i < b.N; i++ {
p.fillFromStatusWithContext(context.Background())
}
}
func Test_fillFromTIDStatWithContext_lx_brandz(t *testing.T) { func Test_fillFromTIDStatWithContext_lx_brandz(t *testing.T) {
pids, err := ioutil.ReadDir("testdata/lx_brandz/") pids, err := ioutil.ReadDir("testdata/lx_brandz/")
if err != nil { if err != nil {

@ -0,0 +1 @@
ksoftirqd/0

@ -0,0 +1 @@
test(cmd).sh
Loading…
Cancel
Save