[lxc-devel] [lxd/master] cpu rebalance: support holes in @usage

hallyn on Github lxc-bot at linuxcontainers.org
Wed Apr 6 18:01:09 UTC 2016


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 596 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20160406/39b3930c/attachment.bin>
-------------- next part --------------
From 0c16c135d2df6a15b3aff48187aa235ede1c09e8 Mon Sep 17 00:00:00 2001
From: Serge Hallyn <serge.hallyn at ubuntu.com>
Date: Wed, 6 Apr 2016 12:56:59 -0500
Subject: [PATCH] cpu rebalance: support holes in @usage

Say you have 8 cpus, and two are offline.  If we try to dereference
usage[7] it will fail, because the array only has entires 0-6.

Turn usage into a map which supports holes, then use an array of
the entries to sort when rebalancing.

Signed-off-by: Serge Hallyn <serge.hallyn at ubuntu.com>
---
 lxd/devices.go | 22 ++++++++++++++++------
 1 file changed, 16 insertions(+), 6 deletions(-)

diff --git a/lxd/devices.go b/lxd/devices.go
index 38051d1..f25a02c 100644
--- a/lxd/devices.go
+++ b/lxd/devices.go
@@ -245,7 +245,7 @@ func deviceTaskBalance(d *Daemon) {
 
 	// Balance things
 	pinning := map[container][]string{}
-	usage := make(deviceTaskCPUs, 0)
+	usage := map[int]deviceTaskCPU{}
 
 	for _, id := range cpus {
 		cpu := deviceTaskCPU{}
@@ -254,11 +254,16 @@ func deviceTaskBalance(d *Daemon) {
 		count := 0
 		cpu.count = &count
 
-		usage = append(usage, cpu)
+		usage[id] = cpu
 	}
 
 	for cpu, ctns := range fixedContainers {
-		id := usage[cpu].strId
+		c, ok := usage[cpu]
+		if !ok {
+			shared.Log.Error("Internal error: container using unavailable cpu")
+			continue
+		}
+		id := c.strId
 		for _, ctn := range ctns {
 			_, ok := pinning[ctn]
 			if ok {
@@ -266,13 +271,18 @@ func deviceTaskBalance(d *Daemon) {
 			} else {
 				pinning[ctn] = []string{id}
 			}
-			*usage[cpu].count += 1
+			*c.count += 1
 		}
 	}
 
+	sortedUsage := make(deviceTaskCPUs, 0)
+	for _, value := range usage {
+		sortedUsage = append(sortedUsage, value)
+	}
+
 	for ctn, count := range balancedContainers {
-		sort.Sort(usage)
-		for _, cpu := range usage {
+		sort.Sort(sortedUsage)
+		for _, cpu := range sortedUsage {
 			if count == 0 {
 				break
 			}


More information about the lxc-devel mailing list