2222
2323#include <errno.h>
2424#include <limits.h>
25+ #if HAVE_MNTENT_H
26+ # include <mntent.h>
27+ #endif
2528#include <stdlib.h>
29+ #include <stdio.h>
30+ #include <string.h>
2631#include <unistd.h>
2732
2833#if HAVE_PTHREAD_GETAFFINITY_NP && 0
6267
6368#define ARRAY_SIZE (a ) (sizeof (a) / sizeof ((a)[0]))
6469
70+ #define NPROC_MINIMUM 1
71+
6572/* Return the number of processors available to the current process, based
6673 on a modern system call that returns the "affinity" between the current
6774 process and each CPU. Return 0 if unknown or if such a system call does
@@ -244,7 +251,7 @@ num_processors_via_affinity_mask (void)
244251/* Return the total number of processors. Here QUERY must be one of
245252 NPROC_ALL, NPROC_CURRENT. The result is guaranteed to be at least 1. */
246253static unsigned long int
247- num_processors_ignoring_omp (enum nproc_query query )
254+ num_processors_available (enum nproc_query query )
248255{
249256 /* On systems with a modern affinity mask system call, we have
250257 sysconf (_SC_NPROCESSORS_CONF)
@@ -377,7 +384,159 @@ num_processors_ignoring_omp (enum nproc_query query)
377384 }
378385#endif
379386
380- return 1 ;
387+ return NPROC_MINIMUM ;
388+ }
389+
390+ #if defined __linux__ || defined __ANDROID__
391+ /* Identify the cgroup2 mount point,
392+ initially at the usual location for efficiency,
393+ resorting to searching mount points otherwise.
394+ Return NULL if the mount point is not found.
395+ The returned string can be freed. */
396+ static char *
397+ cgroup2_mount (void )
398+ {
399+ FILE * fp ;
400+ char * ret = NULL ;
401+
402+ /* Check the usual location first. */
403+ if (access ("/sys/fs/cgroup/cgroup.controllers" , F_OK ) == 0 )
404+ return strdup ("/sys/fs/cgroup" );
405+
406+ #if HAVE_MNTENT_H
407+ /* Otherwise look for the mount point. */
408+ struct mntent * mnt ;
409+ if (! (fp = setmntent ("/proc/mounts" , "r" )))
410+ return NULL ;
411+ while ((mnt = getmntent (fp )) != NULL )
412+ {
413+ if (strcmp (mnt -> mnt_type , "cgroup2" ) == 0 )
414+ {
415+ ret = strdup (mnt -> mnt_dir );
416+ break ;
417+ }
418+ }
419+ endmntent (fp );
420+ #endif
421+
422+ return ret ;
423+ }
424+
425+ /* Return the minimum configured cgroupv2 CPU quota for the current process.
426+ Return ULONG_MAX if quota can't be read.
427+ Returned value will be >= 1. */
428+ static unsigned long int
429+ get_cgroup2_cpu_quota (void )
430+ {
431+ unsigned long int cpu_quota = ULONG_MAX ;
432+ FILE * fp ;
433+
434+ fp = fopen ("/proc/self/cgroup" , "r" );
435+ if (! fp )
436+ return cpu_quota ;
437+
438+ /* Get our cgroupv2 (unififed) hierarchy. */
439+ char * cgroup = NULL ;
440+ char * cgroup_str = NULL ;
441+ size_t cgroup_size = 0 ;
442+ ssize_t read ;
443+ while ((read = getline (& cgroup_str , & cgroup_size , fp )) != -1 )
444+ {
445+ if (strncmp (cgroup_str , "0::/" , 4 ) == 0 )
446+ {
447+ char * end = cgroup_str + read - 1 ;
448+ if (* end == '\n' )
449+ * end = '\0' ;
450+ cgroup = cgroup_str + 3 ;
451+ break ;
452+ }
453+ }
454+ fclose (fp );
455+
456+ char * mount = NULL ;
457+ if (cgroup && ! (mount = cgroup2_mount ()))
458+ cgroup = NULL ;
459+
460+ /* Find the lowest quota in the hierarchy. */
461+ char * quota_str = NULL ;
462+ size_t quota_size = 0 ;
463+ while (cgroup && * cgroup )
464+ {
465+ /* Walk back up the nested cgroup hierarchy
466+ to find the lowest cpu quota as defined in a cpu.max file.
467+ Note this file may not be present if the cpu controller
468+ is not enabled for that part of the hierarchy. */
469+
470+ char cpu_max_file [PATH_MAX ];
471+ snprintf (cpu_max_file , sizeof (cpu_max_file ),
472+ "%s%s/cpu.max" , mount , cgroup );
473+
474+ if ((fp = fopen (cpu_max_file , "r" ))
475+ && getline (& quota_str , & quota_size , fp ) != -1
476+ && strncmp (quota_str , "max" , 3 ) != 0 )
477+ {
478+ long quota , period ;
479+ if (sscanf (quota_str , "%ld %ld" , & quota , & period ) == 2 && period )
480+ {
481+ double ncpus = (double )quota / period ;
482+ if (cpu_quota == ULONG_MAX || ncpus < cpu_quota )
483+ {
484+ cpu_quota = MAX (1 , (long )(ncpus + 0.5 ));
485+ /* nproc will return 1 minimum, so no point going lower */
486+ if (cpu_quota == 1 )
487+ * cgroup = '\0' ;
488+ }
489+ }
490+ }
491+
492+ if (fp )
493+ fclose (fp );
494+
495+ char * last_sep = strrchr (cgroup , '/' );
496+ if (! last_sep )
497+ break ;
498+ if (last_sep == cgroup && * (cgroup + 1 ))
499+ * (cgroup + 1 ) = '\0' ; /* Iterate on "/" also. */
500+ else
501+ * last_sep = '\0' ;
502+ }
503+
504+ free (quota_str );
505+ free (mount );
506+ free (cgroup_str );
507+
508+ return cpu_quota ;
509+ }
510+ #endif
511+
512+
513+ /* Return the cgroupv2 CPU quota if the current scheduler honors it.
514+ Otherwise return ULONG_MAX.
515+ Returned value will be >= 1. */
516+ static unsigned long int
517+ cpu_quota (void )
518+ {
519+ unsigned long int quota = ULONG_MAX ;
520+
521+ #if defined __linux__ || defined __ANDROID__
522+ # if HAVE_SCHED_GETAFFINITY_LIKE_GLIBC && defined SCHED_DEADLINE
523+ /* We've a new enough sched.h */
524+ switch (sched_getscheduler (0 ))
525+ {
526+ case -1 :
527+ case SCHED_FIFO :
528+ case SCHED_RR :
529+ case SCHED_DEADLINE :
530+ quota = ULONG_MAX ;
531+ break ;
532+ default :
533+ quota = get_cgroup2_cpu_quota ();
534+ break ;
535+ }
536+ # endif
537+ #endif
538+
539+ return quota ;
381540}
382541
383542/* Parse OMP environment variables without dependence on OMP.
@@ -416,13 +575,13 @@ parse_omp_threads (char const* threads)
416575unsigned long int
417576num_processors (enum nproc_query query )
418577{
419- unsigned long int omp_env_limit = ULONG_MAX ;
578+ unsigned long int nproc_limit = ULONG_MAX ;
420579
580+ /* Honor the OpenMP environment variables, recognized also by all
581+ programs that are based on OpenMP. */
421582 if (query == NPROC_CURRENT_OVERRIDABLE )
422583 {
423- unsigned long int omp_env_threads ;
424- /* Honor the OpenMP environment variables, recognized also by all
425- programs that are based on OpenMP. */
584+ unsigned long int omp_env_threads , omp_env_limit ;
426585 omp_env_threads = parse_omp_threads (getenv ("OMP_NUM_THREADS" ));
427586 omp_env_limit = parse_omp_threads (getenv ("OMP_THREAD_LIMIT" ));
428587 if (! omp_env_limit )
@@ -431,14 +590,22 @@ num_processors (enum nproc_query query)
431590 if (omp_env_threads )
432591 return MIN (omp_env_threads , omp_env_limit );
433592
593+ nproc_limit = omp_env_limit ;
434594 query = NPROC_CURRENT ;
435595 }
436- /* Here query is one of NPROC_ALL, NPROC_CURRENT. */
437- if (omp_env_limit == 1 )
438- /* No need to even call num_processors_ignoring_omp (query). */
439- return 1 ;
440- {
441- unsigned long nprocs = num_processors_ignoring_omp (query );
442- return MIN (nprocs , omp_env_limit );
443- }
596+
597+ /* Honor any CPU quotas. */
598+ if (query == NPROC_CURRENT && nproc_limit > NPROC_MINIMUM )
599+ {
600+ unsigned long int quota = cpu_quota ();
601+ nproc_limit = MIN (quota , nproc_limit );
602+ }
603+
604+ if (nproc_limit > NPROC_MINIMUM )
605+ {
606+ unsigned long nprocs = num_processors_available (query );
607+ nproc_limit = MIN (nprocs , nproc_limit );
608+ }
609+
610+ return nproc_limit ;
444611}
0 commit comments