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

Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions modules/core/src/parallel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -870,7 +870,22 @@ int getNumberOfCPUsImpl(const char *filename)

#if defined CV_HAVE_CGROUPS
static inline
unsigned getNumberOfCPUsCFS()
unsigned getNumberOfCPUsCFSv2()
{
int cfs_quota = 0;
int cfs_period = 0;

std::ifstream ss_cpu_max("/sys/fs/cgroup/cpu.max", std::ios::in | std::ios::binary);
ss_cpu_max >> cfs_quota >> cfs_period;

if (ss_cpu_max.fail() || cfs_quota < 1 || cfs_period < 1) /* values must not be 0 or negative */
return 0;

return (unsigned)max(1, cfs_quota/cfs_period);
}

static inline
unsigned getNumberOfCPUsCFSv1()
{
int cfs_quota = 0;
{
Expand Down Expand Up @@ -966,8 +981,11 @@ int getNumberOfCPUs_()
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

ncpus = minNonZero(ncpus, ncpus_impl_cpuset);

static unsigned ncpus_impl_cfs = getNumberOfCPUsCFS();
ncpus = minNonZero(ncpus, ncpus_impl_cfs);
static unsigned ncpus_impl_cfs_v1 = getNumberOfCPUsCFSv1();
ncpus = minNonZero(ncpus, ncpus_impl_cfs_v1);

static unsigned ncpus_impl_cfs_v2 = getNumberOfCPUsCFSv2();
ncpus = minNonZero(ncpus, ncpus_impl_cfs_v2);
#endif

static unsigned ncpus_impl_devices = (unsigned)getNumberOfCPUsImpl("/sys/devices/system/cpu/online");
Expand Down