resource-control: add helper function for setting CPU affinity

Let's abstract the CPU affinity

Fixes: #6044

Signed-off-by: Eric Ernst <eric_ernst@apple.com>
This commit is contained in:
Eric Ernst 2023-01-11 16:05:18 -08:00
parent c6b7f69040
commit f137048be3
2 changed files with 25 additions and 0 deletions

View File

@ -0,0 +1,10 @@
// Copyright (c) 2023 Apple Inc.
//
// SPDX-License-Identifier: Apache-2.0
//
package resourcecontrol
func SetThreadAffinity(threadID int, cpuSetSlice []int) error {
return nil
}

View File

@ -15,6 +15,7 @@ import (
systemdDbus "github.com/coreos/go-systemd/v22/dbus"
"github.com/godbus/dbus/v5"
"github.com/opencontainers/runc/libcontainer/cgroups/systemd"
"golang.org/x/sys/unix"
)
// DefaultResourceControllerID runtime-determined location in the cgroups hierarchy.
@ -141,3 +142,17 @@ func getSliceAndUnit(cgroupPath string) (string, string, error) {
return "", "", fmt.Errorf("Path: %s is not valid systemd's cgroups path", cgroupPath)
}
func SetThreadAffinity(threadID int, cpuSetSlice []int) error {
unixCPUSet := unix.CPUSet{}
for cpuId := range cpuSetSlice {
unixCPUSet.Set(cpuId)
}
if err := unix.SchedSetaffinity(threadID, &unixCPUSet); err != nil {
return fmt.Errorf("failed to set vcpu thread %d affinity to cpu %d: %v", threadID, cpuSetSlice, err)
}
return nil
}