From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-ot1-x336.google.com (mail-ot1-x336.google.com [IPv6:2607:f8b0:4864:20::336]) by sourceware.org (Postfix) with ESMTPS id 72B0A3858C2C for ; Wed, 23 Mar 2022 20:38:40 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 72B0A3858C2C Received: by mail-ot1-x336.google.com with SMTP id k18-20020a9d7012000000b005cdb11186f2so631950otj.9 for ; Wed, 23 Mar 2022 13:38:40 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:mime-version :content-transfer-encoding; bh=FZzxNSrguV1r/C3xMQOzMhYeyAZPjb4tfZMx0Ww6SkU=; b=x0ZdIESfMNGoQCkk9xq5VXCw8YshXTc4zlf4uMG1b44PfuTG/UsB0hJ6e5hAd8jCw+ VNEK5rItSqDg6YbuMZ2EyJwg5OooURrHMln3toV4qf9WP8WuY+yZBGSmc0Z30kLCjgEW sYndY5Y5+TnRdM2bzcauUrDztLBk4ARg40Dx1yBw6eYH24X2Cgb2xXkZDzqcOJibnye8 DuqmGeQdT6A6ir8byW9ectGbk74OZhMJfx4oFko+XwciAOb32bfi0q0Ku3r+HDadWIR0 6LyO0XV4iXisVPODUmITO7yH/1bDBkmql/rFgm7dFte0zXvmnPA6/ECwwofJohhtvLn8 H+xg== X-Gm-Message-State: AOAM5319est/STex8SUMwPtRegrOS4uMxlDFiXVLYTPs70Hw20cI0Ol8 28siD/qSYjrTbTo1WM/F2KztZuwBGOZD/w== X-Google-Smtp-Source: ABdhPJwJbrwZZHD9wscVN+XmRf/XMwjpXkALretmCYU1ruPb6YFybFjHHK+5IMiBiu2u09ZL6MHEAw== X-Received: by 2002:a05:6830:25cc:b0:5c9:5fc5:32b1 with SMTP id d12-20020a05683025cc00b005c95fc532b1mr823926otu.138.1648067919411; Wed, 23 Mar 2022 13:38:39 -0700 (PDT) Received: from birita.. ([2804:431:c7ca:2d55:7794:d006:fba1:8b8f]) by smtp.gmail.com with ESMTPSA id k14-20020a056830168e00b005b22c20b195sm456004otr.45.2022.03.23.13.38.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 23 Mar 2022 13:38:38 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org, Robert O'Callahan , Andreas Schwab Subject: [PATCH] linux: Use /sys/devices/system/cpu on __get_nprocs_conf (BZ#28991) Date: Wed, 23 Mar 2022 17:38:35 -0300 Message-Id: <20220323203835.3653589-1-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.32.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-12.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 23 Mar 2022 20:38:43 -0000 Currently on Linux __get_nprocs_conf first tries to enumerate the cpus present in the system by iterating on /sys/devices/system/cpuX directories. This only enumerates the CPUs that are present in system (but possibly offline), not taking in account possible CPU that might added in the system through hotplugging. Linux provides the maximum number of configured cpus on the /sys/devices/system/cpu file. Although it might present a larger value of possible active CPUs on some system (where kernel either get the information from firmaware or is configured at boot time), the information is what kernel presents to userland. This also change the returned value of _SC_NPROCESSORS_CONF, which aligns as the maximum configure cpu in the system. Checked on x86_64-linux-gnu. --- sysdeps/unix/sysv/linux/getsysstats.c | 36 +++------------------------ 1 file changed, 4 insertions(+), 32 deletions(-) diff --git a/sysdeps/unix/sysv/linux/getsysstats.c b/sysdeps/unix/sysv/linux/getsysstats.c index d1ea074f0d..064eaa08ae 100644 --- a/sysdeps/unix/sysv/linux/getsysstats.c +++ b/sysdeps/unix/sysv/linux/getsysstats.c @@ -19,7 +19,6 @@ #include #include #include -#include #include #include #include @@ -136,7 +135,7 @@ get_nproc_stat (void) } static int -get_nprocs_cpu_online (void) +read_sysfs_file (const char *fname) { enum { buffer_size = 1024 }; char buffer[buffer_size]; @@ -146,7 +145,7 @@ get_nprocs_cpu_online (void) const int flags = O_RDONLY | O_CLOEXEC; /* This file contains comma-separated ranges. */ - int fd = __open_nocancel ("/sys/devices/system/cpu/online", flags); + int fd = __open_nocancel (fname, flags); char *l; int result = 0; if (fd != -1) @@ -190,31 +189,6 @@ get_nprocs_cpu_online (void) return result; } -static int -get_nprocs_cpu (void) -{ - int count = 0; - DIR *dir = __opendir ("/sys/devices/system/cpu"); - if (dir != NULL) - { - struct dirent64 *d; - - while ((d = __readdir64 (dir)) != NULL) - /* NB: the sysfs has d_type support. */ - if (d->d_type == DT_DIR && strncmp (d->d_name, "cpu", 3) == 0) - { - char *endp; - unsigned long int nr = strtoul (d->d_name + 3, &endp, 10); - if (nr != ULONG_MAX && endp != d->d_name + 3 && *endp == '\0') - ++count; - } - - __closedir (dir); - - } - return count; -} - static int get_nprocs_fallback (void) { @@ -239,8 +213,7 @@ get_nprocs_fallback (void) int __get_nprocs (void) { - /* Try /sys/devices/system/cpu/online first. */ - int result = get_nprocs_cpu_online (); + int result = read_sysfs_file ("/sys/devices/system/cpu/online"); if (result != 0) return result; @@ -255,8 +228,7 @@ weak_alias (__get_nprocs, get_nprocs) int __get_nprocs_conf (void) { - /* Try /sys/devices/system/cpu/ first. */ - int result = get_nprocs_cpu (); + int result = read_sysfs_file ("/sys/devices/system/cpu/possible"); if (result != 0) return result; -- 2.32.0