Skip to content

Commit

Permalink
Review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
tomwilkie committed Dec 16, 2015
1 parent 21a1677 commit 8efa0d1
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 16 deletions.
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
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

0 comments on commit 8efa0d1

Please sign in to comment.