mirror of
https://github.com/opencontainers/runc.git
synced 2026-07-11 06:03:57 +08:00
libct/cg/sd/v2: support cpu.max unified resource
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
This commit is contained in:
@@ -45,7 +45,9 @@ func NewUnifiedManager(config *configs.Cgroup, path string, rootless bool) cgrou
|
||||
// For the list of keys, see https://www.kernel.org/doc/Documentation/cgroup-v2.txt
|
||||
//
|
||||
// For the list of systemd unit properties, see systemd.resource-control(5).
|
||||
func unifiedResToSystemdProps(res map[string]string) (props []systemdDbus.Property, _ error) {
|
||||
func unifiedResToSystemdProps(conn *systemdDbus.Conn, res map[string]string) (props []systemdDbus.Property, _ error) {
|
||||
var err error
|
||||
|
||||
for k, v := range res {
|
||||
if strings.Contains(k, "/") {
|
||||
return nil, fmt.Errorf("unified resource %q must be a file name (no slashes)", k)
|
||||
@@ -57,7 +59,32 @@ func unifiedResToSystemdProps(res map[string]string) (props []systemdDbus.Proper
|
||||
// Kernel is quite forgiving to extra whitespace
|
||||
// around the value, and so should we.
|
||||
v = strings.TrimSpace(v)
|
||||
// Please keep cases in alphabetical order.
|
||||
switch k {
|
||||
case "cpu.max":
|
||||
// value: quota [period]
|
||||
quota := int64(0) // 0 means "unlimited" for addCpuQuota, if period is set
|
||||
period := defCPUQuotaPeriod
|
||||
sv := strings.Fields(v)
|
||||
if len(sv) < 1 || len(sv) > 2 {
|
||||
return nil, fmt.Errorf("unified resource %q value invalid: %q", k, v)
|
||||
}
|
||||
// quota
|
||||
if sv[0] != "max" {
|
||||
quota, err = strconv.ParseInt(sv[0], 10, 64)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unified resource %q period value conversion error: %w", k, err)
|
||||
}
|
||||
}
|
||||
// period
|
||||
if len(sv) == 2 {
|
||||
period, err = strconv.ParseUint(sv[1], 10, 64)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unified resource %q quota value conversion error: %w", k, err)
|
||||
}
|
||||
}
|
||||
addCpuQuota(conn, &props, quota, period)
|
||||
|
||||
case "pids.max":
|
||||
num := uint64(math.MaxUint64)
|
||||
if v != "max" {
|
||||
@@ -131,7 +158,7 @@ func genV2ResourcesProperties(c *configs.Cgroup, conn *systemdDbus.Conn) ([]syst
|
||||
|
||||
// convert Resources.Unified map to systemd properties
|
||||
if r.Unified != nil {
|
||||
unifiedProps, err := unifiedResToSystemdProps(r.Unified)
|
||||
unifiedProps, err := unifiedResToSystemdProps(conn, r.Unified)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -218,6 +218,7 @@ function setup() {
|
||||
echo "$output" | grep -q '^cpu.max:10000 100000$'
|
||||
|
||||
check_systemd_value "TasksMax" 99
|
||||
check_cpu_quota 10000 100000 "100ms"
|
||||
}
|
||||
|
||||
@test "runc run (cgroup v2 resources.unified override)" {
|
||||
@@ -226,10 +227,15 @@ function setup() {
|
||||
set_cgroups_path "$BUSYBOX_BUNDLE"
|
||||
update_config ' .linux.resources.memory |= {"limit": 33554432}
|
||||
| .linux.resources.memorySwap |= {"limit": 33554432}
|
||||
| .linux.resources.cpu |= {
|
||||
"quota": 40000,
|
||||
"period": 100000
|
||||
}
|
||||
| .linux.resources.unified |= {
|
||||
"memory.min": "131072",
|
||||
"memory.max": "10485760",
|
||||
"pids.max": "42"
|
||||
"pids.max": "42",
|
||||
"cpu.max": "5000 50000"
|
||||
}' "$BUSYBOX_BUNDLE"
|
||||
|
||||
runc run -d --console-socket "$CONSOLE_SOCKET" test_cgroups_unified
|
||||
@@ -247,4 +253,6 @@ function setup() {
|
||||
[ "$status" -eq 0 ]
|
||||
[ "$output" = '42' ]
|
||||
check_systemd_value "TasksMax" 42
|
||||
|
||||
check_cpu_quota 5000 50000 "100ms"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user