Skip to content

Commit ce46c24

Browse files
committed
Add numa cpuset support
This change adds support for specifying a set of cpu and memory nodes during the CreateVM call. This will run the jailed process on those set of cpus and restrict access to the specified nodes. This change also adds the cpuset.Builder which allows for easy creation of cpus and mems strings Signed-off-by: xibz <[email protected]>
1 parent 3b71606 commit ce46c24

File tree

9 files changed

+440
-85
lines changed

9 files changed

+440
-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: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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+
)
19+
20+
// Builder will allow building of cpuset fields
21+
// http://man7.org/linux/man-pages/man7/cpuset.7.html
22+
type Builder struct {
23+
cpus []int
24+
cpuRanges []_range
25+
26+
mems []int
27+
memRanges []_range
28+
}
29+
30+
type _range struct {
31+
min, max int
32+
}
33+
34+
// CPUSet represents the linux CPUSet which is a series of configurable values
35+
// that allow processes to run on a specific CPUs and those CPUs are then bound
36+
// to the memory nodes specified.
37+
type CPUSet struct {
38+
cpus string
39+
mems string
40+
}
41+
42+
// CPUs returns the cpuset.cpus string
43+
func (c CPUSet) CPUs() string {
44+
return c.cpus
45+
}
46+
47+
// Mems returns the cpuset.mems string
48+
func (c CPUSet) Mems() string {
49+
return c.mems
50+
}
51+
52+
// AddCPU will add the physical CPU number that the process is allowed to run
53+
// on.
54+
func (b Builder) AddCPU(cpu int) Builder {
55+
b.cpus = append(b.cpus, cpu)
56+
return b
57+
}
58+
59+
// AddCPURange adds a range of physical CPU numbers that the process is allowed
60+
// to run on
61+
func (b Builder) AddCPURange(min, max int) Builder {
62+
b.cpuRanges = append(b.cpuRanges, _range{
63+
min: min,
64+
max: max,
65+
})
66+
67+
return b
68+
}
69+
70+
// AddMem adds a memory node which limit where the cpus can allocate memory
71+
func (b Builder) AddMem(mem int) Builder {
72+
b.mems = append(b.mems, mem)
73+
return b
74+
}
75+
76+
// AddMemRange adds a range of memory nodes by utilizing the minimum node to
77+
// use to the maximum node to use.
78+
func (b Builder) AddMemRange(min, max int) Builder {
79+
b.memRanges = append(b.memRanges, _range{
80+
min: min,
81+
max: max,
82+
})
83+
84+
return b
85+
}
86+
87+
// Build constructs a new CPUSet
88+
func (b Builder) Build() CPUSet {
89+
cpus := stringify(b.cpus, b.cpuRanges)
90+
mems := stringify(b.mems, b.memRanges)
91+
92+
return CPUSet{
93+
cpus: cpus,
94+
mems: mems,
95+
}
96+
}
97+
98+
func stringify(elems []int, ranges []_range) string {
99+
str := ""
100+
for _, elem := range elems {
101+
sep := ","
102+
if len(str) == 0 {
103+
sep = ""
104+
}
105+
106+
str += fmt.Sprintf("%s%d", sep, elem)
107+
}
108+
109+
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)
116+
}
117+
118+
return str
119+
}

0 commit comments

Comments
 (0)