From c6c910cce14b65d3fcecae45345b5556fa2bf815 Mon Sep 17 00:00:00 2001 From: Tom Barker Date: Mon, 22 Nov 2021 13:24:26 -0500 Subject: [PATCH] Add additional process states. --- v3/process/process.go | 63 +++++++++++++++++++++++++++++--------- v3/process/process_test.go | 6 +++- 2 files changed, 54 insertions(+), 15 deletions(-) diff --git a/v3/process/process.go b/v3/process/process.go index 81be652a0..e766a8071 100644 --- a/v3/process/process.go +++ b/v3/process/process.go @@ -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 { @@ -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 } } diff --git a/v3/process/process_test.go b/v3/process/process_test.go index 9f383fd80..78827d8c4 100644 --- a/v3/process/process_test.go +++ b/v3/process/process_test.go @@ -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))