From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS17314 8.43.84.0/22 X-Spam-Status: No, score=-4.7 required=3.0 tests=AWL,BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,SPF_HELO_PASS,SPF_PASS shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by dcvr.yhbt.net (Postfix) with ESMTPS id 9C4BF1F953 for ; Thu, 25 Nov 2021 14:02:58 +0000 (UTC) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A7315385841A for ; Thu, 25 Nov 2021 14:02:57 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A7315385841A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1637848977; bh=Ze86dxOVHiR3tNDIW8t3COw7gtbkLrRng89sFCVQ7F4=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=rIUSM1kIFnKqkv5+M8/Jv0mq8504pvbDoQXeES+IS35IaybXpknCDDyvHSoM5YlpX 5/nTI/0bDn47hrMqlyZVafV3eapd6+TDGIrXuO0rXAN/oeegPhYfMe3hOpvLD10sF2 zdDuyS8B4p7pVITRGlIYPqVCHM3bVjGF4pTNa49A= Received: from mail-ua1-x932.google.com (mail-ua1-x932.google.com [IPv6:2607:f8b0:4864:20::932]) by sourceware.org (Postfix) with ESMTPS id 7B0843858D35 for ; Thu, 25 Nov 2021 14:02:36 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 7B0843858D35 Received: by mail-ua1-x932.google.com with SMTP id y5so12489972ual.7 for ; Thu, 25 Nov 2021 06:02:36 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=Ze86dxOVHiR3tNDIW8t3COw7gtbkLrRng89sFCVQ7F4=; b=6gwjJuEjEi6SmcE2VD+j/BAilibeArf7tW5n6/LNUzs/ZPJgjBEyh8/RnzMWnOGLrU WOj6P+KASXd+IUbHrQjAqWwYgEywcEiBAsZYI4aYXzoipL8l1HK1OZJ1KJ38VwHMEkzA pY0m/E8mDbXJu5NfcxFEakWQqUjHEq/BGlQWHJlE8EvNiUPosdFRlnTRC7H86Zxm0u/y CK1LM5KWWLu0jea3JIYeQUzzjZJwkW/hz5Bc/7zE41ZyoPTWdHo5YJGhcEgP6AdQWzhs iN/egA+n79jhHAVMcQ37VNnedaIxQYfnVpcJxjpIC1IJlqnkcPOl955FKndXA6Lcijaa qojA== X-Gm-Message-State: AOAM530OU4xsPmFOLGiWwEPf7OIXODSDMwJbs3ZH5EBPNIPozinwKHUm Xpv6+WsL7ss2uTAdNQgIDh2EhFzUab9vGw== X-Google-Smtp-Source: ABdhPJz3TOJeACKWVeSjjFtaeKVx2zG1x2TB7dhDPMcq+8YaTZ1cJJPfZoiWXKlmBjmsDXmImAk1Qg== X-Received: by 2002:a67:7105:: with SMTP id m5mr8779065vsc.81.1637848953862; Thu, 25 Nov 2021 06:02:33 -0800 (PST) Received: from birita.. ([2804:431:c7cb:e054:2b7b:edb0:2264:6dae]) by smtp.gmail.com with ESMTPSA id l7sm1753553vsi.20.2021.11.25.06.02.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Nov 2021 06:02:33 -0800 (PST) To: libc-alpha@sourceware.org, Andreas Schwab Subject: [PATCH v2] linux: Use /proc/stat fallback for __get_nprocs_conf (BZ #28624) Date: Thu, 25 Nov 2021 11:02:30 -0300 Message-Id: <20211125140230.669269-1-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.32.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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: , From: Adhemerval Zanella via Libc-alpha Reply-To: Adhemerval Zanella Errors-To: libc-alpha-bounces+e=80x24.org@sourceware.org Sender: "Libc-alpha" The /proc/statm fallback was removed by f13fb81ad3159 if sysfs is not available, reinstate it. Checked on x86_64-linux-gnu. --- Changes from v1: * Reuse the array from the caller. --- sysdeps/unix/sysv/linux/getsysstats.c | 60 ++++++++++++++++----------- 1 file changed, 35 insertions(+), 25 deletions(-) diff --git a/sysdeps/unix/sysv/linux/getsysstats.c b/sysdeps/unix/sysv/linux/getsysstats.c index 15ad91cf2f..d376f05d08 100644 --- a/sysdeps/unix/sysv/linux/getsysstats.c +++ b/sysdeps/unix/sysv/linux/getsysstats.c @@ -107,6 +107,37 @@ next_line (int fd, char *const buffer, char **cp, char **re, return res == *re ? NULL : res; } +static int +get_nproc_stat (char *buffer, size_t buffer_size) +{ + char *buffer_end = buffer + buffer_size; + char *cp = buffer_end; + char *re = buffer_end; + + /* Default to an SMP system in case we cannot obtain an accurate + number. */ + int result = 2; + + const int flags = O_RDONLY | O_CLOEXEC; + int fd = __open_nocancel ("/proc/stat", flags); + if (fd != -1) + { + result = 0; + + char *l; + 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; + + __close_nocancel_nostatus (fd); + } + + return result; +} int __get_nprocs (void) @@ -162,30 +193,7 @@ __get_nprocs (void) return result; } - cp = buffer_end; - re = buffer_end; - - /* Default to an SMP system in case we cannot obtain an accurate - number. */ - result = 2; - - 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; - - __close_nocancel_nostatus (fd); - } - - return result; + return get_nproc_stat (buffer, buffer_size); } libc_hidden_def (__get_nprocs) weak_alias (__get_nprocs, get_nprocs) @@ -219,7 +227,9 @@ __get_nprocs_conf (void) return count; } - return 1; + enum { buffer_size = 1024 }; + char buffer[buffer_size]; + return get_nproc_stat (buffer, buffer_size); } libc_hidden_def (__get_nprocs_conf) weak_alias (__get_nprocs_conf, get_nprocs_conf) -- 2.32.0