diff --git a/src/runtime/pkg/resourcecontrol/utils_darwin.go b/src/runtime/pkg/resourcecontrol/utils_darwin.go new file mode 100644 index 0000000000..86c50ae783 --- /dev/null +++ b/src/runtime/pkg/resourcecontrol/utils_darwin.go @@ -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 +} diff --git a/src/runtime/pkg/resourcecontrol/utils_linux.go b/src/runtime/pkg/resourcecontrol/utils_linux.go index a3e7ef7105..04c2e1944b 100644 --- a/src/runtime/pkg/resourcecontrol/utils_linux.go +++ b/src/runtime/pkg/resourcecontrol/utils_linux.go @@ -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 +}