Skip to content

Commit

Permalink
feature: limit blkio device's read/write Bps/IOps
Browse files Browse the repository at this point in the history
fixes AliyunContainerService#2509

Signed-off-by: Leno Hou <lenohou@gmail.com>
  • Loading branch information
houstar committed Dec 16, 2018
1 parent 9e879af commit f61d2bc
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 9 deletions.
2 changes: 1 addition & 1 deletion apis/types/resources.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 16 additions & 8 deletions cli/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ func (uc *UpdateCommand) addFlags() {
flagSet.StringSliceVarP(&uc.labels, "label", "l", nil, "Set label for container")
flagSet.StringVar(&uc.restartPolicy, "restart", "", "Restart policy to apply when container exits")
flagSet.StringSliceVar(&uc.diskQuota, "disk-quota", nil, "Update disk quota for container(/=10g)")
flagSet.Var(&uc.blkioDeviceReadBps, "device-read-bps", "Update read rate (bytes per second) from a device")
flagSet.Var(&uc.blkioDeviceWriteBps, "device-write-bps", "Update write rate (bytes per second) from a device")
flagSet.Var(&uc.blkioDeviceReadIOps, "device-read-iops", "Update read rate (io per second) from a device")
flagSet.Var(&uc.blkioDeviceWriteIOps, "device-write-iops", "Update write rate (io per second) from a device")
}

// updateRun is the entry of update command.
Expand All @@ -69,14 +73,18 @@ func (uc *UpdateCommand) updateRun(args []string) error {
}

resource := types.Resources{
CPUPeriod: uc.cpuperiod,
CPUShares: uc.cpushare,
CPUQuota: uc.cpuquota,
CpusetCpus: uc.cpusetcpus,
CpusetMems: uc.cpusetmems,
Memory: memory,
MemorySwap: memorySwap,
BlkioWeight: uc.blkioWeight,
CPUPeriod: uc.cpuperiod,
CPUShares: uc.cpushare,
CPUQuota: uc.cpuquota,
CpusetCpus: uc.cpusetcpus,
CpusetMems: uc.cpusetmems,
Memory: memory,
MemorySwap: memorySwap,
BlkioWeight: uc.blkioWeight,
BlkioDeviceReadBps: uc.blkioDeviceReadBps.Value(),
BlkioDeviceWriteBps: uc.blkioDeviceWriteBps.Value(),
BlkioDeviceReadIOps: uc.blkioDeviceReadIOps.Value(),
BlkioDeviceWriteIOps: uc.blkioDeviceWriteIOps.Value(),
}

restartPolicy, err := opts.ParseRestartPolicy(uc.restartPolicy)
Expand Down
100 changes: 100 additions & 0 deletions test/cli_run_blkio_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,103 @@ func (suite *PouchRunBlkioSuite) TestRunWithBlkioWeight(c *check.C) {
defer DelContainerForceMultyTime(c, name)
res.Assert(c, icmd.Success)
}

// TestRunWithBlkIODeviceWriteBps is to verify blkio write bps
func (suite *PouchRunBlkioSuite) TestRunBlockIODeviceWriteBps(c *check.C) {
cname := "TestRunBlockIODeviceWriteBps"
testDisk := "/dev/null"

number, exist := util.GetMajMinNumOfDevice(testDisk)
if !exist {
c.Skip("fail to get major:minor device number")
}

limitSpeed := "100"
expected := fmt.Sprintf("%s %s\n", number, limitSpeed)

blkioDeviceWriteBpsFile := "/sys/fs/cgroup/blkio/blkio.throttle.write_bps_device"
throttleDev := testDisk + ":" + limitSpeed

res := command.PouchRun("run", "--name", cname,
"--device-write-bps ", throttleDev, busyboxImage, "cat", blkioDeviceWriteBpsFile)
defer DelContainerForceMultyTime(c, cname)
res.Assert(c, icmd.Success)

out := res.Stdout()
c.Assert(out, check.Equals, expected)
}

// TestRunWithBlkIODeviceReadBps is to verify blkio read bps
func (suite *PouchRunBlkioSuite) TestRunBlockIODeviceReadBps(c *check.C) {
cname := "TestRunBlockIODeviceReadBps"
testDisk := "/dev/null"

number, exist := util.GetMajMinNumOfDevice(testDisk)
if !exist {
c.Skip("fail to get major:minor device number")
}

limitSpeed := "100"
expected := fmt.Sprintf("%s %s\n", number, limitSpeed)

blkioDeviceReadBpsFile := "/sys/fs/cgroup/blkio/blkio.throttle.read_bps_device"
throttleDev := testDisk + ":" + limitSpeed

res := command.PouchRun("run", "--name", cname,
"--device-read-bps ", throttleDev, busyboxImage, "cat", blkioDeviceReadBpsFile)
defer DelContainerForceMultyTime(c, cname)
res.Assert(c, icmd.Success)

out := res.Stdout()
c.Assert(out, check.Equals, expected)
}

// TestRunWithBlkIODeviceWriteIOps is to verify blkio write iops
func (suite *PouchRunBlkioSuite) TestRunBlockIODeviceWriteIOps(c *check.C) {
cname := "TestRunBlockIODeviceWriteIOps"
testDisk := "/dev/null"

number, exist := util.GetMajMinNumOfDevice(testDisk)
if !exist {
c.Skip("fail to get major:minor device number")
}

limitSpeed := "100"
expected := fmt.Sprintf("%s %s\n", number, limitSpeed)

blkioDeviceWriteIOpsFile := "/sys/fs/cgroup/blkio/blkio.throttle.write_iops_device"
throttleDev := testDisk + ":" + limitSpeed

res := command.PouchRun("run", "--name", cname,
"--device-write-iops ", throttleDev, busyboxImage, "cat", blkioDeviceWriteIOpsFile)
defer DelContainerForceMultyTime(c, cname)
res.Assert(c, icmd.Success)

out := res.Stdout()
c.Assert(out, check.Equals, expected)
}

// TestRunWithBlkIODeviceWriteIOps is to verify blkio read iops
func (suite *PouchRunBlkioSuite) TestRunBlockIODeviceReadIOps(c *check.C) {
cname := "TestRunBlockIODeviceReadIOps"
testDisk := "/dev/null"

number, exist := util.GetMajMinNumOfDevice(testDisk)
if !exist {
c.Skip("fail to get major:minor device number")
}

limitSpeed := "100"
expected := fmt.Sprintf("%s %s\n", number, limitSpeed)

blkioDeviceReadIOpsFile := "/sys/fs/cgroup/blkio/blkio.throttle.read_iops_device"
throttleDev := testDisk + ":" + limitSpeed

res := command.PouchRun("run", "--name", cname,
"--device-read-iops ", throttleDev, busyboxImage, "cat", blkioDeviceReadIOpsFile)
defer DelContainerForceMultyTime(c, cname)
res.Assert(c, icmd.Success)

out := res.Stdout()
c.Assert(out, check.Equals, expected)
}

0 comments on commit f61d2bc

Please sign in to comment.