Skip to content

Commit 23fe833

Browse files
committed
fix:cpu cpu affinity
Signed-off-by: ningmingxiao <[email protected]>
1 parent a85b5fc commit 23fe833

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

libcontainer/process_linux.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package libcontainer
22

33
import (
4+
"bufio"
45
"context"
56
"encoding/json"
67
"errors"
@@ -12,6 +13,7 @@ import (
1213
"path/filepath"
1314
"runtime"
1415
"strconv"
16+
"strings"
1517
"sync"
1618
"time"
1719

@@ -615,6 +617,13 @@ func (p *initProcess) start() (retErr error) {
615617
return fmt.Errorf("unable to apply cgroup configuration: %w", err)
616618
}
617619
}
620+
cpuset := unix.CPUSet{}
621+
for i := 0; i < getCPUCores(); i++ {
622+
cpuset.Set(i)
623+
}
624+
if err := unix.SchedSetaffinity(p.pid(), &cpuset); err != nil {
625+
return fmt.Errorf("error resetting pid %d affinity: %w", p.pid(), err)
626+
}
618627
if p.intelRdtManager != nil {
619628
if err := p.intelRdtManager.Apply(p.pid()); err != nil {
620629
return fmt.Errorf("unable to apply Intel RDT configuration: %w", err)
@@ -981,3 +990,20 @@ func (p *Process) InitializeIO(rootuid, rootgid int) (i *IO, err error) {
981990
}
982991
return i, nil
983992
}
993+
994+
func getCPUCores() int {
995+
file, err := os.Open("/proc/cpuinfo")
996+
if err != nil {
997+
return 0
998+
}
999+
defer file.Close()
1000+
scanner := bufio.NewScanner(file)
1001+
cores := 0
1002+
for scanner.Scan() {
1003+
line := scanner.Text()
1004+
if strings.HasPrefix(line, "processor") {
1005+
cores++
1006+
}
1007+
}
1008+
return cores
1009+
}

0 commit comments

Comments
 (0)