Skip to content

Commit

Permalink
fs2: fallback to setting io.weight if io.bfq.weight
Browse files Browse the repository at this point in the history
if bfq is not loaded, then io.bfq.weight is not available. io.weight
should always be available and is the next best equivalent thing.

Signed-off-by: Daniel Dao <dqminh89@gmail.com>
  • Loading branch information
dqminh committed Feb 24, 2021
1 parent c153261 commit 669f92b
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 3 deletions.
11 changes: 10 additions & 1 deletion libcontainer/cgroups/fs2/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,16 @@ func setIo(dirPath string, cgroup *configs.Cgroup) error {
filename := "io.bfq.weight"
if err := fscommon.WriteFile(dirPath, filename,
strconv.FormatUint(uint64(cgroup.Resources.BlkioWeight), 10)); err != nil {
return err
// if io.bfq.weight does not exist, then bfq module is not loaded.
// Fallback to use io.weight with a conversion scheme
if os.IsNotExist(err) {
v := cgroups.ConvertBlkIOToIOWeightValue(cgroup.Resources.BlkioWeight)
if err := fscommon.WriteFile(dirPath, "io.weight", strconv.FormatUint(v, 10)); err != nil {
return err
}
} else {
return err
}
}
}
for _, td := range cgroup.Resources.BlkioThrottleReadBpsDevice {
Expand Down
11 changes: 11 additions & 0 deletions libcontainer/cgroups/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -439,3 +439,14 @@ func ConvertMemorySwapToCgroupV2Value(memorySwap, memory int64) (int64, error) {

return memorySwap - memory, nil
}

// Since the OCI spec is designed for cgroup v1, in some cases
// there is need to convert from the cgroup v1 configuration to cgroup v2
// the formula for BlkIOWeight to IOWeight is y = (1 + (x - 10) * 9999 / 990)
// convert linearly from [10-1000] to [1-10000]
func ConvertBlkIOToIOWeightValue(blkIoWeight uint16) uint64 {
if blkIoWeight == 0 {
return 0
}
return uint64(1 + (uint64(blkIoWeight)-10)*9999/990)
}
14 changes: 14 additions & 0 deletions libcontainer/cgroups/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -642,3 +642,17 @@ func TestConvertMemorySwapToCgroupV2Value(t *testing.T) {
}
}
}

func TestConvertBlkIOToIOWeightValue(t *testing.T) {
cases := map[uint16]uint64{
0: 0,
10: 1,
1000: 10000,
}
for i, expected := range cases {
got := ConvertBlkIOToIOWeightValue(i)
if got != expected {
t.Errorf("expected ConvertBlkIOToIOWeightValue(%d) to be %d, got %d", i, expected, got)
}
}
}
8 changes: 6 additions & 2 deletions tests/integration/cgroups.bats
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,12 @@ function setup() {
[ "$status" -eq 0 ]

runc exec test_cgroups_unified sh -c 'cat /sys/fs/cgroup/io.bfq.weight'
[ "$status" -eq 0 ]
[ "$output" = 'default 750' ]
if [[ "$status" -eq 0 ]]; then
[ "$output" = 'default 750' ]
else
runc exec test_cgroups_unified sh -c 'cat /sys/fs/cgroup/io.weight'
[ "$output" = 'default 7475' ]
fi
}

@test "runc run (cgroup v2 resources.unified only)" {
Expand Down

0 comments on commit 669f92b

Please sign in to comment.