public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] linux: Use /proc/stat fallback for __get_nprocs_conf (BZ #28624)
@ 2021-11-25 12:27 Adhemerval Zanella
  2021-11-25 13:40 ` Andreas Schwab
  0 siblings, 1 reply; 2+ messages in thread
From: Adhemerval Zanella @ 2021-11-25 12:27 UTC (permalink / raw)
  To: libc-alpha

The /proc/statm fallback was removed by f13fb81ad3159 if sysfs is
not available, reinstate it.

Checked on x86_64-linux-gnu.
---
 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..0165e58b05 100644
--- a/sysdeps/unix/sysv/linux/getsysstats.c
+++ b/sysdeps/unix/sysv/linux/getsysstats.c
@@ -107,6 +107,39 @@ next_line (int fd, char *const buffer, char **cp, char **re,
   return res == *re ? NULL : res;
 }
 
+static int
+get_nproc_stat (void)
+{
+  enum { buffer_size = 1024 };
+  char buffer[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 +195,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 ();
 }
 libc_hidden_def (__get_nprocs)
 weak_alias (__get_nprocs, get_nprocs)
@@ -219,7 +229,7 @@ __get_nprocs_conf (void)
       return count;
     }
 
-  return 1;
+  return get_nproc_stat ();
 }
 libc_hidden_def (__get_nprocs_conf)
 weak_alias (__get_nprocs_conf, get_nprocs_conf)
-- 
2.32.0


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] linux: Use /proc/stat fallback for __get_nprocs_conf (BZ #28624)
  2021-11-25 12:27 [PATCH] linux: Use /proc/stat fallback for __get_nprocs_conf (BZ #28624) Adhemerval Zanella
@ 2021-11-25 13:40 ` Andreas Schwab
  0 siblings, 0 replies; 2+ messages in thread
From: Andreas Schwab @ 2021-11-25 13:40 UTC (permalink / raw)
  To: Adhemerval Zanella via Libc-alpha

On Nov 25 2021, Adhemerval Zanella via Libc-alpha wrote:

> diff --git a/sysdeps/unix/sysv/linux/getsysstats.c b/sysdeps/unix/sysv/linux/getsysstats.c
> index 15ad91cf2f..0165e58b05 100644
> --- a/sysdeps/unix/sysv/linux/getsysstats.c
> +++ b/sysdeps/unix/sysv/linux/getsysstats.c
> @@ -107,6 +107,39 @@ next_line (int fd, char *const buffer, char **cp, char **re,
>    return res == *re ? NULL : res;
>  }
>  
> +static int
> +get_nproc_stat (void)
> +{
> +  enum { buffer_size = 1024 };
> +  char buffer[buffer_size];

Perhaps reuse the array from the caller (for __get_nprocs)?

Andreas.

-- 
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 7578 EB47 D4E5 4D69 2510  2552 DF73 E780 A9DA AEC1
"And now for something completely different."

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-11-25 13:40 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-11-25 12:27 [PATCH] linux: Use /proc/stat fallback for __get_nprocs_conf (BZ #28624) Adhemerval Zanella
2021-11-25 13:40 ` Andreas Schwab

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).