@@ -26,6 +26,7 @@ import (
2626 "k8s.io/kubernetes/pkg/kubelet/cm/cpumanager/topology"
2727 "k8s.io/kubernetes/pkg/kubelet/cm/cpuset"
2828 "k8s.io/kubernetes/pkg/kubelet/cm/topologymanager"
29+ "k8s.io/kubernetes/pkg/kubelet/cm/topologymanager/socketmask"
2930)
3031
3132type staticPolicyTest struct {
@@ -740,3 +741,93 @@ func TestStaticPolicyRemove(t *testing.T) {
740741 }
741742 }
742743}
744+
745+ func TestTopologyAwareAllocateCPUs (t * testing.T ) {
746+ testCases := []struct {
747+ description string
748+ topo * topology.CPUTopology
749+ stAssignments state.ContainerCPUAssignments
750+ stDefaultCPUSet cpuset.CPUSet
751+ numRequested int
752+ socketMask socketmask.SocketMask
753+ expCSet cpuset.CPUSet
754+ }{
755+ {
756+ description : "Request 2 CPUs, No SocketMask" ,
757+ topo : topoDualSocketHT ,
758+ stAssignments : state.ContainerCPUAssignments {},
759+ stDefaultCPUSet : cpuset .NewCPUSet (0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 ),
760+ numRequested : 2 ,
761+ socketMask : nil ,
762+ expCSet : cpuset .NewCPUSet (0 , 6 ),
763+ },
764+ {
765+ description : "Request 2 CPUs, SocketMask on Socket 0" ,
766+ topo : topoDualSocketHT ,
767+ stAssignments : state.ContainerCPUAssignments {},
768+ stDefaultCPUSet : cpuset .NewCPUSet (0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 ),
769+ numRequested : 2 ,
770+ socketMask : func () socketmask.SocketMask {
771+ mask , _ := socketmask .NewSocketMask (0 )
772+ return mask
773+ }(),
774+ expCSet : cpuset .NewCPUSet (0 , 6 ),
775+ },
776+ {
777+ description : "Request 2 CPUs, SocketMask on Socket 1" ,
778+ topo : topoDualSocketHT ,
779+ stAssignments : state.ContainerCPUAssignments {},
780+ stDefaultCPUSet : cpuset .NewCPUSet (0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 ),
781+ numRequested : 2 ,
782+ socketMask : func () socketmask.SocketMask {
783+ mask , _ := socketmask .NewSocketMask (1 )
784+ return mask
785+ }(),
786+ expCSet : cpuset .NewCPUSet (1 , 7 ),
787+ },
788+ {
789+ description : "Request 8 CPUs, SocketMask on Socket 0" ,
790+ topo : topoDualSocketHT ,
791+ stAssignments : state.ContainerCPUAssignments {},
792+ stDefaultCPUSet : cpuset .NewCPUSet (0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 ),
793+ numRequested : 8 ,
794+ socketMask : func () socketmask.SocketMask {
795+ mask , _ := socketmask .NewSocketMask (0 )
796+ return mask
797+ }(),
798+ expCSet : cpuset .NewCPUSet (0 , 6 , 2 , 8 , 4 , 10 , 1 , 7 ),
799+ },
800+ {
801+ description : "Request 8 CPUs, SocketMask on Socket 1" ,
802+ topo : topoDualSocketHT ,
803+ stAssignments : state.ContainerCPUAssignments {},
804+ stDefaultCPUSet : cpuset .NewCPUSet (0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 ),
805+ numRequested : 8 ,
806+ socketMask : func () socketmask.SocketMask {
807+ mask , _ := socketmask .NewSocketMask (1 )
808+ return mask
809+ }(),
810+ expCSet : cpuset .NewCPUSet (1 , 7 , 3 , 9 , 5 , 11 , 0 , 6 ),
811+ },
812+ }
813+ for _ , tc := range testCases {
814+ policy := NewStaticPolicy (tc .topo , 0 , topologymanager .NewFakeManager ()).(* staticPolicy )
815+ st := & mockState {
816+ assignments : tc .stAssignments ,
817+ defaultCPUSet : tc .stDefaultCPUSet ,
818+ }
819+ policy .Start (st )
820+
821+ cset , err := policy .allocateCPUs (st , tc .numRequested , tc .socketMask )
822+ if err != nil {
823+ t .Errorf ("StaticPolicy allocateCPUs() error (%v). expected CPUSet %v not error %v" ,
824+ tc .description , tc .expCSet , err )
825+ continue
826+ }
827+
828+ if ! reflect .DeepEqual (tc .expCSet , cset ) {
829+ t .Errorf ("StaticPolicy allocateCPUs() error (%v). expected CPUSet %v but got %v" ,
830+ tc .description , tc .expCSet , cset )
831+ }
832+ }
833+ }
0 commit comments