Fix cgroupv2 bug that set the wrong iops throttle.

PiperOrigin-RevId: 425483482
This commit is contained in:
Lucas Manning
2022-01-31 16:08:11 -08:00
committed by gVisor bot
parent d1dadc9c19
commit 0f8db423e2
2 changed files with 104 additions and 1 deletions
+1 -1
View File
@@ -513,7 +513,7 @@ func (*io2) set(spec *specs.LinuxResources, path string) error {
return err
}
if err := setThrottle2(path, "riops", blkio.ThrottleWriteIOPSDevice); err != nil {
if err := setThrottle2(path, "wiops", blkio.ThrottleWriteIOPSDevice); err != nil {
return err
}
+103
View File
@@ -15,12 +15,115 @@
package cgroup
import (
"io/ioutil"
"os"
"path/filepath"
"strconv"
"strings"
"testing"
specs "github.com/opencontainers/runtime-spec/specs-go"
"gvisor.dev/gvisor/pkg/test/testutil"
)
var cgroupv2MountInfo = `29 22 0:26 / /sys/fs/cgroup rw shared:4 - cgroup2 cgroup2 rw,seclabel,nsdelegate`
func TestIO(t *testing.T) {
for _, tc := range []struct {
name string
spec *specs.LinuxBlockIO
path string
wants string
}{
{
name: "simple",
spec: &specs.LinuxBlockIO{
Weight: uint16Ptr(1),
},
path: "io.weight",
wants: strconv.FormatUint(convertBlkIOToIOWeightValue(1), 10),
},
{
name: "throttlereadbps",
spec: &specs.LinuxBlockIO{
ThrottleReadBpsDevice: []specs.LinuxThrottleDevice{
makeLinuxThrottleDevice(1, 2, 3),
},
},
path: "io.max",
wants: "1:2 rbps=3",
},
{
name: "throttlewritebps",
spec: &specs.LinuxBlockIO{
ThrottleWriteBpsDevice: []specs.LinuxThrottleDevice{
makeLinuxThrottleDevice(4, 5, 6),
},
},
path: "io.max",
wants: "4:5 wbps=6",
},
{
name: "throttlereadiops",
spec: &specs.LinuxBlockIO{
ThrottleReadIOPSDevice: []specs.LinuxThrottleDevice{
makeLinuxThrottleDevice(7, 8, 9),
},
},
path: "io.max",
wants: "7:8 riops=9",
},
{
name: "throttlewriteiops",
spec: &specs.LinuxBlockIO{
ThrottleWriteIOPSDevice: []specs.LinuxThrottleDevice{
makeLinuxThrottleDevice(10, 11, 12),
},
},
path: "io.max",
wants: "10:11 wiops=12",
},
{
name: "nil_values",
spec: &specs.LinuxBlockIO{},
path: "not_used",
wants: "",
},
} {
t.Run(tc.name, func(t *testing.T) {
testutil.TmpDir()
dir, err := ioutil.TempDir(testutil.TmpDir(), "cgroup")
if err != nil {
t.Fatalf("error creating temporary directory: %v", err)
}
defer os.RemoveAll(dir)
fd, err := os.Create(filepath.Join(dir, tc.path))
if err != nil {
t.Fatalf("os.CreatTemp(): %v", err)
}
fd.Close()
spec := &specs.LinuxResources{
BlockIO: tc.spec,
}
ctrlr := io2{}
if err := ctrlr.set(spec, dir); err != nil {
t.Fatalf("ctrlr.set(): %v", err)
}
gotBytes, err := ioutil.ReadFile(filepath.Join(dir, tc.path))
if err != nil {
t.Fatal(err.Error())
}
got := strings.TrimSuffix(string(gotBytes), "\n")
if got != tc.wants {
t.Errorf("wrong file content, file: %q, want: %q, got: %q", tc.path, tc.wants, got)
}
})
}
}
func TestLoadPathsCgroupv2(t *testing.T) {
for _, tc := range []struct {
name string