Skip to content

Commit

Permalink
Merge pull request #80315 from klueska/upstream-cleanup-socketmask
Browse files Browse the repository at this point in the history
Cleanup the TopologyManager socketmask abstraction
  • Loading branch information
k8s-ci-robot committed Jul 23, 2019
2 parents 304e2c2 + 65b0731 commit 5b496fe
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 14 deletions.
48 changes: 34 additions & 14 deletions pkg/kubelet/cm/topologymanager/socketmask/socketmask.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"fmt"
)

//SocketMask interface allows hint providers to create SocketMasks for TopologyHints
// SocketMask interface allows hint providers to create SocketMasks for TopologyHints
type SocketMask interface {
Add(sockets ...int) error
Remove(sockets ...int) error
Expand All @@ -39,7 +39,13 @@ type SocketMask interface {

type socketMask uint64

//NewSocketMask creates a new SocketMask
// NewEmptySocketMask creates a new, empty SocketMask
func NewEmptySocketMask() SocketMask {
s := socketMask(0)
return &s
}

// NewSocketMask creates a new SocketMask
func NewSocketMask(sockets ...int) (SocketMask, error) {
s := socketMask(0)
err := (&s).Add(sockets...)
Expand All @@ -49,7 +55,7 @@ func NewSocketMask(sockets ...int) (SocketMask, error) {
return &s, nil
}

//Add adds the sockets with topology affinity to the SocketMask
// Add adds the sockets with topology affinity to the SocketMask
func (s *socketMask) Add(sockets ...int) error {
mask := *s
for _, i := range sockets {
Expand All @@ -62,7 +68,7 @@ func (s *socketMask) Add(sockets ...int) error {
return nil
}

//Remove removes specified sockets from SocketMask
// Remove removes specified sockets from SocketMask
func (s *socketMask) Remove(sockets ...int) error {
mask := *s
for _, i := range sockets {
Expand All @@ -75,44 +81,44 @@ func (s *socketMask) Remove(sockets ...int) error {
return nil
}

//And performs and operation on all bits in masks
// And performs and operation on all bits in masks
func (s *socketMask) And(masks ...SocketMask) {
for _, m := range masks {
*s &= *m.(*socketMask)
}
}

//Or performs or operation on all bits in masks
// Or performs or operation on all bits in masks
func (s *socketMask) Or(masks ...SocketMask) {
for _, m := range masks {
*s |= *m.(*socketMask)
}
}

//Clear resets all bits in mask to zero
// Clear resets all bits in mask to zero
func (s *socketMask) Clear() {
*s = 0
}

//Fill sets all bits in mask to one
// Fill sets all bits in mask to one
func (s *socketMask) Fill() {
*s = socketMask(^uint64(0))
}

//IsEmpty checks mask to see if all bits are zero
// 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
// 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
// IsEqual checks if masks are equal
func (s *socketMask) IsEqual(mask SocketMask) bool {
return *s == *mask.(*socketMask)
}
Expand All @@ -131,7 +137,7 @@ func (s *socketMask) IsNarrowerThan(mask SocketMask) bool {
return s.Count() < mask.Count()
}

//String converts mask to string
// String converts mask to string
func (s *socketMask) String() string {
str := ""
for i := uint64(0); i < 64; i++ {
Expand All @@ -144,7 +150,7 @@ func (s *socketMask) String() string {
return str
}

//Count counts number of bits in mask set to one
// Count counts number of bits in mask set to one
func (s *socketMask) Count() int {
count := 0
for i := uint64(0); i < 64; i++ {
Expand All @@ -155,7 +161,7 @@ func (s *socketMask) Count() int {
return count
}

//GetSockets returns each socket number with bits set to one
// 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++ {
Expand All @@ -165,3 +171,17 @@ func (s *socketMask) GetSockets() []int {
}
return sockets
}

// And is a package level implementation of 'and' between first and masks
func And(first SocketMask, masks ...SocketMask) SocketMask {
s := *first.(*socketMask)
s.And(masks...)
return &s
}

// Or is a package level implementation of 'or' between first and masks
func Or(first SocketMask, masks ...SocketMask) SocketMask {
s := *first.(*socketMask)
s.Or(masks...)
return &s
}
12 changes: 12 additions & 0 deletions pkg/kubelet/cm/topologymanager/socketmask/socketmask_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ func TestAnd(t *testing.T) {
for _, tc := range tcases {
firstMask, _ := NewSocketMask(tc.firstMaskBit)
secondMask, _ := NewSocketMask(tc.secondMaskBit)

result := And(firstMask, secondMask)
if result.String() != string(tc.andMask) {
t.Errorf("Expected mask to be %v, got %v", tc.andMask, result)
}

firstMask.And(secondMask)
if firstMask.String() != string(tc.andMask) {
t.Errorf("Expected mask to be %v, got %v", tc.andMask, firstMask)
Expand All @@ -130,6 +136,12 @@ func TestOr(t *testing.T) {
for _, tc := range tcases {
firstMask, _ := NewSocketMask(tc.firstMaskBit)
secondMask, _ := NewSocketMask(tc.secondMaskBit)

result := Or(firstMask, secondMask)
if result.String() != string(tc.orMask) {
t.Errorf("Expected mask to be %v, got %v", tc.orMask, result)
}

firstMask.Or(secondMask)
if firstMask.String() != string(tc.orMask) {
t.Errorf("Expected mask to be %v, got %v", tc.orMask, firstMask)
Expand Down

0 comments on commit 5b496fe

Please sign in to comment.