Skip to content

Commit a33efce

Browse files
committed
fixup! Add numa cpuset support
Signed-off-by: xibz <[email protected]>
1 parent da1b105 commit a33efce

File tree

3 files changed

+42
-20
lines changed

3 files changed

+42
-20
lines changed

runtime/cpuset/cpuset_builder.go

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,38 @@ package cpuset
1515

1616
import (
1717
"fmt"
18+
"strings"
1819
)
1920

20-
// Builder will allow building of cpuset fields
21-
// http://man7.org/linux/man-pages/man7/cpuset.7.html
21+
// Builder will allow building of cpuset fields such as cpuset.cpus and
22+
// cpuset.mems
23+
//
24+
// Example:
25+
// cset := cpuset.Builder{}.
26+
// AddCPU(0).
27+
// AddMemRange(0).
28+
// Build()
29+
//
30+
// fcClient, err := fcclient.New(containerdTTRPCAddress)
31+
// if err != nil {
32+
// return err
33+
// }
34+
//
35+
// defer fcClient.Close()
36+
//
37+
// vmID := "cpuset-builder-example"
38+
// createVMRequest := &proto.CreateVMRequest{
39+
// VMID: vmID,
40+
// JailerConfig: proto.JailerConfig{
41+
// CPUs: cset.CPUs(),
42+
// Mems: cset.Mems(),
43+
// },
44+
// }
45+
//
46+
// _, err = fcClient.CreateVM(ctx, createVMRequest)
47+
// if err != nil {
48+
// return errors.Wrap(err, "failed to create VM")
49+
// }
2250
type Builder struct {
2351
cpus []int
2452
cpuRanges []_range
@@ -34,6 +62,8 @@ type _range struct {
3462
// CPUSet represents the linux CPUSet which is a series of configurable values
3563
// that allow processes to run on a specific CPUs and those CPUs are then bound
3664
// to the memory nodes specified.
65+
//
66+
// More information can be found here: http://man7.org/linux/man-pages/man7/cpuset.7.html
3767
type CPUSet struct {
3868
cpus string
3969
mems string
@@ -67,14 +97,13 @@ func (b Builder) AddCPURange(min, max int) Builder {
6797
return b
6898
}
6999

70-
// AddMem adds a memory node which limit where the cpus can allocate memory
100+
// AddMem adds a memory node which limits where the cpus can allocate memory
71101
func (b Builder) AddMem(mem int) Builder {
72102
b.mems = append(b.mems, mem)
73103
return b
74104
}
75105

76-
// AddMemRange adds a range of memory nodes by utilizing the minimum node to
77-
// use to the maximum node to use.
106+
// AddMemRange adds a range of memory nodes to be used.
78107
func (b Builder) AddMemRange(min, max int) Builder {
79108
b.memRanges = append(b.memRanges, _range{
80109
min: min,
@@ -96,24 +125,14 @@ func (b Builder) Build() CPUSet {
96125
}
97126

98127
func stringify(elems []int, ranges []_range) string {
99-
str := ""
128+
strs := []string{}
100129
for _, elem := range elems {
101-
sep := ","
102-
if len(str) == 0 {
103-
sep = ""
104-
}
105-
106-
str += fmt.Sprintf("%s%d", sep, elem)
130+
strs = append(strs, fmt.Sprintf("%d", elem))
107131
}
108132

109133
for _, r := range ranges {
110-
sep := ","
111-
if len(str) == 0 {
112-
sep = ""
113-
}
114-
115-
str += fmt.Sprintf("%s%d-%d", sep, r.min, r.max)
134+
strs = append(strs, fmt.Sprintf("%d-%d", r.min, r.max))
116135
}
117136

118-
return str
137+
return strings.Join(strs, ",")
119138
}

runtime/cpuset/cpuset_builder_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ func TestCPUSet(t *testing.T) {
2828
memRanges []_range
2929
expectedCPUSet CPUSet
3030
}{
31+
{
32+
name: "empty set",
33+
},
3134
{
3235
name: "single cpu only",
3336
cpus: []int{0},

runtime/jailer_integ_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func testJailer(t *testing.T, jailerConfig *proto.JailerConfig) {
8181
}()
8282
}
8383

84-
func TestJailerWithNumaNode_Isolated(t *testing.T) {
84+
func TestJailerCPUSet_Isolated(t *testing.T) {
8585
prepareIntegTest(t, withJailer())
8686

8787
t.Run("Jailer NumaNode", func(t *testing.T) {

0 commit comments

Comments
 (0)