fix(ram): do not read from cgroup (#7606)

Signed-off-by: Ettore Di Giacinto <mudler@localai.io>
This commit is contained in:
Ettore Di Giacinto
2025-12-16 13:28:11 +01:00
committed by GitHub
parent 67baf66555
commit e3e5f59965
2 changed files with 47 additions and 49 deletions

View File

@@ -3,13 +3,11 @@ package xsysinfo
import (
"bytes"
"encoding/json"
"fmt"
"os/exec"
"strconv"
"strings"
"sync"
sigar "github.com/cloudfoundry/gosigar"
"github.com/jaypipes/ghw"
"github.com/jaypipes/ghw/pkg/gpu"
"github.com/rs/zerolog/log"
@@ -53,15 +51,6 @@ type GPUAggregateInfo struct {
GPUCount int `json:"gpu_count"`
}
// SystemRAMInfo contains system RAM usage information
type SystemRAMInfo struct {
Total uint64 `json:"total"`
Used uint64 `json:"used"`
Free uint64 `json:"free"`
Available uint64 `json:"available"`
UsagePercent float64 `json:"usage_percent"`
}
// AggregateMemoryInfo contains aggregate memory information (unified for GPU/RAM)
type AggregateMemoryInfo struct {
TotalMemory uint64 `json:"total_memory"`
@@ -144,21 +133,6 @@ func isUnifiedMemoryDevice(gpuName string) bool {
return false
}
// getSystemRAM returns system RAM information using ghw
func getSystemRAM() (total, used, free uint64, err error) {
mem := sigar.Mem{}
//swap := sigar.Swap{}
mem.Get() //nolint:errcheck
//swap.Get() //nolint:errcheck
total = mem.Total
free = mem.ActualFree
used = mem.ActualUsed
return total, used, free, nil
}
// GetGPUMemoryUsage returns real-time GPU memory usage for all detected GPUs.
// It tries multiple vendor-specific tools in order: NVIDIA, AMD, Intel, Vulkan.
// Returns an empty slice if no GPU monitoring tools are available.
@@ -558,29 +532,6 @@ func getIntelGPUTop() []GPUMemoryInfo {
return nil
}
// GetSystemRAMInfo returns real-time system RAM usage
func GetSystemRAMInfo() (*SystemRAMInfo, error) {
total, used, free, err := getSystemRAM()
if err != nil {
return nil, err
}
usagePercent := 0.0
if total > 0 {
usagePercent = float64(used) / float64(total) * 100
}
fmt.Println("total", total, "used", used, "free", free)
return &SystemRAMInfo{
Total: total,
Used: used,
Free: free,
Available: total - used,
UsagePercent: usagePercent,
}, nil
}
// GetResourceInfo returns GPU info if available, otherwise system RAM info
func GetResourceInfo() ResourceInfo {
gpus := GetGPUMemoryUsage()

47
pkg/xsysinfo/memory.go Normal file
View File

@@ -0,0 +1,47 @@
package xsysinfo
import (
sigar "github.com/cloudfoundry/gosigar"
"github.com/rs/zerolog/log"
)
// SystemRAMInfo contains system RAM usage information
type SystemRAMInfo struct {
Total uint64 `json:"total"`
Used uint64 `json:"used"`
Free uint64 `json:"free"`
Available uint64 `json:"available"`
UsagePercent float64 `json:"usage_percent"`
}
// GetSystemRAMInfo returns real-time system RAM usage
func GetSystemRAMInfo() (*SystemRAMInfo, error) {
total, used, free, err := getSystemRAM()
if err != nil {
return nil, err
}
usagePercent := 0.0
if total > 0 {
usagePercent = float64(used) / float64(total) * 100
}
log.Debug().Uint64("total", total).Uint64("used", used).Uint64("free", free).Float64("usage_percent", usagePercent).Msg("System RAM Info")
return &SystemRAMInfo{
Total: total,
Used: used,
Free: free,
Available: total - used,
UsagePercent: usagePercent,
}, nil
}
// getSystemRAM returns system RAM information using ghw
func getSystemRAM() (total, used, free uint64, err error) {
mem := sigar.Mem{}
if err := mem.GetIgnoringCGroups(); err != nil {
return 0, 0, 0, err
}
return mem.Total, mem.ActualUsed, mem.ActualFree, nil
}