Skip to content

Commit

Permalink
Add additional process states.
Browse files Browse the repository at this point in the history
  • Loading branch information
tbarker25 committed Nov 22, 2021
1 parent 78065a7 commit c6c910c
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 15 deletions.
63 changes: 49 additions & 14 deletions v3/process/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,30 @@ type Process struct {

// Process status
const (
// Running marks a task a running or runnable (on the run queue)
Running = "running"
Sleep = "sleep"
Stop = "stop"
Idle = "idle"
Zombie = "zombie"
Wait = "wait"
Lock = "lock"
// Blocked marks a task waiting on a short, uninterruptable operation (usually IO)
Blocked = "blocked"
// Idle marks a task sleeping for more than about 20 seconds
Idle = "idle"
// Lock marks a task waiting to acquire a lock
Lock = "lock"
// Sleep marks task waiting for short, interruptable operation
Sleep = "sleep"
// Stop marks a stopped process
Stop = "stop"
// Wait marks an idle interrupt thread (or paging in pre 2.6.xx Linux)
Wait = "wait"
// Zombie marks a defunct process, terminated but not reaped by its parent
Zombie = "zombie"

// Solaris states. See https://github.com/collectd/collectd/blob/1da3305c10c8ff9a63081284cf3d4bb0f6daffd8/src/processes.c#L2115
Daemon = "daemon"
Detached = "detached"
System = "system"
Orphan = "orphan"

UnknownState = ""
)

type OpenFilesStat struct {
Expand Down Expand Up @@ -554,23 +571,41 @@ func (p *Process) Environ() ([]string, error) {
return p.EnvironWithContext(context.Background())
}

// convertStatusChar as reported by the ps command across different platforms.
func convertStatusChar(letter string) string {
// Sources
// Darwin: http://www.mywebuniversity.com/Man_Pages/Darwin/man_ps.html
// FreeBSD: https://www.freebsd.org/cgi/man.cgi?ps
// Linux https://man7.org/linux/man-pages/man1/ps.1.html
// OpenBSD: https://man.openbsd.org/ps.1#state
// Solaris: https://github.com/collectd/collectd/blob/1da3305c10c8ff9a63081284cf3d4bb0f6daffd8/src/processes.c#L2115
switch letter {
case "A":
return Daemon
case "D", "U":
return Blocked
case "E":
return Detached
case "I":
return Idle
case "L":
return Lock
case "O":
return Orphan
case "R":
return Running
case "S":
return Sleep
case "T":
case "T", "t":
// "t" is used by Linux to signal stopped by the debugger during tracing
return Stop
case "I":
return Idle
case "Z":
return Zombie
case "W":
return Wait
case "L":
return Lock
case "Y":
return System
case "Z":
return Zombie
default:
return ""
return UnknownState
}
}
6 changes: 5 additions & 1 deletion v3/process/process_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,11 @@ func Test_CPUTimes(t *testing.T) {

func Test_OpenFiles(t *testing.T) {
fp, err := os.Open("process_test.go")
defer fp.Close()
assert.Nil(t, err)
defer func() {
err := fp.Close()
assert.Nil(t, err)
}()

pid := os.Getpid()
p, err := NewProcess(int32(pid))
Expand Down

0 comments on commit c6c910c

Please sign in to comment.