incusd/cgroup: Add swap accounting detection

Signed-off-by: Stéphane Graber <stgraber@stgraber.org>
This commit is contained in:
Stéphane Graber 2026-07-27 15:02:07 -04:00
parent 9416a0367d
commit 89c6a76a3c
No known key found for this signature in database
GPG Key ID: C638974D64792D67
2 changed files with 24 additions and 3 deletions

View File

@ -153,7 +153,7 @@ func (cg *CGroup) GetProcessesUsage() (int64, error) {
// SetMemorySwapLimit sets the hard limit for swap.
func (cg *CGroup) SetMemorySwapLimit(limit int64) error {
if !cgControllers["memory"] {
if !cgControllers["memory.swap"] {
return ErrControllerMissing
}
@ -314,7 +314,7 @@ func (cg *CGroup) SetMemorySwappiness(limit int64) error {
// GetMemorySwapLimit returns the hard limit on swap usage.
func (cg *CGroup) GetMemorySwapLimit() (int64, error) {
if !cgControllers["memory"] {
if !cgControllers["memory.swap"] {
return -1, ErrControllerMissing
}
@ -337,7 +337,7 @@ func (cg *CGroup) GetMemorySwapLimit() (int64, error) {
// GetMemorySwapUsage return current usage of swap.
func (cg *CGroup) GetMemorySwapUsage() (int64, error) {
if !cgControllers["memory"] {
if !cgControllers["memory.swap"] {
return -1, ErrControllerMissing
}

View File

@ -11,6 +11,7 @@ import (
"github.com/lxc/incus/v7/internal/server/db/cluster"
"github.com/lxc/incus/v7/internal/server/db/warningtype"
"github.com/lxc/incus/v7/shared/logger"
"github.com/lxc/incus/v7/shared/util"
)
var cgControllers = map[string]bool{}
@ -37,6 +38,9 @@ const (
// Memory resource control.
Memory
// MemorySwap resource control.
MemorySwap
// Pids resource control.
Pids
)
@ -54,6 +58,8 @@ func Supports(resource Resource) bool {
return cgControllers["io"]
case Memory:
return cgControllers["memory"]
case MemorySwap:
return cgControllers["memory.swap"]
case Pids:
return cgControllers["pids"]
}
@ -98,6 +104,11 @@ func Warnings() []cluster.Warning {
TypeCode: warningtype.MissingCGroupMemoryController,
LastMessage: "memory limits will be ignored",
})
} else if !Supports(MemorySwap) {
warnings = append(warnings, cluster.Warning{
TypeCode: warningtype.MissingCGroupMemorySwapAccounting,
LastMessage: "swap limits will be ignored",
})
}
if !Supports(Pids) {
@ -156,5 +167,15 @@ func Init() {
}
_ = controllers.Close()
// Check for swap accounting support (can be disabled through kernel arguments).
if cgControllers["memory"] {
for _, path := range []string{filepath.Join(cgPath, fields[2]), filepath.Join(cgPath, "init.scope")} {
if util.PathExists(filepath.Join(path, "memory.swap.current")) {
cgControllers["memory.swap"] = true
break
}
}
}
}
}