Skip to content
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

Gather per-process CPU and memory metrics. #767

Merged
merged 3 commits into from
Dec 16, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Review feedback
  • Loading branch information
tomwilkie committed Dec 16, 2015
commit 8efa0d147bca1d08cc5a4752895ecce2c53186ba
11 changes: 4 additions & 7 deletions probe/process/walker.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type Walker interface {
// CachingWalker is a walker than caches a copy of the output from another
// Walker, and then allows other concurrent readers to Walk that copy.
type CachingWalker struct {
cache []Process
cache map[int]Process
previousByPID map[int]Process
cacheLock sync.RWMutex
source Walker

This comment was marked as abuse.

This comment was marked as abuse.

Expand All @@ -47,20 +47,17 @@ func (c *CachingWalker) Walk(f func(Process, Process)) error {

// Tick updates cached copy of process list
func (c *CachingWalker) Tick() error {
newCache := []Process{}
newCache := map[int]Process{}
err := c.source.Walk(func(p, _ Process) {
newCache = append(newCache, p)
newCache[p.PID] = p
})
if err != nil {
return err
}

c.cacheLock.Lock()
defer c.cacheLock.Unlock()
c.previousByPID = map[int]Process{}
for _, p := range c.cache {
c.previousByPID[p.PID] = p
}
c.previousByPID = c.cache
c.cache = newCache
return nil
}
17 changes: 9 additions & 8 deletions probe/process/walker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,16 @@ func TestCache(t *testing.T) {
t.Fatal(err)
}

want, err := all(walker)
have, err := all(cachingWalker)
if err != nil || !reflect.DeepEqual(processes, have) {
t.Errorf("%v (%v)", test.Diff(processes, have), err)
if err != nil || !reflect.DeepEqual(want, have) {
t.Errorf("%v (%v)", test.Diff(want, have), err)
}

walker.processes = []process.Process{}
have, err = all(cachingWalker)
if err != nil || !reflect.DeepEqual(processes, have) {
t.Errorf("%v (%v)", test.Diff(processes, have), err)
if err != nil || !reflect.DeepEqual(want, have) {
t.Errorf("%v (%v)", test.Diff(want, have), err)
}

err = cachingWalker.Tick()
Expand All @@ -51,16 +52,16 @@ func TestCache(t *testing.T) {
}

have, err = all(cachingWalker)
want := []process.Process{}
want = map[process.Process]struct{}{}
if err != nil || !reflect.DeepEqual(want, have) {
t.Errorf("%v (%v)", test.Diff(want, have), err)
}
}

func all(w process.Walker) ([]process.Process, error) {
all := []process.Process{}
func all(w process.Walker) (map[process.Process]struct{}, error) {
all := map[process.Process]struct{}{}
err := w.Walk(func(p, _ process.Process) {
all = append(all, p)
all[p] = struct{}{}
})
return all, err
}
2 changes: 1 addition & 1 deletion render/detailed_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ func hostOriginTable(nmd report.Node) (Table, bool) {
fmt formatter
}{
{host.CPUUsage, "CPU Usage", formatPercent},
{host.MemUsage, "Memory Usage", formatPercent},
{host.MemUsage, "Memory Usage", formatMemory},
} {
if val, ok := nmd.Metrics[tuple.key]; ok {
rows = append(rows, sparklineRow(tuple.human, val, tuple.fmt))
Expand Down