(firstReadyCh chan struct{})
| 181 | } |
| 182 | |
| 183 | func (s *procCacheState) runLoop(firstReadyCh chan struct{}) { |
| 184 | firstDone := false |
| 185 | defer func() { |
| 186 | if panichandler.PanicHandler("procCache.runLoop", recover()) == nil { |
| 187 | return |
| 188 | } |
| 189 | s.lock.Lock() |
| 190 | defer s.lock.Unlock() |
| 191 | s.running = false |
| 192 | if !firstDone { |
| 193 | close(firstReadyCh) |
| 194 | s.ready = nil |
| 195 | } |
| 196 | }() |
| 197 | |
| 198 | numCPU := max(runtime.NumCPU(), 1) |
| 199 | |
| 200 | for { |
| 201 | iterStart := time.Now() |
| 202 | |
| 203 | s.lastCPUEpoch++ |
| 204 | result := s.collectSnapshot(numCPU) |
| 205 | |
| 206 | // Remove stale entries (pids that weren't seen this epoch). |
| 207 | for pid, sample := range s.lastCPUSamples { |
| 208 | if sample.Epoch < s.lastCPUEpoch { |
| 209 | delete(s.lastCPUSamples, pid) |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | if s.updateCacheAndCheckIdle(result, &firstDone, firstReadyCh) { |
| 214 | return |
| 215 | } |
| 216 | |
| 217 | elapsed := time.Since(iterStart) |
| 218 | time.Sleep(max(ProcCacheMinSleep, ProcCachePollInterval-elapsed)) |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | // lookupUID resolves a uid to a username, using the per-run cache to avoid |
| 223 | // repeated syscalls for the same uid. |
no test coverage detected