Skip to content

Commit

Permalink
Merge pull request #692 from stgraber/cgroup
Browse files Browse the repository at this point in the history
Allow byte values in `limits.memory.swap`
  • Loading branch information
tych0 authored Mar 28, 2024
2 parents 0ba8d1e + fc18bb7 commit b965408
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 37 deletions.
3 changes: 3 additions & 0 deletions doc/api-extensions.md
Original file line number Diff line number Diff line change
Expand Up @@ -2430,3 +2430,6 @@ Those integrations attach to network peers through some new fields:

* `type` (`local` for current behavior, `remote` for integrations)
* `target_integration` (reference to the integration)

## `instance_memory_swap_bytes`
This extends `limits.memory.swap` to allow for a total limit in bytes.
8 changes: 5 additions & 3 deletions doc/config_options.txt
Original file line number Diff line number Diff line change
Expand Up @@ -439,9 +439,11 @@ If this option is set to `false`, regular system memory is used.
:condition: "container"
:defaultdesc: "`true`"
:liveupdate: "yes"
:shortdesc: "Whether to encourage/discourage swapping less used pages for this instance"
:type: "bool"

:shortdesc: "Control swap usage by the instance"
:type: "string"
When set to `true` or `false`, it controls whether the container is likely to get some of
its memory swapped by the kernel. Alternatively, it can be set to a bytes value which will
then allow the container to make use of additional memory through swap.
```

```{config:option} limits.memory.swap.priority instance-resource-limits
Expand Down
10 changes: 6 additions & 4 deletions internal/instance/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -506,14 +506,16 @@ var InstanceConfigKeysContainer = map[string]func(value string) error{
"limits.memory.enforce": validate.Optional(validate.IsOneOf("soft", "hard")),

// gendoc:generate(entity=instance, group=resource-limits, key=limits.memory.swap)
//
// When set to `true` or `false`, it controls whether the container is likely to get some of
// its memory swapped by the kernel. Alternatively, it can be set to a bytes value which will
// then allow the container to make use of additional memory through swap.
// ---
// type: bool
// type: string
// defaultdesc: `true`
// liveupdate: yes
// condition: container
// shortdesc: Whether to encourage/discourage swapping less used pages for this instance
"limits.memory.swap": validate.Optional(validate.IsBool),
// shortdesc: Control swap usage by the instance
"limits.memory.swap": validate.Optional(validate.Or(validate.IsBool, validate.IsSize)),

// gendoc:generate(entity=instance, group=resource-limits, key=limits.memory.swap.priority)
// Specify an integer between 0 and 10.
Expand Down
55 changes: 28 additions & 27 deletions internal/server/instance/drivers/driver_lxc.go
Original file line number Diff line number Diff line change
Expand Up @@ -1155,27 +1155,28 @@ func (d *lxc) initLXC(config bool) (*liblxc.Container, error) {
return nil, err
}
} else {
if d.state.OS.CGInfo.Supports(cgroup.MemorySwap, cg) {
err = cg.SetMemoryLimit(valueInt)
if err != nil {
return nil, err
}
err = cg.SetMemoryLimit(valueInt)
if err != nil {
return nil, err
}

if util.IsFalse(memorySwap) {
if d.state.OS.CGInfo.Supports(cgroup.MemorySwap, cg) {
if util.IsTrueOrEmpty(memorySwap) || util.IsFalse(memorySwap) {
err = cg.SetMemorySwapLimit(0)
if err != nil {
return nil, err
}
} else {
err = cg.SetMemorySwapLimit(valueInt)
// Additional memory as swap.
swapInt, err := units.ParseByteSizeString(memorySwap)
if err != nil {
return nil, err
}

err = cg.SetMemorySwapLimit(swapInt)
if err != nil {
return nil, err
}
}
} else {
err = cg.SetMemoryLimit(valueInt)
if err != nil {
return nil, err
}
}

Expand Down Expand Up @@ -4624,31 +4625,32 @@ func (d *lxc) Update(args db.InstanceArgs, userRequested bool) error {
return err
}
} else {
if d.state.OS.CGInfo.Supports(cgroup.MemorySwap, cg) {
err = cg.SetMemoryLimit(memoryInt)
if err != nil {
revertMemory()
return err
}
err = cg.SetMemoryLimit(memoryInt)
if err != nil {
revertMemory()
return err
}

if util.IsFalse(memorySwap) {
if d.state.OS.CGInfo.Supports(cgroup.MemorySwap, cg) {
if util.IsTrueOrEmpty(memorySwap) || util.IsFalse(memorySwap) {
err = cg.SetMemorySwapLimit(0)
if err != nil {
revertMemory()
return err
}
} else {
err = cg.SetMemorySwapLimit(memoryInt)
// Additional memory as swap.
swapInt, err := units.ParseByteSizeString(memorySwap)
if err != nil {
revertMemory()
return err
}

err = cg.SetMemorySwapLimit(swapInt)
if err != nil {
revertMemory()
return err
}
}
} else {
err = cg.SetMemoryLimit(memoryInt)
if err != nil {
revertMemory()
return err
}
}

Expand All @@ -4666,7 +4668,6 @@ func (d *lxc) Update(args db.InstanceArgs, userRequested bool) error {

// Configure the swappiness
if key == "limits.memory.swap" || key == "limits.memory.swap.priority" {
memorySwap := d.expandedConfig["limits.memory.swap"]
memorySwapPriority := d.expandedConfig["limits.memory.swap.priority"]
if util.IsFalse(memorySwap) {
err = cg.SetMemorySwappiness(0)
Expand Down
6 changes: 3 additions & 3 deletions internal/server/metadata/configuration.json
Original file line number Diff line number Diff line change
Expand Up @@ -453,9 +453,9 @@
"condition": "container",
"defaultdesc": "`true`",
"liveupdate": "yes",
"longdesc": "",
"shortdesc": "Whether to encourage/discourage swapping less used pages for this instance",
"type": "bool"
"longdesc": "When set to `true` or `false`, it controls whether the container is likely to get some of\nits memory swapped by the kernel. Alternatively, it can be set to a bytes value which will\nthen allow the container to make use of additional memory through swap.",
"shortdesc": "Control swap usage by the instance",
"type": "string"
}
},
{
Expand Down
1 change: 1 addition & 0 deletions internal/server/project/permissions.go
Original file line number Diff line number Diff line change
Expand Up @@ -836,6 +836,7 @@ func isContainerLowLevelOptionForbidden(key string) bool {
"boot.host_shutdown_action",
"boot.host_shutdown_timeout",
"linux.kernel_modules",
"limits.memory.swap",
"raw.apparmor",
"raw.idmap",
"raw.lxc",
Expand Down
1 change: 1 addition & 0 deletions internal/version/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,7 @@ var APIExtensions = []string{
"numa_cpu_balanced",
"image_restriction_nesting",
"network_integrations",
"instance_memory_swap_bytes",
}

// APIExtensionsCount returns the number of available API extensions.
Expand Down

0 comments on commit b965408

Please sign in to comment.