diff --git a/pkg/xsysinfo/gpu.go b/pkg/xsysinfo/gpu.go index 8ed91ba67..02b4186ac 100644 --- a/pkg/xsysinfo/gpu.go +++ b/pkg/xsysinfo/gpu.go @@ -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() diff --git a/pkg/xsysinfo/memory.go b/pkg/xsysinfo/memory.go new file mode 100644 index 000000000..fe4e6edd9 --- /dev/null +++ b/pkg/xsysinfo/memory.go @@ -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 +}