|
| 1 | +/* |
| 2 | +Copyright 2021 The Kubernetes Authors All rights reserved. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package kvm |
| 18 | + |
| 19 | +import ( |
| 20 | + "bytes" |
| 21 | + "fmt" |
| 22 | + "strconv" |
| 23 | + "strings" |
| 24 | + "text/template" |
| 25 | +) |
| 26 | + |
| 27 | +// NUMATmpl NUMA XML Template |
| 28 | +const NUMATmpl = ` |
| 29 | +<numa> |
| 30 | + {{- range $idx,$val :=. }} |
| 31 | + <cell id='{{$idx}}' cpus='{{$val.CPUTopology}}' memory='{{$val.Memory}}' unit='MiB'/> |
| 32 | + {{- end }} |
| 33 | +</numa> |
| 34 | +` |
| 35 | + |
| 36 | +// NUMA this struct use for NUMATmpl |
| 37 | +type NUMA struct { |
| 38 | + CPUCount int |
| 39 | + Memory int |
| 40 | + CPUTopology string |
| 41 | +} |
| 42 | + |
| 43 | +// GetNUMAXml generate numa xml |
| 44 | +// evenly distributed cpu core & memory to each numa node |
| 45 | +func GetNUMAXml(cpu, memory, numaCount int) (string, error) { |
| 46 | + if numaCount < 1 { |
| 47 | + return "", fmt.Errorf("numa node count must >= 1") |
| 48 | + } |
| 49 | + if cpu < numaCount { |
| 50 | + return "", fmt.Errorf("cpu count must >= numa node count") |
| 51 | + } |
| 52 | + numaNodes := make([]*NUMA, numaCount) |
| 53 | + CPUSeq := 0 |
| 54 | + cpuBaseCount := cpu / numaCount |
| 55 | + cpuExtraCount := cpu % numaCount |
| 56 | + |
| 57 | + for i := range numaNodes { |
| 58 | + numaNodes[i] = &NUMA{CPUCount: cpuBaseCount} |
| 59 | + } |
| 60 | + |
| 61 | + for i := 0; i < cpuExtraCount; i++ { |
| 62 | + numaNodes[i].CPUCount++ |
| 63 | + } |
| 64 | + for i := range numaNodes { |
| 65 | + CPUTopologySlice := make([]string, 0) |
| 66 | + for seq := CPUSeq; seq < CPUSeq+numaNodes[i].CPUCount; seq++ { |
| 67 | + CPUTopologySlice = append(CPUTopologySlice, strconv.Itoa(seq)) |
| 68 | + } |
| 69 | + numaNodes[i].CPUTopology = strings.Join(CPUTopologySlice, ",") |
| 70 | + CPUSeq += numaNodes[i].CPUCount |
| 71 | + } |
| 72 | + |
| 73 | + memoryBaseCount := memory / numaCount |
| 74 | + memoryExtraCount := memory % numaCount |
| 75 | + |
| 76 | + for i := range numaNodes { |
| 77 | + numaNodes[i].Memory = memoryBaseCount |
| 78 | + } |
| 79 | + |
| 80 | + for i := 0; i < memoryExtraCount; i++ { |
| 81 | + numaNodes[i].Memory++ |
| 82 | + } |
| 83 | + |
| 84 | + tmpl := template.Must(template.New("numa").Parse(NUMATmpl)) |
| 85 | + var NUMAXML bytes.Buffer |
| 86 | + if err := tmpl.Execute(&NUMAXML, numaNodes); err != nil { |
| 87 | + return "", fmt.Errorf("couldn't generate numa XML: %v", err) |
| 88 | + } |
| 89 | + return NUMAXML.String(), nil |
| 90 | +} |
0 commit comments