cpupin_linux.go 672 B

1234567891011121314151617181920212223
  1. //go:build linux && !android && !e2e_testing
  2. package nebula
  3. import (
  4. "runtime"
  5. "golang.org/x/sys/unix"
  6. )
  7. // pinThreadToCPU restricts the calling OS thread to the given CPU via
  8. // sched_setaffinity(2). Combined with runtime.LockOSThread on the
  9. // goroutine, this prevents the kernel from migrating us across CPUs and
  10. // in turn keeps every sendmmsg from this goroutine going through the
  11. // same XPS-selected TX ring, eliminating the wire-side reorder that
  12. // otherwise fragments one nebula flow across multiple rings.
  13. func pinThreadToCPU(cpu int) error {
  14. runtime.LockOSThread()
  15. var set unix.CPUSet
  16. set.Zero()
  17. set.Set(cpu)
  18. return unix.SchedSetaffinity(0, &set)
  19. }