Skip to content

Commit 878c9d4

Browse files
authored
fix: improve ram estimation (#7603)
* fix: default to 10seconds of watchdog if runtime setting is malformed Signed-off-by: Ettore Di Giacinto <[email protected]> * fix: use gosigar for RAM estimation Signed-off-by: Ettore Di Giacinto <[email protected]> --------- Signed-off-by: Ettore Di Giacinto <[email protected]>
1 parent b841a49 commit 878c9d4

File tree

4 files changed

+21
-67
lines changed

4 files changed

+21
-67
lines changed

core/application/startup.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@ func loadRuntimeSettingsFromFile(options *config.ApplicationConfig) {
278278
options.WatchDogInterval = dur
279279
} else {
280280
log.Warn().Err(err).Str("interval", *settings.WatchdogInterval).Msg("invalid watchdog interval in runtime_settings.json")
281+
options.WatchDogInterval = 10 * time.Second
281282
}
282283
}
283284
}

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ require (
6262
)
6363

6464
require (
65+
github.com/cloudfoundry/gosigar v1.3.112 // indirect
6566
github.com/ghodss/yaml v1.0.0 // indirect
6667
github.com/labstack/gommon v0.4.2 // indirect
6768
github.com/swaggo/files/v2 v2.0.2 // indirect
@@ -203,7 +204,7 @@ require (
203204
github.com/google/btree v1.1.3 // indirect
204205
github.com/google/go-cmp v0.7.0 // indirect
205206
github.com/google/gopacket v1.1.19 // indirect
206-
github.com/google/pprof v0.0.0-20250403155104-27863c87afa6 // indirect
207+
github.com/google/pprof v0.0.0-20250630185457-6e76a2b096b5 // indirect
207208
github.com/gorilla/css v1.0.1 // indirect
208209
github.com/gorilla/websocket v1.5.3
209210
github.com/hashicorp/golang-lru v1.0.2 // indirect

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ github.com/charmbracelet/x/term v0.2.1/go.mod h1:oQ4enTYFV7QN4m0i9mzHrViD7TQKvNE
8383
github.com/chengxilo/virtualterm v1.0.4 h1:Z6IpERbRVlfB8WkOmtbHiDbBANU7cimRIof7mk9/PwM=
8484
github.com/chengxilo/virtualterm v1.0.4/go.mod h1:DyxxBZz/x1iqJjFxTFcr6/x+jSpqN0iwWCOK1q10rlY=
8585
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
86+
github.com/cloudfoundry/gosigar v1.3.112 h1:cGGZ2sj1GKyiwSxzouIR7ATNbgAkC4zqwWDxYQ2ObPc=
87+
github.com/cloudfoundry/gosigar v1.3.112/go.mod h1:Ldc+tVw3dfqPwasZ9om1LT2aRwpjC1eFfbWKfv2WbDI=
8688
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
8789
github.com/containerd/cgroups v1.1.0 h1:v8rEWFl6EoqHB+swVNjVoCJE8o3jX7e8nqBGPLaDFBM=
8890
github.com/containerd/cgroups v1.1.0/go.mod h1:6ppBcbh/NOOUU+dMKrykgaBnK9lCIBxHqJDGwsa1mIw=
@@ -275,6 +277,8 @@ github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXi
275277
github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
276278
github.com/google/pprof v0.0.0-20250403155104-27863c87afa6 h1:BHT72Gu3keYf3ZEu2J0b1vyeLSOYI8bm5wbJM/8yDe8=
277279
github.com/google/pprof v0.0.0-20250403155104-27863c87afa6/go.mod h1:boTsfXsheKC2y+lKOCMpSfarhxDeIzfZG1jqGcPl3cA=
280+
github.com/google/pprof v0.0.0-20250630185457-6e76a2b096b5 h1:xhMrHhTJ6zxu3gA4enFM9MLn9AY7613teCdFnlUVbSQ=
281+
github.com/google/pprof v0.0.0-20250630185457-6e76a2b096b5/go.mod h1:5hDyRhoBCxViHszMt12TnOpEI4VVi+U8Gm9iphldiMA=
278282
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
279283
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
280284
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=

pkg/xsysinfo/gpu.go

Lines changed: 14 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ package xsysinfo
33
import (
44
"bytes"
55
"encoding/json"
6+
"fmt"
67
"os/exec"
78
"strconv"
89
"strings"
910
"sync"
1011

12+
sigar "github.com/cloudfoundry/gosigar"
1113
"github.com/jaypipes/ghw"
1214
"github.com/jaypipes/ghw/pkg/gpu"
1315
"github.com/rs/zerolog/log"
@@ -144,17 +146,15 @@ func isUnifiedMemoryDevice(gpuName string) bool {
144146

145147
// getSystemRAM returns system RAM information using ghw
146148
func getSystemRAM() (total, used, free uint64, err error) {
147-
memory, err := ghw.Memory()
148-
if err != nil {
149-
return 0, 0, 0, err
150-
}
149+
mem := sigar.Mem{}
150+
//swap := sigar.Swap{}
151151

152-
total = uint64(memory.TotalUsableBytes)
153-
// ghw doesn't provide used/free directly, but we can estimate
154-
// For unified memory GPUs, we report total system RAM as available VRAM
155-
// since the GPU can potentially use all of it
156-
free = total
157-
used = 0
152+
mem.Get() //nolint:errcheck
153+
//swap.Get() //nolint:errcheck
154+
155+
total = mem.Total
156+
free = mem.ActualFree
157+
used = mem.ActualUsed
158158

159159
return total, used, free, nil
160160
}
@@ -560,79 +560,27 @@ func getIntelGPUTop() []GPUMemoryInfo {
560560

561561
// GetSystemRAMInfo returns real-time system RAM usage
562562
func GetSystemRAMInfo() (*SystemRAMInfo, error) {
563-
memory, err := ghw.Memory()
563+
total, used, free, err := getSystemRAM()
564564
if err != nil {
565565
return nil, err
566566
}
567567

568-
total := uint64(memory.TotalUsableBytes)
569-
570-
// Try to get more accurate memory info from /proc/meminfo on Linux
571-
used, available, free := getDetailedMemoryInfo(total)
572-
573568
usagePercent := 0.0
574569
if total > 0 {
575570
usagePercent = float64(used) / float64(total) * 100
576571
}
577572

573+
fmt.Println("total", total, "used", used, "free", free)
574+
578575
return &SystemRAMInfo{
579576
Total: total,
580577
Used: used,
581578
Free: free,
582-
Available: available,
579+
Available: total - used,
583580
UsagePercent: usagePercent,
584581
}, nil
585582
}
586583

587-
// getDetailedMemoryInfo tries to get detailed memory info from /proc/meminfo on Linux
588-
// Returns used, available, and free memory in bytes
589-
func getDetailedMemoryInfo(total uint64) (used, available, free uint64) {
590-
// Try to read /proc/meminfo for more accurate data
591-
cmd := exec.Command("cat", "/proc/meminfo")
592-
var stdout bytes.Buffer
593-
cmd.Stdout = &stdout
594-
595-
if err := cmd.Run(); err != nil {
596-
// Fallback: assume all memory is available
597-
return 0, total, total
598-
}
599-
600-
lines := strings.Split(stdout.String(), "\n")
601-
memInfo := make(map[string]uint64)
602-
603-
for _, line := range lines {
604-
parts := strings.Fields(line)
605-
if len(parts) < 2 {
606-
continue
607-
}
608-
key := strings.TrimSuffix(parts[0], ":")
609-
value, err := strconv.ParseUint(parts[1], 10, 64)
610-
if err != nil {
611-
continue
612-
}
613-
// Values in /proc/meminfo are in kB
614-
memInfo[key] = value * 1024
615-
}
616-
617-
// Get MemAvailable if present (preferred), otherwise calculate from free + buffers + cached
618-
if avail, ok := memInfo["MemAvailable"]; ok {
619-
available = avail
620-
} else {
621-
available = memInfo["MemFree"] + memInfo["Buffers"] + memInfo["Cached"]
622-
}
623-
624-
free = memInfo["MemFree"]
625-
626-
// Calculate used memory
627-
if total > available {
628-
used = total - available
629-
} else {
630-
used = 0
631-
}
632-
633-
return used, available, free
634-
}
635-
636584
// GetResourceInfo returns GPU info if available, otherwise system RAM info
637585
func GetResourceInfo() ResourceInfo {
638586
gpus := GetGPUMemoryUsage()

0 commit comments

Comments
 (0)