Modern Docker Management, Designed for Everyone
Go composecontainer-managementcontainersdockerdocker-composedocker-managementgoself-hostedsveltekittypescriptweb-ui
refactor: split ws_handler and skip pagination counts when not needed (#2440)
Kyle Mendell461e95af607c
diff --git a/backend/internal/api/ws_handler.go b/backend/internal/api/ws_handler.goindex f262fc939..4a5390c47 100644--- a/backend/internal/api/ws_handler.go+++ b/backend/internal/api/ws_handler.go@@ -1,17 +1,12 @@package apiimport (- "bytes""context"- "encoding/csv""encoding/json""errors"- "fmt""io""log/slog""net/http"- "os"- "os/exec""runtime""strconv""strings"@@ -24,6 +19,7 @@ import ("github.com/getarcaneapp/arcane/backend/internal/middleware""github.com/getarcaneapp/arcane/backend/internal/services"docker "github.com/getarcaneapp/arcane/backend/pkg/dockerutil"+ "github.com/getarcaneapp/arcane/backend/pkg/libarcane/system"ws "github.com/getarcaneapp/arcane/backend/pkg/libarcane/ws"httputil "github.com/getarcaneapp/arcane/backend/pkg/utils/httpx"systemtypes "github.com/getarcaneapp/arcane/types/system"@@ -35,13 +31,7 @@ import ("github.com/shirou/gopsutil/v4/mem")-const (- gpuCacheDuration = 30 * time.Second- cgroupCacheTTL = 30 * time.Second-)--// amdGPUSysfsPath is the base path for AMD GPU sysfs entries-const amdGPUSysfsPath = "/sys/class/drm"+const cgroupCacheTTL = 30 * time.Second// ============================================================================// WebSocket Metrics@@ -184,33 +174,18 @@ type WebSocketHandler struct {ready chan struct{}running bool}- cgroupCache struct {- sync.RWMutex- value *docker.CgroupLimits- detected bool- timestamp time.Time- }+ cgroupCache *system.CgroupCache+ gpuMonitor *system.GPUMonitor+diskUsagePathCache struct {sync.RWMutexvalue stringtimestamp time.Time}- gpuDetectionCache struct {- sync.RWMutex- detected bool- timestamp time.Time- gpuType string- toolPath string- }- detectionDone bool- detectionMutex sync.Mutex- gpuMonitoringEnabled bool- gpuType stringprojectLogStreamer func(ctx context.Context, projectID string, logsChan chan<- string, follow bool, tail, since string, timestamps bool) errorcontainerLogStreamer func(ctx context.Context, containerID string, logsChan chan<- string, follow bool, tail, since string, timestamps bool) errorsystemStatsCollector func(ctx context.Context) systemtypes.SystemStatscpuUsageReader func(interval time.Duration) (float64, bool)- cgroupLimitsDetector func() (*docker.CgroupLimits, error)}type wsLogStream struct {@@ -346,14 +321,14 @@ func NewWebSocketHandler(cfg *config.Config,) {handler := &WebSocketHandler{- projectService: projectService,- containerService: containerService,- swarmService: swarmService,- systemService: systemService,- wsMetrics: defaultWebSocketMetrics,- logStreams: make(map[string]*wsLogStream),- gpuMonitoringEnabled: cfg.GPUMonitoringEnabled,- gpuType: cfg.GPUType,+ projectService: projectService,+ containerService: containerService,+ swarmService: swarmService,+ systemService: systemService,+ wsMetrics: defaultWebSocketMetrics,+ logStreams: make(map[string]*wsLogStream),+ cgroupCache: system.NewCgroupCache(cgroupCacheTTL),+ gpuMonitor: system.NewGPUMonitor(cfg.GPUMonitoringEnabled, cfg.GPUType),wsUpgrader: websocket.Upgrader{CheckOrigin: httputil.ValidateWebSocketOrigin(cfg.GetAppURL()),ReadBufferSize: 32 * 1024,@@ -1273,10 +1248,10 @@ func (h *WebSocketHandler) getHostname() string {// getGPUInfo returns GPU statistics if monitoring is enabled.func (h *WebSocketHandler) getGPUInfo(ctx context.Context) ([]systemtypes.GPUStats, int) {- if !h.gpuMonitoringEnabled {+ if h.gpuMonitor == nil || !h.gpuMonitor.Enabled() {return nil, 0}- gpuData, err := h.getGPUStats(ctx)+ gpuData, err := h.gpuMonitor.Stats(ctx)if err != nil {return nil, 0}@@ -1336,47 +1311,10 @@ func (h *WebSocketHandler) storeCPUCacheValueInternal(value float64) {}func (h *WebSocketHandler) getCachedCgroupLimitsInternal() *docker.CgroupLimits {- h.cgroupCache.RLock()- value := h.cgroupCache.value- detected := h.cgroupCache.detected- fresh := time.Since(h.cgroupCache.timestamp) < cgroupCacheTTL- h.cgroupCache.RUnlock()- if fresh {- if detected {- return value- }- return nil- }-- h.cgroupCache.Lock()- defer h.cgroupCache.Unlock()-- if time.Since(h.cgroupCache.timestamp) < cgroupCacheTTL {- if h.cgroupCache.detected {- return h.cgroupCache.value- }- return nil- }-- detect := h.cgroupLimitsDetector- if detect == nil {- detect = docker.DetectCgroupLimits- }-- limits, err := detect()- h.cgroupCache.timestamp = time.Now()- if err != nil {- h.cgroupCache.value = nil- h.cgroupCache.detected = false- } else {- h.cgroupCache.value = limits- h.cgroupCache.detected = true- }- if !h.cgroupCache.detected {+ if h.cgroupCache == nil {return nil}-- return h.cgroupCache.value+ return h.cgroupCache.Get()}// SystemStats streams system stats over WebSocket.@@ -1511,325 +1449,3 @@ func (h *WebSocketHandler) getDiskUsagePath(ctx context.Context) string {return path}--// ============================================================================-// GPU Monitoring-// ============================================================================--// getGPUStats collects and returns GPU statistics for all available GPUs-func (h *WebSocketHandler) getGPUStats(ctx context.Context) ([]systemtypes.GPUStats, error) {- h.detectionMutex.Lock()- done := h.detectionDone- h.detectionMutex.Unlock()-- if !done {- if err := h.detectGPUs(ctx); err != nil {- return nil, err- }- }-- h.gpuDetectionCache.RLock()- if h.gpuDetectionCache.detected && time.Since(h.gpuDetectionCache.timestamp) < gpuCacheDuration {- gpuType := h.gpuDetectionCache.gpuType- h.gpuDetectionCache.RUnlock()-- switch gpuType {- case "nvidia":- return h.getNvidiaStats(ctx)- case "amd":- return h.getAMDStats(ctx)- case "intel":- return h.getIntelStats(ctx)- }- }- h.gpuDetectionCache.RUnlock()-- if err := h.detectGPUs(ctx); err != nil {- return nil, err- }-- h.gpuDetectionCache.RLock()- gpuType := h.gpuDetectionCache.gpuType- h.gpuDetectionCache.RUnlock()-- switch gpuType {- case "nvidia":- return h.getNvidiaStats(ctx)- case "amd":- return h.getAMDStats(ctx)- case "intel":- return h.getIntelStats(ctx)- default:- return nil, fmt.Errorf("no supported GPU found")- }-}--// detectGPUs detects available GPU management tools-func (h *WebSocketHandler) detectGPUs(ctx context.Context) error {- h.detectionMutex.Lock()- defer h.detectionMutex.Unlock()-- if h.gpuType != "" && h.gpuType != "auto" {- switch h.gpuType {- case "nvidia":- if path, err := exec.LookPath("nvidia-smi"); err == nil {- h.gpuDetectionCache.Lock()- h.gpuDetectionCache.detected = true- h.gpuDetectionCache.gpuType = "nvidia"- h.gpuDetectionCache.toolPath = path- h.gpuDetectionCache.timestamp = time.Now()- h.gpuDetectionCache.Unlock()- h.detectionDone = true- slog.InfoContext(ctx, "Using configured GPU type", "type", "nvidia")- return nil- }- return fmt.Errorf("nvidia-smi not found but GPU_TYPE set to nvidia")-- case "amd":- if hasAMDGPUInternal() {- h.gpuDetectionCache.Lock()- h.gpuDetectionCache.detected = true- h.gpuDetectionCache.gpuType = "amd"- h.gpuDetectionCache.toolPath = amdGPUSysfsPath- h.gpuDetectionCache.timestamp = time.Now()- h.gpuDetectionCache.Unlock()- h.detectionDone = true- slog.InfoContext(ctx, "Using configured GPU type", "type", "amd")- return nil- }- return fmt.Errorf("AMD GPU not found in sysfs but GPU_TYPE set to amd")-- case "intel":- if path, err := exec.LookPath("intel_gpu_top"); err == nil {- h.gpuDetectionCache.Lock()- h.gpuDetectionCache.detected = true- h.gpuDetectionCache.gpuType = "intel"- h.gpuDetectionCache.toolPath = path- h.gpuDetectionCache.timestamp = time.Now()- h.gpuDetectionCache.Unlock()- h.detectionDone = true- slog.InfoContext(ctx, "Using configured GPU type", "type", "intel")- return nil- }- return fmt.Errorf("intel_gpu_top not found but GPU_TYPE set to intel")-- default:- slog.WarnContext(ctx, "Invalid GPU_TYPE specified, falling back to auto-detection", "gpu_type", h.gpuType)- }- }-- if path, err := exec.LookPath("nvidia-smi"); err == nil {- h.gpuDetectionCache.Lock()- h.gpuDetectionCache.detected = true- h.gpuDetectionCache.gpuType = "nvidia"- h.gpuDetectionCache.toolPath = path- h.gpuDetectionCache.timestamp = time.Now()- h.gpuDetectionCache.Unlock()- h.detectionDone = true- slog.InfoContext(ctx, "NVIDIA GPU detected", "tool", "nvidia-smi", "path", path)- return nil- }-- if hasAMDGPUInternal() {- h.gpuDetectionCache.Lock()- h.gpuDetectionCache.detected = true- h.gpuDetectionCache.gpuType = "amd"- h.gpuDetectionCache.toolPath = amdGPUSysfsPath- h.gpuDetectionCache.timestamp = time.Now()- h.gpuDetectionCache.Unlock()- h.detectionDone = true- slog.InfoContext(ctx, "AMD GPU detected", "method", "sysfs", "path", amdGPUSysfsPath)- return nil- }-- if path, err := exec.LookPath("intel_gpu_top"); err == nil {- h.gpuDetectionCache.Lock()- h.gpuDetectionCache.detected = true- h.gpuDetectionCache.gpuType = "intel"- h.gpuDetectionCache.toolPath = path- h.gpuDetectionCache.timestamp = time.Now()- h.gpuDetectionCache.Unlock()- h.detectionDone = true- slog.InfoContext(ctx, "Intel GPU detected", "tool", "intel_gpu_top", "path", path)- return nil- }-- h.detectionDone = true- return fmt.Errorf("no supported GPU found")-}--// getNvidiaStats collects NVIDIA GPU statistics using nvidia-smi-func (h *WebSocketHandler) getNvidiaStats(ctx context.Context) ([]systemtypes.GPUStats, error) {- ctx, cancel := context.WithTimeout(ctx, 3*time.Second)- defer cancel()-- cmd := exec.CommandContext(ctx, "nvidia-smi",- "--query-gpu=index,name,memory.used,memory.total",- "--format=csv,noheader,nounits")-- output, err := cmd.Output()- if err != nil {- slog.WarnContext(ctx, "Failed to execute nvidia-smi", "error", err)- return nil, fmt.Errorf("nvidia-smi execution failed: %w", err)- }-- reader := csv.NewReader(bytes.NewReader(output))- reader.TrimLeadingSpace = true-- records, err := reader.ReadAll()- if err != nil {- slog.WarnContext(ctx, "Failed to parse nvidia-smi CSV output", "error", err)- return nil, fmt.Errorf("failed to parse nvidia-smi output: %w", err)- }-- var stats []systemtypes.GPUStats- for _, record := range records {- if len(record) < 4 {- continue- }-- index, err := strconv.Atoi(strings.TrimSpace(record[0]))- if err != nil {- slog.WarnContext(ctx, "Failed to parse GPU index", "value", record[0])- continue- }-- name := strings.TrimSpace(record[1])-- memUsed, err := strconv.ParseFloat(strings.TrimSpace(record[2]), 64)- if err != nil {- slog.WarnContext(ctx, "Failed to parse memory used", "value", record[2])- continue- }-- memTotal, err := strconv.ParseFloat(strings.TrimSpace(record[3]), 64)- if err != nil {- slog.WarnContext(ctx, "Failed to parse memory total", "value", record[3])- continue- }-- stats = append(stats, systemtypes.GPUStats{- Name: name,- Index: index,- MemoryUsed: memUsed * 1024 * 1024,- MemoryTotal: memTotal * 1024 * 1024,- })- }-- if len(stats) == 0 {- return nil, fmt.Errorf("no GPU data parsed from nvidia-smi")- }-- slog.DebugContext(ctx, "Collected NVIDIA GPU stats", "gpu_count", len(stats))- return stats, nil-}--// getAMDStats collects AMD GPU statistics using sysfs-func (h *WebSocketHandler) getAMDStats(ctx context.Context) ([]systemtypes.GPUStats, error) {- var stats []systemtypes.GPUStats-- // Find AMD GPU cards by looking for mem_info_vram_total in /sys/class/drm/card*/device/- entries, err := os.ReadDir(amdGPUSysfsPath)- if err != nil {- slog.WarnContext(ctx, "Failed to read DRM sysfs directory", "error", err)- return nil, fmt.Errorf("failed to read sysfs: %w", err)- }-- index := 0- for _, entry := range entries {- name := entry.Name()- // Only check card* entries (card0, card1, etc.) - skip renderD* and connector entries- if !strings.HasPrefix(name, "card") {- continue- }- // Skip connector entries like card0-DP-1, card0-HDMI-A-1- if strings.Contains(name, "-") {- continue- }-- devicePath := fmt.Sprintf("%s/%s/device", amdGPUSysfsPath, name)-- // Check if this is an AMD GPU by looking for VRAM info files- memTotalPath := fmt.Sprintf("%s/mem_info_vram_total", devicePath)- memUsedPath := fmt.Sprintf("%s/mem_info_vram_used", devicePath)-- memTotalBytes, err := readSysfsValueInternal(memTotalPath)- if err != nil {- // Not an AMD GPU or doesn't have VRAM info- continue- }-- memUsedBytes, err := readSysfsValueInternal(memUsedPath)- if err != nil {- slog.WarnContext(ctx, "Failed to read AMD GPU memory used", "card", name, "error", err)- continue- }-- stats = append(stats, systemtypes.GPUStats{- Name: fmt.Sprintf("AMD GPU %d", index),- Index: index,- MemoryUsed: float64(memUsedBytes),- MemoryTotal: float64(memTotalBytes),- })- index++- }-- if len(stats) == 0 {- return nil, fmt.Errorf("no AMD GPU data found in sysfs")- }-- slog.DebugContext(ctx, "Collected AMD GPU stats", "gpu_count", len(stats))- return stats, nil-}--// readSysfsValueInternal reads a numeric value from a sysfs file-func readSysfsValueInternal(path string) (uint64, error) {- data, err := os.ReadFile(path)- if err != nil {- return 0, err- }- return strconv.ParseUint(strings.TrimSpace(string(data)), 10, 64)-}--// hasAMDGPUInternal checks if an AMD GPU is present by looking for VRAM info in sysfs-func hasAMDGPUInternal() bool {- entries, err := os.ReadDir(amdGPUSysfsPath)- if err != nil {- return false- }-- for _, entry := range entries {- name := entry.Name()- // Only check card* entries (card0, card1, etc.) - skip card0-DP-1 style entries- if !strings.HasPrefix(name, "card") {- continue- }- // Skip connector entries like card0-DP-1, card0-HDMI-A-1- if strings.Contains(name, "-") {- continue- }-- // Check if this card has AMD VRAM info- memTotalPath := fmt.Sprintf("%s/%s/device/mem_info_vram_total", amdGPUSysfsPath, name)- if _, err := os.Stat(memTotalPath); err == nil {- return true- }- }-- return false-}--// getIntelStats collects Intel GPU statistics using intel_gpu_top-func (h *WebSocketHandler) getIntelStats(ctx context.Context) ([]systemtypes.GPUStats, error) {- stats := []systemtypes.GPUStats{- {- Name: "Intel GPU",- Index: 0,- MemoryUsed: 0,- MemoryTotal: 0,- },- }-- slog.DebugContext(ctx, "Intel GPU detected but detailed stats not yet implemented")- return stats, nil-}diff --git a/backend/internal/api/ws_handler_test.go b/backend/internal/api/ws_handler_test.goindex 5519f2240..a82ac4f2f 100644--- a/backend/internal/api/ws_handler_test.go+++ b/backend/internal/api/ws_handler_test.go@@ -13,6 +13,7 @@ import ("time"docker "github.com/getarcaneapp/arcane/backend/pkg/dockerutil"+ "github.com/getarcaneapp/arcane/backend/pkg/libarcane/system"ws "github.com/getarcaneapp/arcane/backend/pkg/libarcane/ws"systemtypes "github.com/getarcaneapp/arcane/types/system""github.com/gin-gonic/gin"@@ -105,8 +106,10 @@ func newTestWSPairInternal(t *testing.T) (clientConn *websocket.Conn, serverConnfunc newTestWebSocketHandler() *WebSocketHandler {return &WebSocketHandler{- wsMetrics: NewWebSocketMetrics(),- logStreams: make(map[string]*wsLogStream),+ wsMetrics: NewWebSocketMetrics(),+ logStreams: make(map[string]*wsLogStream),+ cgroupCache: system.NewCgroupCache(cgroupCacheTTL),+ gpuMonitor: system.NewGPUMonitor(false, ""),wsUpgrader: websocket.Upgrader{CheckOrigin: func(r *http.Request) bool { return true },},@@ -575,17 +578,18 @@ func TestWebSocketHandler_LogStream_CancelsOnlyOnce(t *testing.T) {func TestWebSocketHandler_GetCachedCgroupLimitsInternal_DeduplicatesRefresh(t *testing.T) {handler := newTestWebSocketHandler()- handler.cgroupCache.timestamp = time.Now().Add(-2 * cgroupCacheTTL)var calls atomic.Int32start := make(chan struct{})release := make(chan struct{})- handler.cgroupLimitsDetector = func() (*docker.CgroupLimits, error) {+ // A fresh cache has a zero timestamp, so the first Get takes the refresh path+ // and subsequent concurrent Gets dedupe behind the write lock.+ handler.cgroupCache = system.NewCgroupCacheWithDetector(cgroupCacheTTL, func() (*docker.CgroupLimits, error) {calls.Add(1)close(start)<-releasereturn &docker.CgroupLimits{CPUCount: 2}, nil- }+ })const goroutines = 8results := make(chan *docker.CgroupLimits, goroutines)diff --git a/backend/pkg/libarcane/system/cgroup.go b/backend/pkg/libarcane/system/cgroup.gonew file mode 100644index 000000000..a4e3ad2e0--- /dev/null+++ b/backend/pkg/libarcane/system/cgroup.go@@ -0,0 +1,70 @@+// Package system encapsulates host-level resource probing used by the WebSocket system stats+// endpoint: cgroup limits and GPU detection/stats. The types here own their own caching and+// synchronization so handlers can hold them as plain fields.+package system++import (+ "sync"+ "time"++ docker "github.com/getarcaneapp/arcane/backend/pkg/dockerutil"+)++// CgroupCache memoizes the result of cgroup limit detection for a configurable TTL.+// A single in-flight refresh is shared across concurrent callers (singleflight semantics+// via the write lock), so a stampede of system-stats samplers never duplicates the work.+type CgroupCache struct {+ mu sync.RWMutex+ value *docker.CgroupLimits+ detected bool+ timestamp time.Time+ ttl time.Duration+ detector func() (*docker.CgroupLimits, error)+}++// NewCgroupCache returns a cache that caches detection results for ttl using the+// default docker.DetectCgroupLimits detector.+func NewCgroupCache(ttl time.Duration) *CgroupCache {+ return NewCgroupCacheWithDetector(ttl, docker.DetectCgroupLimits)+}++// NewCgroupCacheWithDetector returns a cache backed by a custom detector. Useful when+// callers (e.g. tests) need to control detection behaviour or simulate failures.+func NewCgroupCacheWithDetector(ttl time.Duration, detector func() (*docker.CgroupLimits, error)) *CgroupCache {+ return &CgroupCache{ttl: ttl, detector: detector}+}++// Get returns the cached cgroup limits, refreshing if the entry is older than the TTL.+// Returns nil when no cgroup limits have been detected (host is not running under cgroups).+func (c *CgroupCache) Get() *docker.CgroupLimits {+ c.mu.RLock()+ value, detected, fresh := c.value, c.detected, time.Since(c.timestamp) < c.ttl+ c.mu.RUnlock()+ if fresh {+ if detected {+ return value+ }+ return nil+ }++ c.mu.Lock()+ defer c.mu.Unlock()++ if time.Since(c.timestamp) < c.ttl {+ if c.detected {+ return c.value+ }+ return nil+ }++ limits, err := c.detector()+ c.timestamp = time.Now()+ if err != nil {+ c.value = nil+ c.detected = false+ return nil+ }+ c.value = limits+ c.detected = true+ return c.value+}diff --git a/backend/pkg/libarcane/system/gpu.go b/backend/pkg/libarcane/system/gpu.gonew file mode 100644index 000000000..a785391b0--- /dev/null+++ b/backend/pkg/libarcane/system/gpu.go@@ -0,0 +1,301 @@+package system++import (+ "bytes"+ "context"+ "encoding/csv"+ "fmt"+ "log/slog"+ "os"+ "os/exec"+ "strconv"+ "strings"+ "sync"+ "time"++ systemtypes "github.com/getarcaneapp/arcane/types/system"+)++// AMDGPUSysfsPath is the sysfs base used to discover AMD GPUs.+const AMDGPUSysfsPath = "/sys/class/drm"++// gpuDetectionTTL bounds how long a successful detection result is reused before re-detecting.+const gpuDetectionTTL = 30 * time.Second++// GPUMonitor probes for an attached GPU (NVIDIA / AMD / Intel) and reports VRAM usage.+// Detection is cached for gpuDetectionTTL; once a vendor is detected, subsequent Stats+// calls invoke the vendor-specific tool directly.+type GPUMonitor struct {+ enabled bool+ configuredType string++ detectionMu sync.Mutex+ detectionDone bool++ cacheMu sync.RWMutex+ detected bool+ timestamp time.Time+ gpuType string+ toolPath string+}++// NewGPUMonitor creates a monitor. enabled gates Stats; when false, Stats returns+// (nil, nil). configuredType is the user-pinned vendor ("nvidia"|"amd"|"intel"|"auto"|"")+// — anything else falls back to auto-detection.+func NewGPUMonitor(enabled bool, configuredType string) *GPUMonitor {+ return &GPUMonitor{enabled: enabled, configuredType: configuredType}+}++// Enabled reports whether GPU monitoring is on.+func (m *GPUMonitor) Enabled() bool { return m.enabled }++// Stats returns per-GPU VRAM stats. Returns (nil, 0, nil) when monitoring is disabled+// or no GPU is detected; vendor-specific errors are propagated otherwise.+func (m *GPUMonitor) Stats(ctx context.Context) ([]systemtypes.GPUStats, error) {+ if !m.enabled {+ return nil, nil+ }++ m.detectionMu.Lock()+ done := m.detectionDone+ m.detectionMu.Unlock()+ if !done {+ if err := m.detectInternal(ctx); err != nil {+ return nil, err+ }+ }++ m.cacheMu.RLock()+ if m.detected && time.Since(m.timestamp) < gpuDetectionTTL {+ t := m.gpuType+ m.cacheMu.RUnlock()+ return m.statsForTypeInternal(ctx, t)+ }+ m.cacheMu.RUnlock()++ if err := m.detectInternal(ctx); err != nil {+ return nil, err+ }++ m.cacheMu.RLock()+ t := m.gpuType+ m.cacheMu.RUnlock()+ if t == "" {+ return nil, fmt.Errorf("no supported GPU found")+ }+ return m.statsForTypeInternal(ctx, t)+}++func (m *GPUMonitor) statsForTypeInternal(ctx context.Context, gpuType string) ([]systemtypes.GPUStats, error) {+ switch gpuType {+ case "nvidia":+ return getNvidiaStatsInternal(ctx)+ case "amd":+ return getAMDStatsInternal(ctx)+ case "intel":+ return getIntelStatsInternal(ctx)+ default:+ return nil, fmt.Errorf("no supported GPU found")+ }+}++// markDetected records a successful detection. Caller must NOT hold cacheMu.+func (m *GPUMonitor) markDetectedInternal(gpuType, toolPath string) {+ m.cacheMu.Lock()+ m.detected = true+ m.gpuType = gpuType+ m.toolPath = toolPath+ m.timestamp = time.Now()+ m.cacheMu.Unlock()+ m.detectionDone = true+}++// detect runs vendor probing under detectionMu. The configuredType pin is honored when set+// to a known vendor; otherwise vendors are tried in order: nvidia → amd → intel.+func (m *GPUMonitor) detectInternal(ctx context.Context) error {+ m.detectionMu.Lock()+ defer m.detectionMu.Unlock()++ if t := m.configuredType; t != "" && t != "auto" {+ switch t {+ case "nvidia":+ if path, err := exec.LookPath("nvidia-smi"); err == nil {+ m.markDetectedInternal("nvidia", path)+ slog.InfoContext(ctx, "Using configured GPU type", "type", "nvidia")+ return nil+ }+ return fmt.Errorf("nvidia-smi not found but GPU_TYPE set to nvidia")+ case "amd":+ if HasAMDGPU() {+ m.markDetectedInternal("amd", AMDGPUSysfsPath)+ slog.InfoContext(ctx, "Using configured GPU type", "type", "amd")+ return nil+ }+ return fmt.Errorf("AMD GPU not found in sysfs but GPU_TYPE set to amd")+ case "intel":+ if path, err := exec.LookPath("intel_gpu_top"); err == nil {+ m.markDetectedInternal("intel", path)+ slog.InfoContext(ctx, "Using configured GPU type", "type", "intel")+ return nil+ }+ return fmt.Errorf("intel_gpu_top not found but GPU_TYPE set to intel")+ default:+ slog.WarnContext(ctx, "Invalid GPU_TYPE specified, falling back to auto-detection", "gpu_type", t)+ }+ }++ if path, err := exec.LookPath("nvidia-smi"); err == nil {+ m.markDetectedInternal("nvidia", path)+ slog.InfoContext(ctx, "NVIDIA GPU detected", "tool", "nvidia-smi", "path", path)+ return nil+ }+ if HasAMDGPU() {+ m.markDetectedInternal("amd", AMDGPUSysfsPath)+ slog.InfoContext(ctx, "AMD GPU detected", "method", "sysfs", "path", AMDGPUSysfsPath)+ return nil+ }+ if path, err := exec.LookPath("intel_gpu_top"); err == nil {+ m.markDetectedInternal("intel", path)+ slog.InfoContext(ctx, "Intel GPU detected", "tool", "intel_gpu_top", "path", path)+ return nil+ }++ m.detectionDone = true+ return fmt.Errorf("no supported GPU found")+}++// HasAMDGPU reports whether a card with mem_info_vram_total exists under AMDGPUSysfsPath.+func HasAMDGPU() bool {+ entries, err := os.ReadDir(AMDGPUSysfsPath)+ if err != nil {+ return false+ }+ for _, entry := range entries {+ name := entry.Name()+ if !strings.HasPrefix(name, "card") || strings.Contains(name, "-") {+ continue+ }+ if _, err := os.Stat(fmt.Sprintf("%s/%s/device/mem_info_vram_total", AMDGPUSysfsPath, name)); err == nil {+ return true+ }+ }+ return false+}++// readSysfsValueInternal parses a numeric value from a sysfs file.+func readSysfsValueInternal(path string) (uint64, error) {+ data, err := os.ReadFile(path)+ if err != nil {+ return 0, err+ }+ return strconv.ParseUint(strings.TrimSpace(string(data)), 10, 64)+}++func getNvidiaStatsInternal(ctx context.Context) ([]systemtypes.GPUStats, error) {+ ctx, cancel := context.WithTimeout(ctx, 3*time.Second)+ defer cancel()++ cmd := exec.CommandContext(ctx, "nvidia-smi",+ "--query-gpu=index,name,memory.used,memory.total",+ "--format=csv,noheader,nounits")++ output, err := cmd.Output()+ if err != nil {+ slog.WarnContext(ctx, "Failed to execute nvidia-smi", "error", err)+ return nil, fmt.Errorf("nvidia-smi execution failed: %w", err)+ }++ reader := csv.NewReader(bytes.NewReader(output))+ reader.TrimLeadingSpace = true+ records, err := reader.ReadAll()+ if err != nil {+ slog.WarnContext(ctx, "Failed to parse nvidia-smi CSV output", "error", err)+ return nil, fmt.Errorf("failed to parse nvidia-smi output: %w", err)+ }++ var stats []systemtypes.GPUStats+ for _, record := range records {+ if len(record) < 4 {+ continue+ }+ index, err := strconv.Atoi(strings.TrimSpace(record[0]))+ if err != nil {+ slog.WarnContext(ctx, "Failed to parse GPU index", "value", record[0])+ continue+ }+ memUsed, err := strconv.ParseFloat(strings.TrimSpace(record[2]), 64)+ if err != nil {+ slog.WarnContext(ctx, "Failed to parse memory used", "value", record[2])+ continue+ }+ memTotal, err := strconv.ParseFloat(strings.TrimSpace(record[3]), 64)+ if err != nil {+ slog.WarnContext(ctx, "Failed to parse memory total", "value", record[3])+ continue+ }+ stats = append(stats, systemtypes.GPUStats{+ Name: strings.TrimSpace(record[1]),+ Index: index,+ MemoryUsed: memUsed * 1024 * 1024,+ MemoryTotal: memTotal * 1024 * 1024,+ })+ }++ if len(stats) == 0 {+ return nil, fmt.Errorf("no GPU data parsed from nvidia-smi")+ }++ slog.DebugContext(ctx, "Collected NVIDIA GPU stats", "gpu_count", len(stats))+ return stats, nil+}++func getAMDStatsInternal(ctx context.Context) ([]systemtypes.GPUStats, error) {+ entries, err := os.ReadDir(AMDGPUSysfsPath)+ if err != nil {+ slog.WarnContext(ctx, "Failed to read DRM sysfs directory", "error", err)+ return nil, fmt.Errorf("failed to read sysfs: %w", err)+ }++ var stats []systemtypes.GPUStats+ index := 0+ for _, entry := range entries {+ name := entry.Name()+ if !strings.HasPrefix(name, "card") || strings.Contains(name, "-") {+ continue+ }++ devicePath := fmt.Sprintf("%s/%s/device", AMDGPUSysfsPath, name)+ memTotalBytes, err := readSysfsValueInternal(fmt.Sprintf("%s/mem_info_vram_total", devicePath))+ if err != nil {+ continue+ }+ memUsedBytes, err := readSysfsValueInternal(fmt.Sprintf("%s/mem_info_vram_used", devicePath))+ if err != nil {+ slog.WarnContext(ctx, "Failed to read AMD GPU memory used", "card", name, "error", err)+ continue+ }++ stats = append(stats, systemtypes.GPUStats{+ Name: fmt.Sprintf("AMD GPU %d", index),+ Index: index,+ MemoryUsed: float64(memUsedBytes),+ MemoryTotal: float64(memTotalBytes),+ })+ index+++ }++ if len(stats) == 0 {+ return nil, fmt.Errorf("no AMD GPU data found in sysfs")+ }++ slog.DebugContext(ctx, "Collected AMD GPU stats", "gpu_count", len(stats))+ return stats, nil+}++func getIntelStatsInternal(ctx context.Context) ([]systemtypes.GPUStats, error) {+ stats := []systemtypes.GPUStats{+ {Name: "Intel GPU", Index: 0, MemoryUsed: 0, MemoryTotal: 0},+ }+ slog.DebugContext(ctx, "Intel GPU detected but detailed stats not yet implemented")+ return stats, nil+}diff --git a/backend/pkg/pagination/pagination.go b/backend/pkg/pagination/pagination.goindex 503da8590..b61e428de 100644--- a/backend/pkg/pagination/pagination.go+++ b/backend/pkg/pagination/pagination.go@@ -3,8 +3,16 @@ package paginationtype PaginationParams struct {Start intLimit int+ // SkipCount opts out of the COUNT(*) query that backs TotalItems/TotalPages.+ // When true, paginated DB helpers return UnknownTotal (-1) for both fields and+ // the response is suitable for infinite-scroll UIs that don't render page numbers.+ // Default (false) preserves the historical behaviour for all existing callers.+ SkipCount bool}+// UnknownTotal is the sentinel returned in TotalItems/TotalPages when SkipCount is set.+const UnknownTotal int64 = -1+func paginateItemsFunction[T any](items []T, params PaginationParams) []T {if params.Limit <= 0 {return itemsdiff --git a/backend/pkg/pagination/reflect.go b/backend/pkg/pagination/reflect.goindex d1856526b..6e0a1a02d 100644--- a/backend/pkg/pagination/reflect.go+++ b/backend/pkg/pagination/reflect.go@@ -35,7 +35,7 @@ func PaginateAndSortDB(params QueryParams, query *gorm.DB, result any) (Responselimit := params.Limit// limit = -1 means "show all" - skip paginationif limit == -1 {- return paginateDBAll(query, result)+ return paginateDBAll(query, result, params.SkipCount)}if limit <= 0 {limit = 20@@ -48,20 +48,33 @@ func PaginateAndSortDB(params QueryParams, query *gorm.DB, result any) (Responsepage = (params.Start / limit) + 1}- return paginateDB(page, limit, query, result)+ return paginateDB(page, limit, query, result, params.SkipCount)}-// paginateDBAll returns all results without pagination limits-func paginateDBAll(query *gorm.DB, result any) (Response, error) {+// paginateDBAll returns all results without pagination limits.+// When skipCount is true the COUNT(*) is elided and TotalItems/TotalPages are returned as UnknownTotal.+// Count runs before Find so it sees a clean session (Find sets Statement.Dest in GORM v2).+func paginateDBAll(query *gorm.DB, result any, skipCount bool) (Response, error) {var totalItems int64- if err := query.Count(&totalItems).Error; err != nil {- return Response{}, err+ if !skipCount {+ if err := query.Count(&totalItems).Error; err != nil {+ return Response{}, err+ }}if err := query.Find(result).Error; err != nil {return Response{}, err}+ if skipCount {+ return Response{+ TotalPages: UnknownTotal,+ TotalItems: UnknownTotal,+ CurrentPage: 1,+ ItemsPerPage: 0,+ }, nil+ }+return Response{TotalPages: 1,TotalItems: totalItems,@@ -70,7 +83,10 @@ func paginateDBAll(query *gorm.DB, result any) (Response, error) {}, nil}-func paginateDB(page int, pageSize int, query *gorm.DB, result any) (Response, error) {+// paginateDB applies offset/limit pagination. When skipCount is true the COUNT(*) is elided+// and TotalItems/TotalPages are returned as UnknownTotal.+// Count runs before Find so it sees a clean session (Find sets Statement.Dest in GORM v2).+func paginateDB(page int, pageSize int, query *gorm.DB, result any, skipCount bool) (Response, error) {if page < 1 {page = 1}@@ -78,14 +94,25 @@ func paginateDB(page int, pageSize int, query *gorm.DB, result any) (Response, eoffset := (page - 1) * pageSizevar totalItems int64- if err := query.Count(&totalItems).Error; err != nil {- return Response{}, err+ if !skipCount {+ if err := query.Count(&totalItems).Error; err != nil {+ return Response{}, err+ }}if err := query.Offset(offset).Limit(pageSize).Find(result).Error; err != nil {return Response{}, err}+ if skipCount {+ return Response{+ TotalPages: UnknownTotal,+ TotalItems: UnknownTotal,+ CurrentPage: page,+ ItemsPerPage: pageSize,+ }, nil+ }+totalPages := (totalItems + int64(pageSize) - 1) / int64(pageSize)if totalItems == 0 {totalPages = 1diff --git a/backend/pkg/pagination/skipcount_test.go b/backend/pkg/pagination/skipcount_test.gonew file mode 100644index 000000000..7f07b20c8--- /dev/null+++ b/backend/pkg/pagination/skipcount_test.go@@ -0,0 +1,70 @@+package pagination++import (+ "testing"++ glsqlite "github.com/glebarez/sqlite"+ "github.com/stretchr/testify/require"+ "gorm.io/gorm"+ "gorm.io/gorm/logger"+)++type widget struct {+ ID string `gorm:"primaryKey"`+ Name string `sortable:"true"`+}++func newSkipCountTestDB(t *testing.T) *gorm.DB {+ t.Helper()+ db, err := gorm.Open(glsqlite.Open(":memory:"), &gorm.Config{Logger: logger.Default.LogMode(logger.Silent)})+ require.NoError(t, err)+ require.NoError(t, db.AutoMigrate(&widget{}))+ for i := range 5 {+ require.NoError(t, db.Create(&widget{ID: string(rune('a' + i)), Name: string(rune('A' + i))}).Error)+ }+ return db+}++func TestPaginateAndSortDB_SkipCountReturnsUnknownTotals(t *testing.T) {+ db := newSkipCountTestDB(t)++ var got []widget+ resp, err := PaginateAndSortDB(QueryParams{+ PaginationParams: PaginationParams{Start: 0, Limit: 2, SkipCount: true},+ SortParams: SortParams{Sort: "Name", Order: SortOrder("asc")},+ }, db.Model(&widget{}), &got)+ require.NoError(t, err)+ require.Len(t, got, 2)+ require.Equal(t, UnknownTotal, resp.TotalItems)+ require.Equal(t, UnknownTotal, resp.TotalPages)+ require.Equal(t, 1, resp.CurrentPage)+ require.Equal(t, 2, resp.ItemsPerPage)+}++func TestPaginateAndSortDB_DefaultStillCounts(t *testing.T) {+ db := newSkipCountTestDB(t)++ var got []widget+ resp, err := PaginateAndSortDB(QueryParams{+ PaginationParams: PaginationParams{Start: 0, Limit: 2},+ SortParams: SortParams{Sort: "Name", Order: SortOrder("asc")},+ }, db.Model(&widget{}), &got)+ require.NoError(t, err)+ require.Len(t, got, 2)+ require.Equal(t, int64(5), resp.TotalItems)+ require.Equal(t, int64(3), resp.TotalPages)+}++func TestPaginateAndSortDB_SkipCountShowAll(t *testing.T) {+ db := newSkipCountTestDB(t)++ var got []widget+ resp, err := PaginateAndSortDB(QueryParams{+ PaginationParams: PaginationParams{Start: 0, Limit: -1, SkipCount: true},+ SortParams: SortParams{Sort: "Name", Order: SortOrder("asc")},+ }, db.Model(&widget{}), &got)+ require.NoError(t, err)+ require.Len(t, got, 5)+ require.Equal(t, UnknownTotal, resp.TotalItems)+ require.Equal(t, UnknownTotal, resp.TotalPages)+}