perf(agent): sort top procs by RAM only, skip per-process CPUPercent

CPUPercent() on every process reads /proc/<pid>/stat for all processes,
causing ~19% CPU spike on servers with many containers. Sorting by RAM
is a single /proc read per process and much cheaper.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Lizandro Guarnizo
2026-06-25 10:34:46 -05:00
co-authored by Claude Sonnet 4.6
parent 42dc7020fc
commit ad531c6dfc
2 changed files with 10 additions and 16 deletions
+6 -8
View File
@@ -245,17 +245,15 @@ func collectMetrics() (*Metricas, error) {
m.TCPConns = len(conns)
}
// Top 10 procesos por CPU (luego por RAM como desempate)
// Top 10 procesos por RAM (CPUPercent por proceso es muy costoso en sistemas con muchos contenedores)
if procs, err := process.Processes(); err == nil {
type pd struct {
pid int32
name string
cpu float64
ramMB float64
}
var lista []pd
for _, p := range procs {
cpuPct, _ := p.CPUPercent()
name, _ := p.Name()
if name == "" {
continue
@@ -264,12 +262,12 @@ func collectMetrics() (*Metricas, error) {
if mi, err := p.MemoryInfo(); err == nil && mi != nil {
ramMB = round2(float64(mi.RSS) / (1024 * 1024))
}
lista = append(lista, pd{p.Pid, name, round2(cpuPct), ramMB})
if ramMB < 1 {
continue
}
lista = append(lista, pd{p.Pid, name, ramMB})
}
sort.Slice(lista, func(i, j int) bool {
if lista[i].cpu != lista[j].cpu {
return lista[i].cpu > lista[j].cpu
}
return lista[i].ramMB > lista[j].ramMB
})
limit := 10
@@ -277,7 +275,7 @@ func collectMetrics() (*Metricas, error) {
limit = len(lista)
}
for _, d := range lista[:limit] {
m.TopProcs = append(m.TopProcs, ProcesoInfo{d.pid, d.name, d.cpu, d.ramMB})
m.TopProcs = append(m.TopProcs, ProcesoInfo{d.pid, d.name, 0, d.ramMB})
}
}
+4 -8
View File
@@ -155,24 +155,20 @@
<div x-data="{ showProcs: false }">
<button @click="showProcs=!showProcs" class="w-full text-left text-[10px] text-slate-400 hover:text-slate-600 flex items-center gap-1">
<svg class="w-3 h-3 transition-transform" :class="showProcs ? 'rotate-90' : ''" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5l7 7-7 7"/></svg>
Top procesos (<span x-text="(m.top_procs||[]).length"></span>)
Top procesos por RAM (<span x-text="(m.top_procs||[]).length"></span>)
</button>
<template x-if="showProcs">
<div class="mt-1.5 bg-slate-50 rounded-lg overflow-hidden">
<table class="w-full text-[10px]">
<thead><tr class="text-slate-400 border-b border-slate-200">
<th class="px-2 py-1 text-left">Proceso</th>
<th class="px-2 py-1 text-right">CPU%</th>
<th class="px-2 py-1 text-right">RAM</th>
</tr></thead>
<tbody>
<template x-for="p in (m.top_procs||[]).slice(0,8)" :key="p.pid">
<template x-for="p in (m.top_procs||[]).slice(0,10)" :key="p.pid">
<tr class="border-b border-slate-100 last:border-0">
<td class="px-2 py-1 font-mono truncate max-w-[90px]" x-text="p.nombre"></td>
<td class="px-2 py-1 text-right font-bold"
:class="p.cpu > 50 ? 'text-red-600' : p.cpu > 20 ? 'text-amber-600' : 'text-slate-600'"
x-text="p.cpu + '%'"></td>
<td class="px-2 py-1 text-right text-slate-500 font-mono" x-text="fmtMB(p.ram_mb)"></td>
<td class="px-2 py-1 font-mono truncate max-w-[120px]" x-text="p.nombre"></td>
<td class="px-2 py-1 text-right text-slate-600 font-mono font-bold" x-text="fmtMB(p.ram_mb)"></td>
</tr>
</template>
</tbody>