RedFlag/agent/internal/system/process_darwin.go
Fimeg 67e26be2d9 projection: begin the exact-path public epoch
The public tree and its history contain only the listed paths. Earlier projection history remains preserved internally.

Source-Sha: 913fde029b935671833254797f0f20f1eb9fabba

Policy-Sha: 913fde029b935671833254797f0f20f1eb9fabba

Tree-Digest: 180ae530c1058a2a5c89837bdce2d323ae83e669e38590ca72e75b8d92b7262f
2026-09-08 21:59:33 -04:00

52 lines
1 KiB
Go

//go:build darwin
// +build darwin
package system
import (
"os/exec"
"sort"
"strconv"
"strings"
)
// getTopProcesses on macOS uses `ps aux` — no /proc, no sysctl for per-process.
// Can be optimized later with sysctl KERN_PROC if needed.
func getTopProcesses(limit int) ([]TopProcess, error) {
out, err := exec.Command("ps", "aux", "--sort=-%cpu").Output()
if err != nil {
return nil, err
}
lines := strings.Split(string(out), "\n")
var procs []TopProcess
for i, line := range lines {
if i == 0 || strings.TrimSpace(line) == "" {
continue
}
fields := strings.Fields(line)
if len(fields) < 11 {
continue
}
pid, _ := strconv.Atoi(fields[1])
cpu, _ := strconv.ParseFloat(fields[2], 64)
mem, _ := strconv.ParseFloat(fields[3], 64)
name := fields[10]
procs = append(procs, TopProcess{
Name: name,
PID: pid,
CPU: cpu,
Mem: mem,
})
}
sort.Slice(procs, func(i, j int) bool {
return procs[i].CPU > procs[j].CPU
})
if limit > 0 && len(procs) > limit {
procs = procs[:limit]
}
return procs, nil
}