修改cgroups中的定义

This commit is contained in:
lwch
2022-08-03 15:51:15 +08:00
parent 4c6e646d7f
commit b3c9b4a592
3 changed files with 13 additions and 52 deletions

View File

@@ -4,7 +4,6 @@
package limit
import (
"bufio"
"os"
"strconv"
"strings"
@@ -96,39 +95,33 @@ func limitMemory(group cgroups.Cgroup, limit int64) {
}
func limitDisk(group cgroups.Cgroup, limits diskLimits) {
mnt := readMnt()
var block specs.LinuxBlockIO
for _, disk := range limits {
dev := mnt[disk.MountPoint]
if len(dev) == 0 {
logging.Warning("can not get dev of mount_point %s", disk.MountPoint)
continue
}
write := func(value uint64, target []specs.LinuxThrottleDevice) []specs.LinuxThrottleDevice {
var device specs.LinuxThrottleDevice
device.Major, device.Minor = parseDev(dev)
device.Major, device.Minor = parseDev(disk.Dev)
device.Rate = value
return append(target, device)
}
if disk.ReadBytes > 0 {
block.ThrottleReadBpsDevice = write(disk.ReadBytes, block.ThrottleReadBpsDevice)
logging.Info(" - set read_bytes limit by mount_point %s(%s): %s",
disk.MountPoint, dev, humanize.IBytes(disk.ReadBytes))
logging.Info(" - set read_bytes limit by mount_point %s: %s",
disk.Dev, humanize.IBytes(disk.ReadBytes))
}
if disk.WriteBytes > 0 {
block.ThrottleWriteBpsDevice = write(disk.WriteBytes, block.ThrottleWriteBpsDevice)
logging.Info(" - set write_bytes limit by mount_point %s(%s): %s",
disk.MountPoint, dev, humanize.IBytes(disk.WriteBytes))
logging.Info(" - set write_bytes limit by mount_point %s: %s",
disk.Dev, humanize.IBytes(disk.WriteBytes))
}
if disk.ReadIOPS > 0 {
block.ThrottleReadIOPSDevice = write(disk.ReadIOPS, block.ThrottleReadIOPSDevice)
logging.Info(" - set read_iops limit by mount_point %s(%s): %s",
disk.MountPoint, dev, humanize.IBytes(disk.ReadIOPS))
logging.Info(" - set read_iops limit by mount_point %s: %s",
disk.Dev, humanize.IBytes(disk.ReadIOPS))
}
if disk.WriteIOPS > 0 {
block.ThrottleWriteIOPSDevice = write(disk.WriteIOPS, block.ThrottleWriteIOPSDevice)
logging.Info(" - set write_iops limit by mount_point %s(%s): %s",
disk.MountPoint, dev, humanize.IBytes(disk.WriteIOPS))
logging.Info(" - set write_iops limit by mount_point %s: %s",
disk.Dev, humanize.IBytes(disk.WriteIOPS))
}
}
err := group.Update(&specs.LinuxResources{
@@ -141,38 +134,6 @@ func limitDisk(group cgroups.Cgroup, limits diskLimits) {
logging.Info("set disk_limit successed")
}
// mount_point => dev
func readMnt() map[string]string {
f, err := os.Open("/proc/self/mountinfo")
if err != nil {
logging.Warning("can not open /proc/self/mountinfo: %v", err)
return nil
}
defer f.Close()
s := bufio.NewScanner(f)
ret := make(map[string]string)
for s.Scan() {
tmp := strings.Split(s.Text(), " ")
// id: 1
// parent: 2
// maj:min: 3
// root: 4
// target: 5
// vfs options: 6
// optional: 7, next "-"
// fs type: 8
// source: 9
// fs options: 10
// ret[tmp[2]] = tmp[8]
if tmp[6] == "-" {
ret[tmp[8]] = tmp[2]
} else {
ret[tmp[9]] = tmp[2]
}
}
return ret
}
func parseDev(str string) (int64, int64) {
tmp := strings.SplitN(str, ":", 2)
major, _ := strconv.ParseInt(tmp[0], 10, 64)

View File

@@ -11,8 +11,8 @@ func TestCGroups(t *testing.T) {
cfg.CpuQuota = 100 // 1core
cfg.Memory = utils.Bytes(100 * 1024 * 1024) // 100MB
cfg.Disks = append(cfg.Disks, DiskLimit{
MountPoint: "/dev/sdb",
ReadBytes: 1024, // 1KB
Dev: "8:0",
ReadBytes: 1024, // 1KB
})
// want root permission
cfg.Do("testing")

View File

@@ -7,8 +7,8 @@ import (
)
type DiskLimit struct {
// mountpoint eg: /
MountPoint string `json:"mount_point" yaml:"mount_point" kv:"mount_point"`
// device version of lsblk command eg: 8:0
Dev string `json:"dev" yaml:"dev" kv:"dev"`
// read bytes
ReadBytes uint64 `json:"read_bytes" yaml:"read_bytes" kv:"read_bytes"`
// write bytes