From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-qk1-x730.google.com (mail-qk1-x730.google.com [IPv6:2607:f8b0:4864:20::730]) by sourceware.org (Postfix) with ESMTPS id EF4503861810 for ; Wed, 5 May 2021 16:53:09 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org EF4503861810 Received: by mail-qk1-x730.google.com with SMTP id k127so2170964qkc.6 for ; Wed, 05 May 2021 09:53:09 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:from:to:references:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=iLhfInKpAVg3kmfEFdGODjdF8C7ClR1WbWNitue4pIg=; b=Cvqq+K7wguBUOPfLp9zmBdPevjES89SBBE1l/EiqseIm4X/fudTKTDzg3zjuC/f214 yVS0T+d+OUYHEoqTdrNodBA2zEGVYyOhbNZ3pGZ8oB1kMOr2FjMokNlhocO+ybfZSPuo At2iW0kKsTyEpRYhi+5AxV1NfIyU5qczfCQyEWSb+dE29ctagNCnjpKj+8WYW+bECbdf BrksVFCCqLx+oxL3aXyxvFEds4ZL3702ANfi645YXRpEaP3j5v1wgRTT8HMTGnktnSLQ TfYs5p63qkWpi4RYf/EVsteO1A3HjzWbCUvLnLfYlt47RMpiAVGtRnjKGjGeHZuc5iw/ ofSQ== X-Gm-Message-State: AOAM531gWVttupdhaw0YyBy0sXgSZTNP7O7s88xwk4jUI0uXYGn1z5/A BrRBy8fwDrfvzIMdZ52336GAqc1Cut7VoQ== X-Google-Smtp-Source: ABdhPJyDvjpsX3CDBz6cp0JpTnrvKs2g1tNmyphor6NKIBa92pTJ0BWO3JRV2SXCZqCkKvOHDlX/AA== X-Received: by 2002:a37:40d5:: with SMTP id n204mr30395143qka.79.1620233588085; Wed, 05 May 2021 09:53:08 -0700 (PDT) Received: from [192.168.1.4] ([177.194.37.86]) by smtp.gmail.com with ESMTPSA id m4sm5787394qtg.21.2021.05.05.09.53.07 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 05 May 2021 09:53:07 -0700 (PDT) Subject: Re: [PATCH 3/4] linux: Use /sys/devices/system/cpu/possible on __get_nprocs_conf From: Adhemerval Zanella To: libc-alpha@sourceware.org References: <20210329182520.323665-1-adhemerval.zanella@linaro.org> <20210329182520.323665-3-adhemerval.zanella@linaro.org> Message-ID: <10c93654-d50f-154c-12b9-c475a5be91c8@linaro.org> Date: Wed, 5 May 2021 13:53:06 -0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.8.1 MIME-Version: 1.0 In-Reply-To: <20210329182520.323665-3-adhemerval.zanella@linaro.org> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-12.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, NICE_REPLY_A, 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-List-Received-Date: Wed, 05 May 2021 16:53:11 -0000 Ping. On 29/03/2021 15:25, Adhemerval Zanella wrote: > Instead of iterate over all the cpu in the sysfs folder. It consumes > slight less resources on large system (which might require extra > getdents call) and memory (a limited stack buffer instead a large > malloced one for opendir). > > Checked on x86_64-linux-gnu, aarch64-linux-gnu, and > powerpc64le-linux-gnu. > --- > sysdeps/unix/sysv/linux/getsysstats.c | 51 +++++++++++++++------------ > 1 file changed, 29 insertions(+), 22 deletions(-) > > diff --git a/sysdeps/unix/sysv/linux/getsysstats.c b/sysdeps/unix/sysv/linux/getsysstats.c > index f8a4a31d1b..5069951246 100644 > --- a/sysdeps/unix/sysv/linux/getsysstats.c > +++ b/sysdeps/unix/sysv/linux/getsysstats.c > @@ -17,7 +17,8 @@ > License along with the GNU C Library; if not, see > . */ > > -#include > +#include > +#include > #include > #include > #include > @@ -63,33 +64,39 @@ weak_alias (__get_nprocs, get_nprocs) > int > __get_nprocs_conf (void) > { > - /* XXX Here will come a test for the new system call. */ > + int result = 1; > > /* Try to use the sysfs filesystem. It has actual information about > online processors. */ > - DIR *dir = __opendir ("/sys/devices/system/cpu"); > - if (dir != NULL) > + int fd = __open64_nocancel ("/sys/devices/system/cpu/possible", O_RDONLY); > + if (fd != -1) > { > - int count = 0; > - 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; > + /* The entry is in the form of '[cpuX]-[cpuY]'. */ > + char buf[2 * INT_STRLEN_BOUND (unsigned int) + 1]; > + > + ssize_t n = __read_nocancel (fd, buf, sizeof (buf)); > + if (n > 0) > + { > + buf[n] = '\0'; > + > + /* Start on the right, to find highest node number. */ > + int m = 1; > + while (--n) > + { > + if ((buf[n] == ',') || (buf[n] == '-')) > + break; > + /* Ignore '\n' */ > + if (! isdigit (buf[n])) > + continue; > + result += (buf[n] - '0') * m; > + m *= 10; > + } > + } > + > + __close_nocancel (fd); > + return result + 1; > } > > - int result = 1; > - > #ifdef GET_NPROCS_CONF_PARSER > /* If we haven't found an appropriate entry return 1. */ > FILE *fp = fopen ("/proc/cpuinfo", "rce"); >