Merge pull request #25285 from johnteslade:cgroupsv2-support

core: Add cgroupsv2 support to parallel.cpp
This commit is contained in:
Alexander Smorkalov 2024-03-30 11:26:23 +03:00 committed by GitHub
commit 9f123f8d74
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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;
{
@ -966,8 +981,11 @@ int getNumberOfCPUs_()
static unsigned ncpus_impl_cpuset = (unsigned)getNumberOfCPUsImpl("/sys/fs/cgroup/cpuset/cpuset.cpus");
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");