forked from kubernetes/minikube
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request kubernetes#10471 from phantooom/master
kvm2 driver: Add flag --kvm-numa-count" support topology-manager simulate numa
- Loading branch information
Showing
9 changed files
with
190 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* | ||
Copyright 2021 The Kubernetes Authors All rights reserved. | ||
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 kvm | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"strconv" | ||
"strings" | ||
"text/template" | ||
) | ||
|
||
// numaTmpl NUMA XML Template | ||
const numaTmpl = ` | ||
<numa> | ||
{{- range $idx,$val :=. }} | ||
<cell id='{{$idx}}' cpus='{{$val.CPUTopology}}' memory='{{$val.Memory}}' unit='MiB'/> | ||
{{- end }} | ||
</numa> | ||
` | ||
|
||
// NUMA this struct use for numaTmpl | ||
type NUMA struct { | ||
// cpu count on numa node | ||
CPUCount int | ||
// memory on numa node | ||
Memory int | ||
// cpu sequence on numa node eg: 0,1,2,3 | ||
CPUTopology string | ||
} | ||
|
||
// numaXML generate numa xml | ||
// evenly distributed cpu core & memory to each numa node | ||
func numaXML(cpu, memory, numaCount int) (string, error) { | ||
if numaCount < 1 { | ||
return "", fmt.Errorf("numa node count must >= 1") | ||
} | ||
if cpu < numaCount { | ||
return "", fmt.Errorf("cpu count must >= numa node count") | ||
} | ||
numaNodes := make([]*NUMA, numaCount) | ||
CPUSeq := 0 | ||
cpuBaseCount := cpu / numaCount | ||
cpuExtraCount := cpu % numaCount | ||
|
||
for i := range numaNodes { | ||
numaNodes[i] = &NUMA{CPUCount: cpuBaseCount} | ||
} | ||
|
||
for i := 0; i < cpuExtraCount; i++ { | ||
numaNodes[i].CPUCount++ | ||
} | ||
for i := range numaNodes { | ||
CPUTopologySlice := make([]string, 0) | ||
for seq := CPUSeq; seq < CPUSeq+numaNodes[i].CPUCount; seq++ { | ||
CPUTopologySlice = append(CPUTopologySlice, strconv.Itoa(seq)) | ||
} | ||
numaNodes[i].CPUTopology = strings.Join(CPUTopologySlice, ",") | ||
CPUSeq += numaNodes[i].CPUCount | ||
} | ||
|
||
memoryBaseCount := memory / numaCount | ||
memoryExtraCount := memory % numaCount | ||
|
||
for i := range numaNodes { | ||
numaNodes[i].Memory = memoryBaseCount | ||
} | ||
|
||
for i := 0; i < memoryExtraCount; i++ { | ||
numaNodes[i].Memory++ | ||
} | ||
|
||
tmpl := template.Must(template.New("numa").Parse(numaTmpl)) | ||
var numaXML bytes.Buffer | ||
if err := tmpl.Execute(&numaXML, numaNodes); err != nil { | ||
return "", fmt.Errorf("couldn't generate numa XML: %v", err) | ||
} | ||
return numaXML.String(), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
Copyright 2021 The Kubernetes Authors All rights reserved. | ||
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 kvm | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestNumaXml(t *testing.T) { | ||
_, err := numaXML(1, 1024, 0) | ||
if err == nil { | ||
t.Errorf("check invalid numa count failed: %s", err) | ||
} | ||
|
||
xml, err := numaXML(10, 10240, 8) | ||
expXML := `<numa> | ||
<cell id='0' cpus='0,1' memory='1280' unit='MiB'/> | ||
<cell id='1' cpus='2,3' memory='1280' unit='MiB'/> | ||
<cell id='2' cpus='4' memory='1280' unit='MiB'/> | ||
<cell id='3' cpus='5' memory='1280' unit='MiB'/> | ||
<cell id='4' cpus='6' memory='1280' unit='MiB'/> | ||
<cell id='5' cpus='7' memory='1280' unit='MiB'/> | ||
<cell id='6' cpus='8' memory='1280' unit='MiB'/> | ||
<cell id='7' cpus='9' memory='1280' unit='MiB'/> | ||
</numa>` | ||
if err != nil { | ||
t.Errorf("gen xml failed: %s", err) | ||
} | ||
if strings.TrimSpace(xml) != expXML { | ||
t.Errorf("gen xml: %s not match expect xml: %s", xml, expXML) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters