Skip to content

Commit

Permalink
virtcontainers: use resource control for setting CPU affinity
Browse files Browse the repository at this point in the history
Let's abstract the CPU affinity, instead of calling linux only code from
sandbox.

Fixes: #6044

Signed-off-by: Eric Ernst <eric_ernst@apple.com>
  • Loading branch information
egernst committed Jan 12, 2023
1 parent f137048 commit e3d3b72
Showing 1 changed file with 5 additions and 15 deletions.
20 changes: 5 additions & 15 deletions src/runtime/virtcontainers/sandbox.go
Expand Up @@ -44,7 +44,6 @@ import (
"github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/rootless"
"github.com/kata-containers/kata-containers/src/runtime/virtcontainers/types"
"github.com/kata-containers/kata-containers/src/runtime/virtcontainers/utils"
"golang.org/x/sys/unix"
)

// sandboxTracingTags defines tags for the trace span
Expand Down Expand Up @@ -2540,33 +2539,24 @@ func (s *Sandbox) checkVCPUsPinning(ctx context.Context) error {
}
return nil
}

// if equal, we can now start vCPU threads pinning
i := 0
for _, tid := range vCPUThreadsMap.vcpus {
unixCPUSet := unix.CPUSet{}
unixCPUSet.Set(cpuSetSlice[i])
if err := unix.SchedSetaffinity(tid, &unixCPUSet); err != nil {
// if equal, we can use vCPU thread pinning
for i, tid := range vCPUThreadsMap.vcpus {
if err := resCtrl.SetThreadAffinity(tid, cpuSetSlice[i:i+1]); err != nil {
if err := s.resetVCPUsPinning(ctx, vCPUThreadsMap, cpuSetSlice); err != nil {
return err
}
return fmt.Errorf("failed to set vcpu thread %d affinity to cpu %d: %v", tid, cpuSetSlice[i], err)
}
i++
}
s.isVCPUsPinningOn = true
return nil
}

// resetVCPUsPinning cancels current pinning and restores default random vCPU threads scheduling
func (s *Sandbox) resetVCPUsPinning(ctx context.Context, vCPUThreadsMap VcpuThreadIDs, cpuSetSlice []int) error {
unixCPUSet := unix.CPUSet{}
for cpuId := range cpuSetSlice {
unixCPUSet.Set(cpuId)
}
for _, tid := range vCPUThreadsMap.vcpus {
if err := unix.SchedSetaffinity(tid, &unixCPUSet); err != nil {
return fmt.Errorf("failed to reset vcpu thread %d affinity to default mode: %v", tid, err)
if err := resCtrl.SetThreadAffinity(tid, cpuSetSlice); err != nil {
return fmt.Errorf("failed to reset vcpu thread %d affinity: %v", tid, err)
}
}
return nil
Expand Down

0 comments on commit e3d3b72

Please sign in to comment.