Add additional process states.

pull/1185/head
Tom Barker 4 years ago
parent 78065a7ce2
commit c6c910cce1

@ -45,13 +45,30 @@ type Process struct {
// Process status // Process status
const ( const (
// Running marks a task a running or runnable (on the run queue)
Running = "running" Running = "running"
Sleep = "sleep" // Blocked marks a task waiting on a short, uninterruptable operation (usually IO)
Stop = "stop" Blocked = "blocked"
Idle = "idle" // Idle marks a task sleeping for more than about 20 seconds
Zombie = "zombie" Idle = "idle"
Wait = "wait" // Lock marks a task waiting to acquire a lock
Lock = "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 { type OpenFilesStat struct {
@ -554,23 +571,41 @@ func (p *Process) Environ() ([]string, error) {
return p.EnvironWithContext(context.Background()) return p.EnvironWithContext(context.Background())
} }
// convertStatusChar as reported by the ps command across different platforms.
func convertStatusChar(letter string) string { 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 { 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": case "R":
return Running return Running
case "S": case "S":
return Sleep return Sleep
case "T": case "T", "t":
// "t" is used by Linux to signal stopped by the debugger during tracing
return Stop return Stop
case "I":
return Idle
case "Z":
return Zombie
case "W": case "W":
return Wait return Wait
case "L": case "Y":
return Lock return System
case "Z":
return Zombie
default: default:
return "" return UnknownState
} }
} }

@ -660,7 +660,11 @@ func Test_CPUTimes(t *testing.T) {
func Test_OpenFiles(t *testing.T) { func Test_OpenFiles(t *testing.T) {
fp, err := os.Open("process_test.go") 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() pid := os.Getpid()
p, err := NewProcess(int32(pid)) p, err := NewProcess(int32(pid))

Loading…
Cancel
Save