perf(agent): read CPUPercent only on top-10 RAM processes, not all

Filter by RAM first (cheap), then call CPUPercent on just the 10 winners.
Reduces /proc reads from hundreds to 10 per interval.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Lizandro Guarnizo
2026-06-25 10:35:48 -05:00
co-authored by Claude Sonnet 4.6
parent ad531c6dfc
commit 71db59b005
2 changed files with 11 additions and 6 deletions
+5 -4
View File
@@ -245,10 +245,10 @@ func collectMetrics() (*Metricas, error) {
m.TCPConns = len(conns) m.TCPConns = len(conns)
} }
// Top 10 procesos por RAM (CPUPercent por proceso es muy costoso en sistemas con muchos contenedores) // Top 10 procesos: filtrar por RAM primero (barato), luego leer CPU solo en esos 10
if procs, err := process.Processes(); err == nil { if procs, err := process.Processes(); err == nil {
type pd struct { type pd struct {
pid int32 proc *process.Process
name string name string
ramMB float64 ramMB float64
} }
@@ -265,7 +265,7 @@ func collectMetrics() (*Metricas, error) {
if ramMB < 1 { if ramMB < 1 {
continue continue
} }
lista = append(lista, pd{p.Pid, name, ramMB}) lista = append(lista, pd{p, name, ramMB})
} }
sort.Slice(lista, func(i, j int) bool { sort.Slice(lista, func(i, j int) bool {
return lista[i].ramMB > lista[j].ramMB return lista[i].ramMB > lista[j].ramMB
@@ -275,7 +275,8 @@ func collectMetrics() (*Metricas, error) {
limit = len(lista) limit = len(lista)
} }
for _, d := range lista[:limit] { for _, d := range lista[:limit] {
m.TopProcs = append(m.TopProcs, ProcesoInfo{d.pid, d.name, 0, d.ramMB}) cpuPct, _ := d.proc.CPUPercent() // solo 10 lecturas, no cientos
m.TopProcs = append(m.TopProcs, ProcesoInfo{d.proc.Pid, d.name, round2(cpuPct), d.ramMB})
} }
} }
+6 -2
View File
@@ -162,13 +162,17 @@
<table class="w-full text-[10px]"> <table class="w-full text-[10px]">
<thead><tr class="text-slate-400 border-b border-slate-200"> <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-left">Proceso</th>
<th class="px-2 py-1 text-right">CPU%</th>
<th class="px-2 py-1 text-right">RAM</th> <th class="px-2 py-1 text-right">RAM</th>
</tr></thead> </tr></thead>
<tbody> <tbody>
<template x-for="p in (m.top_procs||[]).slice(0,10)" :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"> <tr class="border-b border-slate-100 last:border-0">
<td class="px-2 py-1 font-mono truncate max-w-[120px]" x-text="p.nombre"></td> <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 text-slate-600 font-mono font-bold" x-text="fmtMB(p.ram_mb)"></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>
</tr> </tr>
</template> </template>
</tbody> </tbody>