Skip to content

Commit e0e8b3e

Browse files
committed
Update CPUManager topology helpers to accept multiple ids
1 parent dcc9f66 commit e0e8b3e

File tree

3 files changed

+73
-59
lines changed

3 files changed

+73
-59
lines changed

pkg/kubelet/cm/cpumanager/cpu_assignment.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,12 @@ func (a *cpuAccumulator) take(cpus cpuset.CPUSet) {
5050

5151
// Returns true if the supplied socket is fully available in `topoDetails`.
5252
func (a *cpuAccumulator) isSocketFree(socketID int) bool {
53-
return a.details.CPUsInSocket(socketID).Size() == a.topo.CPUsPerSocket()
53+
return a.details.CPUsInSockets(socketID).Size() == a.topo.CPUsPerSocket()
5454
}
5555

5656
// Returns true if the supplied core is fully available in `topoDetails`.
5757
func (a *cpuAccumulator) isCoreFree(coreID int) bool {
58-
return a.details.CPUsInCore(coreID).Size() == a.topo.CPUsPerCore()
58+
return a.details.CPUsInCores(coreID).Size() == a.topo.CPUsPerCore()
5959
}
6060

6161
// Returns free socket IDs as a slice sorted by:
@@ -72,14 +72,14 @@ func (a *cpuAccumulator) freeCores() []int {
7272
socketIDs := a.details.Sockets().ToSliceNoSort()
7373
sort.Slice(socketIDs,
7474
func(i, j int) bool {
75-
iCores := a.details.CoresInSocket(socketIDs[i]).Filter(a.isCoreFree)
76-
jCores := a.details.CoresInSocket(socketIDs[j]).Filter(a.isCoreFree)
75+
iCores := a.details.CoresInSockets(socketIDs[i]).Filter(a.isCoreFree)
76+
jCores := a.details.CoresInSockets(socketIDs[j]).Filter(a.isCoreFree)
7777
return iCores.Size() < jCores.Size() || socketIDs[i] < socketIDs[j]
7878
})
7979

8080
coreIDs := []int{}
8181
for _, s := range socketIDs {
82-
coreIDs = append(coreIDs, a.details.CoresInSocket(s).Filter(a.isCoreFree).ToSlice()...)
82+
coreIDs = append(coreIDs, a.details.CoresInSockets(s).Filter(a.isCoreFree).ToSlice()...)
8383
}
8484
return coreIDs
8585
}
@@ -100,25 +100,25 @@ func (a *cpuAccumulator) freeCPUs() []int {
100100
iCore := cores[i]
101101
jCore := cores[j]
102102

103-
iCPUs := a.topo.CPUDetails.CPUsInCore(iCore).ToSlice()
104-
jCPUs := a.topo.CPUDetails.CPUsInCore(jCore).ToSlice()
103+
iCPUs := a.topo.CPUDetails.CPUsInCores(iCore).ToSlice()
104+
jCPUs := a.topo.CPUDetails.CPUsInCores(jCore).ToSlice()
105105

106106
iSocket := a.topo.CPUDetails[iCPUs[0]].SocketID
107107
jSocket := a.topo.CPUDetails[jCPUs[0]].SocketID
108108

109109
// Compute the number of CPUs in the result reside on the same socket
110110
// as each core.
111-
iSocketColoScore := a.topo.CPUDetails.CPUsInSocket(iSocket).Intersection(a.result).Size()
112-
jSocketColoScore := a.topo.CPUDetails.CPUsInSocket(jSocket).Intersection(a.result).Size()
111+
iSocketColoScore := a.topo.CPUDetails.CPUsInSockets(iSocket).Intersection(a.result).Size()
112+
jSocketColoScore := a.topo.CPUDetails.CPUsInSockets(jSocket).Intersection(a.result).Size()
113113

114114
// Compute the number of available CPUs available on the same socket
115115
// as each core.
116-
iSocketFreeScore := a.details.CPUsInSocket(iSocket).Size()
117-
jSocketFreeScore := a.details.CPUsInSocket(jSocket).Size()
116+
iSocketFreeScore := a.details.CPUsInSockets(iSocket).Size()
117+
jSocketFreeScore := a.details.CPUsInSockets(jSocket).Size()
118118

119119
// Compute the number of available CPUs on each core.
120-
iCoreFreeScore := a.details.CPUsInCore(iCore).Size()
121-
jCoreFreeScore := a.details.CPUsInCore(jCore).Size()
120+
iCoreFreeScore := a.details.CPUsInCores(iCore).Size()
121+
jCoreFreeScore := a.details.CPUsInCores(jCore).Size()
122122

123123
return iSocketColoScore > jSocketColoScore ||
124124
iSocketFreeScore < jSocketFreeScore ||
@@ -129,7 +129,7 @@ func (a *cpuAccumulator) freeCPUs() []int {
129129

130130
// For each core, append sorted CPU IDs to result.
131131
for _, core := range cores {
132-
result = append(result, a.details.CPUsInCore(core).ToSlice()...)
132+
result = append(result, a.details.CPUsInCores(core).ToSlice()...)
133133
}
134134
return result
135135
}
@@ -161,7 +161,7 @@ func takeByTopology(topo *topology.CPUTopology, availableCPUs cpuset.CPUSet, num
161161
if acc.needs(acc.topo.CPUsPerSocket()) {
162162
for _, s := range acc.freeSockets() {
163163
klog.V(4).Infof("[cpumanager] takeByTopology: claiming socket [%d]", s)
164-
acc.take(acc.details.CPUsInSocket(s))
164+
acc.take(acc.details.CPUsInSockets(s))
165165
if acc.isSatisfied() {
166166
return acc.result, nil
167167
}
@@ -176,7 +176,7 @@ func takeByTopology(topo *topology.CPUTopology, availableCPUs cpuset.CPUSet, num
176176
if acc.needs(acc.topo.CPUsPerCore()) {
177177
for _, c := range acc.freeCores() {
178178
klog.V(4).Infof("[cpumanager] takeByTopology: claiming core [%d]", c)
179-
acc.take(acc.details.CPUsInCore(c))
179+
acc.take(acc.details.CPUsInCores(c))
180180
if acc.isSatisfied() {
181181
return acc.result, nil
182182
}

pkg/kubelet/cm/cpumanager/policy_static.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ func (p *staticPolicy) allocateCPUs(s state.State, numCPUs int, numaAffinity soc
258258
if numaAffinity != nil {
259259
alignedCPUs := cpuset.NewCPUSet()
260260
for _, numaNodeID := range numaAffinity.GetSockets() {
261-
alignedCPUs = alignedCPUs.Union(p.assignableCPUs(s).Intersection(p.topology.CPUDetails.CPUsInNUMANode(numaNodeID)))
261+
alignedCPUs = alignedCPUs.Union(p.assignableCPUs(s).Intersection(p.topology.CPUDetails.CPUsInNUMANodes(numaNodeID)))
262262
}
263263

264264
numAlignedToAlloc := alignedCPUs.Size()

pkg/kubelet/cm/cpumanager/topology/topology.go

Lines changed: 56 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,15 @@ func (d CPUDetails) NUMANodes() cpuset.CPUSet {
9090
return b.Result()
9191
}
9292

93-
// NUMANodesInSocket returns all of the logical NUMANode IDs associated with
94-
// the given Socket ID in this CPUDetails.
95-
func (d CPUDetails) NUMANodesInSocket(id int) cpuset.CPUSet {
93+
// NUMANodesInSockets returns all of the logical NUMANode IDs associated with
94+
// the given socket IDs in this CPUDetails.
95+
func (d CPUDetails) NUMANodesInSockets(ids ...int) cpuset.CPUSet {
9696
b := cpuset.NewBuilder()
97-
for _, info := range d {
98-
if info.SocketID == id {
99-
b.Add(info.NUMANodeID)
97+
for _, id := range ids {
98+
for _, info := range d {
99+
if info.SocketID == id {
100+
b.Add(info.NUMANodeID)
101+
}
100102
}
101103
}
102104
return b.Result()
@@ -112,25 +114,29 @@ func (d CPUDetails) Sockets() cpuset.CPUSet {
112114
return b.Result()
113115
}
114116

115-
// CPUsInSocket returns all of the logical CPU IDs associated with the
116-
// given socket ID in this CPUDetails.
117-
func (d CPUDetails) CPUsInSocket(id int) cpuset.CPUSet {
117+
// CPUsInSockets returns all of the logical CPU IDs associated with the given
118+
// socket IDs in this CPUDetails.
119+
func (d CPUDetails) CPUsInSockets(ids ...int) cpuset.CPUSet {
118120
b := cpuset.NewBuilder()
119-
for cpu, info := range d {
120-
if info.SocketID == id {
121-
b.Add(cpu)
121+
for _, id := range ids {
122+
for cpu, info := range d {
123+
if info.SocketID == id {
124+
b.Add(cpu)
125+
}
122126
}
123127
}
124128
return b.Result()
125129
}
126130

127-
// SocketsInNUMANode returns all of the logical Socket IDs associated with the
128-
// given NUMANode ID in this CPUDetails.
129-
func (d CPUDetails) SocketsInNUMANode(id int) cpuset.CPUSet {
131+
// SocketsInNUMANodes returns all of the logical Socket IDs associated with the
132+
// given NUMANode IDs in this CPUDetails.
133+
func (d CPUDetails) SocketsInNUMANodes(ids ...int) cpuset.CPUSet {
130134
b := cpuset.NewBuilder()
131-
for _, info := range d {
132-
if info.NUMANodeID == id {
133-
b.Add(info.SocketID)
135+
for _, id := range ids {
136+
for _, info := range d {
137+
if info.NUMANodeID == id {
138+
b.Add(info.SocketID)
139+
}
134140
}
135141
}
136142
return b.Result()
@@ -146,25 +152,29 @@ func (d CPUDetails) Cores() cpuset.CPUSet {
146152
return b.Result()
147153
}
148154

149-
// CoresInNUMANode returns all of the core IDs associated with the given
150-
// NUMA ID in this CPUDetails.
151-
func (d CPUDetails) CoresInNUMANode(id int) cpuset.CPUSet {
155+
// CoresInNUMANodes returns all of the core IDs associated with the given
156+
// NUMANode IDs in this CPUDetails.
157+
func (d CPUDetails) CoresInNUMANodes(ids ...int) cpuset.CPUSet {
152158
b := cpuset.NewBuilder()
153-
for _, info := range d {
154-
if info.NUMANodeID == id {
155-
b.Add(info.CoreID)
159+
for _, id := range ids {
160+
for _, info := range d {
161+
if info.NUMANodeID == id {
162+
b.Add(info.CoreID)
163+
}
156164
}
157165
}
158166
return b.Result()
159167
}
160168

161-
// CoresInSocket returns all of the core IDs associated with the given
162-
// socket ID in this CPUDetails.
163-
func (d CPUDetails) CoresInSocket(id int) cpuset.CPUSet {
169+
// CoresInSockets returns all of the core IDs associated with the given socket
170+
// IDs in this CPUDetails.
171+
func (d CPUDetails) CoresInSockets(ids ...int) cpuset.CPUSet {
164172
b := cpuset.NewBuilder()
165-
for _, info := range d {
166-
if info.SocketID == id {
167-
b.Add(info.CoreID)
173+
for _, id := range ids {
174+
for _, info := range d {
175+
if info.SocketID == id {
176+
b.Add(info.CoreID)
177+
}
168178
}
169179
}
170180
return b.Result()
@@ -179,25 +189,29 @@ func (d CPUDetails) CPUs() cpuset.CPUSet {
179189
return b.Result()
180190
}
181191

182-
// CPUsInNUMANode returns all of the logical CPU IDs associated with the given
183-
// NUMANode ID in this CPUDetails.
184-
func (d CPUDetails) CPUsInNUMANode(id int) cpuset.CPUSet {
192+
// CPUsInNUMANodes returns all of the logical CPU IDs associated with the given
193+
// NUMANode IDs in this CPUDetails.
194+
func (d CPUDetails) CPUsInNUMANodes(ids ...int) cpuset.CPUSet {
185195
b := cpuset.NewBuilder()
186-
for cpu, info := range d {
187-
if info.NUMANodeID == id {
188-
b.Add(cpu)
196+
for _, id := range ids {
197+
for cpu, info := range d {
198+
if info.NUMANodeID == id {
199+
b.Add(cpu)
200+
}
189201
}
190202
}
191203
return b.Result()
192204
}
193205

194-
// CPUsInCore returns all of the logical CPU IDs associated with the
195-
// given core ID in this CPUDetails.
196-
func (d CPUDetails) CPUsInCore(id int) cpuset.CPUSet {
206+
// CPUsInCores returns all of the logical CPU IDs associated with the given
207+
// core IDs in this CPUDetails.
208+
func (d CPUDetails) CPUsInCores(ids ...int) cpuset.CPUSet {
197209
b := cpuset.NewBuilder()
198-
for cpu, info := range d {
199-
if info.CoreID == id {
200-
b.Add(cpu)
210+
for _, id := range ids {
211+
for cpu, info := range d {
212+
if info.CoreID == id {
213+
b.Add(cpu)
214+
}
201215
}
202216
}
203217
return b.Result()

0 commit comments

Comments
 (0)