Skip to content

Commit aa3378b

Browse files
authored
Merge pull request #1185 from tbarker25/process-states
Add additional process states
2 parents 5707c55 + c6c910c commit aa3378b

File tree

2 files changed

+54
-15
lines changed

2 files changed

+54
-15
lines changed

v3/process/process.go

+49-14
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,30 @@ type Process struct {
4545

4646
// Process status
4747
const (
48+
// Running marks a task a running or runnable (on the run queue)
4849
Running = "running"
49-
Sleep = "sleep"
50-
Stop = "stop"
51-
Idle = "idle"
52-
Zombie = "zombie"
53-
Wait = "wait"
54-
Lock = "lock"
50+
// Blocked marks a task waiting on a short, uninterruptable operation (usually IO)
51+
Blocked = "blocked"
52+
// Idle marks a task sleeping for more than about 20 seconds
53+
Idle = "idle"
54+
// Lock marks a task waiting to acquire a lock
55+
Lock = "lock"
56+
// Sleep marks task waiting for short, interruptable operation
57+
Sleep = "sleep"
58+
// Stop marks a stopped process
59+
Stop = "stop"
60+
// Wait marks an idle interrupt thread (or paging in pre 2.6.xx Linux)
61+
Wait = "wait"
62+
// Zombie marks a defunct process, terminated but not reaped by its parent
63+
Zombie = "zombie"
64+
65+
// Solaris states. See https://github.com/collectd/collectd/blob/1da3305c10c8ff9a63081284cf3d4bb0f6daffd8/src/processes.c#L2115
66+
Daemon = "daemon"
67+
Detached = "detached"
68+
System = "system"
69+
Orphan = "orphan"
70+
71+
UnknownState = ""
5572
)
5673

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

574+
// convertStatusChar as reported by the ps command across different platforms.
557575
func convertStatusChar(letter string) string {
576+
// Sources
577+
// Darwin: http://www.mywebuniversity.com/Man_Pages/Darwin/man_ps.html
578+
// FreeBSD: https://www.freebsd.org/cgi/man.cgi?ps
579+
// Linux https://man7.org/linux/man-pages/man1/ps.1.html
580+
// OpenBSD: https://man.openbsd.org/ps.1#state
581+
// Solaris: https://github.com/collectd/collectd/blob/1da3305c10c8ff9a63081284cf3d4bb0f6daffd8/src/processes.c#L2115
558582
switch letter {
583+
case "A":
584+
return Daemon
585+
case "D", "U":
586+
return Blocked
587+
case "E":
588+
return Detached
589+
case "I":
590+
return Idle
591+
case "L":
592+
return Lock
593+
case "O":
594+
return Orphan
559595
case "R":
560596
return Running
561597
case "S":
562598
return Sleep
563-
case "T":
599+
case "T", "t":
600+
// "t" is used by Linux to signal stopped by the debugger during tracing
564601
return Stop
565-
case "I":
566-
return Idle
567-
case "Z":
568-
return Zombie
569602
case "W":
570603
return Wait
571-
case "L":
572-
return Lock
604+
case "Y":
605+
return System
606+
case "Z":
607+
return Zombie
573608
default:
574-
return ""
609+
return UnknownState
575610
}
576611
}

v3/process/process_test.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,11 @@ func Test_CPUTimes(t *testing.T) {
660660

661661
func Test_OpenFiles(t *testing.T) {
662662
fp, err := os.Open("process_test.go")
663-
defer fp.Close()
663+
assert.Nil(t, err)
664+
defer func() {
665+
err := fp.Close()
666+
assert.Nil(t, err)
667+
}()
664668

665669
pid := os.Getpid()
666670
p, err := NewProcess(int32(pid))

0 commit comments

Comments
 (0)