Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: talostrading/sonic
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v0.5.7
Choose a base ref
...
head repository: talostrading/sonic
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v0.6.0
Choose a head ref
  • 1 commit
  • 3 files changed
  • 1 contributor

Commits on Sep 29, 2023

  1. Pin to multiple cpus

    sergiu128 committed Sep 29, 2023

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    a602571 View commit details
Showing with 51 additions and 9 deletions.
  1. +1 −1 util/cpu_pin_bsd.go
  2. +13 −6 util/cpu_pin_linux.go
  3. +37 −2 util/cpu_pin_test.go
2 changes: 1 addition & 1 deletion util/cpu_pin_bsd.go
Original file line number Diff line number Diff line change
@@ -2,6 +2,6 @@

package util

func PinTo(int) error {
func PinTo(...int) error {
return nil
}
19 changes: 13 additions & 6 deletions util/cpu_pin_linux.go
Original file line number Diff line number Diff line change
@@ -8,11 +8,13 @@ import (
"golang.org/x/sys/unix"
)

func PinTo(cpuID int) error {
cpuset := &unix.CPUSet{}
cpuset.Set(cpuID)
func PinTo(cpus ...int) error {
set := &unix.CPUSet{}
for _, cpu := range cpus {
set.Set(cpu)
}

err := unix.SchedSetaffinity(0, cpuset)
err := unix.SchedSetaffinity(0, set)
if err != nil {
return err
}
@@ -23,8 +25,13 @@ func PinTo(cpuID int) error {
return err
}

if verify.Count() != 1 {
return fmt.Errorf("could not pin to CPU %d", cpuID)
if verify.Count() != len(cpus) {
return fmt.Errorf("could not pin to CPUs %v", cpus)
}
for _, cpu := range cpus {
if !verify.IsSet(cpu) {
return fmt.Errorf("could not pin to CPUs %v", cpus)
}
}

return nil
39 changes: 37 additions & 2 deletions util/cpu_pin_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,44 @@
package util

import "testing"
import (
"log"
"os"
"sort"
"strconv"
"strings"
"testing"
)

func getAllCPUs() ([]int, bool) {
entries, err := os.ReadDir("/sys/devices/system/cpu")
if err == nil {
var cpus []int
for _, entry := range entries {
cpuid, ok := strings.CutPrefix(entry.Name(), "cpu")
if ok {
id, err := strconv.ParseInt(cpuid, 10, 64)
if err == nil {
cpus = append(cpus, int(id))
}
}
}
sort.Slice(cpus, func(i, j int) bool {
return cpus[i] < cpus[j]
})
return cpus, true
}
return nil, false
}

func TestCPUPin(t *testing.T) {
if err := PinTo(0); err != nil {
t.Fatal(err)
t.Fatalf("could not pin to cpu 0 err=%v", err)
}

if cpus, ok := getAllCPUs(); ok {
log.Printf("pinning to all cpus: %v", cpus)
if err := PinTo(cpus...); err != nil {
t.Fatalf("could not pin to cpus %v err=%v", cpus, err)
}
}
}