core: Add cgroupsv2 support to parallel.cpp#25285
Merged
asmorkalov merged 1 commit intoopencv:4.xfrom Mar 30, 2024
Merged
Conversation
The parallel code works out how many CPUs are on the system by checking
the quota it has been assigned in the Linux cgroup. The existing code
works under cgroups v1 but the file structure changed in cgroups v2.
From [1]:
"cpu.cfs_quota_us" and "cpu.cfs_period_us" are replaced by "cpu.max"
which contains both quota and period.
This commit add support to parallel so it will read from the cgroups v2
location. v1 support is still retained.
Resolves opencv#25284
[1] torvalds/linux@0d59363
Contributor
Author
|
I've build and tested this locally on my Linux system (Fedora 38) running cgroups v2 and the opencv is now reporting the expected number of threads when running in a container with a quota. |
mshabunin
reviewed
Mar 28, 2024
| @@ -966,8 +981,11 @@ int getNumberOfCPUs_() | |||
| static unsigned ncpus_impl_cpuset = (unsigned)getNumberOfCPUsImpl("/sys/fs/cgroup/cpuset/cpuset.cpus"); | |||
Contributor
There was a problem hiding this comment.
What about cpuset? Apparently on my system with cgroups2 (Ubuntu 22.04 - based) it has changed location to /sys/fs/cgroup/cpuset.cpus or cpuset.cpus.effective (idk which is right).
$ docker run --cpuset-cpus 1-3,8 -it ubuntu:22.04
root@f0e45a761cea:/# cat /sys/fs/cgroup/cpuset.cpus
1-3,8
Contributor
There was a problem hiding this comment.
It is a workaround for callers.
Because we could assign CPUs and also limit their usage:
docker run --rm --cpuset-cpus 1-3,8 --cpu-quota=300000 --cpu-period=100000 -it ubuntu:22.04
root@d4db947e6eba:/# cat /sys/fs/cgroup/cpuset.cpus
1-3,8
root@d4db947e6eba:/# cat /sys/fs/cgroup/cpu.max
300000 100000
opencv-alalek
approved these changes
Mar 30, 2024
Contributor
opencv-alalek
left a comment
There was a problem hiding this comment.
LGTM 👍 Thank you for contribution!
Merged
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
The parallel code works out how many CPUs are on the system by checking the quota it has been assigned in the Linux cgroup. The existing code works under cgroups v1 but the file structure changed in cgroups v2. From [1]:
which contains both quota and period.
This commit add support to parallel so it will read from the cgroups v2 location. v1 support is still retained.
Resolves #25284
[1] torvalds/linux@0d59363
Pull Request Readiness Checklist
See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request
Patch to opencv_extra has the same branch name.