Fix handling of cpumask for docker driver. Raw driver still reports all

cpus. Will fix it to read cpumask for cgroup seperately.

Docker-DCO-1.1-Signed-off-by: Rohit Jnagal <jnagal@google.com> (github: rjnagal)
This commit is contained in:
Rohit Jnagal 2014-07-24 01:50:32 +00:00
parent 6213f8b0c3
commit 1fa48e8c39
4 changed files with 42 additions and 28 deletions

View File

@ -157,12 +157,12 @@ func libcontainerConfigToContainerSpec(config *libcontainer.Config, mi *info.Mac
if config.Cgroups.CpuShares != 0 { if config.Cgroups.CpuShares != 0 {
spec.Cpu.Limit = uint64(config.Cgroups.CpuShares) spec.Cpu.Limit = uint64(config.Cgroups.CpuShares)
} }
n := (mi.NumCores + 63) / 64 if config.Cgroups.CpusetCpus == "" {
spec.Cpu.Mask.Data = make([]uint64, n) // All cores are active.
for i := 0; i < n; i++ { spec.Cpu.Mask = fmt.Sprintf("0-%d", mi.NumCores-1)
spec.Cpu.Mask.Data[i] = math.MaxUint64 } else {
spec.Cpu.Mask = config.Cgroups.CpusetCpus
} }
// TODO(vmarmol): Get CPUs from config.Cgroups.CpusetCpus
return spec return spec
} }

View File

@ -15,9 +15,9 @@
package raw package raw
import ( import (
"fmt"
"io/ioutil" "io/ioutil"
"log" "log"
"math"
"path/filepath" "path/filepath"
"strconv" "strconv"
"strings" "strings"
@ -102,11 +102,7 @@ func (self *rawContainerHandler) GetSpec() (*info.ContainerSpec, error) {
spec.Cpu.Limit = readInt64(cpuRoot, "cpu.shares") spec.Cpu.Limit = readInt64(cpuRoot, "cpu.shares")
// TODO(vmarmol): Get CPUs from config.Cgroups.CpusetCpus // TODO(vmarmol): Get CPUs from config.Cgroups.CpusetCpus
n := (mi.NumCores + 63) / 64 spec.Cpu.Mask = fmt.Sprintf("0-%d", mi.NumCores-1)
spec.Cpu.Mask.Data = make([]uint64, n)
for i := 0; i < n; i++ {
spec.Cpu.Mask.Data[i] = math.MaxUint64
}
} }
} }

View File

@ -21,14 +21,10 @@ import (
"time" "time"
) )
type CpuSpecMask struct {
Data []uint64 `json:"data,omitempty"`
}
type CpuSpec struct { type CpuSpec struct {
Limit uint64 `json:"limit"` Limit uint64 `json:"limit"`
MaxLimit uint64 `json:"max_limit"` MaxLimit uint64 `json:"max_limit"`
Mask CpuSpecMask `json:"mask,omitempty"` Mask string `json:"mask,omitempty"`
} }
type MemorySpec struct { type MemorySpec struct {

View File

@ -88,18 +88,12 @@ func containerLink(container info.ContainerReference, basenameOnly bool, cssClas
return template.HTML(fmt.Sprintf("<a class=\"%s\" href=\"%s%s\">%s</a>", cssClasses, ContainersPage[:len(ContainersPage)-1], containerName, displayName)) return template.HTML(fmt.Sprintf("<a class=\"%s\" href=\"%s%s\">%s</a>", cssClasses, ContainersPage[:len(ContainersPage)-1], containerName, displayName))
} }
func printMask(mask *info.CpuSpecMask, numCores int) interface{} { func printMask(mask string, numCores int) interface{} {
// TODO(vmarmol): Detect this correctly.
// TODO(vmarmol): Support more than 64 cores.
rawMask := uint64(0)
if len(mask.Data) > 0 {
rawMask = mask.Data[0]
}
masks := make([]string, numCores) masks := make([]string, numCores)
for i := uint(0); i < uint(numCores); i++ { activeCores := getActiveCores(mask)
for i := 0; i < numCores; i++ {
coreClass := "inactive-cpu" coreClass := "inactive-cpu"
// by default, all cores are active if activeCores[i] {
if ((0x1<<i)&rawMask) != 0 || len(mask.Data) == 0 {
coreClass = "active-cpu" coreClass = "active-cpu"
} }
masks[i] = fmt.Sprintf("<span class=\"%s\">%d</span>", coreClass, i) masks[i] = fmt.Sprintf("<span class=\"%s\">%d</span>", coreClass, i)
@ -107,6 +101,34 @@ func printMask(mask *info.CpuSpecMask, numCores int) interface{} {
return template.HTML(strings.Join(masks, "&nbsp;")) return template.HTML(strings.Join(masks, "&nbsp;"))
} }
func getActiveCores(mask string) map[int]bool {
activeCores := make(map[int]bool)
for _, corebits := range strings.Split(mask, ",") {
cores := strings.Split(corebits, "-")
if len(cores) == 1 {
index, err := strconv.Atoi(cores[0])
if err != nil {
// Ignore malformed strings.
continue
}
activeCores[index] = true
} else if len(cores) == 2 {
start, err := strconv.Atoi(cores[0])
if err != nil {
continue
}
end, err := strconv.Atoi(cores[1])
if err != nil {
continue
}
for i := start; i <= end; i++ {
activeCores[i] = true
}
}
}
return activeCores
}
func printCores(millicores *uint64) string { func printCores(millicores *uint64) string {
// TODO(vmarmol): Detect this correctly // TODO(vmarmol): Detect this correctly
if *millicores > 1024*1000 { if *millicores > 1024*1000 {