Skip to content

Add numa cpuset support #334

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 58 additions & 37 deletions proto/firecracker.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions proto/firecracker.proto
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,25 @@ message GetVMMetadataResponse {

message JailerConfig {
string NetNS = 1;

// List of the physical numbers of the CPUs on which processes in that
// cpuset are allowed to execute. See List Format below for a description
// of the format of cpus.
//
// The CPUs allowed to a cpuset may be changed by writing a new list to its
// cpus file.
// Taken from http://man7.org/linux/man-pages/man7/cpuset.7.html
//
// This is formatted as specified in the cpuset man page under "List Format"
// http://man7.org/linux/man-pages/man7/cpuset.7.html
string CPUs = 2;
// List of memory nodes on which processes in this cpuset are allowed to
// allocate memory. See List Format below for a description of the format
// of mems.
// Taken from http://man7.org/linux/man-pages/man7/cpuset.7.html
//
// This is formatted as specified in the cpuset man page under "List Format"
// http://man7.org/linux/man-pages/man7/cpuset.7.html
string Mems = 3;
}

143 changes: 143 additions & 0 deletions runtime/cpuset/cpuset_builder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
// Copyright 2018-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file is distributed
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing
// permissions and limitations under the License.

package cpuset

import (
"fmt"
"strconv"
"strings"
)

// Builder will allow building of cpuset fields such as cpuset.cpus and
// cpuset.mems
//
// Example:
// cset := cpuset.Builder{}.
// AddCPU(0).
// AddMemRange(0).
// Build()
//
// fcClient, err := fcclient.New(containerdTTRPCAddress)
// if err != nil {
// return err
// }
//
// defer fcClient.Close()
//
// vmID := "cpuset-builder-example"
// createVMRequest := &proto.CreateVMRequest{
// VMID: vmID,
// JailerConfig: proto.JailerConfig{
// CPUs: cset.CPUs(),
// Mems: cset.Mems(),
// },
// }
//
// _, err = fcClient.CreateVM(ctx, createVMRequest)
// if err != nil {
// return errors.Wrap(err, "failed to create VM")
// }
type Builder struct {
cpus []int
cpuRanges []_range

mems []int
memRanges []_range
}

type _range struct {
min, max int
}

func (r _range) String() string {
return fmt.Sprintf("%d-%d", r.min, r.max)
}

// CPUSet represents the linux CPUSet which is a series of configurable values
// that allow processes to run on a specific CPUs and those CPUs are then bound
// to the memory nodes specified.
//
// More information can be found here: http://man7.org/linux/man-pages/man7/cpuset.7.html
type CPUSet struct {
cpus string
mems string
}

// CPUs returns the cpuset.cpus string
func (c CPUSet) CPUs() string {
return c.cpus
}

// Mems returns the cpuset.mems string
func (c CPUSet) Mems() string {
return c.mems
}

// AddCPU will add the physical CPU number that the process is allowed to run
// on.
func (b Builder) AddCPU(cpu int) Builder {
b.cpus = append(b.cpus, cpu)
return b
}

// AddCPURange adds a range of physical CPU numbers that the process is allowed
// to run on
func (b Builder) AddCPURange(min, max int) Builder {
b.cpuRanges = append(b.cpuRanges, _range{
min: min,
max: max,
})

return b
}

// AddMem adds a memory node which limits where the cpus can allocate memory
func (b Builder) AddMem(mem int) Builder {
b.mems = append(b.mems, mem)
return b
}

// AddMemRange adds a range of memory nodes to be used.
func (b Builder) AddMemRange(min, max int) Builder {
b.memRanges = append(b.memRanges, _range{
min: min,
max: max,
})

return b
}

// Build constructs a new CPUSet
func (b Builder) Build() CPUSet {
cpus := stringify(b.cpus, b.cpuRanges)
mems := stringify(b.mems, b.memRanges)

return CPUSet{
cpus: cpus,
mems: mems,
}
}

func stringify(elems []int, ranges []_range) string {
strs := []string{}
for _, elem := range elems {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason for doing this instead of using strings.Join?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gah, good call out. Just slipped the mind.

Copy link
Contributor

@samuelkarp samuelkarp Nov 29, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@xibz Can you update this to use strings.Join?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This returns a string using Join. Was there something else you had in mind? Cause I think this is probably the simplest way to go?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@xibz My apologies, I misread this. I might use strconv.Itoa instead of fmt.Sprintf, but that's really minor.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah cool, and no worries! I'll go ahead and fix the fmt.Sprintf issue. Thanks!

strs = append(strs, strconv.Itoa(elem))
}

for _, r := range ranges {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same question here.

strs = append(strs, r.String())
}

return strings.Join(strs, ",")
}
Loading