Skip to content

Commit

Permalink
internal/instance: Allows the VM's limits.memory configuration to be …
Browse files Browse the repository at this point in the history
…set to a percentage value

Signed-off-by: XinJun Ma <xinjun.ma@qq.com>
  • Loading branch information
itviewer committed Oct 1, 2024
1 parent b579232 commit 88f5b0d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 33 deletions.
35 changes: 4 additions & 31 deletions internal/server/instance/drivers/driver_lxc.go
Original file line number Diff line number Diff line change
Expand Up @@ -1133,24 +1133,9 @@ func (d *lxc) initLXC(config bool) (*liblxc.Container, error) {

// Configure the memory limits
if memory != "" {
var valueInt int64
if strings.HasSuffix(memory, "%") {
percent, err := strconv.ParseInt(strings.TrimSuffix(memory, "%"), 10, 64)
if err != nil {
return nil, err
}

memoryTotal, err := linux.DeviceTotalMemory()
if err != nil {
return nil, err
}

valueInt = int64((memoryTotal / 100) * percent)
} else {
valueInt, err = units.ParseByteSizeString(memory)
if err != nil {
return nil, err
}
valueInt, err := ParseMemoryStr(memory)
if err != nil {
return nil, err
}

if memoryEnforce == "soft" {
Expand Down Expand Up @@ -4804,20 +4789,8 @@ func (d *lxc) Update(args db.InstanceArgs, userRequested bool) error {
// Parse memory
if memory == "" {
memoryInt = -1
} else if strings.HasSuffix(memory, "%") {
percent, err := strconv.ParseInt(strings.TrimSuffix(memory, "%"), 10, 64)
if err != nil {
return err
}

memoryTotal, err := linux.DeviceTotalMemory()
if err != nil {
return err
}

memoryInt = int64((memoryTotal / 100) * percent)
} else {
memoryInt, err = units.ParseByteSizeString(memory)
memoryInt, err = ParseMemoryStr(memory)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions internal/server/instance/drivers/driver_qemu.go
Original file line number Diff line number Diff line change
Expand Up @@ -1118,7 +1118,7 @@ func (d *qemu) checkStateStorage() error {
memoryLimitStr = d.expandedConfig["limits.memory"]
}

memoryLimit, err := units.ParseByteSizeString(memoryLimitStr)
memoryLimit, err := ParseMemoryStr(memoryLimitStr)
if err != nil {
return err
}
Expand Down Expand Up @@ -3821,7 +3821,7 @@ func (d *qemu) addCPUMemoryConfig(cfg *[]cfgSection, cpuInfo *cpuTopology) error
memSize = qemudefault.MemSize // Default if no memory limit specified.
}

memSizeBytes, err := units.ParseByteSizeString(memSize)
memSizeBytes, err := ParseMemoryStr(memSize)
if err != nil {
return fmt.Errorf("limits.memory invalid: %w", err)
}
Expand Down
27 changes: 27 additions & 0 deletions internal/server/instance/drivers/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@ import (
"io/fs"
"os"
"slices"
"strconv"
"strings"

"github.com/lxc/incus/v6/internal/linux"
"github.com/lxc/incus/v6/internal/server/db"
"github.com/lxc/incus/v6/internal/server/instance/instancetype"
"github.com/lxc/incus/v6/internal/server/state"
internalUtil "github.com/lxc/incus/v6/internal/util"
"github.com/lxc/incus/v6/shared/api"
"github.com/lxc/incus/v6/shared/units"
)

// GetClusterCPUFlags returns the list of shared CPU flags across.
Expand Down Expand Up @@ -99,3 +103,26 @@ func GetClusterCPUFlags(ctx context.Context, s *state.State, servers []string, a

return flags, nil
}

// ParseMemoryStr parses a human representation of memory value as int64 type.
func ParseMemoryStr(memory string) (valueInt int64, err error) {
if strings.HasSuffix(memory, "%") {
var percent, memoryTotal int64

percent, err = strconv.ParseInt(strings.TrimSuffix(memory, "%"), 10, 64)
if err != nil {
return 0, err
}

memoryTotal, err = linux.DeviceTotalMemory()
if err != nil {
return 0, err
}

valueInt = (memoryTotal / 100) * percent
} else {
valueInt, err = units.ParseByteSizeString(memory)
}

return valueInt, err
}

0 comments on commit 88f5b0d

Please sign in to comment.