more fixes!

This commit is contained in:
JackDoan
2026-07-13 17:54:17 -05:00
parent c7918d1096
commit 1aca2f75ae
20 changed files with 489 additions and 241 deletions
+20
View File
@@ -21,3 +21,23 @@ func PinThreadToCPU(cpu int) error {
set.Set(cpu)
return unix.SchedSetaffinity(0, &set)
}
// AllowedCPUs returns the CPU IDs the calling process is currently allowed to
// run on, as reported by sched_getaffinity(2). Under a cgroup cpuset or a
// `taskset` mask the allowed IDs are frequently not the contiguous range
// 0..NumCPU-1 (e.g. pinned to CPUs 4-7: NumCPU reports 4 while the valid IDs
// are 4,5,6,7). Callers that need a real CPU to pin to must choose from this
// set rather than assuming i % NumCPU is runnable, or every pin fails.
func AllowedCPUs() ([]int, error) {
var set unix.CPUSet
if err := unix.SchedGetaffinity(0, &set); err != nil {
return nil, err
}
cpus := make([]int, 0, set.Count())
for cpu := 0; cpu < len(set)*64; cpu++ {
if set.IsSet(cpu) {
cpus = append(cpus, cpu)
}
}
return cpus, nil
}
+7
View File
@@ -9,3 +9,10 @@ package util
func PinThreadToCPU(_ int) error {
return nil
}
// AllowedCPUs has no meaningful answer off Linux (no sched_getaffinity), so it
// reports "unknown" by returning a nil slice and nil error. Callers treat an
// empty result as "fall back to the default CPU choice".
func AllowedCPUs() ([]int, error) {
return nil, nil
}