Skip to content

Commit

Permalink
Don't report dead processes
Browse files Browse the repository at this point in the history
If the state is reported as dead or zombie, return an error so the
process will be skipped.
  • Loading branch information
bboreham committed Oct 9, 2018
1 parent d71a732 commit 51cc585
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion probe/process/walker_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package process
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"os"
"path"
Expand Down Expand Up @@ -31,6 +32,8 @@ var (
// key: filename in /proc. Example: "42"
// value: two strings separated by a '\0'
cmdlineCache = freecache.NewCache(1024 * 16)

deadProcessError = errors.New("The process is dead")
)

const (
Expand Down Expand Up @@ -75,6 +78,7 @@ func readStats(path string) (ppid, threads int, jiffies, rss, rssLimit uint64, e
const (
// /proc/<pid>/stat field positions, counting from zero
// see "man 5 proc"
procStatFieldState int = 2
procStatFieldPpid int = 3
procStatFieldUserJiffies int = 13
procStatFieldSysJiffies int = 14
Expand All @@ -94,7 +98,16 @@ func readStats(path string) (ppid, threads int, jiffies, rss, rssLimit uint64, e
// Parse the file without using expensive extra string allocations

pos := 0
skipNSpaces(&buf, &pos, procStatFieldPpid)
skipNSpaces(&buf, &pos, procStatFieldState)

// Error on processes which are in zombie (defunct) or dead state, so they will be skipped
switch buf[pos] {
case 'Z', 'X', 'x':
err = deadProcessError
return
}

pos += 2 // Move past state and space after state
ppid = parseIntWithSpaces(&buf, &pos)

skipNSpaces(&buf, &pos, procStatFieldUserJiffies-procStatFieldPpid)
Expand Down

0 comments on commit 51cc585

Please sign in to comment.