[PATCH v2] linux: fix accuracy of get_nprocs and get_nprocs_conf [BZ #28865]

Adhemerval Zanella adhemerval.zanella@linaro.org
Tue Feb 8 22:58:58 GMT 2022



On 08/02/2022 19:40, Dmitry V. Levin wrote:
> On Tue, Feb 08, 2022 at 04:34:42PM -0300, Adhemerval Zanella wrote:
>> On 07/02/2022 10:57, Dmitry V. Levin wrote:
>>> get_nprocs() and get_nprocs_conf() use various methods to obtain an
>>> accurate number of processors.  Re-introduce __get_nprocs_sched() as
>>> a source of information, and fix the order in which these methods are
>>> used to return the most accurate information.  The primary source of
>>> information used in both functions remains unchanged.
>>>
>>> This also changes __get_nprocs_sched() error return value from 2 to 0,
>>> but all its users are already prepared to handle that.
>>>
>>> Old behavior:
>>>   get_nprocs:
>>>     /sys/devices/system/cpu/online -> /proc/stat -> 2
>>>   get_nprocs_conf:
>>>     /sys/devices/system/cpu/ -> /proc/stat -> 2
>>>
>>> New behavior:
>>>   get_nprocs:
>>>     /sys/devices/system/cpu/online -> /proc/stat -> sched_getaffinity -> 2
>>>   get_nprocs_conf:
>>>     /sys/devices/system/cpu/ -> /proc/stat -> sched_getaffinity -> 2
>>>
>>> Fixes: 342298278e ("linux: Revert the use of sched_getaffinity on get_nproc")
>>> Closes: BZ #28865
>>
>> I am still not fully sure if sched_getaffinity is a correct fallback to an 
>> API that should return a system overview, the issue that lead 
>> sched_getaffinity removal was that it is subject to per-process filtering
>> (either by seccomp, cgroup, etc.) and it might trigger some wrong behavior
>> in some programs  (such as monitoring tools and jvms).
>>
>> However if the environment does not provide a way to actually obtain such
>> information I guess sched_getaffinity should not make things worse (it does
>> not make sense to assume multiprocessor if the process is not allowed more
>> than one CPU, and monitoring tools should not work if sysfs/procfs are
>> not present).
>>
>> LGTM with some nits below.  I think you might use brackets instead of
>> square bracket in title (to trigger the bugzilla scripts).
> 
> I don't think it's the kind of brackets that trigger bugzilla scripts.
> 
>> Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>
> 
> Corrected nits and pushed, thanks.

I forgot to ask you to hold on backports to see if anything breaks, but it
seems your were faster than me.


More information about the Libc-alpha mailing list