mirror of
https://github.com/shirou/gopsutil.git
synced 2025-05-08 19:29:25 +08:00
[v3 migration] change process.Status returns []string with no letter status string.
This commit is contained in:
parent
4b5a200e71
commit
b7e74218ca
@ -41,6 +41,17 @@ type Process struct {
|
||||
tgid int32
|
||||
}
|
||||
|
||||
// Process status
|
||||
const (
|
||||
Running = "running"
|
||||
Sleep = "sleep"
|
||||
Stop = "stop"
|
||||
Idle = "idle"
|
||||
Zombie = "zombie"
|
||||
Wait = "wait"
|
||||
Lock = "lock"
|
||||
)
|
||||
|
||||
type OpenFilesStat struct {
|
||||
Path string `json:"path"`
|
||||
Fd uint64 `json:"fd"`
|
||||
@ -66,8 +77,8 @@ type SignalInfoStat struct {
|
||||
|
||||
type RlimitStat struct {
|
||||
Resource int32 `json:"resource"`
|
||||
Soft uint64 `json:"soft"`
|
||||
Hard uint64 `json:"hard"`
|
||||
Soft uint64 `json:"soft"`
|
||||
Hard uint64 `json:"hard"`
|
||||
Used uint64 `json:"used"`
|
||||
}
|
||||
|
||||
@ -375,7 +386,7 @@ func (p *Process) Parent() (*Process, error) {
|
||||
// R: Running S: Sleep T: Stop I: Idle
|
||||
// Z: Zombie W: Wait L: Lock
|
||||
// The character is same within all supported platforms.
|
||||
func (p *Process) Status() (string, error) {
|
||||
func (p *Process) Status() ([]string, error) {
|
||||
return p.StatusWithContext(context.Background())
|
||||
}
|
||||
|
||||
@ -493,7 +504,6 @@ func (p *Process) ConnectionsMax(max int) ([]net.ConnectionStat, error) {
|
||||
return p.ConnectionsMaxWithContext(context.Background(), max)
|
||||
}
|
||||
|
||||
|
||||
// MemoryMaps get memory maps from /proc/(pid)/smaps
|
||||
func (p *Process) MemoryMaps(grouped bool) (*[]MemoryMapsStat, error) {
|
||||
return p.MemoryMapsWithContext(context.Background(), grouped)
|
||||
@ -534,3 +544,23 @@ func (p *Process) Username() (string, error) {
|
||||
return p.UsernameWithContext(context.Background())
|
||||
}
|
||||
|
||||
func convertStatusChar(letter string) string {
|
||||
switch letter {
|
||||
case "R":
|
||||
return Running
|
||||
case "S":
|
||||
return Sleep
|
||||
case "T":
|
||||
return Stop
|
||||
case "I":
|
||||
return Idle
|
||||
case "Z":
|
||||
return Zombie
|
||||
case "W":
|
||||
return Wait
|
||||
case "L":
|
||||
return Lock
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
@ -164,13 +164,13 @@ func (p *Process) ParentWithContext(ctx context.Context) (*Process, error) {
|
||||
return nil, fmt.Errorf("could not find parent line")
|
||||
}
|
||||
|
||||
func (p *Process) StatusWithContext(ctx context.Context) (string, error) {
|
||||
func (p *Process) StatusWithContext(ctx context.Context) ([]string, error) {
|
||||
r, err := callPsWithContext(ctx, "state", p.Pid, false)
|
||||
if err != nil {
|
||||
return "", err
|
||||
return []string{""}, err
|
||||
}
|
||||
|
||||
return r[0][0][0:1], err
|
||||
status := convertStatusChar(r[0][0][0:1])
|
||||
return []string{status}, err
|
||||
}
|
||||
|
||||
func (p *Process) ForegroundWithContext(ctx context.Context) (bool, error) {
|
||||
@ -458,4 +458,3 @@ func callPsWithContext(ctx context.Context, arg string, pid int32, threadOption
|
||||
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
|
@ -76,8 +76,8 @@ func (p *Process) ParentWithContext(ctx context.Context) (*Process, error) {
|
||||
return nil, common.ErrNotImplementedError
|
||||
}
|
||||
|
||||
func (p *Process) StatusWithContext(ctx context.Context) (string, error) {
|
||||
return "", common.ErrNotImplementedError
|
||||
func (p *Process) StatusWithContext(ctx context.Context) ([]string, error) {
|
||||
return []string{""}, common.ErrNotImplementedError
|
||||
}
|
||||
|
||||
func (p *Process) ForegroundWithContext(ctx context.Context) (bool, error) {
|
||||
@ -199,4 +199,3 @@ func (p *Process) KillWithContext(ctx context.Context) error {
|
||||
func (p *Process) UsernameWithContext(ctx context.Context) (string, error) {
|
||||
return "", common.ErrNotImplementedError
|
||||
}
|
||||
|
||||
|
@ -113,30 +113,30 @@ func (p *Process) ParentWithContext(ctx context.Context) (*Process, error) {
|
||||
return nil, common.ErrNotImplementedError
|
||||
}
|
||||
|
||||
func (p *Process) StatusWithContext(ctx context.Context) (string, error) {
|
||||
func (p *Process) StatusWithContext(ctx context.Context) ([]string, error) {
|
||||
k, err := p.getKProc()
|
||||
if err != nil {
|
||||
return "", err
|
||||
return []string{""}, err
|
||||
}
|
||||
var s string
|
||||
switch k.Stat {
|
||||
case SIDL:
|
||||
s = "I"
|
||||
s = Idle
|
||||
case SRUN:
|
||||
s = "R"
|
||||
s = Running
|
||||
case SSLEEP:
|
||||
s = "S"
|
||||
s = Sleep
|
||||
case SSTOP:
|
||||
s = "T"
|
||||
s = Stop
|
||||
case SZOMB:
|
||||
s = "Z"
|
||||
s = Zombie
|
||||
case SWAIT:
|
||||
s = "W"
|
||||
s = Wait
|
||||
case SLOCK:
|
||||
s = "L"
|
||||
s = Lock
|
||||
}
|
||||
|
||||
return s, nil
|
||||
return []string{s}, nil
|
||||
}
|
||||
|
||||
func (p *Process) ForegroundWithContext(ctx context.Context) (bool, error) {
|
||||
@ -336,4 +336,3 @@ func (p *Process) getKProc() (*KinfoProc, error) {
|
||||
}
|
||||
return &k, nil
|
||||
}
|
||||
|
||||
|
@ -125,12 +125,12 @@ func (p *Process) ParentWithContext(ctx context.Context) (*Process, error) {
|
||||
return NewProcessWithContext(ctx, p.parent)
|
||||
}
|
||||
|
||||
func (p *Process) StatusWithContext(ctx context.Context) (string, error) {
|
||||
func (p *Process) StatusWithContext(ctx context.Context) ([]string, error) {
|
||||
err := p.fillFromStatusWithContext(ctx)
|
||||
if err != nil {
|
||||
return "", err
|
||||
return []string{""}, err
|
||||
}
|
||||
return p.status, nil
|
||||
return []string{p.status}, nil
|
||||
}
|
||||
|
||||
func (p *Process) ForegroundWithContext(ctx context.Context) (bool, error) {
|
||||
@ -814,7 +814,7 @@ func (p *Process) fillFromStatusWithContext(ctx context.Context) error {
|
||||
}
|
||||
}
|
||||
case "State":
|
||||
p.status = value[0:1]
|
||||
p.status = convertStatusChar(value[0:1])
|
||||
case "PPid", "Ppid":
|
||||
pval, err := strconv.ParseInt(value, 10, 32)
|
||||
if err != nil {
|
||||
@ -1114,4 +1114,3 @@ func readPidsFromDir(path string) ([]int32, error) {
|
||||
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
|
@ -109,26 +109,26 @@ func (p *Process) ParentWithContext(ctx context.Context) (*Process, error) {
|
||||
return nil, common.ErrNotImplementedError
|
||||
}
|
||||
|
||||
func (p *Process) StatusWithContext(ctx context.Context) (string, error) {
|
||||
func (p *Process) StatusWithContext(ctx context.Context) ([]string, error) {
|
||||
k, err := p.getKProc()
|
||||
if err != nil {
|
||||
return "", err
|
||||
return []string{""}, err
|
||||
}
|
||||
var s string
|
||||
switch k.Stat {
|
||||
case SIDL:
|
||||
case SRUN:
|
||||
case SONPROC:
|
||||
s = "R"
|
||||
s = Running
|
||||
case SSLEEP:
|
||||
s = "S"
|
||||
s = Sleep
|
||||
case SSTOP:
|
||||
s = "T"
|
||||
s = Stop
|
||||
case SDEAD:
|
||||
s = "Z"
|
||||
s = Zombie
|
||||
}
|
||||
|
||||
return s, nil
|
||||
return []string{s}, nil
|
||||
}
|
||||
|
||||
func (p *Process) ForegroundWithContext(ctx context.Context) (bool, error) {
|
||||
@ -360,4 +360,3 @@ func callKernProcSyscall(op int32, arg int32) ([]byte, uint64, error) {
|
||||
|
||||
return buf, length, nil
|
||||
}
|
||||
|
||||
|
@ -195,8 +195,11 @@ func Test_Process_Status(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Errorf("getting status error %v", err)
|
||||
}
|
||||
if v != "R" && v != "S" {
|
||||
t.Errorf("could not get state %v", v)
|
||||
if len(v) == 0 {
|
||||
t.Errorf("could not get state")
|
||||
}
|
||||
if v[0] != "R" && v[0] != "S" {
|
||||
t.Errorf("get wrong state, %v", v)
|
||||
}
|
||||
}
|
||||
|
||||
@ -683,4 +686,3 @@ func BenchmarkProcessPpid(b *testing.B) {
|
||||
p.Ppid()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -318,8 +318,8 @@ func (p *Process) ParentWithContext(ctx context.Context) (*Process, error) {
|
||||
return NewProcessWithContext(ctx, ppid)
|
||||
}
|
||||
|
||||
func (p *Process) StatusWithContext(ctx context.Context) (string, error) {
|
||||
return "", common.ErrNotImplementedError
|
||||
func (p *Process) StatusWithContext(ctx context.Context) ([]string, error) {
|
||||
return []string{""}, common.ErrNotImplementedError
|
||||
}
|
||||
|
||||
func (p *Process) ForegroundWithContext(ctx context.Context) (bool, error) {
|
||||
@ -849,4 +849,3 @@ func convertUTF16ToString(src []byte) string {
|
||||
}
|
||||
return syscall.UTF16ToString(codePoints)
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user