@@ -15,10 +15,38 @@ package cpuset
15
15
16
16
import (
17
17
"fmt"
18
+ "strings"
18
19
)
19
20
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
+ // }
22
50
type Builder struct {
23
51
cpus []int
24
52
cpuRanges []_range
@@ -34,6 +62,8 @@ type _range struct {
34
62
// CPUSet represents the linux CPUSet which is a series of configurable values
35
63
// that allow processes to run on a specific CPUs and those CPUs are then bound
36
64
// to the memory nodes specified.
65
+ //
66
+ // More information can be found here: http://man7.org/linux/man-pages/man7/cpuset.7.html
37
67
type CPUSet struct {
38
68
cpus string
39
69
mems string
@@ -67,14 +97,13 @@ func (b Builder) AddCPURange(min, max int) Builder {
67
97
return b
68
98
}
69
99
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
71
101
func (b Builder ) AddMem (mem int ) Builder {
72
102
b .mems = append (b .mems , mem )
73
103
return b
74
104
}
75
105
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.
78
107
func (b Builder ) AddMemRange (min , max int ) Builder {
79
108
b .memRanges = append (b .memRanges , _range {
80
109
min : min ,
@@ -96,24 +125,14 @@ func (b Builder) Build() CPUSet {
96
125
}
97
126
98
127
func stringify (elems []int , ranges []_range ) string {
99
- str := ""
128
+ strs := [] string {}
100
129
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 ))
107
131
}
108
132
109
133
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 ))
116
135
}
117
136
118
- return str
137
+ return strings . Join ( strs , "," )
119
138
}
0 commit comments