Skip to content

Commit 1b3d6bd

Browse files
author
Zhou Hao
authored
Merge pull request #235 from Mashimiao/generate-add-blkio-options
generate: support blkio related options
2 parents df88d82 + a9c6787 commit 1b3d6bd

File tree

5 files changed

+379
-4
lines changed

5 files changed

+379
-4
lines changed

cmd/oci-runtime-tool/generate.go

Lines changed: 155 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,19 @@ var generateFlags = []cli.Flag{
2929
cli.StringFlag{Name: "hostname", Usage: "hostname value for the container"},
3030
cli.StringSliceFlag{Name: "label", Usage: "add annotations to the configuration e.g. key=value"},
3131
cli.StringFlag{Name: "linux-apparmor", Usage: "specifies the the apparmor profile for the container"},
32+
cli.IntFlag{Name: "linux-blkio-leaf-weight", Usage: "Block IO (relative leaf weight), the range is from 10 to 1000"},
33+
cli.StringSliceFlag{Name: "linux-blkio-leaf-weight-device", Usage: "Block IO (relative device leaf weight), e.g. major:minor:leaf-weight"},
34+
cli.StringSliceFlag{Name: "linux-blkio-read-bps-device", Usage: "Limit read rate (bytes per second) from a device"},
35+
cli.StringSliceFlag{Name: "linux-blkio-read-iops-device", Usage: "Limit read rate (IO per second) from a device"},
36+
cli.IntFlag{Name: "linux-blkio-weight", Usage: "Block IO (relative weight), the range is from 10 to 1000"},
37+
cli.StringSliceFlag{Name: "linux-blkio-weight-device", Usage: "Block IO (relative device weight), e.g. major:minor:weight"},
38+
cli.StringSliceFlag{Name: "linux-blkio-write-bps-device", Usage: "Limit write rate (bytes per second) to a device"},
39+
cli.StringSliceFlag{Name: "linux-blkio-write-iops-device", Usage: "Limit write rate (IO per second) to a device"},
3240
cli.StringFlag{Name: "linux-cgroups-path", Usage: "specify the path to the cgroups"},
3341
cli.Uint64Flag{Name: "linux-cpu-period", Usage: "the CPU period to be used for hardcapping (in usecs)"},
3442
cli.Uint64Flag{Name: "linux-cpu-quota", Usage: "the allowed CPU time in a given period (in usecs)"},
35-
cli.Uint64Flag{Name: "linux-cpu-shares", Usage: "the relative share of CPU time available to the tasks in a cgroup"},
3643
cli.StringFlag{Name: "linux-cpus", Usage: "CPUs to use within the cpuset (default is to use any CPU available)"},
44+
cli.Uint64Flag{Name: "linux-cpu-shares", Usage: "the relative share of CPU time available to the tasks in a cgroup"},
3745
cli.StringSliceFlag{Name: "linux-device-add", Usage: "add a device which must be made available in the container"},
3846
cli.StringSliceFlag{Name: "linux-device-remove", Usage: "remove a device which must be made available in the container"},
3947
cli.BoolFlag{Name: "linux-device-remove-all", Usage: "remove all devices which must be made available in the container"},
@@ -500,6 +508,104 @@ func setupSpec(g *generate.Generator, context *cli.Context) error {
500508
g.SetProcessOOMScoreAdj(context.Int("linux-oom-score-adj"))
501509
}
502510

511+
if context.IsSet("linux-blkio-leaf-weight") {
512+
g.SetLinuxResourcesBlockIOLeafWeight(uint16(context.Uint64("linux-blkio-leaf-weight")))
513+
}
514+
515+
if context.IsSet("linux-blkio-leaf-weight-device") {
516+
devLeafWeight := context.StringSlice("linux-blkio-leaf-weight-device")
517+
for _, v := range devLeafWeight {
518+
major, minor, leafWeight, err := parseDeviceWeight(v)
519+
if err != nil {
520+
return err
521+
}
522+
if leafWeight == -1 {
523+
g.DropLinuxResourcesBlockIOLeafWeightDevice(major, minor)
524+
} else {
525+
g.AddLinuxResourcesBlockIOLeafWeightDevice(major, minor, uint16(leafWeight))
526+
}
527+
}
528+
}
529+
530+
if context.IsSet("linux-blkio-read-bps-device") {
531+
throttleDevices := context.StringSlice("linux-blkio-read-bps-device")
532+
for _, v := range throttleDevices {
533+
major, minor, rate, err := parseThrottleDevice(v)
534+
if err != nil {
535+
return err
536+
}
537+
if rate == -1 {
538+
g.DropLinuxResourcesBlockIOThrottleReadBpsDevice(major, minor)
539+
} else {
540+
g.AddLinuxResourcesBlockIOThrottleReadBpsDevice(major, minor, uint64(rate))
541+
}
542+
}
543+
}
544+
545+
if context.IsSet("linux-blkio-read-iops-device") {
546+
throttleDevices := context.StringSlice("linux-blkio-read-iops-device")
547+
for _, v := range throttleDevices {
548+
major, minor, rate, err := parseThrottleDevice(v)
549+
if err != nil {
550+
return err
551+
}
552+
if rate == -1 {
553+
g.DropLinuxResourcesBlockIOThrottleReadIOPSDevice(major, minor)
554+
} else {
555+
g.AddLinuxResourcesBlockIOThrottleReadIOPSDevice(major, minor, uint64(rate))
556+
}
557+
}
558+
}
559+
560+
if context.IsSet("linux-blkio-weight") {
561+
g.SetLinuxResourcesBlockIOWeight(uint16(context.Uint64("linux-blkio-weight")))
562+
}
563+
564+
if context.IsSet("linux-blkio-weight-device") {
565+
devWeight := context.StringSlice("linux-blkio-weight-device")
566+
for _, v := range devWeight {
567+
major, minor, weight, err := parseDeviceWeight(v)
568+
if err != nil {
569+
return err
570+
}
571+
if weight == -1 {
572+
g.DropLinuxResourcesBlockIOWeightDevice(major, minor)
573+
} else {
574+
g.AddLinuxResourcesBlockIOWeightDevice(major, minor, uint16(weight))
575+
}
576+
}
577+
}
578+
579+
if context.IsSet("linux-blkio-write-bps-device") {
580+
throttleDevices := context.StringSlice("linux-blkio-write-bps-device")
581+
for _, v := range throttleDevices {
582+
major, minor, rate, err := parseThrottleDevice(v)
583+
if err != nil {
584+
return err
585+
}
586+
if rate == -1 {
587+
g.DropLinuxResourcesBlockIOThrottleWriteBpsDevice(major, minor)
588+
} else {
589+
g.AddLinuxResourcesBlockIOThrottleWriteBpsDevice(major, minor, uint64(rate))
590+
}
591+
}
592+
}
593+
594+
if context.IsSet("linux-blkio-write-iops-device") {
595+
throttleDevices := context.StringSlice("linux-blkio-write-iops-device")
596+
for _, v := range throttleDevices {
597+
major, minor, rate, err := parseThrottleDevice(v)
598+
if err != nil {
599+
return err
600+
}
601+
if rate == -1 {
602+
g.DropLinuxResourcesBlockIOThrottleWriteIOPSDevice(major, minor)
603+
} else {
604+
g.AddLinuxResourcesBlockIOThrottleWriteIOPSDevice(major, minor, uint64(rate))
605+
}
606+
}
607+
}
608+
503609
if context.IsSet("linux-cpu-shares") {
504610
g.SetLinuxResourcesCPUShares(context.Uint64("linux-cpu-shares"))
505611
}
@@ -1158,3 +1264,51 @@ type ErrBadEnvVariable struct {
11581264
func (e ErrBadEnvVariable) Error() string {
11591265
return fmt.Sprintf("poorly formatted environment: %s", e.msg)
11601266
}
1267+
1268+
func parseDeviceWeight(weightDevice string) (int64, int64, int16, error) {
1269+
list := strings.Split(weightDevice, ":")
1270+
if len(list) != 3 {
1271+
return 0, 0, 0, fmt.Errorf("invalid format: %s", weightDevice)
1272+
}
1273+
1274+
major, err := strconv.Atoi(list[0])
1275+
if err != nil {
1276+
return 0, 0, 0, err
1277+
}
1278+
1279+
minor, err := strconv.Atoi(list[1])
1280+
if err != nil {
1281+
return 0, 0, 0, err
1282+
}
1283+
1284+
weight, err := strconv.Atoi(list[2])
1285+
if err != nil {
1286+
return 0, 0, 0, err
1287+
}
1288+
1289+
return int64(major), int64(minor), int16(weight), nil
1290+
}
1291+
1292+
func parseThrottleDevice(throttleDevice string) (int64, int64, int64, error) {
1293+
list := strings.Split(throttleDevice, ":")
1294+
if len(list) != 3 {
1295+
return 0, 0, 0, fmt.Errorf("invalid format: %s", throttleDevice)
1296+
}
1297+
1298+
major, err := strconv.Atoi(list[0])
1299+
if err != nil {
1300+
return 0, 0, 0, err
1301+
}
1302+
1303+
minor, err := strconv.Atoi(list[1])
1304+
if err != nil {
1305+
return 0, 0, 0, err
1306+
}
1307+
1308+
rate, err := strconv.Atoi(list[2])
1309+
if err != nil {
1310+
return 0, 0, 0, err
1311+
}
1312+
1313+
return int64(major), int64(minor), int64(rate), nil
1314+
}

completions/bash/oci-runtime-tool

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,14 @@ _oci-runtime-tool_generate() {
311311
--hostname
312312
--label
313313
--linux-apparmor
314+
--linux-blkio-leaf-weight
315+
--linux-blkio-leaf-weight-device
316+
--linux-blkio-read-bps-device
317+
--linux-blkio-read-iops-device
318+
--linux-blkio-weight
319+
--linux-blkio-weight-device
320+
--linux-blkio-write-bps-device
321+
--linux-blkio-write-iops-device
314322
--linux-cgroups-path
315323
--linux-cpu-period
316324
--linux-cpu-quota

generate/generate.go

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,144 @@ func (g *Generator) SetProcessOOMScoreAdj(adj int) {
504504
g.spec.Process.OOMScoreAdj = &adj
505505
}
506506

507+
// SetLinuxResourcesBlockIOLeafWeight sets g.spec.Linux.Resources.BlockIO.LeafWeight.
508+
func (g *Generator) SetLinuxResourcesBlockIOLeafWeight(weight uint16) {
509+
g.initSpecLinuxResourcesBlockIO()
510+
g.spec.Linux.Resources.BlockIO.LeafWeight = &weight
511+
}
512+
513+
// AddLinuxResourcesBlockIOLeafWeightDevice adds or sets g.spec.Linux.Resources.BlockIO.WeightDevice.LeafWeight.
514+
func (g *Generator) AddLinuxResourcesBlockIOLeafWeightDevice(major int64, minor int64, weight uint16) {
515+
g.initSpecLinuxResourcesBlockIO()
516+
for i, weightDevice := range g.spec.Linux.Resources.BlockIO.WeightDevice {
517+
if weightDevice.Major == major && weightDevice.Minor == minor {
518+
g.spec.Linux.Resources.BlockIO.WeightDevice[i].LeafWeight = &weight
519+
return
520+
}
521+
}
522+
weightDevice := new(rspec.LinuxWeightDevice)
523+
weightDevice.Major = major
524+
weightDevice.Minor = minor
525+
weightDevice.LeafWeight = &weight
526+
g.spec.Linux.Resources.BlockIO.WeightDevice = append(g.spec.Linux.Resources.BlockIO.WeightDevice, *weightDevice)
527+
}
528+
529+
// DropLinuxResourcesBlockIOLeafWeightDevice drops a item form g.spec.Linux.Resources.BlockIO.WeightDevice.LeafWeight
530+
func (g *Generator) DropLinuxResourcesBlockIOLeafWeightDevice(major int64, minor int64) {
531+
g.initSpecLinuxResourcesBlockIO()
532+
for i, weightDevice := range g.spec.Linux.Resources.BlockIO.WeightDevice {
533+
if weightDevice.Major == major && weightDevice.Minor == minor {
534+
if weightDevice.Weight != nil {
535+
newWeightDevice := new(rspec.LinuxWeightDevice)
536+
newWeightDevice.Major = major
537+
newWeightDevice.Minor = minor
538+
newWeightDevice.Weight = weightDevice.Weight
539+
g.spec.Linux.Resources.BlockIO.WeightDevice[i] = *newWeightDevice
540+
} else {
541+
g.spec.Linux.Resources.BlockIO.WeightDevice = append(g.spec.Linux.Resources.BlockIO.WeightDevice[:i], g.spec.Linux.Resources.BlockIO.WeightDevice[i+1:]...)
542+
}
543+
return
544+
}
545+
}
546+
}
547+
548+
// SetLinuxResourcesBlockIOWeight sets g.spec.Linux.Resources.BlockIO.Weight.
549+
func (g *Generator) SetLinuxResourcesBlockIOWeight(weight uint16) {
550+
g.initSpecLinuxResourcesBlockIO()
551+
g.spec.Linux.Resources.BlockIO.Weight = &weight
552+
}
553+
554+
// AddLinuxResourcesBlockIOWeightDevice adds or sets g.spec.Linux.Resources.BlockIO.WeightDevice.Weight.
555+
func (g *Generator) AddLinuxResourcesBlockIOWeightDevice(major int64, minor int64, weight uint16) {
556+
g.initSpecLinuxResourcesBlockIO()
557+
for i, weightDevice := range g.spec.Linux.Resources.BlockIO.WeightDevice {
558+
if weightDevice.Major == major && weightDevice.Minor == minor {
559+
g.spec.Linux.Resources.BlockIO.WeightDevice[i].Weight = &weight
560+
return
561+
}
562+
}
563+
weightDevice := new(rspec.LinuxWeightDevice)
564+
weightDevice.Major = major
565+
weightDevice.Minor = minor
566+
weightDevice.Weight = &weight
567+
g.spec.Linux.Resources.BlockIO.WeightDevice = append(g.spec.Linux.Resources.BlockIO.WeightDevice, *weightDevice)
568+
}
569+
570+
// DropLinuxResourcesBlockIOWeightDevice drops a item form g.spec.Linux.Resources.BlockIO.WeightDevice.Weight
571+
func (g *Generator) DropLinuxResourcesBlockIOWeightDevice(major int64, minor int64) {
572+
g.initSpecLinuxResourcesBlockIO()
573+
for i, weightDevice := range g.spec.Linux.Resources.BlockIO.WeightDevice {
574+
if weightDevice.Major == major && weightDevice.Minor == minor {
575+
if weightDevice.LeafWeight != nil {
576+
newWeightDevice := new(rspec.LinuxWeightDevice)
577+
newWeightDevice.Major = major
578+
newWeightDevice.Minor = minor
579+
newWeightDevice.LeafWeight = weightDevice.LeafWeight
580+
g.spec.Linux.Resources.BlockIO.WeightDevice[i] = *newWeightDevice
581+
} else {
582+
g.spec.Linux.Resources.BlockIO.WeightDevice = append(g.spec.Linux.Resources.BlockIO.WeightDevice[:i], g.spec.Linux.Resources.BlockIO.WeightDevice[i+1:]...)
583+
}
584+
return
585+
}
586+
}
587+
}
588+
589+
// AddLinuxResourcesBlockIOThrottleReadBpsDevice adds or sets g.spec.Linux.Resources.BlockIO.ThrottleReadBpsDevice.
590+
func (g *Generator) AddLinuxResourcesBlockIOThrottleReadBpsDevice(major int64, minor int64, rate uint64) {
591+
g.initSpecLinuxResourcesBlockIO()
592+
throttleDevices := addOrReplaceBlockIOThrottleDevice(g.spec.Linux.Resources.BlockIO.ThrottleReadBpsDevice, major, minor, rate)
593+
g.spec.Linux.Resources.BlockIO.ThrottleReadBpsDevice = throttleDevices
594+
}
595+
596+
// DropLinuxResourcesBlockIOThrottleReadBpsDevice drops a item from g.spec.Linux.Resources.BlockIO.ThrottleReadBpsDevice.
597+
func (g *Generator) DropLinuxResourcesBlockIOThrottleReadBpsDevice(major int64, minor int64) {
598+
g.initSpecLinuxResourcesBlockIO()
599+
throttleDevices := dropBlockIOThrottleDevice(g.spec.Linux.Resources.BlockIO.ThrottleReadBpsDevice, major, minor)
600+
g.spec.Linux.Resources.BlockIO.ThrottleReadBpsDevice = throttleDevices
601+
}
602+
603+
// AddLinuxResourcesBlockIOThrottleReadIOPSDevice adds or sets g.spec.Linux.Resources.BlockIO.ThrottleReadIOPSDevice.
604+
func (g *Generator) AddLinuxResourcesBlockIOThrottleReadIOPSDevice(major int64, minor int64, rate uint64) {
605+
g.initSpecLinuxResourcesBlockIO()
606+
throttleDevices := addOrReplaceBlockIOThrottleDevice(g.spec.Linux.Resources.BlockIO.ThrottleReadIOPSDevice, major, minor, rate)
607+
g.spec.Linux.Resources.BlockIO.ThrottleReadIOPSDevice = throttleDevices
608+
}
609+
610+
// DropLinuxResourcesBlockIOThrottleReadIOPSDevice drops a item from g.spec.Linux.Resources.BlockIO.ThrottleReadIOPSDevice.
611+
func (g *Generator) DropLinuxResourcesBlockIOThrottleReadIOPSDevice(major int64, minor int64) {
612+
g.initSpecLinuxResourcesBlockIO()
613+
throttleDevices := dropBlockIOThrottleDevice(g.spec.Linux.Resources.BlockIO.ThrottleReadIOPSDevice, major, minor)
614+
g.spec.Linux.Resources.BlockIO.ThrottleReadIOPSDevice = throttleDevices
615+
}
616+
617+
// AddLinuxResourcesBlockIOThrottleWriteBpsDevice adds or sets g.spec.Linux.Resources.BlockIO.ThrottleWriteBpsDevice.
618+
func (g *Generator) AddLinuxResourcesBlockIOThrottleWriteBpsDevice(major int64, minor int64, rate uint64) {
619+
g.initSpecLinuxResourcesBlockIO()
620+
throttleDevices := addOrReplaceBlockIOThrottleDevice(g.spec.Linux.Resources.BlockIO.ThrottleWriteBpsDevice, major, minor, rate)
621+
g.spec.Linux.Resources.BlockIO.ThrottleWriteBpsDevice = throttleDevices
622+
}
623+
624+
// DropLinuxResourcesBlockIOThrottleWriteBpsDevice drops a item from g.spec.Linux.Resources.BlockIO.ThrottleWriteBpsDevice.
625+
func (g *Generator) DropLinuxResourcesBlockIOThrottleWriteBpsDevice(major int64, minor int64) {
626+
g.initSpecLinuxResourcesBlockIO()
627+
throttleDevices := dropBlockIOThrottleDevice(g.spec.Linux.Resources.BlockIO.ThrottleWriteBpsDevice, major, minor)
628+
g.spec.Linux.Resources.BlockIO.ThrottleWriteBpsDevice = throttleDevices
629+
}
630+
631+
// AddLinuxResourcesBlockIOThrottleWriteIOPSDevice adds or sets g.spec.Linux.Resources.BlockIO.ThrottleWriteIOPSDevice.
632+
func (g *Generator) AddLinuxResourcesBlockIOThrottleWriteIOPSDevice(major int64, minor int64, rate uint64) {
633+
g.initSpecLinuxResourcesBlockIO()
634+
throttleDevices := addOrReplaceBlockIOThrottleDevice(g.spec.Linux.Resources.BlockIO.ThrottleWriteIOPSDevice, major, minor, rate)
635+
g.spec.Linux.Resources.BlockIO.ThrottleWriteIOPSDevice = throttleDevices
636+
}
637+
638+
// DropLinuxResourcesBlockIOThrottleWriteIOPSDevice drops a item from g.spec.Linux.Resources.BlockIO.ThrottleWriteIOPSDevice.
639+
func (g *Generator) DropLinuxResourcesBlockIOThrottleWriteIOPSDevice(major int64, minor int64) {
640+
g.initSpecLinuxResourcesBlockIO()
641+
throttleDevices := dropBlockIOThrottleDevice(g.spec.Linux.Resources.BlockIO.ThrottleWriteIOPSDevice, major, minor)
642+
g.spec.Linux.Resources.BlockIO.ThrottleWriteIOPSDevice = throttleDevices
643+
}
644+
507645
// SetLinuxResourcesCPUShares sets g.spec.Linux.Resources.CPU.Shares.
508646
func (g *Generator) SetLinuxResourcesCPUShares(shares uint64) {
509647
g.initSpecLinuxResourcesCPU()
@@ -1258,3 +1396,32 @@ func (g *Generator) AddLinuxReadonlyPaths(path string) {
12581396
g.initSpecLinux()
12591397
g.spec.Linux.ReadonlyPaths = append(g.spec.Linux.ReadonlyPaths, path)
12601398
}
1399+
1400+
func addOrReplaceBlockIOThrottleDevice(tmpList []rspec.LinuxThrottleDevice, major int64, minor int64, rate uint64) []rspec.LinuxThrottleDevice {
1401+
throttleDevices := tmpList
1402+
for i, throttleDevice := range throttleDevices {
1403+
if throttleDevice.Major == major && throttleDevice.Minor == minor {
1404+
throttleDevices[i].Rate = rate
1405+
return throttleDevices
1406+
}
1407+
}
1408+
throttleDevice := new(rspec.LinuxThrottleDevice)
1409+
throttleDevice.Major = major
1410+
throttleDevice.Minor = minor
1411+
throttleDevice.Rate = rate
1412+
throttleDevices = append(throttleDevices, *throttleDevice)
1413+
1414+
return throttleDevices
1415+
}
1416+
1417+
func dropBlockIOThrottleDevice(tmpList []rspec.LinuxThrottleDevice, major int64, minor int64) []rspec.LinuxThrottleDevice {
1418+
throttleDevices := tmpList
1419+
for i, throttleDevice := range throttleDevices {
1420+
if throttleDevice.Major == major && throttleDevice.Minor == minor {
1421+
throttleDevices = append(throttleDevices[:i], throttleDevices[i+1:]...)
1422+
return throttleDevices
1423+
}
1424+
}
1425+
1426+
return throttleDevices
1427+
}

generate/spec.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,13 @@ func (g *Generator) initSpecLinuxResources() {
8080
}
8181
}
8282

83+
func (g *Generator) initSpecLinuxResourcesBlockIO() {
84+
g.initSpecLinuxResources()
85+
if g.spec.Linux.Resources.BlockIO == nil {
86+
g.spec.Linux.Resources.BlockIO = &rspec.LinuxBlockIO{}
87+
}
88+
}
89+
8390
func (g *Generator) initSpecLinuxResourcesCPU() {
8491
g.initSpecLinuxResources()
8592
if g.spec.Linux.Resources.CPU == nil {

0 commit comments

Comments
 (0)