7e3e94a08a
Add a comprehensive performance monitoring system that collects CPU, memory, swap, load average, network IO, and GPU stats. Provides both a REST API for the UI and a Prometheus /metrics endpoint. Backend changes: - New internal/perf package with configurable interval-based stats collection - GPU monitoring via LACT (Unix socket) and nvidia-smi fallback on Linux - Ring buffer (internal/ring) for time-series stat storage - Prometheus /metrics endpoint with all system and GPU metrics - Moved LogMonitor to internal/logmon package - New PerformanceConfig for hot-reloadable monitoring settings - REST /api/performance endpoint replacing SSE streaming UI changes: - New Performance page with real-time charts for CPU, memory, GPU, and network - Reusable PerformanceChart component - LLAMA_SWAP_URL environment variable support - Improved capture dialog display Other: - Example Grafana dashboard for Prometheus metrics - monitor-test standalone binary - Config schema and example updates fixes #596
56 lines
1.3 KiB
Go
56 lines
1.3 KiB
Go
package perf
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/mostlygeek/llama-swap/internal/logmon"
|
|
"github.com/shirou/gopsutil/v4/cpu"
|
|
"github.com/shirou/gopsutil/v4/load"
|
|
"github.com/shirou/gopsutil/v4/mem"
|
|
)
|
|
|
|
func getGpuStats(ctx context.Context, every time.Duration, logger *logmon.Monitor) (chan []GpuStat, error) {
|
|
return nil, ErrNotImplemented
|
|
}
|
|
|
|
func readSysStats() (SysStat, error) {
|
|
cpuPcts, err := cpu.Percent(0, true)
|
|
if err != nil {
|
|
return SysStat{}, err
|
|
}
|
|
|
|
vmStat, err := mem.VirtualMemory()
|
|
if err != nil {
|
|
return SysStat{}, err
|
|
}
|
|
|
|
const toMB = 1024 * 1024
|
|
|
|
var swapTotalMB, swapUsedMB int
|
|
if swapStat, err := mem.SwapMemory(); err == nil {
|
|
swapTotalMB = int(swapStat.Total / toMB)
|
|
swapUsedMB = int(swapStat.Used / toMB)
|
|
}
|
|
|
|
var loadAvg1, loadAvg5, loadAvg15 float64
|
|
if loadStat, err := load.Avg(); err == nil {
|
|
loadAvg1 = loadStat.Load1
|
|
loadAvg5 = loadStat.Load5
|
|
loadAvg15 = loadStat.Load15
|
|
}
|
|
|
|
return SysStat{
|
|
Timestamp: time.Now(),
|
|
CpuUtilPerCore: cpuPcts,
|
|
MemTotalMB: int(vmStat.Total / toMB),
|
|
MemUsedMB: int(vmStat.Used / toMB),
|
|
MemFreeMB: int(vmStat.Free / toMB),
|
|
SwapTotalMB: swapTotalMB,
|
|
SwapUsedMB: swapUsedMB,
|
|
LoadAvg1: loadAvg1,
|
|
LoadAvg5: loadAvg5,
|
|
LoadAvg15: loadAvg15,
|
|
}, nil
|
|
}
|