Skip to content

Commit

Permalink
wip
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 1f296a7 commit 2664990
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 20 deletions.
36 changes: 18 additions & 18 deletions libcontainer/init_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,24 @@ func startInitialization() (retErr error) {

logrus.SetOutput(logPipe)
logrus.SetFormatter(new(logrus.JSONFormatter))
logrus.Debug("child process in init()")
logrus.Debugf("child process in init(), pid=%d,tid=%d", unix.Getpid(), unix.Gettid())

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

// Only init processes have FIFOFD.
var fifoFile *os.File
Expand Down Expand Up @@ -199,23 +216,6 @@ func startInitialization() (retErr error) {
}
}()

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

var config initConfig
if err := json.NewDecoder(initPipe).Decode(&config); err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions libcontainer/nsenter/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ void write_log(int level, const char *format, ...)
goto out;
}
}
ret = asprintf(&json, "{\"level\":\"%s\", \"msg\": \"%s[%d]: %s\"}\n",
level_str[level], stage, getpid(), message);
ret = asprintf(&json, "{\"level\":\"%s\", \"msg\": \"%s[%d,%d]: %s\"}\n",
level_str[level], stage, getpid(), gettid(), message);
if (ret < 0) {
json = NULL;
goto out;
Expand Down
23 changes: 23 additions & 0 deletions libcontainer/nsenter/nsexec.c
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,22 @@ static void update_timens_offsets(pid_t pid, char *map, size_t map_len)
bail("failed to update /proc/%d/timens_offsets", pid);
}

void print_cpu_affinity() {
cpu_set_t cpus = {};

if (sched_getaffinity(0, sizeof(cpus), &cpus) >= 0) {
char buf[128], *bp;
bp = buf;
for (int i = 0; i < 32; i++) {
if CPU_ISSET(i, &cpus)
bp += sprintf(bp, "%d ", i);
}
write_log(DEBUG, "CPUs: %s", buf);
} else {
write_log(WARNING, "sched_getaffinity: %m");
}
}

void nsexec(void)
{
int pipenum;
Expand All @@ -697,6 +713,8 @@ void nsexec(void)
return;
}

print_cpu_affinity();

write_log(DEBUG, "=> nsexec container setup");

/* Parse all of the netlink configuration. */
Expand Down Expand Up @@ -803,6 +821,8 @@ void nsexec(void)
current_stage = STAGE_PARENT;
prctl(PR_SET_NAME, (unsigned long)"runc:[0:PARENT]", 0, 0, 0);
write_log(DEBUG, "~> nsexec stage-0");
print_cpu_affinity();


/* Start the process of getting a container. */
write_log(DEBUG, "spawn stage-1");
Expand Down Expand Up @@ -967,6 +987,8 @@ void nsexec(void)
/* For debugging. */
prctl(PR_SET_NAME, (unsigned long)"runc:[1:CHILD]", 0, 0, 0);
write_log(DEBUG, "~> nsexec stage-1");
print_cpu_affinity();


/*
* We need to setns first. We cannot do this earlier (in stage 0)
Expand Down Expand Up @@ -1174,6 +1196,7 @@ void nsexec(void)

/* Finish executing, let the Go runtime take over. */
write_log(DEBUG, "<= nsexec container setup");
print_cpu_affinity();
write_log(DEBUG, "booting up go runtime ...");
return;
}
Expand Down
9 changes: 9 additions & 0 deletions tests/integration/cpu_affinity.bats
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ function first_cpu() {
exp=${exp//-/ } # 2. "-" --> " ".
echo "CPUS: $cpus, exp: $exp"
runc --debug exec --process <(echo "$proc") ct1
[[ "$output" == *"nsexec["*": CPUs: $exp "* ]]
[[ "$output" == *"nsexec-0["*": CPUs: $exp "* ]]
[[ "$output" == *"nsexec-1["*": CPUs: $exp "* ]]
[[ "$output" == *"nsexec-2["*": CPUs: $exp "* ]]
[[ "$output" == *"Initial CPU affinity: $cpus"* ]]
[[ "$output" == *"Initial CPUs: [$exp]"* ]]
done
Expand All @@ -59,6 +63,11 @@ function first_cpu() {

runc --debug exec ct1 grep "Cpus_allowed_list:" /proc/self/status
[ "$status" -eq 0 ]
[[ "$output" == *"nsexec["*": CPUs: $first "* ]]
[[ "$output" == *"nsexec-0["*": CPUs: $first "* ]]
[[ "$output" == *"nsexec-1["*": CPUs: $first "* ]]
[[ "$output" == *"nsexec-2["*": CPUs: $first "* ]]
[[ "$output" == *"Initial CPU affinity: $first"* ]]
[[ "$output" == *"Initial CPUs: [$first]"* ]]
[[ "$output" == *"Cpus_allowed_list: $second"* ]] # Mind the literal tab.
}

0 comments on commit 2664990

Please sign in to comment.