Skip to content

Commit

Permalink
wipt
Browse files Browse the repository at this point in the history
Signed-off-by: Kir Kolyshkin <[email protected]>
  • Loading branch information
kolyshkin committed Jan 11, 2025
1 parent dbf1e6d commit fb90dcd
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
26 changes: 20 additions & 6 deletions libcontainer/init_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,23 @@ type initConfig struct {

// Init is part of "runc init" implementation.
func Init() {
var cpus0, cpus1 unix.CPUSet
err := unix.SchedGetaffinity(0, &cpus0)
if err != nil {
fmt.Fprintf(os.Stderr, "sched_getaffinity 0 error: %s\n", err)
os.Exit(255)
}

runtime.GOMAXPROCS(1)
runtime.LockOSThread()

if err := startInitialization(); err != nil {
err = unix.SchedGetaffinity(0, &cpus1)
if err != nil {
fmt.Fprintf(os.Stderr, "sched_getaffinity 0 error: %s\n", err)
os.Exit(255)
}

if err := startInitialization(&cpus0, &cpus1); err != nil {
// If the error is returned, it was not communicated
// back to the parent (which is not a common case),
// so print it to stderr here as a last resort.
Expand All @@ -97,7 +110,7 @@ func Init() {
// Normally, this function does not return. If it returns, with or without an
// error, it means the initialization has failed. If the error is returned,
// it means the error can not be communicated back to the parent.
func startInitialization() (retErr error) {
func startInitialization(cpus0, cpus1 *unix.CPUSet) (retErr error) {
// Get the synchronisation pipe.
envSyncPipe := os.Getenv("_LIBCONTAINER_SYNCPIPE")
syncPipeFd, err := strconv.Atoi(envSyncPipe)
Expand Down Expand Up @@ -155,18 +168,19 @@ func startInitialization() (retErr error) {

// See tests/integration/cpu_affinity.bats.
if logrus.GetLevel() >= logrus.DebugLevel {
var cpus unix.CPUSet
err := unix.SchedGetaffinity(0, &cpus)
var cpus2 unix.CPUSet
err := unix.SchedGetaffinity(0, &cpus2)
if err != nil {
logrus.Debugf("sched_getaffinity: error %v", err)
} else {
}
for n, cpus := range []*unix.CPUSet{cpus0, cpus1, &cpus2} {
var list []int
for i := 0; i < 32; i++ {
if cpus.IsSet(i) {
list = append(list, i)
}
}
logrus.Debugf("Initial CPUs: %v", list)
logrus.Debugf("Initial CPUs %d: %v", n, list)
}
}

Expand Down
8 changes: 6 additions & 2 deletions tests/integration/cpu_affinity.bats
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ function first_cpu() {
[[ "$output" == *"nsexec-1["*": CPUs: $exp "* ]]
[[ "$output" == *"nsexec-2["*": CPUs: $exp "* ]]
[[ "$output" == *"Initial CPU affinity: $cpus"* ]]
[[ "$output" == *"Initial CPUs: [$exp]"* ]]
[[ "$output" == *"Initial CPUs 0: [$exp]"* ]]
[[ "$output" == *"Initial CPUs 1: [$exp]"* ]]
[[ "$output" == *"Initial CPUs 2: [$exp]"* ]]
done
}

Expand All @@ -68,6 +70,8 @@ function first_cpu() {
[[ "$output" == *"nsexec-1["*": CPUs: $first "* ]]
[[ "$output" == *"nsexec-2["*": CPUs: $first "* ]]
[[ "$output" == *"Initial CPU affinity: $first"* ]]
[[ "$output" == *"Initial CPUs: [$first]"* ]]
[[ "$output" == *"Initial CPUs 0: [$first]"* ]]
[[ "$output" == *"Initial CPUs 1: [$first]"* ]]
[[ "$output" == *"Initial CPUs 2: [$first]"* ]]
[[ "$output" == *"Cpus_allowed_list: $second"* ]] # Mind the literal tab.
}

0 comments on commit fb90dcd

Please sign in to comment.