Skip to content

Commit

Permalink
v2: manager: factor out memory.events parsing
Browse files Browse the repository at this point in the history
This makes waitForEvents() more shorter and more readable.

Signed-off-by: Jeremi Piotrowski <jpiotrowski@microsoft.com>
  • Loading branch information
jepio committed Jan 31, 2022
1 parent 35b5b55 commit 6a46df2
Showing 1 changed file with 39 additions and 35 deletions.
74 changes: 39 additions & 35 deletions v2/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,41 @@ func (c *Manager) EventChan() (<-chan Event, <-chan error) {
return ec, errCh
}

func parseMemoryEvents(out map[string]interface{}) (Event, error) {
e := Event{}
if v, ok := out["high"]; ok {
e.High, ok = v.(uint64)
if !ok {
return Event{}, fmt.Errorf("cannot convert high to uint64: %+v", v)
}
}
if v, ok := out["low"]; ok {
e.Low, ok = v.(uint64)
if !ok {
return Event{}, fmt.Errorf("cannot convert low to uint64: %+v", v)
}
}
if v, ok := out["max"]; ok {
e.Max, ok = v.(uint64)
if !ok {
return Event{}, fmt.Errorf("cannot convert max to uint64: %+v", v)
}
}
if v, ok := out["oom"]; ok {
e.OOM, ok = v.(uint64)
if !ok {
return Event{}, fmt.Errorf("cannot convert oom to uint64: %+v", v)
}
}
if v, ok := out["oom_kill"]; ok {
e.OOMKill, ok = v.(uint64)
if !ok {
return Event{}, fmt.Errorf("cannot convert oom_kill to uint64: %+v", v)
}
}
return e, nil
}

func (c *Manager) waitForEvents(ec chan<- Event, errCh chan<- error) {
defer close(errCh)

Expand All @@ -626,41 +661,10 @@ func (c *Manager) waitForEvents(ec chan<- Event, errCh chan<- error) {
if bytesRead >= syscall.SizeofInotifyEvent {
out := make(map[string]interface{})
if err := readKVStatsFile(c.path, "memory.events", out); err == nil {
e := Event{}
if v, ok := out["high"]; ok {
e.High, ok = v.(uint64)
if !ok {
errCh <- fmt.Errorf("cannot convert high to uint64: %+v", v)
return
}
}
if v, ok := out["low"]; ok {
e.Low, ok = v.(uint64)
if !ok {
errCh <- fmt.Errorf("cannot convert low to uint64: %+v", v)
return
}
}
if v, ok := out["max"]; ok {
e.Max, ok = v.(uint64)
if !ok {
errCh <- fmt.Errorf("cannot convert max to uint64: %+v", v)
return
}
}
if v, ok := out["oom"]; ok {
e.OOM, ok = v.(uint64)
if !ok {
errCh <- fmt.Errorf("cannot convert oom to uint64: %+v", v)
return
}
}
if v, ok := out["oom_kill"]; ok {
e.OOMKill, ok = v.(uint64)
if !ok {
errCh <- fmt.Errorf("cannot convert oom_kill to uint64: %+v", v)
return
}
e, err := parseMemoryEvents(out)
if err != nil {
errCh <- err
return
}
ec <- e
} else {
Expand Down

0 comments on commit 6a46df2

Please sign in to comment.