Skip to content

Commit

Permalink
write back cpu.max.burst after cpu.max modified in cgroup v2
Browse files Browse the repository at this point in the history
If the burst is not set, we need to read the current
value to write it back after `cpu.max` modified.

Signed-off-by: lifubang <lifubang@acmcoder.com>
  • Loading branch information
lifubang committed Mar 15, 2024
1 parent 60f312d commit 7823197
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
18 changes: 18 additions & 0 deletions libcontainer/cgroups/fs2/cpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package fs2
import (
"bufio"
"errors"
"fmt"
"os"
"strconv"
"strings"

"golang.org/x/sys/unix"

Expand Down Expand Up @@ -35,6 +37,22 @@ func setCpu(dirPath string, r *configs.Resources) error {
}
}

// If the burst is not set, we need to read the current
// value to write it back after `cpu.max` modified.
if (r.CpuQuota != 0 || r.CpuPeriod != 0) && r.CpuBurst == nil {
burst, _ := cgroups.ReadFile(dirPath, "cpu.max.burst")
if burst != "" {
v, err := strconv.ParseUint(strings.TrimSpace(burst), 10, 64)
if err != nil {
return fmt.Errorf("invalid value for cpu.max.burst: %w", err)
}
if v != 0 {
v *= 1000
r.CpuBurst = &v
}
}
}

var burst string
if r.CpuBurst != nil {
burst = strconv.FormatUint(*r.CpuBurst, 10)
Expand Down
16 changes: 16 additions & 0 deletions libcontainer/cgroups/systemd/v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,22 @@ func (m *UnifiedManager) Set(r *configs.Resources) error {
return err
}

// If the burst is not set, we need to read the current
// value to write it back after `cpu.max` modified.
if (r.CpuQuota != 0 || r.CpuPeriod != 0) && r.CpuBurst == nil {
burst, _ := cgroups.ReadFile(m.path, "cpu.max.burst")
if burst != "" {
v, err := strconv.ParseUint(strings.TrimSpace(burst), 10, 64)
if err != nil {
return fmt.Errorf("invalid value for cpu.max.burst: %w", err)
}
if v != 0 {
v *= 1000
r.CpuBurst = &v
}
}
}

if err := setUnitProperties(m.dbus, getUnitName(m.cgroups), properties...); err != nil {
return fmt.Errorf("unable to set unit properties: %w", err)
}
Expand Down

0 comments on commit 7823197

Please sign in to comment.