|
|
@ -3,14 +3,14 @@
|
|
|
|
package process
|
|
|
|
package process
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"context"
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"fmt"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"io/ioutil"
|
|
|
|
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/shirou/gopsutil/v3/internal/common"
|
|
|
|
"github.com/shirou/gopsutil/v3/internal/common"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
)
|
|
|
@ -66,11 +66,11 @@ func Test_Process_splitProcStat_fromFile(t *testing.T) {
|
|
|
|
defer f()
|
|
|
|
defer f()
|
|
|
|
for _, pid := range pids {
|
|
|
|
for _, pid := range pids {
|
|
|
|
pid, err := strconv.ParseInt(pid.Name(), 0, 32)
|
|
|
|
pid, err := strconv.ParseInt(pid.Name(), 0, 32)
|
|
|
|
if err != nil{
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
statFile := fmt.Sprintf("testdata/linux/%d/stat", pid)
|
|
|
|
statFile := fmt.Sprintf("testdata/linux/%d/stat", pid)
|
|
|
|
if _, err := os.Stat(statFile); err != nil{
|
|
|
|
if _, err := os.Stat(statFile); err != nil {
|
|
|
|
continue
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
contents, err := ioutil.ReadFile(statFile)
|
|
|
|
contents, err := ioutil.ReadFile(statFile)
|
|
|
@ -85,12 +85,34 @@ func Test_Process_splitProcStat_fromFile(t *testing.T) {
|
|
|
|
assert.Equal(t, fields[2], "test(cmd).sh")
|
|
|
|
assert.Equal(t, fields[2], "test(cmd).sh")
|
|
|
|
assert.Equal(t, fields[3], "S")
|
|
|
|
assert.Equal(t, fields[3], "S")
|
|
|
|
assert.Equal(t, fields[4], ppid)
|
|
|
|
assert.Equal(t, fields[4], ppid)
|
|
|
|
assert.Equal(t, fields[5], pidStr) // pgrp
|
|
|
|
assert.Equal(t, fields[5], pidStr) // pgrp
|
|
|
|
assert.Equal(t, fields[6], ppid) // session
|
|
|
|
assert.Equal(t, fields[6], ppid) // session
|
|
|
|
assert.Equal(t, fields[8], pidStr) // tpgrp
|
|
|
|
assert.Equal(t, fields[8], pidStr) // tpgrp
|
|
|
|
assert.Equal(t, fields[18], "20") // priority
|
|
|
|
assert.Equal(t, fields[18], "20") // priority
|
|
|
|
assert.Equal(t, fields[20], "1") // num threads
|
|
|
|
assert.Equal(t, fields[20], "1") // num threads
|
|
|
|
assert.Equal(t, fields[52], "0") // exit code
|
|
|
|
assert.Equal(t, fields[52], "0") // exit code
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -103,10 +125,10 @@ func Test_fillFromStatusWithContext(t *testing.T) {
|
|
|
|
defer f()
|
|
|
|
defer f()
|
|
|
|
for _, pid := range pids {
|
|
|
|
for _, pid := range pids {
|
|
|
|
pid, err := strconv.ParseInt(pid.Name(), 0, 32)
|
|
|
|
pid, err := strconv.ParseInt(pid.Name(), 0, 32)
|
|
|
|
if err != nil{
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if _, err := os.Stat(fmt.Sprintf("testdata/linux/%d/status", pid)); err != nil{
|
|
|
|
if _, err := os.Stat(fmt.Sprintf("testdata/linux/%d/status", pid)); err != nil {
|
|
|
|
continue
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p, _ := NewProcess(int32(pid))
|
|
|
|
p, _ := NewProcess(int32(pid))
|
|
|
@ -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 {
|
|
|
|