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
Detect the number of CPUs used by Docker (#5464) #5466
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
00c232b
#5464 Detect CPUs in used by Docker
uilianries c1890e6
#5464 Fail safe when reading cgroup
uilianries d7ac67a
#5464 truncate docker cpus
uilianries 9e95602
#5464 Load cgroup files
uilianries 440bad6
#5464 Ignore docker container
uilianries 75982de
#5464 Validate cpu count in containers
uilianries 5a30048
#5464 Validate cpu quota by zero
uilianries cb602d9
#5464 Fix tests
uilianries 61ba286
Merge branch 'develop' into hotfix/cpu-count-docker
uilianries 8c91142
#5464 Use CpuProperties directly
uilianries 777f629
#5464 Remove temporary cpu variable
uilianries File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,8 +4,8 @@ | |
import subprocess | ||
import sys | ||
import tempfile | ||
from subprocess import PIPE | ||
|
||
import math | ||
from subprocess import CalledProcessError, PIPE | ||
import six | ||
|
||
from conans.client.tools.env import environment_append | ||
|
@@ -24,14 +24,35 @@ def args_to_string(args): | |
return " ".join("'" + arg.replace("'", r"'\''") + "'" for arg in args) | ||
|
||
|
||
class CpuProperties(object): | ||
|
||
def get_cpu_quota(self): | ||
return int(load("/sys/fs/cgroup/cpu/cpu.cfs_quota_us")) | ||
|
||
def get_cpu_period(self): | ||
return int(load("/sys/fs/cgroup/cpu/cpu.cfs_period_us")) | ||
|
||
def get_cpus(self): | ||
try: | ||
cfs_quota_us = self.get_cpu_quota() | ||
cfs_period_us = self.get_cpu_period() | ||
if cfs_quota_us > 0 and cfs_period_us > 0: | ||
return int(math.ceil(cfs_quota_us / cfs_period_us)) | ||
except: | ||
pass | ||
return multiprocessing.cpu_count() | ||
|
||
|
||
def cpu_count(output=None): | ||
output = default_output(output, 'conans.client.tools.oss.cpu_count') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why removing this output? This would make the |
||
try: | ||
env_cpu_count = os.getenv("CONAN_CPU_COUNT", None) | ||
if env_cpu_count is not None and not env_cpu_count.isdigit(): | ||
raise ConanException("Invalid CONAN_CPU_COUNT value '%s', " | ||
"please specify a positive integer" % env_cpu_count) | ||
return int(env_cpu_count) if env_cpu_count else multiprocessing.cpu_count() | ||
if env_cpu_count: | ||
return int(env_cpu_count) | ||
else: | ||
return CpuProperties().get_cpus() | ||
except NotImplementedError: | ||
output.warn("multiprocessing.cpu_count() not implemented. Defaulting to 1 cpu") | ||
return 1 # Safe guess | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This is a bit too much Object Oriented for me without a real goal:
Now it is easy to test too, and also testing is more real, it test also the load of file + conversion to integer