From 27906ae23668d8e28c6620bdb63e5a5f36be1d9c Mon Sep 17 00:00:00 2001 From: illustris Date: Tue, 14 Apr 2026 01:34:02 +0530 Subject: [PATCH] fix ParseMem to handle hotplug memory format --- src/internal/procfs/procfs.go | 17 +++++++++++++++-- src/internal/procfs/procfs_test.go | 18 ++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/internal/procfs/procfs.go b/src/internal/procfs/procfs.go index 116259f..2d07659 100644 --- a/src/internal/procfs/procfs.go +++ b/src/internal/procfs/procfs.go @@ -255,6 +255,7 @@ func ParseVcores(cmdline []string) int { // ParseMem extracts max memory in kB from cmdline. // Simple: -m 1024 -> 1024*1024 kB +// Hotplug: -m size=4096M,slots=255,maxmem=524288M -> parse size= key // NUMA: memory-backend-ram...size=NM -> sum * 1024 kB func ParseMem(cmdline []string) int64 { mVal := FlagValue(cmdline, "-m") @@ -265,11 +266,23 @@ func ParseMem(cmdline []string) int64 { if n, err := strconv.ParseInt(mVal, 10, 64); err == nil { return n * 1024 // MB to kB } - // NUMA case: search for memory-backend-ram in all args + // Hotplug/NUMA: -m size=NM,slots=N,maxmem=NM + // Parse the size= key from the -m value itself + for _, part := range strings.Split(mVal, ",") { + if strings.HasPrefix(part, "size=") { + sizeStr := strings.TrimPrefix(part, "size=") + if strings.HasSuffix(sizeStr, "M") { + sizeStr = strings.TrimSuffix(sizeStr, "M") + if n, err := strconv.ParseInt(sizeStr, 10, 64); err == nil { + return n * 1024 // MB to kB + } + } + } + } + // NUMA fallback: sum memory-backend-ram object sizes var total int64 for _, arg := range cmdline { if strings.Contains(arg, "memory-backend-ram") { - // Format: ...size=XXXM for _, part := range strings.Split(arg, ",") { if strings.HasPrefix(part, "size=") { sizeStr := strings.TrimPrefix(part, "size=") diff --git a/src/internal/procfs/procfs_test.go b/src/internal/procfs/procfs_test.go index 683fb36..95c508f 100644 --- a/src/internal/procfs/procfs_test.go +++ b/src/internal/procfs/procfs_test.go @@ -93,6 +93,24 @@ func TestParseMem(t *testing.T) { }, 4096 * 1024, // 2048+2048 MB in kB }, + { + "hotplug without numa", + []string{"-m", "size=131072M,slots=255,maxmem=524288M"}, + 131072 * 1024, + }, + { + "large vm simple", + []string{ + "/usr/bin/kvm", "-id", "106", + "-name", "vm-106,debug-threads=on", + "-smp", "32,sockets=1,cores=32,maxcpus=32", + "-cpu", "host,+kvm_pv_eoi,+kvm_pv_unhalt", + "-m", "131072", + "-object", "iothread,id=iothread-virtio0", + "-machine", "type=q35+pve1", + }, + 131072 * 1024, + }, { "missing", []string{"-smp", "4"},