mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2024-11-10 03:16:17 +01:00
Use CPU_COUNT to get the number of set CPUs
This fixes an issue with the number getting very big due to CPU_ISSET not returning exactly 0 or 1. This also fixes scenarios where there are holes in the CPU set. E.g. for a simple run like `taskset --cpu-list 1,2,4 ...` the old code would return 2 instead of 3, due to iterating until `ncores` (which is 3) and therefore not accounting for CPUs further in the set. Ref https://gitlab.gnome.org/GNOME/glib/-/merge_requests/3784
This commit is contained in:
parent
e124c34ad6
commit
cc25486b23
@ -1092,7 +1092,6 @@ g_get_num_processors (void)
|
||||
return count;
|
||||
#elif defined(_SC_NPROCESSORS_ONLN) && defined(THREADS_POSIX) && defined(HAVE_PTHREAD_GETAFFINITY_NP)
|
||||
{
|
||||
int idx;
|
||||
int ncores = MIN (sysconf (_SC_NPROCESSORS_ONLN), CPU_SETSIZE);
|
||||
cpu_set_t cpu_mask;
|
||||
CPU_ZERO (&cpu_mask);
|
||||
@ -1100,8 +1099,7 @@ g_get_num_processors (void)
|
||||
int af_count = 0;
|
||||
int err = pthread_getaffinity_np (pthread_self (), sizeof (cpu_mask), &cpu_mask);
|
||||
if (!err)
|
||||
for (idx = 0; idx < ncores && idx < CPU_SETSIZE; ++idx)
|
||||
af_count += CPU_ISSET (idx, &cpu_mask);
|
||||
af_count = CPU_COUNT (&cpu_mask);
|
||||
|
||||
int count = (af_count > 0) ? af_count : ncores;
|
||||
return count;
|
||||
|
Loading…
Reference in New Issue
Block a user