Skip to content

Commit

Permalink
goprocess: report info about a single process
Browse files Browse the repository at this point in the history
  • Loading branch information
rakyll committed Sep 4, 2017
1 parent a5b3172 commit bfd379c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
33 changes: 26 additions & 7 deletions goprocess/gp.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (
ps "github.com/keybase/go-ps"
)

// GoProcess represents a Go process.
type GoProcess struct {
// P represents a Go process.
type P struct {
PID int
PPID int
Exec string
Expand All @@ -24,10 +24,9 @@ type GoProcess struct {
Agent bool
}

// Find returns all the Go processes currently
// running on this host.
func Find() []GoProcess {
var results []GoProcess
// FindAll returns all the Go processes currently running on this host.
func FindAll() []P {
var results []P

pss, err := ps.Processes()
if err != nil {
Expand All @@ -49,7 +48,7 @@ func Find() []GoProcess {
if !ok {
return
}
results = append(results, GoProcess{
results = append(results, P{
PID: pr.Pid(),
PPID: pr.PPid(),
Exec: pr.Executable(),
Expand All @@ -63,6 +62,26 @@ func Find() []GoProcess {
return results
}

// Find finds info about the process identified with the given PID.
func Find(pid int) (p P, ok bool, err error) {
pr, err := ps.FindProcess(pid)
if err != nil {
return P{}, false, err
}
path, version, agent, ok, err := isGo(pr)
if !ok {
return P{}, false, nil
}
return P{
PID: pr.Pid(),
PPID: pr.PPid(),
Exec: pr.Executable(),
Path: path,
BuildVersion: version,
Agent: agent,
}, true, nil
}

// isGo looks up the runtime.buildVersion symbol
// in the process' binary and determines if the process
// if a Go process or not. If the process is a Go process,
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func main() {
}

func processes() {
ps := goprocess.Find()
ps := goprocess.FindAll()

var maxPID, maxExec, maxVersion int
for _, p := range ps {
Expand Down

0 comments on commit bfd379c

Please sign in to comment.