|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/opencontainers/runtime-tools/cgroups" |
| 7 | + "github.com/opencontainers/runtime-tools/validation/util" |
| 8 | +) |
| 9 | + |
| 10 | +func main() { |
| 11 | + var weight uint16 = 500 |
| 12 | + var leafWeight uint16 = 300 |
| 13 | + var major, minor int64 = 8, 0 |
| 14 | + var rate uint64 = 102400 |
| 15 | + g := util.GetDefaultGenerator() |
| 16 | + g.SetLinuxCgroupsPath(cgroups.RelCgroupPath) |
| 17 | + g.SetLinuxResourcesBlockIOWeight(weight) |
| 18 | + g.SetLinuxResourcesBlockIOLeafWeight(leafWeight) |
| 19 | + g.AddLinuxResourcesBlockIOWeightDevice(major, minor, weight) |
| 20 | + g.AddLinuxResourcesBlockIOLeafWeightDevice(major, minor, leafWeight) |
| 21 | + g.AddLinuxResourcesBlockIOThrottleReadBpsDevice(major, minor, rate) |
| 22 | + g.AddLinuxResourcesBlockIOThrottleWriteBpsDevice(major, minor, rate) |
| 23 | + g.AddLinuxResourcesBlockIOThrottleReadIOPSDevice(major, minor, rate) |
| 24 | + g.AddLinuxResourcesBlockIOThrottleWriteIOPSDevice(major, minor, rate) |
| 25 | + err := util.RuntimeOutsideValidate(g, cgroups.RelCgroupPath, func(pid int, path string) error { |
| 26 | + cg, err := cgroups.FindCgroup() |
| 27 | + if err != nil { |
| 28 | + return err |
| 29 | + } |
| 30 | + lbd, err := cg.GetBlockIOData(pid, path) |
| 31 | + if err != nil { |
| 32 | + return err |
| 33 | + } |
| 34 | + if *lbd.Weight != weight { |
| 35 | + return fmt.Errorf("blkio weight is not set correctly, expect: %d, actual: %d", weight, lbd.Weight) |
| 36 | + } |
| 37 | + if *lbd.LeafWeight != leafWeight { |
| 38 | + return fmt.Errorf("blkio leafWeight is not set correctly, expect: %d, actual: %d", weight, lbd.LeafWeight) |
| 39 | + } |
| 40 | + |
| 41 | + found := false |
| 42 | + for _, wd := range lbd.WeightDevice { |
| 43 | + if wd.Major == major && wd.Minor == minor { |
| 44 | + found = true |
| 45 | + if *wd.Weight != weight { |
| 46 | + return fmt.Errorf("blkio weight for %d:%d is not set correctly, expect: %d, actual: %d", major, minor, weight, wd.Weight) |
| 47 | + } |
| 48 | + if *wd.LeafWeight != leafWeight { |
| 49 | + return fmt.Errorf("blkio leafWeight for %d:%d is not set correctly, expect: %d, actual: %d", major, minor, leafWeight, wd.LeafWeight) |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + if !found { |
| 54 | + return fmt.Errorf("blkio weightDevice for %d:%d is not set", major, minor) |
| 55 | + } |
| 56 | + |
| 57 | + found = false |
| 58 | + for _, trbd := range lbd.ThrottleReadBpsDevice { |
| 59 | + if trbd.Major == major && trbd.Minor == minor { |
| 60 | + found = true |
| 61 | + if trbd.Rate != rate { |
| 62 | + return fmt.Errorf("blkio read bps for %d:%d is not set correctly, expect: %d, actual: %d", major, minor, rate, trbd.Rate) |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + if !found { |
| 67 | + return fmt.Errorf("blkio read bps for %d:%d is not set", major, minor) |
| 68 | + } |
| 69 | + |
| 70 | + found = false |
| 71 | + for _, twbd := range lbd.ThrottleWriteBpsDevice { |
| 72 | + if twbd.Major == major && twbd.Minor == minor { |
| 73 | + found = true |
| 74 | + if twbd.Rate != rate { |
| 75 | + return fmt.Errorf("blkio write bps for %d:%d is not set correctly, expect: %d, actual: %d", major, minor, rate, twbd.Rate) |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + if !found { |
| 80 | + return fmt.Errorf("blkio write bps for %d:%d is not set", major, minor) |
| 81 | + } |
| 82 | + |
| 83 | + found = false |
| 84 | + for _, trid := range lbd.ThrottleReadIOPSDevice { |
| 85 | + if trid.Major == major && trid.Minor == minor { |
| 86 | + found = true |
| 87 | + if trid.Rate != rate { |
| 88 | + return fmt.Errorf("blkio read iops for %d:%d is not set correctly, expect: %d, actual: %d", major, minor, rate, trid.Rate) |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + if !found { |
| 93 | + return fmt.Errorf("blkio read iops for %d:%d is not set", major, minor) |
| 94 | + } |
| 95 | + |
| 96 | + found = false |
| 97 | + for _, twid := range lbd.ThrottleWriteIOPSDevice { |
| 98 | + if twid.Major == major && twid.Minor == minor { |
| 99 | + found = true |
| 100 | + if twid.Rate != rate { |
| 101 | + return fmt.Errorf("blkio write iops for %d:%d is not set correctly, expect: %d, actual: %d", major, minor, rate, twid.Rate) |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + if !found { |
| 106 | + return fmt.Errorf("blkio write iops for %d:%d is not set", major, minor) |
| 107 | + } |
| 108 | + |
| 109 | + return nil |
| 110 | + }) |
| 111 | + |
| 112 | + if err != nil { |
| 113 | + util.Fatal(err) |
| 114 | + } |
| 115 | +} |
0 commit comments