Files
weave-scope/probe/process/tree.go
Tom Wilkie b9e968ff43 Cache the walk of the process tree, reusing it in docker tagger and process reporter.
Update the cache every spy tick.

This change make CPU usage of scope on my box go from ~40% to ~17%.
2015-06-29 11:16:38 +00:00

35 lines
656 B
Go

package process
import (
"fmt"
)
// Tree represents all processes on the machine.
type Tree interface {
GetParent(pid int) (int, error)
}
type tree struct {
processes map[int]*Process
}
// NewTree returns a new Tree that can be polled.
func NewTree(walker Walker) (Tree, error) {
pt := tree{processes: map[int]*Process{}}
err := walker.Walk(func(p *Process) {
pt.processes[p.PID] = p
})
return &pt, err
}
// GetParent returns the pid of the parent process for a given pid
func (pt *tree) GetParent(pid int) (int, error) {
proc, ok := pt.processes[pid]
if !ok {
return -1, fmt.Errorf("PID %d not found", pid)
}
return proc.PPID, nil
}