unofficial mirror of libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Adhemerval Zanella via Libc-alpha <libc-alpha@sourceware.org>
To: libc-alpha@sourceware.org, Andreas Schwab <schwab@linux-m68k.org>
Subject: [PATCH v2] linux: Use /proc/stat fallback for __get_nprocs_conf (BZ #28624)
Date: Thu, 25 Nov 2021 11:02:30 -0300	[thread overview]
Message-ID: <20211125140230.669269-1-adhemerval.zanella@linaro.org> (raw)

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


             reply	other threads:[~2021-11-25 14:02 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-11-25 14:02 Adhemerval Zanella via Libc-alpha [this message]
2021-11-25 14:24 ` [PATCH v2] linux: Use /proc/stat fallback for __get_nprocs_conf (BZ #28624) Andreas Schwab

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/libc/involved.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20211125140230.669269-1-adhemerval.zanella@linaro.org \
    --to=libc-alpha@sourceware.org \
    --cc=adhemerval.zanella@linaro.org \
    --cc=schwab@linux-m68k.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).