Skip to content
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

[bug] get_cpus is not supporting cgroup2 #11635 #11667

Merged
merged 2 commits into from Jul 22, 2022

Conversation

MariaMozgunova
Copy link
Contributor

@MariaMozgunova MariaMozgunova commented Jul 18, 2022

Changelog: Fix: Get the cpu count for cgroup2 from /sys/fs/cgroup/cpu.max.
Docs: conan-io/docs#2658

Fixes #11635

Gets the cpu count for cgroup2 from /sys/fs/cgroup/cpu.max file. From docs, /sys/fs/cgroup/cpu.max file has the following format: $MAX $PERIOD.
In case $MAX is "max", return 1.
If there is no $PERIOD, use only $MAX as the return value.
When both values are integers, use them as before.

  • Refer to the issue that supports this Pull Request.
  • If the issue has missing info, explain the purpose/use case/pain/need that covers this Pull Request.
  • I've read the Contributing guide.
  • I've followed the PEP8 style guides for Python code.
  • I've opened another PR in the Conan docs repo to the develop branch, documenting this one.

Note: By default this PR will skip the slower tests and will use a limited set of python versions. Check here how to increase the testing level by writing some tags in the current PR body text.

@CLAassistant
Copy link

CLAassistant commented Jul 18, 2022

CLA assistant check
All committers have signed the CLA.

@MariaMozgunova
Copy link
Contributor Author

Should I document the changes to the Conan docs?

@memsharded memsharded added this to the 1.51 milestone Jul 18, 2022
@memsharded
Copy link
Member

Should I document the changes to the Conan docs?

Better wait until it is merged, and then yes, a PR to the docs, if necessary, would be nice, just a couple of lines if it makes sense

@MariaMozgunova
Copy link
Contributor Author

Do I need to do something for this PR to be merged?

@memsharded
Copy link
Member

Do I need to do something for this PR to be merged?

No, it is good, it just takes some time to review, too many ongoing things. It doesn't have tests, but honestly all tests are going to be pure mocks, so probably it is good.

Maybe @maikelvdh want to comment something more, if not, this will be merged and released in 1.51 (still a few days, Conan 2.0-beta.2 is in the next release)

if os.path.exists("/sys/fs/cgroup/cgroup.controllers"):
cpu_max = load("/sys/fs/cgroup/cpu.max").split()
if cpu_max[0] == "max":
return 1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In case of max it means basically no limits are established and therefore we should be able to rely on the multiprocessing.cpu_count(). In the current logic you would otherwise effectively in case of docker when cgroup V2 present and without any limiting of CPU cores applied just run with 1 jobs always.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But is it not an issue that multiprocessing.cpu_count() is used? because it will not match the docker assigned cores, and it can also overflow the container capacity?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is exactly what you want in case of docker if you are not using the runtime options to limit the CPU (https://docs.docker.com/config/containers/resource_constraints/#cpu). In case you are using this options to limit you will get not max as value.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, thanks very much for the clarification. @MariaMozgunova could you please apply this in your PR? Thanks!

conan/tools/build/cpu.py Outdated Show resolved Hide resolved
@maikelvdh
Copy link
Contributor

Thanks a lot @MariaMozgunova for addressing this problem 🚀 !

Maria Mozgunova added 2 commits July 21, 2022 06:01
When $MAX == "max", there is no limit on the cpu usage set
Use multiprocessing.cpu_count() function in that case
@memsharded memsharded merged commit 63fffb7 into conan-io:develop Jul 22, 2022
@memsharded
Copy link
Member

Merged, thanks for your contribution! It will be part of 1.51

If you now want to do a PR to the docs, something very simple about the behavior for cgroups2, a couple of lines would be enough, that would be great.

@MariaMozgunova
Copy link
Contributor Author

@memsharded Can you please hint to me what file precisely should be modified?

@uilianries
Copy link
Member

@MariaMozgunova The documentation already describes cgroup usage, so with your hotfix, the follow part should updated:

https://github.com/conan-io/docs/blob/79feb32d0be4d531a363b8948b6d8a2289e059ab/reference/tools.rst#toolscpu_count

The website will look like this: https://docs.conan.io/en/latest/reference/tools.html#tools-cpu-count

Thank you for your contribution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[bug] get_cpus is not supporting cgroup2
6 participants