Add BUILD files and Boilerplates

Updates based on comments
* Export comments added
* glog changed to klog
* Other small edits
This commit is contained in:
nolancon 2019-01-16 14:05:42 +00:00
parent f10e76962f
commit a273333f1f
5 changed files with 286 additions and 122 deletions

View File

@ -166,6 +166,7 @@ filegroup(
"//pkg/kubelet/cm/cpumanager:all-srcs", "//pkg/kubelet/cm/cpumanager:all-srcs",
"//pkg/kubelet/cm/cpuset:all-srcs", "//pkg/kubelet/cm/cpuset:all-srcs",
"//pkg/kubelet/cm/devicemanager:all-srcs", "//pkg/kubelet/cm/devicemanager:all-srcs",
"//pkg/kubelet/cm/topologymanager:all-srcs",
"//pkg/kubelet/cm/util:all-srcs", "//pkg/kubelet/cm/util:all-srcs",
], ],
tags = ["automanaged"], tags = ["automanaged"],

View File

@ -0,0 +1,23 @@
load("@io_bazel_rules_go//go:def.bzl", "go_test")
go_test(
name = "go_default_test",
srcs = ["socketmask_test.go"],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [
":package-srcs",
"//pkg/kubelet/cm/topologymanager/socketmask:all-srcs",
],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)

View File

@ -0,0 +1,23 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")
go_library(
name = "go_default_library",
srcs = ["socketmask.go"],
importpath = "k8s.io/kubernetes/pkg/kubelet/cm/topologymanager/socketmask",
visibility = ["//visibility:public"],
deps = ["//vendor/k8s.io/klog:go_default_library"],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)

View File

@ -1,52 +1,68 @@
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package socketmask package socketmask
import ( import (
"bytes"
"k8s.io/klog"
"math"
"strconv" "strconv"
"strings" "strings"
"bytes"
"math"
"github.com/golang/glog"
) )
// SocketMask represents the NUMA affinity of a socket.
type SocketMask []int64 type SocketMask []int64
// NewSocketMask creates a new socket mask.
func NewSocketMask(Mask []int64) SocketMask { func NewSocketMask(Mask []int64) SocketMask {
sm := Mask return Mask
return sm
} }
// GetSocketMask calculates the socket mask.
func (sm SocketMask) GetSocketMask(socketMask []SocketMask, maskHolder []string, count int) (SocketMask, []string) { func (sm SocketMask) GetSocketMask(socketMask []SocketMask, maskHolder []string, count int) (SocketMask, []string) {
var socketAffinityInt64 [][]int64 var socketAffinityInt64 [][]int64
for r := range socketMask { for r := range socketMask {
socketAffinityVals := []int64(socketMask[r]) socketAffinityVals := []int64(socketMask[r])
socketAffinityInt64 = append(socketAffinityInt64,socketAffinityVals) socketAffinityInt64 = append(socketAffinityInt64, socketAffinityVals)
} }
if count == 0 { if count == 0 {
maskHolder = buildMaskHolder(socketAffinityInt64) maskHolder = buildMaskHolder(socketAffinityInt64)
} }
glog.Infof("[socketmask] MaskHolder : %v", maskHolder) klog.V(4).Infof("[socketmask] MaskHolder : %v", maskHolder)
glog.Infof("[socketmask] %v is passed into arrange function", socketMask) klog.V(4).Infof("[socketmask] %v is passed into arrange function", socketMask)
arrangedMask := arrangeMask(socketAffinityInt64) arrangedMask := arrangeMask(socketAffinityInt64)
newMask := getTopologyAffinity(arrangedMask, maskHolder) newMask := getTopologyAffinity(arrangedMask, maskHolder)
glog.Infof("[socketmask] New Mask after getTopologyAffinity (new mask) : %v ",newMask) klog.V(4).Infof("[socketmask] New Mask after getTopologyAffinity (new mask) : %v ", newMask)
finalMaskValue := parseMask(newMask) finalMaskValue := parseMask(newMask)
glog.Infof("[socketmask] Mask []Int64 (finalMaskValue): %v", finalMaskValue) klog.V(4).Infof("[socketmask] Mask []Int64 (finalMaskValue): %v", finalMaskValue)
maskHolder = newMask maskHolder = newMask
glog.Infof("[socketmask] New MaskHolder: %v", maskHolder) klog.V(4).Infof("[socketmask] New MaskHolder: %v", maskHolder)
finalSocketMask := SocketMask(finalMaskValue) return SocketMask(finalMaskValue), maskHolder
return finalSocketMask, maskHolder
} }
func buildMaskHolder(mask [][]int64) []string { func buildMaskHolder(mask [][]int64) []string {
var maskHolder []string
outerLen := len(mask) outerLen := len(mask)
var innerLen int = 0 var innerLen int
for i := 0; i < outerLen; i++ { for i := 0; i < outerLen; i++ {
if innerLen < len(mask[i]) { if innerLen < len(mask[i]) {
innerLen = len(mask[i]) innerLen = len(mask[i])
} }
} }
var maskHolder []string
var buffer bytes.Buffer var buffer bytes.Buffer
var i, j int = 0, 0 var i, j int = 0, 0
for i = 0; i < outerLen; i++ { for i = 0; i < outerLen; i++ {
@ -61,21 +77,21 @@ func buildMaskHolder(mask [][]int64) []string {
func getTopologyAffinity(arrangedMask, maskHolder []string) []string { func getTopologyAffinity(arrangedMask, maskHolder []string) []string {
var topologyTemp []string var topologyTemp []string
for i:= 0; i < (len(maskHolder)); i++ { for i := 0; i < len(maskHolder); i++ {
for j:= 0; j < (len(arrangedMask)); j++ { for j := 0; j < len(arrangedMask); j++ {
tempStr := andOperation(maskHolder[i],arrangedMask[j]) tempStr := andOperation(maskHolder[i], arrangedMask[j])
if strings.Contains(tempStr, "1") { if strings.Contains(tempStr, "1") {
topologyTemp = append(topologyTemp, tempStr ) topologyTemp = append(topologyTemp, tempStr)
} }
} }
} }
duplicates := map[string]bool{} duplicates := map[string]bool{}
for v:= range topologyTemp { for v := range topologyTemp {
duplicates[topologyTemp[v]] = true duplicates[topologyTemp[v]] = true
} }
// Place all keys from the map into a slice. // Place all keys from the map into a slice.
topologyResult := []string{} topologyResult := []string{}
for key, _ := range duplicates { for key := range duplicates {
topologyResult = append(topologyResult, key) topologyResult = append(topologyResult, key)
} }
@ -99,12 +115,12 @@ func parseMask(mask []string) []int64 {
for _, char := range maskStr { for _, char := range maskStr {
convertedStr, err := strconv.Atoi(string(char)) convertedStr, err := strconv.Atoi(string(char))
if err != nil { if err != nil {
glog.Errorf("Could not convert string to int. Err: %v", err) klog.V(4).Infof("could not convert mask character: %v", err)
return maskInt return maskInt
} }
maskInt = append(maskInt, int64(convertedStr)) maskInt = append(maskInt, int64(convertedStr))
} }
glog.Infof("[socketmask] Mask Int in Parse Mask: %v", maskInt) klog.V(4).Infof("[socketmask] Mask Int in Parse Mask: %v", maskInt)
return maskInt return maskInt
} }
@ -127,7 +143,7 @@ func arrangeMask(mask [][]int64) []string {
return socketStr return socketStr
} }
func andOperation(val1, val2 string) (string) { func andOperation(val1, val2 string) string {
l1, l2 := len(val1), len(val2) l1, l2 := len(val1), len(val2)
//compare lengths of strings - pad shortest with trailing zeros //compare lengths of strings - pad shortest with trailing zeros
if l1 != l2 { if l1 != l2 {
@ -143,12 +159,9 @@ func andOperation(val1, val2 string) (string) {
} }
length := len(val1) length := len(val1)
byteArr := make([]byte, length) byteArr := make([]byte, length)
for i := 0; i < length ; i++ { for i := 0; i < length; i++ {
byteArr[i] = (val1[i] & val2[i]) byteArr[i] = val1[i] & val2[i]
} }
var finalStr string
finalStr = string(byteArr[:])
return finalStr return string(byteArr[:])
} }

View File

@ -0,0 +1,104 @@
/*
Copyright 2015 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package socketmask
import (
"reflect"
"testing"
)
func TestGetSocketmask(t *testing.T) {
tcases := []struct {
name string
socketmask []SocketMask
maskholder []string
count int
expectedMaskHolder []string
expectedFinalSocketMask SocketMask
}{
{
name: "Empty MaskHolder and count set as 0",
socketmask: []SocketMask{{1, 0}, {0, 1}, {1, 1}},
maskholder: []string{""},
count: 0,
expectedMaskHolder: []string{"10", "01", "11"},
expectedFinalSocketMask: SocketMask{1, 0},
},
{
name: "MaskHolder non zero, count set as 1",
socketmask: []SocketMask{{1, 0}, {0, 1}, {1, 1}},
maskholder: []string{"10", "01", "11"},
count: 1,
expectedMaskHolder: []string{"10", "01", "11"},
expectedFinalSocketMask: SocketMask{1, 0},
},
{
name: "Empty MaskHolder and count set as 0",
socketmask: []SocketMask{{0, 1}, {1, 1}},
maskholder: []string{""},
count: 0,
expectedMaskHolder: []string{"01", "11"},
expectedFinalSocketMask: SocketMask{0, 1},
},
{
name: "MaskHolder non zero, count set as 1",
socketmask: []SocketMask{{0, 1}, {1, 1}},
maskholder: []string{"01", "11"},
count: 1,
expectedMaskHolder: []string{"01", "11"},
expectedFinalSocketMask: SocketMask{0, 1},
},
}
for _, tc := range tcases {
sm := NewSocketMask(nil)
actualSocketMask, actualMaskHolder := sm.GetSocketMask(tc.socketmask, tc.maskholder, tc.count)
if !reflect.DeepEqual(actualSocketMask, tc.expectedFinalSocketMask) {
t.Errorf("Expected final socketmask to be %v, got %v", tc.expectedFinalSocketMask, actualSocketMask)
}
if !reflect.DeepEqual(actualMaskHolder, tc.expectedMaskHolder) {
t.Errorf("Expected maskholder to be %v, got %v", tc.expectedMaskHolder, actualMaskHolder)
}
}
}
func TestNewSocketMask(t *testing.T) {
tcases := []struct {
name string
Mask []int64
expectedMask []int64
}{
{
name: "Mask as an int64 binary array",
Mask: []int64{1, 0},
expectedMask: []int64{1, 0},
},
}
for _, tc := range tcases {
sm := NewSocketMask(nil)
if reflect.DeepEqual(sm, tc.expectedMask) {
t.Errorf("Expected socket mask to be %v, got %v", tc.expectedMask, sm)
}
}
}