From 44f9b4c818e8e24449ad732cb6d65fff732009b7 Mon Sep 17 00:00:00 2001 From: wanyaoqi Date: Wed, 13 Mar 2019 13:54:11 +0800 Subject: [PATCH] fix cgroups --- pkg/hostman/guestman/qemu-kvm.go | 33 +++++------------------------ pkg/util/cgrouputils/cgrouputils.go | 30 +++++++++++--------------- 2 files changed, 17 insertions(+), 46 deletions(-) diff --git a/pkg/hostman/guestman/qemu-kvm.go b/pkg/hostman/guestman/qemu-kvm.go index c2e9364916..b30030d91b 100644 --- a/pkg/hostman/guestman/qemu-kvm.go +++ b/pkg/hostman/guestman/qemu-kvm.go @@ -995,35 +995,12 @@ func (s *SKVMGuestInstance) setCgroupIo() { } func (s *SKVMGuestInstance) setCgroupCpu() { - cpu, _ := s.Desc.Int("cpu") - cgrouputils.CgroupSet(strconv.Itoa(s.cgroupPid), int(cpu)) + var ( + cpu, _ = s.Desc.Int("cpu") + cpuWeight = 1024 + ) - // TODO XXX - /* - var ( - cpuWeight = 1024 - cpuPeriod = 0 - cpuQuota = 0 - appTags = s.getApptags() - meta, _ = s.Desc.Get("metadata") - ) - - if meta != nil { - if meta.Contains("__cpu_weight") { - cpuWeight, _ = meta.Int("__cpu_weight") - } - if meta.Contains("__cpu_period") { - cpuPeriod, _ = meta.Int("__cpu_period") - } else { - cpuPeriod = -1 - } - if meta.Contains("__cpu_quota") { - cpuQuota, _ = meta.Int("__cpu_quota") - } else { - cpuQuota = -1 - } - } - */ + cgrouputils.CgroupSet(strconv.Itoa(s.cgroupPid), int(cpu)*cpuWeight) } func (s *SKVMGuestInstance) CreateFromDesc(desc jsonutils.JSONObject) error { diff --git a/pkg/util/cgrouputils/cgrouputils.go b/pkg/util/cgrouputils/cgrouputils.go index 1a71767992..79fbd9ae5c 100644 --- a/pkg/util/cgrouputils/cgrouputils.go +++ b/pkg/util/cgrouputils/cgrouputils.go @@ -30,6 +30,7 @@ var ( ) type ICGroupTask interface { + InitTask(hand ICGroupTask, coreNum int, pid string) SetPid(string) SetWeight(coreNum int) SetHand(hand ICGroupTask) @@ -108,19 +109,9 @@ func GetRootParam(module, name, pid string) string { } func SetRootParam(module, name, value, pid string) bool { - if param := GetRootParam(module, name, pid); param != value { - fi, err := os.Open(GetTaskParamPath(module, name, pid)) - if err == nil { - _, err = fi.Write([]byte(value)) - if err != nil { - err = fi.Close() - } else { - log.Errorln(err) - } - } else { - log.Errorln(err) - } - + param := GetRootParam(module, name, pid) + if param != value { + err := ioutil.WriteFile(GetTaskParamPath(module, name, pid), []byte(value), 0644) if err != nil { if len(pid) == 0 { pid = "root" @@ -168,6 +159,12 @@ func (c *CGroupTask) SetPid(pid string) { c.pid = pid } +func (c *CGroupTask) InitTask(hand ICGroupTask, coreNum int, pid string) { + c.SetHand(hand) + c.SetWeight(coreNum) + c.SetPid(pid) +} + func (c *CGroupTask) Module() string { return "" } @@ -536,9 +533,7 @@ func CgroupSet(pid string, coreNum int) bool { &CGroupMemoryTask{&CGroupTask{}}, } for _, hand := range tasks { - hand.SetHand(hand) - hand.SetPid(pid) - hand.SetWeight(coreNum) + hand.InitTask(hand, coreNum, pid) if !hand.SetTask() { return false } @@ -563,8 +558,7 @@ func CgroupDestroy(pid string) bool { &CGroupIOHardlimitTask{CGroupIOTask: &CGroupIOTask{&CGroupTask{}}}, } for _, hand := range tasks { - hand.SetHand(hand) - hand.SetPid(pid) + hand.InitTask(hand, 0, pid) if !hand.RemoveTask() { return false }