[lxc-devel] [lxd/master] Final bits of CGroupV2 support

stgraber on Github lxc-bot at linuxcontainers.org
Wed Nov 11 22:41:17 UTC 2020


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 558 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20201111/1725ec0f/attachment.bin>
-------------- next part --------------
From dc00b3e9193ec8ed2570be5acf9dc6db46d10d55 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Wed, 11 Nov 2020 16:54:35 -0500
Subject: [PATCH 1/4] lxd/cgroup: Support SetCPUShare on V2
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: Stéphane Graber <stgraber at ubuntu.com>
---
 lxd/cgroup/abstraction.go | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lxd/cgroup/abstraction.go b/lxd/cgroup/abstraction.go
index f78da3ab95..892ccc541d 100644
--- a/lxd/cgroup/abstraction.go
+++ b/lxd/cgroup/abstraction.go
@@ -447,7 +447,7 @@ func (cg *CGroup) SetCPUShare(limit int64) error {
 	case V1:
 		return cg.rw.Set(version, "cpu", "cpu.shares", fmt.Sprintf("%d", limit))
 	case V2:
-		return ErrControllerMissing
+		return cg.rw.Set(version, "cpu", "cpu.weight", fmt.Sprintf("%d", limit))
 	}
 
 	return ErrUnknownVersion

From 5253004394d1d8dde9b9b069c5534b42a27b9cd9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Wed, 11 Nov 2020 17:03:43 -0500
Subject: [PATCH 2/4] lxd/cgroup: Implement SetCPUCfsLimit for V2
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: Stéphane Graber <stgraber at ubuntu.com>
---
 lxd/cgroup/abstraction.go | 34 ++++++++++++++++------------------
 1 file changed, 16 insertions(+), 18 deletions(-)

diff --git a/lxd/cgroup/abstraction.go b/lxd/cgroup/abstraction.go
index 892ccc541d..c57ae2e3bc 100644
--- a/lxd/cgroup/abstraction.go
+++ b/lxd/cgroup/abstraction.go
@@ -453,32 +453,30 @@ func (cg *CGroup) SetCPUShare(limit int64) error {
 	return ErrUnknownVersion
 }
 
-// SetCPUCfsPeriod sets the duration in ms for each scheduling period
-func (cg *CGroup) SetCPUCfsPeriod(limit int64) error {
+// SetCPUCfsLimit sets the quota and duration in ms for each scheduling period
+func (cg *CGroup) SetCPUCfsLimit(limitPeriod int64, limitQuota int64) error {
 	version := cgControllers["cpu"]
 	switch version {
 	case Unavailable:
 		return ErrControllerMissing
 	case V1:
-		return cg.rw.Set(version, "cpu", "cpu.cfs_period_us", fmt.Sprintf("%d", limit))
-	case V2:
-		return ErrControllerMissing
-	}
+		err := cg.rw.Set(version, "cpu", "cpu.cfs_quota_us", fmt.Sprintf("%d", limitQuota))
+		if err != nil {
+			return err
+		}
 
-	return ErrUnknownVersion
-}
+		err = cg.rw.Set(version, "cpu", "cpu.cfs_period_us", fmt.Sprintf("%d", limitPeriod))
+		if err != nil {
+			return err
+		}
 
-// SetCPUCfsQuota sets the max time in ms during each cfs_period_us that
-// the current group can run for
-func (cg *CGroup) SetCPUCfsQuota(limit int64) error {
-	version := cgControllers["cpu"]
-	switch version {
-	case Unavailable:
-		return ErrControllerMissing
-	case V1:
-		return cg.rw.Set(version, "cpu", "cpu.cfs_quota_us", fmt.Sprintf("%d", limit))
+		return nil
 	case V2:
-		return ErrControllerMissing
+		if limitPeriod == -1 && limitQuota == -1 {
+			return cg.rw.Set(version, "cpu", "cpu.max", "max")
+		}
+
+		return cg.rw.Set(version, "cpu", "cpu.max", fmt.Sprintf("%d %d", limitQuota, limitPeriod))
 	}
 
 	return ErrUnknownVersion

From 0e05ad3b98ea956767102fddc51e1f5d00bb408a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Wed, 11 Nov 2020 17:03:50 -0500
Subject: [PATCH 3/4] lxd/instance/lxc: Port to SetCPUCfsLimit
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: Stéphane Graber <stgraber at ubuntu.com>
---
 lxd/instance/drivers/driver_lxc.go | 18 ++++--------------
 1 file changed, 4 insertions(+), 14 deletions(-)

diff --git a/lxd/instance/drivers/driver_lxc.go b/lxd/instance/drivers/driver_lxc.go
index b02f212c3e..101a0078d7 100644
--- a/lxd/instance/drivers/driver_lxc.go
+++ b/lxd/instance/drivers/driver_lxc.go
@@ -1209,15 +1209,8 @@ func (c *lxc) initLXC(config bool) error {
 			}
 		}
 
-		if cpuCfsPeriod != -1 {
-			err = cg.SetCPUCfsPeriod(cpuCfsPeriod)
-			if err != nil {
-				return err
-			}
-		}
-
-		if cpuCfsQuota != -1 {
-			err = cg.SetCPUCfsQuota(cpuCfsQuota)
+		if cpuCfsPeriod != -1 && cpuCfsQuota != -1 {
+			err = cg.SetCPUCfsLimit(cpuCfsPeriod, cpuCfsQuota)
 			if err != nil {
 				return err
 			}
@@ -4428,16 +4421,13 @@ func (c *lxc) Update(args db.InstanceArgs, userRequested bool) error {
 				if err != nil {
 					return err
 				}
+
 				err = cg.SetCPUShare(cpuShares)
 				if err != nil {
 					return err
 				}
-				err = cg.SetCPUCfsPeriod(cpuCfsPeriod)
-				if err != nil {
-					return err
-				}
 
-				err = cg.SetCPUCfsQuota(cpuCfsQuota)
+				err = cg.SetCPUCfsLimit(cpuCfsPeriod, cpuCfsQuota)
 				if err != nil {
 					return err
 				}

From b9b1ed31c108cae7227bea588ac9735c098123fe Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Wed, 11 Nov 2020 17:21:25 -0500
Subject: [PATCH 4/4] lxd/cgroup: Support CGroup V2 in ParseCPU
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: Stéphane Graber <stgraber at ubuntu.com>
---
 lxd/cgroup/cgroup_cpu.go | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)

diff --git a/lxd/cgroup/cgroup_cpu.go b/lxd/cgroup/cgroup_cpu.go
index c808d3392b..bf048dfe85 100644
--- a/lxd/cgroup/cgroup_cpu.go
+++ b/lxd/cgroup/cgroup_cpu.go
@@ -23,6 +23,12 @@ func TaskSchedulerTrigger(srcType string, srcName string, srcStatus string) {
 func ParseCPU(cpuAllowance string, cpuPriority string) (int64, int64, int64, error) {
 	var err error
 
+	// Max shares depending on backend.
+	maxShares := int64(1024)
+	if cgControllers["cpu"] == V2 {
+		maxShares = 100
+	}
+
 	// Parse priority
 	cpuShares := int64(0)
 	cpuPriorityInt := 10
@@ -36,7 +42,7 @@ func ParseCPU(cpuAllowance string, cpuPriority string) (int64, int64, int64, err
 
 	// Parse allowance
 	cpuCfsQuota := int64(-1)
-	cpuCfsPeriod := int64(100000)
+	cpuCfsPeriod := int64(-1)
 
 	if cpuAllowance != "" {
 		if strings.HasSuffix(cpuAllowance, "%") {
@@ -46,7 +52,7 @@ func ParseCPU(cpuAllowance string, cpuPriority string) (int64, int64, int64, err
 				return -1, -1, -1, err
 			}
 
-			cpuShares += int64((10 * percent) + 24)
+			cpuShares += int64(float64(maxShares) / float64(100) * float64(percent))
 		} else {
 			// Time based allocation
 			fields := strings.SplitN(cpuAllowance, "/", 2)
@@ -67,11 +73,11 @@ func ParseCPU(cpuAllowance string, cpuPriority string) (int64, int64, int64, err
 			// Set limit in ms
 			cpuCfsQuota = int64(quota * 1000)
 			cpuCfsPeriod = int64(period * 1000)
-			cpuShares += 1024
+			cpuShares += maxShares
 		}
 	} else {
 		// Default is 100%
-		cpuShares += 1024
+		cpuShares += maxShares
 	}
 
 	// Deal with a potential negative score


More information about the lxc-devel mailing list