-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
kvm2 driver support simulate numa node
- Loading branch information
Showing
20 changed files
with
198 additions
and
69 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
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 { | ||
CPUCount int | ||
Memory int | ||
CPUTopology string | ||
} | ||
|
||
// GetNUMAXml generate numa xml | ||
// evenly distributed cpu core & memory to each numa node | ||
func GetNUMAXml(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,29 @@ | ||
package kvm | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestGetNUMAXml(t *testing.T) { | ||
_, err := GetNUMAXml(1, 1024, 0) | ||
if err == nil { | ||
t.Errorf("check invalid numa count failed: %s", err) | ||
} | ||
|
||
xml, err := GetNUMAXml(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 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
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
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
Oops, something went wrong.