Skip to content

Commit

Permalink
Merge pull request kubernetes#78587 from kad/socketmask-string
Browse files Browse the repository at this point in the history
Use go standard library for common bit operations
  • Loading branch information
k8s-ci-robot authored Aug 13, 2019
2 parents 61af419 + 89481f8 commit ac2295a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 31 deletions.
19 changes: 3 additions & 16 deletions pkg/kubelet/cm/topologymanager/socketmask/socketmask.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package socketmask

import (
"fmt"
"math/bits"
)

// SocketMask interface allows hint providers to create SocketMasks for TopologyHints
Expand Down Expand Up @@ -139,26 +140,12 @@ func (s *socketMask) IsNarrowerThan(mask SocketMask) bool {

// 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 {
str += "0"
}
}
return str
return fmt.Sprintf("%064b", *s)
}

// Count counts number of bits in mask set to one
func (s *socketMask) Count() int {
count := 0
for i := uint64(0); i < 64; i++ {
if (*s & (1 << i)) > 0 {
count++
}
}
return count
return bits.OnesCount64(uint64(*s))
}

// GetSockets returns each socket number with bits set to one
Expand Down
30 changes: 15 additions & 15 deletions pkg/kubelet/cm/topologymanager/socketmask/socketmask_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ func TestNewSocketMask(t *testing.T) {
}{
{
name: "New SocketMask with socket 0 set",
socket: int(0),
expectedMask: "1000000000000000000000000000000000000000000000000000000000000000",
socket: 0,
expectedMask: "0000000000000000000000000000000000000000000000000000000000000001",
},
}
for _, tc := range tcases {
Expand All @@ -52,7 +52,7 @@ func TestAdd(t *testing.T) {
name: "New SocketMask with sockets 0 and 1 set",
firstSocket: 0,
secondSocket: 1,
expectedMask: "1100000000000000000000000000000000000000000000000000000000000000",
expectedMask: "0000000000000000000000000000000000000000000000000000000000000011",
},
}
for _, tc := range tcases {
Expand All @@ -77,7 +77,7 @@ func TestRemove(t *testing.T) {
firstSocketSet: 0,
secondSocketSet: 1,
firstSocketRemove: 0,
expectedMask: "0100000000000000000000000000000000000000000000000000000000000000",
expectedMask: "0000000000000000000000000000000000000000000000000000000000000010",
},
}
for _, tc := range tcases {
Expand All @@ -100,7 +100,7 @@ func TestAnd(t *testing.T) {
name: "And socket masks",
firstMaskBit: 0,
secondMaskBit: 0,
andMask: "1000000000000000000000000000000000000000000000000000000000000000",
andMask: "0000000000000000000000000000000000000000000000000000000000000001",
},
}
for _, tc := range tcases {
Expand Down Expand Up @@ -128,9 +128,9 @@ func TestOr(t *testing.T) {
}{
{
name: "Or socket masks",
firstMaskBit: int(0),
secondMaskBit: int(1),
orMask: "1100000000000000000000000000000000000000000000000000000000000000",
firstMaskBit: 0,
secondMaskBit: 1,
orMask: "0000000000000000000000000000000000000000000000000000000000000011",
},
}
for _, tc := range tcases {
Expand Down Expand Up @@ -158,8 +158,8 @@ func TestClear(t *testing.T) {
}{
{
name: "Clear socket masks",
firstBit: int(0),
secondBit: int(1),
firstBit: 0,
secondBit: 1,
clearedMask: "0000000000000000000000000000000000000000000000000000000000000000",
},
}
Expand Down Expand Up @@ -199,7 +199,7 @@ func TestIsEmpty(t *testing.T) {
}{
{
name: "Check if mask is empty",
maskBit: int(0),
maskBit: 0,
expectedEmpty: false,
},
}
Expand All @@ -220,7 +220,7 @@ func TestIsSet(t *testing.T) {
}{
{
name: "Check if mask bit is set",
maskBit: int(0),
maskBit: 0,
expectedSet: true,
},
}
Expand All @@ -242,8 +242,8 @@ func TestIsEqual(t *testing.T) {
}{
{
name: "Check if two socket masks are equal",
firstMaskBit: int(0),
secondMaskBit: int(0),
firstMaskBit: 0,
secondMaskBit: 0,
isEqual: true,
},
}
Expand All @@ -265,7 +265,7 @@ func TestCount(t *testing.T) {
}{
{
name: "Count number of bits set in full mask",
maskBit: 0,
maskBit: 42,
expectedCount: 1,
},
}
Expand Down

0 comments on commit ac2295a

Please sign in to comment.