From patchwork Mon Mar 29 18:25:18 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 42800 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 74407385040B; Mon, 29 Mar 2021 18:25:31 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 74407385040B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1617042331; bh=RfADeB6Pm4BBCS55x4VMQfvm5sZMCATagT55FgPDJhk=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=rmwp1h2/577E9h4amiKQsv71BrJBpuYYikMgrmd85mcUaezEZWnDpZt5Q0W2/MvmD NDB0Zf3VLFX/WdDRZkMhToxuHGzANuVauvJWABR+ZxW8T9FH/VSOmsuHexq6S8OSsX jfoR1Yh/E/Ym3N4QiprF5n/hOPeCHjtoZsP1oaVk= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-qt1-x836.google.com (mail-qt1-x836.google.com [IPv6:2607:f8b0:4864:20::836]) by sourceware.org (Postfix) with ESMTPS id CA8C43860C34 for ; Mon, 29 Mar 2021 18:25:27 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org CA8C43860C34 Received: by mail-qt1-x836.google.com with SMTP id l6so1805013qtq.2 for ; Mon, 29 Mar 2021 11:25:27 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=RfADeB6Pm4BBCS55x4VMQfvm5sZMCATagT55FgPDJhk=; b=AKDQE4e0aW7TaX2SpQjREdPsRaE9wDQ1CBn0z1YiyYdLq0gN8XzfBLIOJfsIcvnmgk jMp3EfktDxed0dRHUpluQXCvjgfCkKzsO8C2Pt9x1DB+uMKuNV8+LeKa4nsfqW4lY6KG v9BZTsRrYnN0ivv8HCwdnHLEK/y5GzKe2MMqqeaRSK5IIZi7pfB74lRdibJTPtW7drAg ZTV7hadn0coqDpKXUVa49f9LZDp3VCCQ3nGatUGHKKU6sIn2bvHgiC4c/X9R8em3zRiK g8JOKdA7gd9xRAYzuBOdDmyLjykbcJEPtQ60phfLfg2ybxMg7A2a3IeQJyD1uachMw5G KCoA== X-Gm-Message-State: AOAM532Gg4olRdLc8TsbWuttrI6xak+wRcPAoTVtvUpKLAuTPBL0yEip ENaYKoU20W8bVe0uodCEnKvahrqg5zwst3f1 X-Google-Smtp-Source: ABdhPJw7SfB6lnTio1jKTAd0l6Hnc5l1mGVek81uG1F2/Up0de/moxCkBwdi3kWfqvX3U4vDJ4Yk+w== X-Received: by 2002:a05:622a:100c:: with SMTP id d12mr23525344qte.378.1617042326820; Mon, 29 Mar 2021 11:25:26 -0700 (PDT) Received: from localhost.localdomain ([177.194.41.149]) by smtp.googlemail.com with ESMTPSA id o7sm14009565qkb.104.2021.03.29.11.25.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 29 Mar 2021 11:25:26 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH 2/4] linux: Use sched_getaffinity for __get_nprocs (BZ #27645) Date: Mon, 29 Mar 2021 15:25:18 -0300 Message-Id: <20210329182520.323665-2-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20210329182520.323665-1-adhemerval.zanella@linaro.org> References: <20210329182520.323665-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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-Patchwork-Original-From: Adhemerval Zanella via Libc-alpha From: Adhemerval Zanella Reply-To: Adhemerval Zanella Cc: crrodriguez@opensuse.org Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" Both the sysfs and procfs parsing (through GET_NPROCS_PARSER) are removed in favor the syscall. The initial scratch buffer should fit to most of the common usage (1024 bytes with maps to 8192 CPUs). Checked on x86_64-linux-gnu and aarch64-linux-gnu. --- include/bits/cpu-set.h | 8 + posix/sched_cpucount.c | 1 + sysdeps/unix/sysv/linux/alpha/getsysstats.c | 19 -- sysdeps/unix/sysv/linux/getsysstats.c | 217 ++---------------- sysdeps/unix/sysv/linux/m68k/getsysstats.c | 37 --- .../unix/sysv/linux/microblaze/getsysstats.c | 34 --- sysdeps/unix/sysv/linux/mips/getsysstats.c | 36 --- sysdeps/unix/sysv/linux/sparc/getsysstats.c | 17 -- 8 files changed, 31 insertions(+), 338 deletions(-) delete mode 100644 sysdeps/unix/sysv/linux/m68k/getsysstats.c delete mode 100644 sysdeps/unix/sysv/linux/microblaze/getsysstats.c delete mode 100644 sysdeps/unix/sysv/linux/mips/getsysstats.c diff --git a/include/bits/cpu-set.h b/include/bits/cpu-set.h index 388f03cfbd..05aa0a8cf9 100644 --- a/include/bits/cpu-set.h +++ b/include/bits/cpu-set.h @@ -1 +1,9 @@ +#ifndef _BITS_CPU_SET_H #include + +#ifndef _ISOMAC +int __sched_cpucount (size_t __setsize, const cpu_set_t *__setp); +libc_hidden_proto (__sched_cpucount) +#endif + +#endif /* _BITS_CPU_SET_H */ diff --git a/posix/sched_cpucount.c b/posix/sched_cpucount.c index 529286e777..8d853dcc2f 100644 --- a/posix/sched_cpucount.c +++ b/posix/sched_cpucount.c @@ -30,3 +30,4 @@ __sched_cpucount (size_t setsize, const cpu_set_t *setp) } return s; } +libc_hidden_def (__sched_cpucount) diff --git a/sysdeps/unix/sysv/linux/alpha/getsysstats.c b/sysdeps/unix/sysv/linux/alpha/getsysstats.c index bdf794703a..098575faef 100644 --- a/sysdeps/unix/sysv/linux/alpha/getsysstats.c +++ b/sysdeps/unix/sysv/linux/alpha/getsysstats.c @@ -18,25 +18,6 @@ . */ -/* We need to define a special parser for /proc/cpuinfo. */ -#define GET_NPROCS_PARSER(FD, BUFFER, CP, RE, BUFFER_END, RESULT) \ - do \ - { \ - /* Find the line that contains the information about the number of \ - active cpus. We don't have to fear extremely long lines since \ - the kernel will not generate them. 8192 bytes are really enough. \ - If there is no "CPUs ..." line then we are on a UP system. */ \ - char *l; \ - (RESULT) = 1; \ - while ((l = next_line (FD, BUFFER, &CP, &RE, BUFFER_END)) != NULL) \ - if ((sscanf (BUFFER, "cpus active : %d", &(RESULT)) == 1) \ - || (sscanf (BUFFER, "CPUs probed %*d active %d", \ - &(RESULT)) == 1)) \ - break; \ - } \ - while (0) - - /* On the Alpha we can distinguish between the number of configured and active cpus. */ #define GET_NPROCS_CONF_PARSER(FP, BUFFER, RESULT) \ diff --git a/sysdeps/unix/sysv/linux/getsysstats.c b/sysdeps/unix/sysv/linux/getsysstats.c index 2c7f40998a..f8a4a31d1b 100644 --- a/sysdeps/unix/sysv/linux/getsysstats.c +++ b/sysdeps/unix/sysv/linux/getsysstats.c @@ -17,215 +17,42 @@ License along with the GNU C Library; if not, see . */ -#include -#include -#include #include -#include -#include -#include -#include +#include +#include #include #include -#include -#include -#include #include - -#include -#include - - -/* How we can determine the number of available processors depends on - the configuration. There is currently (as of version 2.0.21) no - system call to determine the number. It is planned for the 2.1.x - series to add this, though. - - One possibility to implement it for systems using Linux 2.0 is to - examine the pseudo file /proc/cpuinfo. Here we have one entry for - each processor. - - But not all systems have support for the /proc filesystem. If it - is not available we simply return 1 since there is no way. */ - - -/* Other architectures use different formats for /proc/cpuinfo. This - provides a hook for alternative parsers. */ -#ifndef GET_NPROCS_PARSER -# define GET_NPROCS_PARSER(FD, BUFFER, CP, RE, BUFFER_END, RESULT) \ - do \ - { \ - (RESULT) = 0; \ - /* Read all lines and count the lines starting with the string \ - "processor". We don't have to fear extremely long lines since \ - the kernel will not generate them. 8192 bytes are really \ - enough. */ \ - char *l; \ - while ((l = next_line (FD, BUFFER, &CP, &RE, BUFFER_END)) != NULL) \ - if (strncmp (l, "processor", 9) == 0) \ - ++(RESULT); \ - } \ - while (0) -#endif - - -static char * -next_line (int fd, char *const buffer, char **cp, char **re, - char *const buffer_end) -{ - char *res = *cp; - char *nl = memchr (*cp, '\n', *re - *cp); - if (nl == NULL) - { - if (*cp != buffer) - { - if (*re == buffer_end) - { - memmove (buffer, *cp, *re - *cp); - *re = buffer + (*re - *cp); - *cp = buffer; - - ssize_t n = __read_nocancel (fd, *re, buffer_end - *re); - if (n < 0) - return NULL; - - *re += n; - - nl = memchr (*cp, '\n', *re - *cp); - while (nl == NULL && *re == buffer_end) - { - /* Truncate too long lines. */ - *re = buffer + 3 * (buffer_end - buffer) / 4; - n = __read_nocancel (fd, *re, buffer_end - *re); - if (n < 0) - return NULL; - - nl = memchr (*re, '\n', n); - **re = '\n'; - *re += n; - } - } - else - nl = memchr (*cp, '\n', *re - *cp); - - res = *cp; - } - - if (nl == NULL) - nl = *re - 1; - } - - *cp = nl + 1; - assert (*cp <= *re); - - return res == *re ? NULL : res; -} - +#include int __get_nprocs (void) { - static int cached_result = -1; - static time_t timestamp; - - time_t now = time_now (); - time_t prev = timestamp; - atomic_read_barrier (); - if (now == prev && cached_result > -1) - return cached_result; - - /* XXX Here will come a test for the new system call. */ - - const size_t buffer_size = __libc_use_alloca (8192) ? 8192 : 512; - char *buffer = alloca (buffer_size); - char *buffer_end = buffer + buffer_size; - char *cp = buffer_end; - char *re = buffer_end; + struct scratch_buffer set; + scratch_buffer_init (&set); - const int flags = O_RDONLY | O_CLOEXEC; - /* This file contains comma-separated ranges. */ - int fd = __open_nocancel ("/sys/devices/system/cpu/online", flags); - char *l; - int result = 0; - if (fd != -1) + int r; + while (true) { - l = next_line (fd, buffer, &cp, &re, buffer_end); - if (l != NULL) - do - { - char *endp; - unsigned long int n = strtoul (l, &endp, 10); - if (l == endp) - { - result = 0; - break; - } - - unsigned long int m = n; - if (*endp == '-') - { - l = endp + 1; - m = strtoul (l, &endp, 10); - if (l == endp) - { - result = 0; - break; - } - } - - result += m - n + 1; - - l = endp; - if (l < re && *l == ',') - ++l; - } - while (l < re && *l != '\n'); - - __close_nocancel_nostatus (fd); - - if (result > 0) - goto out; + /* The possible error are EFAULT for an invalid buffer or ESRCH for + invalid pid, none could happen. */ + r = INTERNAL_SYSCALL_CALL (sched_getaffinity, 0, set.length, + set.data); + if (r > 0) + break; + + if (!scratch_buffer_grow (&set)) + /* Default to an SMP system in case we cannot obtain an accurate + number. */ + return 2; } - cp = buffer_end; - re = buffer_end; - - /* Default to an SMP system in case we cannot obtain an accurate - number. */ - result = 2; + /* The scratch_buffer is aligned to max_align_t. */ + r = __sched_cpucount (r, (const cpu_set_t *) set.data); - /* The /proc/stat format is more uniform, use it by default. */ - fd = __open_nocancel ("/proc/stat", flags); - if (fd != -1) - { - result = 0; - - while ((l = next_line (fd, buffer, &cp, &re, buffer_end)) != NULL) - /* The current format of /proc/stat has all the cpu* entries - at the front. We assume here that stays this way. */ - if (strncmp (l, "cpu", 3) != 0) - break; - else if (isdigit (l[3])) - ++result; + scratch_buffer_free (&set); - __close_nocancel_nostatus (fd); - } - else - { - fd = __open_nocancel ("/proc/cpuinfo", flags); - if (fd != -1) - { - GET_NPROCS_PARSER (fd, buffer, cp, re, buffer_end, result); - __close_nocancel_nostatus (fd); - } - } - - out: - cached_result = result; - atomic_write_barrier (); - timestamp = now; - - return result; + return r; } libc_hidden_def (__get_nprocs) weak_alias (__get_nprocs, get_nprocs) diff --git a/sysdeps/unix/sysv/linux/m68k/getsysstats.c b/sysdeps/unix/sysv/linux/m68k/getsysstats.c deleted file mode 100644 index 6915f6d721..0000000000 --- a/sysdeps/unix/sysv/linux/m68k/getsysstats.c +++ /dev/null @@ -1,37 +0,0 @@ -/* Determine various system internal values, Linux/m68k version. - Copyright (C) 2003-2021 Free Software Foundation, Inc. - This file is part of the GNU C Library. - Contributed by Andreas Schwab - - The GNU C Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - The GNU C Library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with the GNU C Library. If not, see - . */ - - -/* We need to define a special parser for /proc/cpuinfo. */ -#define GET_NPROCS_PARSER(FD, BUFFER, CP, RE, BUFFER_END, RESULT) \ - do \ - { \ - (RESULT) = 0; \ - /* Read all lines and count the lines starting with the string \ - "CPU:". We don't have to fear extremely long lines since \ - the kernel will not generate them. 8192 bytes are really \ - enough. */ \ - char *l; \ - while ((l = next_line (FD, BUFFER, &CP, &RE, BUFFER_END)) != NULL) \ - if (strncmp (l, "CPU:", 4) == 0) \ - ++(RESULT); \ - } \ - while (0) - -#include diff --git a/sysdeps/unix/sysv/linux/microblaze/getsysstats.c b/sysdeps/unix/sysv/linux/microblaze/getsysstats.c deleted file mode 100644 index 46403e74a8..0000000000 --- a/sysdeps/unix/sysv/linux/microblaze/getsysstats.c +++ /dev/null @@ -1,34 +0,0 @@ -/* Copyright (C) 1997-2021 Free Software Foundation, Inc. - - This file is part of the GNU C Library. - - The GNU C Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of the - License, or (at your option) any later version. - - The GNU C Library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with the GNU C Library; if not, see - . */ - -/* We need to define a special parser for /proc/cpuinfo. */ -#define GET_NPROCS_PARSER(FD, BUFFER, CP, RE, BUFFER_END, RESULT) \ - do \ - { \ - (RESULT) = 0; \ - /* Read all lines and count the lines starting with the string \ - "CPU-Family:". We don't have to fear extremely long lines since \ - the kernel will not generate them. 8192 bytes are really enough. */ \ - char *l; \ - while ((l = next_line (FD, BUFFER, &CP, &RE, BUFFER_END)) != NULL) \ - if (strncmp (l, "CPU-Family:", 11) == 0) \ - ++(RESULT); \ - } \ - while (0) - -#include diff --git a/sysdeps/unix/sysv/linux/mips/getsysstats.c b/sysdeps/unix/sysv/linux/mips/getsysstats.c deleted file mode 100644 index 34f352e963..0000000000 --- a/sysdeps/unix/sysv/linux/mips/getsysstats.c +++ /dev/null @@ -1,36 +0,0 @@ -/* Determine various system internal values, Linux/MIPS version. - Copyright (C) 2001-2021 Free Software Foundation, Inc. - This file is part of the GNU C Library. - - The GNU C Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - The GNU C Library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with the GNU C Library. If not, see - . */ - - -/* We need to define a special parser for /proc/cpuinfo. */ -#define GET_NPROCS_PARSER(FD, BUFFER, CP, RE, BUFFER_END, RESULT) \ - do \ - { \ - (RESULT) = 0; \ - /* Read all lines and count the lines starting with the string \ - "cpu model". We don't have to fear extremely long lines since \ - the kernel will not generate them. 8192 bytes are really \ - enough. */ \ - char *l; \ - while ((l = next_line (FD, BUFFER, &CP, &RE, BUFFER_END)) != NULL) \ - if (strncmp (l, "cpu model", 9) == 0) \ - ++(RESULT); \ - } \ - while (0) - -#include diff --git a/sysdeps/unix/sysv/linux/sparc/getsysstats.c b/sysdeps/unix/sysv/linux/sparc/getsysstats.c index 8e1760802f..a8adba8321 100644 --- a/sysdeps/unix/sysv/linux/sparc/getsysstats.c +++ b/sysdeps/unix/sysv/linux/sparc/getsysstats.c @@ -19,23 +19,6 @@ . */ -/* We need to define a special parser for /proc/cpuinfo. */ -#define GET_NPROCS_PARSER(FD, BUFFER, CP, RE, BUFFER_END, RESULT) \ - do \ - { \ - (RESULT) = 0; \ - /* Find the line that contains the information about the number of \ - active cpus. We don't have to fear extremely long lines since \ - the kernel will not generate them. 8192 bytes are really \ - enough. */ \ - char *l; \ - while ((l = next_line (FD, BUFFER, &CP, &RE, BUFFER_END)) != NULL) \ - if (sscanf (l, "ncpus active : %d", &(RESULT)) == 1) \ - break; \ - } \ - while (0) - - /* On the Sparc we can distinguish between the number of configured and active cpus. */ #define GET_NPROCS_CONF_PARSER(FP, BUFFER, RESULT) \