Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@johnteslade
Copy link
Contributor

@johnteslade johnteslade commented Mar 28, 2024

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 #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

  • I agree to contribute to the project under Apache 2 License.
  • To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
  • The PR is proposed to the proper branch
  • There is a reference to the original bug report and related work
  • There is accuracy test, performance test and test data in opencv_extra repository, if applicable
    Patch to opencv_extra has the same branch name.
  • The feature is well documented and sample code can be built with the project CMake

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
@johnteslade
Copy link
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.

$ mount -l | grep cgroup
cgroup2 on /sys/fs/cgroup type cgroup2 (rw,nosuid,nodev,noexec,relatime,nsdelegate,memory_recursiveprot)

$ docker run --cpu-quota=300000 --cpu-period=100000 ...

$ opencv_version --threads
Parallel framework: pthreads (nthreads=3)

@asmorkalov asmorkalov self-requested a review March 28, 2024 13:12
@asmorkalov asmorkalov added this to the 4.10.0 milestone Mar 28, 2024
@asmorkalov asmorkalov self-assigned this Mar 28, 2024
#elif defined CV_CPU_GROUPS_1

#if defined CV_HAVE_CGROUPS
static unsigned ncpus_impl_cpuset = (unsigned)getNumberOfCPUsImpl("/sys/fs/cgroup/cpuset/cpuset.cpus");
Copy link
Contributor

@mshabunin mshabunin Mar 28, 2024

Choose a reason for hiding this comment

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

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

Copy link
Contributor

Choose a reason for hiding this comment

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

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

Copy link
Contributor

@opencv-alalek opencv-alalek left a comment

Choose a reason for hiding this comment

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

LGTM 👍 Thank you for contribution!

@asmorkalov asmorkalov merged commit 9f123f8 into opencv:4.x Mar 30, 2024
@asmorkalov asmorkalov mentioned this pull request Apr 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

cv::getCPUCount has no support for Linux cgroups v2

4 participants