-
Notifications
You must be signed in to change notification settings - Fork 712
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve process code in probe #272
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package process | ||
|
||
import ( | ||
"strconv" | ||
|
||
"github.com/weaveworks/scope/probe/tag" | ||
"github.com/weaveworks/scope/report" | ||
) | ||
|
||
// We use these keys in node metadata | ||
const ( | ||
PID = "pid" | ||
Comm = "comm" | ||
PPID = "ppid" | ||
Cmdline = "cmdline" | ||
Threads = "threads" | ||
) | ||
|
||
// Reporter generate Reports containing the Process topology | ||
type reporter struct { | ||
procRoot string | ||
scope string | ||
} | ||
|
||
// NewReporter makes a new Reporter | ||
func NewReporter(procRoot, scope string) tag.Reporter { | ||
return &reporter{ | ||
procRoot: procRoot, | ||
scope: scope, | ||
} | ||
} | ||
|
||
// Report generates a Report containing the Process topology | ||
func (r *reporter) Report() (report.Report, error) { | ||
result := report.MakeReport() | ||
processes, err := r.processTopology() | ||
if err != nil { | ||
return result, err | ||
} | ||
result.Process.Merge(processes) | ||
return result, nil | ||
} | ||
|
||
func (r *reporter) processTopology() (report.Topology, error) { | ||
t := report.NewTopology() | ||
err := Walk(r.procRoot, func(p *Process) { | ||
pidstr := strconv.Itoa(p.PID) | ||
nodeID := report.MakeProcessNodeID(r.scope, pidstr) | ||
t.NodeMetadatas[nodeID] = report.NodeMetadata{ | ||
PID: pidstr, | ||
Comm: p.Comm, | ||
Cmdline: p.Cmdline, | ||
Threads: strconv.Itoa(p.Threads), | ||
} | ||
if p.PPID > 0 { | ||
t.NodeMetadatas[nodeID][PPID] = strconv.Itoa(p.PPID) | ||
} | ||
}) | ||
|
||
return t, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package process_test | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/weaveworks/scope/probe/process" | ||
"github.com/weaveworks/scope/report" | ||
"github.com/weaveworks/scope/test" | ||
) | ||
|
||
func TestReporter(t *testing.T) { | ||
oldWalk := process.Walk | ||
defer func() { process.Walk = oldWalk }() | ||
|
||
process.Walk = func(_ string, f func(*process.Process)) error { | ||
for _, p := range []*process.Process{ | ||
{PID: 1, PPID: 0, Comm: "init"}, | ||
{PID: 2, PPID: 1, Comm: "bash"}, | ||
{PID: 3, PPID: 1, Comm: "apache", Threads: 2}, | ||
{PID: 4, PPID: 2, Comm: "ping", Cmdline: "ping foo.bar.local"}, | ||
} { | ||
f(p) | ||
} | ||
return nil | ||
} | ||
|
||
reporter := process.NewReporter("", "") | ||
want := report.MakeReport() | ||
want.Process = report.Topology{ | ||
Adjacency: report.Adjacency{}, | ||
EdgeMetadatas: report.EdgeMetadatas{}, | ||
NodeMetadatas: report.NodeMetadatas{ | ||
report.MakeProcessNodeID("", "1"): report.NodeMetadata{ | ||
process.PID: "1", | ||
process.Comm: "init", | ||
process.Cmdline: "", | ||
process.Threads: "0", | ||
}, | ||
report.MakeProcessNodeID("", "2"): report.NodeMetadata{ | ||
process.PID: "2", | ||
process.Comm: "bash", | ||
process.PPID: "1", | ||
process.Cmdline: "", | ||
process.Threads: "0", | ||
}, | ||
report.MakeProcessNodeID("", "3"): report.NodeMetadata{ | ||
process.PID: "3", | ||
process.Comm: "apache", | ||
process.PPID: "1", | ||
process.Cmdline: "", | ||
process.Threads: "2", | ||
}, | ||
report.MakeProcessNodeID("", "4"): report.NodeMetadata{ | ||
process.PID: "4", | ||
process.Comm: "ping", | ||
process.PPID: "2", | ||
process.Cmdline: "ping foo.bar.local", | ||
process.Threads: "0", | ||
}, | ||
}, | ||
} | ||
|
||
have, err := reporter.Report() | ||
if err != nil || !reflect.DeepEqual(want, have) { | ||
t.Errorf("%s (%v)", test.Diff(want, have), err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
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(procRoot string) (Tree, error) { | ||
pt := tree{processes: map[int]*Process{}} | ||
err := Walk(procRoot, 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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package process_test | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/weaveworks/scope/probe/process" | ||
) | ||
|
||
func TestTree(t *testing.T) { | ||
oldWalk := process.Walk | ||
defer func() { process.Walk = oldWalk }() | ||
|
||
process.Walk = func(_ string, f func(*process.Process)) error { | ||
for _, p := range []*process.Process{ | ||
{PID: 1, PPID: 0}, | ||
{PID: 2, PPID: 1}, | ||
{PID: 3, PPID: 1}, | ||
{PID: 4, PPID: 2}, | ||
} { | ||
f(p) | ||
} | ||
return nil | ||
} | ||
|
||
tree, err := process.NewTree("foo") | ||
if err != nil { | ||
t.Fatalf("newProcessTree error: %v", err) | ||
} | ||
|
||
for pid, want := range map[int]int{2: 1, 3: 1, 4: 2} { | ||
have, err := tree.GetParent(pid) | ||
if err != nil || !reflect.DeepEqual(want, have) { | ||
t.Errorf("%d: want %#v, have %#v (%v)", pid, want, have, err) | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This comment was marked as abuse.
Sorry, something went wrong.
This comment was marked as abuse.
Sorry, something went wrong.