Update SocketMask based on feedback

TODO: Unit tests to be updated
This commit is contained in:
nolancon 2019-05-27 04:23:13 +01:00
parent e8566caa3f
commit 283dff9335
2 changed files with 108 additions and 119 deletions

View File

@ -5,7 +5,6 @@ go_library(
srcs = ["socketmask.go"], srcs = ["socketmask.go"],
importpath = "k8s.io/kubernetes/pkg/kubelet/cm/topologymanager/socketmask", importpath = "k8s.io/kubernetes/pkg/kubelet/cm/topologymanager/socketmask",
visibility = ["//visibility:public"], visibility = ["//visibility:public"],
deps = ["//vendor/k8s.io/klog:go_default_library"],
) )
filegroup( filegroup(

View File

@ -17,146 +17,136 @@ limitations under the License.
package socketmask package socketmask
import ( import (
"bytes" "fmt"
"k8s.io/klog"
"math"
"strconv"
"strings"
) )
// SocketMask represents the NUMA affinity of a socket. //SocketMask interface allows hint providers to create SocketMasks for TopologyHints
type SocketMask []int64 type SocketMask interface {
Add(sockets ...int) error
// NewSocketMask creates a new socket mask. Remove(sockets ...int) error
func NewSocketMask(Mask []int64) SocketMask { And(masks ...SocketMask)
return Mask Or(masks ...SocketMask)
Clear()
Fill()
IsEqual(mask SocketMask) bool
IsEmpty() bool
IsSet(socket int) bool
String() string
Count() int
GetSockets() []int
} }
// GetSocketMask calculates the socket mask. type socketMask uint64
func (sm SocketMask) GetSocketMask(socketMaskInt64 [][]int64, maskHolder []string, count int) (SocketMask, []string) {
if count == 0 { //NewSocketMask creates a new SocketMask
maskHolder = buildMaskHolder(socketMaskInt64) func NewSocketMask(sockets ...int) (SocketMask, error) {
s := socketMask(0)
err := (&s).Add(sockets...)
if err != nil {
return nil, err
} }
klog.V(4).Infof("[socketmask] MaskHolder : %v", maskHolder) return &s, nil
klog.V(4).Infof("[socketmask] %v is passed into arrange function", socketMaskInt64)
arrangedMask := arrangeMask(socketMaskInt64)
newMask := getTopologyAffinity(arrangedMask, maskHolder)
klog.V(4).Infof("[socketmask] New Mask after getTopologyAffinity (new mask) : %v ", newMask)
finalMaskValue := parseMask(newMask)
klog.V(4).Infof("[socketmask] Mask []Int64 (finalMaskValue): %v", finalMaskValue)
maskHolder = newMask
klog.V(4).Infof("[socketmask] New MaskHolder: %v", maskHolder)
return SocketMask(finalMaskValue), maskHolder
} }
func buildMaskHolder(mask [][]int64) []string { //Add adds the sockets with topology affinity to the SocketMask
outerLen := len(mask) func (s *socketMask) Add(sockets ...int) error {
var innerLen int mask := *s
for i := 0; i < outerLen; i++ { for _, i := range sockets {
if innerLen < len(mask[i]) { if i < 0 || i >= 64 {
innerLen = len(mask[i]) return fmt.Errorf("socket number must be in range 0-63")
} }
mask |= 1 << uint64(i)
} }
var maskHolder []string *s = mask
var buffer bytes.Buffer return nil
var i, j int = 0, 0
for i = 0; i < outerLen; i++ {
for j = 0; j < innerLen; j++ {
buffer.WriteString("1")
}
maskHolder = append(maskHolder, buffer.String())
buffer.Reset()
}
return maskHolder
} }
func getTopologyAffinity(arrangedMask, maskHolder []string) []string { //Remove removes specified sockets from SocketMask
var topologyTemp []string func (s *socketMask) Remove(sockets ...int) error {
for i := 0; i < len(maskHolder); i++ { mask := *s
for j := 0; j < len(arrangedMask); j++ { for _, i := range sockets {
tempStr := andOperation(maskHolder[i], arrangedMask[j]) if i < 0 || i >= 64 {
if strings.Contains(tempStr, "1") { return fmt.Errorf("socket number must be in range 0-63")
topologyTemp = append(topologyTemp, tempStr)
}
} }
mask &^= 1 << uint64(i)
} }
duplicates := map[string]bool{} *s = mask
for v := range topologyTemp { return nil
duplicates[topologyTemp[v]] = true
}
// Place all keys from the map into a slice.
topologyResult := []string{}
for key := range duplicates {
topologyResult = append(topologyResult, key)
}
return topologyResult
} }
func parseMask(mask []string) []int64 { //And performs and operation on all bits in masks
var maskStr string func (s *socketMask) And(masks ...SocketMask) {
min := strings.Count(mask[0], "1") for _, m := range masks {
var num, index int *s &= *m.(*socketMask)
for i := 0; i < len(mask); i++ {
num = strings.Count(mask[i], "1")
if num < min {
min = num
index = i
}
maskStr = mask[index]
} }
var maskInt []int64
for _, char := range maskStr {
convertedStr, err := strconv.Atoi(string(char))
if err != nil {
klog.V(4).Infof("could not convert mask character: %v", err)
return maskInt
}
maskInt = append(maskInt, int64(convertedStr))
}
klog.V(4).Infof("[socketmask] Mask Int in Parse Mask: %v", maskInt)
return maskInt
} }
func arrangeMask(mask [][]int64) []string { //Or performs or operation on all bits in masks
var socketStr []string func (s *socketMask) Or(masks ...SocketMask) {
var bufferNew bytes.Buffer for _, m := range masks {
outerLen := len(mask) *s |= *m.(*socketMask)
innerLen := len(mask[0])
for i := 0; i < outerLen; i++ {
for j := 0; j < innerLen; j++ {
if mask[i][j] == 1 {
bufferNew.WriteString("1")
} else if mask[i][j] == 0 {
bufferNew.WriteString("0")
}
}
socketStr = append(socketStr, bufferNew.String())
bufferNew.Reset()
} }
return socketStr
} }
func andOperation(val1, val2 string) string { //Clear resets all bits in mask to zero
l1, l2 := len(val1), len(val2) func (s *socketMask) Clear() {
//compare lengths of strings - pad shortest with trailing zeros *s = 0
if l1 != l2 { }
// Get the bit difference
var num int //Fill sets all bits in mask to one
diff := math.Abs(float64(l1) - float64(l2)) func (s *socketMask) Fill() {
num = int(diff) *s = socketMask(^uint64(0))
if l1 < l2 { }
val1 = val1 + strings.Repeat("0", num)
//IsEmpty checks mask to see if all bits are zero
func (s *socketMask) IsEmpty() bool {
return *s == 0
}
//IsSet checks socket in mask to see if bit is set to one
func (s *socketMask) IsSet(socket int) bool {
if socket < 0 || socket >= 64 {
return false
}
return (*s & (1 << uint64(socket))) > 0
}
//IsEqual checks if masks are equal
func (s *socketMask) IsEqual(mask SocketMask) bool {
return *s == *mask.(*socketMask)
}
//String converts mask to string
func (s *socketMask) String() string {
str := ""
for i := uint64(0); i < 64; i++ {
if (*s & (1 << i)) > 0 {
str += "1"
} else { } else {
val2 = val2 + strings.Repeat("0", num) str += "0"
} }
} }
length := len(val1) return str
byteArr := make([]byte, length) }
for i := 0; i < length; i++ {
byteArr[i] = val1[i] & val2[i] //Count counts number of bits in mask set to one
} func (s *socketMask) Count() int {
count := 0
return string(byteArr[:]) for i := uint64(0); i < 64; i++ {
if (*s & (1 << i)) > 0 {
count++
}
}
return count
}
//GetSockets returns each socket number with bits set to one
func (s *socketMask) GetSockets() []int {
var sockets []int
for i := uint64(0); i < 64; i++ {
if (*s & (1 << i)) > 0 {
sockets = append(sockets, int(i))
}
}
return sockets
} }