-
Notifications
You must be signed in to change notification settings - Fork 2.2k
fix: cpu affinity #4815
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
fix: cpu affinity #4815
Conversation
What was the issue? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@AkihiroSuda +1, it would be great if we can have some test for this.
hmm I wonder if we can gather the cpuset from the container spec rather than inheriting what runc is being called with... |
I try to get it form cpuset but default is empty. |
I add an if,it only happen on linux.
|
cab02c9
to
d72f7b9
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR! I left several comments, but I guess we want also tests in tests/integration
:)
break | ||
} | ||
line := string(data) | ||
if line[:3] != "cpu" { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the comment that all cpu* lines are at the beginning belongs here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why did you mark this as solved? Am I missing something?
if line[:3] != "cpu" { | ||
break | ||
} | ||
if '0' <= line[3] && line[3] <= '9' { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here we need a commet explaining that there is a "cpu" line that we should ignore and only count cpu lines followed by a number.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
idem
libcontainer/process_linux.go
Outdated
if err := setAffinityAll(p.pid()); err != nil { | ||
return err | ||
} | ||
// Set final CPU affinity right after the process is moved into container's cgroup. | ||
if err := p.setFinalCPUAffinity(); err != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I ignore this, but why do we need to set the affinity to all cpus and then to what is in the config? I don't follow from the link in the description why this step is needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why was this marked as solved? Did I miss something?
ec7d0c0
to
2904639
Compare
@ningmingxiao ping when this is ready for another round of reviews (and the tests are green, hopefully :)) |
be17d50
to
81e87d8
Compare
cpuset := unix.CPUSet{} | ||
for i := 0; i < int(cpus); i++ { | ||
cpuset.Set(i) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was this tested with nested containers?
cpus
here can be different from the number of the available cpus
@@ -168,6 +168,7 @@ jobs: | |||
|
|||
- name: integration test (systemd driver) | |||
run: | | |||
sudo taskset -pc 0-1 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be moved to the bats script
@ningmingxiao if you can ping here when it's ready for review it would be great. Marking conversations as solved in github doesn't send any notification. And it's hard to know when it's ready for review again if you don't say anything or request another review in github. |
ok ! it can be reviewed now, thanks @rata @AkihiroSuda |
Signed-off-by: ningmingxiao <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, left a few more comments. But there are several open comments already, please have a look :)
aff := p.config.CPUAffinity | ||
if aff == nil || aff.Final == nil { | ||
return nil | ||
} | ||
if err := unix.SchedSetaffinity(p.pid(), aff.Final); err != nil { | ||
if err := unix.SchedSetaffinity(p.pid(), p.config.CPUAffinity.Final); err != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is the nil check not needed anymore?
// Set final CPU affinity right after the process is moved into container's cgroup. | ||
if err := p.setFinalCPUAffinity(); err != nil { | ||
return err | ||
|
||
if aff := p.config.CPUAffinity; aff == nil || aff.Final == nil { | ||
if err := setAffinityAll(p.pid()); err != nil { | ||
return err | ||
} | ||
} else { | ||
// Set final CPU affinity right after the process is moved into container's cgroup. | ||
if err := p.setFinalCPUAffinity(); err != nil { | ||
return err | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, it's not obvious to me why if the cpu affinity fails, we should call the set finalCPUAffinity. Can you elaborate?
@@ -615,6 +618,11 @@ func (p *initProcess) start() (retErr error) { | |||
return fmt.Errorf("unable to apply cgroup configuration: %w", err) | |||
} | |||
} | |||
|
|||
if err := setAffinityAll(p.pid()); err != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And why this here again?
Old kernels do that automatically, but new kernels remember the affinity that was set before the cgroup move due to
https://lore.kernel.org/lkml/[email protected]
This is undesirable for containers, because they inherit the systemd affinity when the should really move to the container space cpus.
ref:#4041
this patch https://lore.kernel.org/lkml/[email protected]/ doesn't merge in kernel but merged in redhat I want to find another way to fix it.