lookupUID resolves a uid to a username, using the per-run cache to avoid repeated syscalls for the same uid.
(uid uint32)
| 222 | // lookupUID resolves a uid to a username, using the per-run cache to avoid |
| 223 | // repeated syscalls for the same uid. |
| 224 | func (s *procCacheState) lookupUID(uid uint32) string { |
| 225 | if runtime.GOOS == "windows" { |
| 226 | return "" |
| 227 | } |
| 228 | if s.uidCache == nil { |
| 229 | s.uidCache = make(map[uint32]string) |
| 230 | } |
| 231 | if name, ok := s.uidCache[uid]; ok { |
| 232 | return name |
| 233 | } |
| 234 | u, err := user.LookupId(strconv.FormatUint(uint64(uid), 10)) |
| 235 | if err != nil { |
| 236 | s.uidCache[uid] = "" |
| 237 | return "" |
| 238 | } |
| 239 | name := u.Username |
| 240 | s.uidCache[uid] = name |
| 241 | return name |
| 242 | } |
| 243 | |
| 244 | // collectSnapshot fetches all process info, updates lastCPUSamples with fresh measurements, |
| 245 | // and computes CPU% using each pid's previous sample (if available). |