()
| 99 | } |
| 100 | |
| 101 | func (w *watcher) start() { |
| 102 | go func() { |
| 103 | defer close(w.errCh) |
| 104 | defer w.eventFD.Close() |
| 105 | |
| 106 | var ( |
| 107 | oomKills uint64 |
| 108 | shouldExit bool |
| 109 | ) |
| 110 | for !shouldExit { |
| 111 | buffer := make([]byte, unix.SizeofInotifyEvent*10) |
| 112 | bytesRead, err := w.eventFD.Read(buffer) |
| 113 | if err != nil { |
| 114 | if !errors.Is(err, os.ErrClosed) { |
| 115 | w.errCh <- err |
| 116 | return |
| 117 | } |
| 118 | shouldExit = true |
| 119 | } else { |
| 120 | if bytesRead < unix.SizeofInotifyEvent { |
| 121 | continue |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | // TODO: We should export MemoryEventsStat function |
| 126 | out := make(map[string]uint64) |
| 127 | if err := readKVStatsFile(w.cgroupPath, "memory.events", out); err != nil { |
| 128 | // When cgroup is deleted read may return -ENODEV instead of -ENOENT from open. |
| 129 | if _, statErr := os.Lstat(filepath.Join(w.cgroupPath, "memory.events")); !os.IsNotExist(statErr) { |
| 130 | w.errCh <- err |
| 131 | } |
| 132 | return |
| 133 | } |
| 134 | |
| 135 | if v := out["oom_kill"]; v > oomKills { |
| 136 | oomKills = v |
| 137 | w.eventFn(w.cid) |
| 138 | } |
| 139 | } |
| 140 | }() |
| 141 | } |
| 142 | |
| 143 | func (w *watcher) stop() error { |
| 144 | cerr := w.eventFD.Close() |
no test coverage detected