Skip to content

Commit 831c73f

Browse files
authored
Merge pull request #334 from xibz/jailer-numa-node-clean
Add numa cpuset support
2 parents e80d4cc + 29ee484 commit 831c73f

File tree

9 files changed

+467
-85
lines changed

9 files changed

+467
-85
lines changed

proto/firecracker.pb.go

Lines changed: 58 additions & 37 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

proto/firecracker.proto

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,5 +81,25 @@ message GetVMMetadataResponse {
8181

8282
message JailerConfig {
8383
string NetNS = 1;
84+
85+
// List of the physical numbers of the CPUs on which processes in that
86+
// cpuset are allowed to execute. See List Format below for a description
87+
// of the format of cpus.
88+
//
89+
// The CPUs allowed to a cpuset may be changed by writing a new list to its
90+
// cpus file.
91+
// Taken from http://man7.org/linux/man-pages/man7/cpuset.7.html
92+
//
93+
// This is formatted as specified in the cpuset man page under "List Format"
94+
// http://man7.org/linux/man-pages/man7/cpuset.7.html
95+
string CPUs = 2;
96+
// List of memory nodes on which processes in this cpuset are allowed to
97+
// allocate memory. See List Format below for a description of the format
98+
// of mems.
99+
// Taken from http://man7.org/linux/man-pages/man7/cpuset.7.html
100+
//
101+
// This is formatted as specified in the cpuset man page under "List Format"
102+
// http://man7.org/linux/man-pages/man7/cpuset.7.html
103+
string Mems = 3;
84104
}
85105

runtime/cpuset/cpuset_builder.go

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
// Copyright 2018-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License"). You may
4+
// not use this file except in compliance with the License. A copy of the
5+
// License is located at
6+
//
7+
// http://aws.amazon.com/apache2.0/
8+
//
9+
// or in the "license" file accompanying this file. This file is distributed
10+
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11+
// express or implied. See the License for the specific language governing
12+
// permissions and limitations under the License.
13+
14+
package cpuset
15+
16+
import (
17+
"fmt"
18+
"strconv"
19+
"strings"
20+
)
21+
22+
// Builder will allow building of cpuset fields such as cpuset.cpus and
23+
// cpuset.mems
24+
//
25+
// Example:
26+
// cset := cpuset.Builder{}.
27+
// AddCPU(0).
28+
// AddMemRange(0).
29+
// Build()
30+
//
31+
// fcClient, err := fcclient.New(containerdTTRPCAddress)
32+
// if err != nil {
33+
// return err
34+
// }
35+
//
36+
// defer fcClient.Close()
37+
//
38+
// vmID := "cpuset-builder-example"
39+
// createVMRequest := &proto.CreateVMRequest{
40+
// VMID: vmID,
41+
// JailerConfig: proto.JailerConfig{
42+
// CPUs: cset.CPUs(),
43+
// Mems: cset.Mems(),
44+
// },
45+
// }
46+
//
47+
// _, err = fcClient.CreateVM(ctx, createVMRequest)
48+
// if err != nil {
49+
// return errors.Wrap(err, "failed to create VM")
50+
// }
51+
type Builder struct {
52+
cpus []int
53+
cpuRanges []_range
54+
55+
mems []int
56+
memRanges []_range
57+
}
58+
59+
type _range struct {
60+
min, max int
61+
}
62+
63+
func (r _range) String() string {
64+
return fmt.Sprintf("%d-%d", r.min, r.max)
65+
}
66+
67+
// CPUSet represents the linux CPUSet which is a series of configurable values
68+
// that allow processes to run on a specific CPUs and those CPUs are then bound
69+
// to the memory nodes specified.
70+
//
71+
// More information can be found here: http://man7.org/linux/man-pages/man7/cpuset.7.html
72+
type CPUSet struct {
73+
cpus string
74+
mems string
75+
}
76+
77+
// CPUs returns the cpuset.cpus string
78+
func (c CPUSet) CPUs() string {
79+
return c.cpus
80+
}
81+
82+
// Mems returns the cpuset.mems string
83+
func (c CPUSet) Mems() string {
84+
return c.mems
85+
}
86+
87+
// AddCPU will add the physical CPU number that the process is allowed to run
88+
// on.
89+
func (b Builder) AddCPU(cpu int) Builder {
90+
b.cpus = append(b.cpus, cpu)
91+
return b
92+
}
93+
94+
// AddCPURange adds a range of physical CPU numbers that the process is allowed
95+
// to run on
96+
func (b Builder) AddCPURange(min, max int) Builder {
97+
b.cpuRanges = append(b.cpuRanges, _range{
98+
min: min,
99+
max: max,
100+
})
101+
102+
return b
103+
}
104+
105+
// AddMem adds a memory node which limits where the cpus can allocate memory
106+
func (b Builder) AddMem(mem int) Builder {
107+
b.mems = append(b.mems, mem)
108+
return b
109+
}
110+
111+
// AddMemRange adds a range of memory nodes to be used.
112+
func (b Builder) AddMemRange(min, max int) Builder {
113+
b.memRanges = append(b.memRanges, _range{
114+
min: min,
115+
max: max,
116+
})
117+
118+
return b
119+
}
120+
121+
// Build constructs a new CPUSet
122+
func (b Builder) Build() CPUSet {
123+
cpus := stringify(b.cpus, b.cpuRanges)
124+
mems := stringify(b.mems, b.memRanges)
125+
126+
return CPUSet{
127+
cpus: cpus,
128+
mems: mems,
129+
}
130+
}
131+
132+
func stringify(elems []int, ranges []_range) string {
133+
strs := []string{}
134+
for _, elem := range elems {
135+
strs = append(strs, strconv.Itoa(elem))
136+
}
137+
138+
for _, r := range ranges {
139+
strs = append(strs, r.String())
140+
}
141+
142+
return strings.Join(strs, ",")
143+
}

0 commit comments

Comments
 (0)