0b26f7
commit 822662cf2a4b170ade4c5342f035d68815a03276
0b26f7
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
0b26f7
Date:   Mon Sep 6 14:19:51 2021 -0300
0b26f7
0b26f7
    linux: Revert the use of sched_getaffinity on get_nproc (BZ #28310)
0b26f7
    
0b26f7
    The use of sched_getaffinity on get_nproc and
0b26f7
    sysconf (_SC_NPROCESSORS_ONLN) done in 903bc7dcc2acafc40 (BZ #27645)
0b26f7
    breaks the top command in common hypervisor configurations and also
0b26f7
    other monitoring tools.
0b26f7
    
0b26f7
    The main issue using sched_getaffinity changed the symbols semantic
0b26f7
    from system-wide scope of online CPUs to per-process one (which can
0b26f7
    be changed with kernel cpusets or book parameters in VM).
0b26f7
    
0b26f7
    This patch reverts mostly of the 903bc7dcc2acafc40, with the
0b26f7
    exceptions:
0b26f7
    
0b26f7
      * No more cached values and atomic updates, since they are inherent
0b26f7
        racy.
0b26f7
    
0b26f7
      * No /proc/cpuinfo fallback, since /proc/stat is already used and
0b26f7
        it would require to revert more arch-specific code.
0b26f7
    
0b26f7
      * The alloca is replace with a static buffer of 1024 bytes.
0b26f7
    
0b26f7
    So the implementation first consult the sysfs, and fallbacks to procfs.
0b26f7
    
0b26f7
    Checked on x86_64-linux-gnu.
0b26f7
    
0b26f7
    Reviewed-by: Florian Weimer <fweimer@redhat.com>
0b26f7
    (cherry picked from commit 342298278eabc75baabcaced110a11a02c3d3580)
0b26f7
0b26f7
diff --git a/sysdeps/unix/sysv/linux/getsysstats.c b/sysdeps/unix/sysv/linux/getsysstats.c
0b26f7
index 61d20e7bab8640f2..d70ed9586950615c 100644
0b26f7
--- a/sysdeps/unix/sysv/linux/getsysstats.c
0b26f7
+++ b/sysdeps/unix/sysv/linux/getsysstats.c
0b26f7
@@ -18,6 +18,8 @@
0b26f7
    <https://www.gnu.org/licenses/>.  */
0b26f7
 
0b26f7
 #include <array_length.h>
0b26f7
+#include <assert.h>
0b26f7
+#include <ctype.h>
0b26f7
 #include <dirent.h>
0b26f7
 #include <errno.h>
0b26f7
 #include <ldsodefs.h>
0b26f7
@@ -30,7 +32,7 @@
0b26f7
 #include <sysdep.h>
0b26f7
 
0b26f7
 int
0b26f7
-__get_nprocs (void)
0b26f7
+__get_nprocs_sched (void)
0b26f7
 {
0b26f7
   enum
0b26f7
     {
0b26f7
@@ -53,14 +55,141 @@ __get_nprocs (void)
0b26f7
      atomics are needed). */
0b26f7
   return 2;
0b26f7
 }
0b26f7
-libc_hidden_def (__get_nprocs)
0b26f7
-weak_alias (__get_nprocs, get_nprocs)
0b26f7
+
0b26f7
+static char *
0b26f7
+next_line (int fd, char *const buffer, char **cp, char **re,
0b26f7
+           char *const buffer_end)
0b26f7
+{
0b26f7
+  char *res = *cp;
0b26f7
+  char *nl = memchr (*cp, '\n', *re - *cp);
0b26f7
+  if (nl == NULL)
0b26f7
+    {
0b26f7
+      if (*cp != buffer)
0b26f7
+        {
0b26f7
+          if (*re == buffer_end)
0b26f7
+            {
0b26f7
+              memmove (buffer, *cp, *re - *cp);
0b26f7
+              *re = buffer + (*re - *cp);
0b26f7
+              *cp = buffer;
0b26f7
+
0b26f7
+              ssize_t n = __read_nocancel (fd, *re, buffer_end - *re);
0b26f7
+              if (n < 0)
0b26f7
+                return NULL;
0b26f7
+
0b26f7
+              *re += n;
0b26f7
+
0b26f7
+              nl = memchr (*cp, '\n', *re - *cp);
0b26f7
+              while (nl == NULL && *re == buffer_end)
0b26f7
+                {
0b26f7
+                  /* Truncate too long lines.  */
0b26f7
+                  *re = buffer + 3 * (buffer_end - buffer) / 4;
0b26f7
+                  n = __read_nocancel (fd, *re, buffer_end - *re);
0b26f7
+                  if (n < 0)
0b26f7
+                    return NULL;
0b26f7
+
0b26f7
+                  nl = memchr (*re, '\n', n);
0b26f7
+                  **re = '\n';
0b26f7
+                  *re += n;
0b26f7
+                }
0b26f7
+            }
0b26f7
+          else
0b26f7
+            nl = memchr (*cp, '\n', *re - *cp);
0b26f7
+
0b26f7
+          res = *cp;
0b26f7
+        }
0b26f7
+
0b26f7
+      if (nl == NULL)
0b26f7
+        nl = *re - 1;
0b26f7
+    }
0b26f7
+
0b26f7
+  *cp = nl + 1;
0b26f7
+  assert (*cp <= *re);
0b26f7
+
0b26f7
+  return res == *re ? NULL : res;
0b26f7
+}
0b26f7
+
0b26f7
 
0b26f7
 int
0b26f7
-__get_nprocs_sched (void)
0b26f7
+__get_nprocs (void)
0b26f7
 {
0b26f7
-  return __get_nprocs ();
0b26f7
+  enum { buffer_size = 1024 };
0b26f7
+  char buffer[buffer_size];
0b26f7
+  char *buffer_end = buffer + buffer_size;
0b26f7
+  char *cp = buffer_end;
0b26f7
+  char *re = buffer_end;
0b26f7
+
0b26f7
+  const int flags = O_RDONLY | O_CLOEXEC;
0b26f7
+  /* This file contains comma-separated ranges.  */
0b26f7
+  int fd = __open_nocancel ("/sys/devices/system/cpu/online", flags);
0b26f7
+  char *l;
0b26f7
+  int result = 0;
0b26f7
+  if (fd != -1)
0b26f7
+    {
0b26f7
+      l = next_line (fd, buffer, &cp, &re, buffer_end);
0b26f7
+      if (l != NULL)
0b26f7
+	do
0b26f7
+	  {
0b26f7
+	    char *endp;
0b26f7
+	    unsigned long int n = strtoul (l, &endp, 10);
0b26f7
+	    if (l == endp)
0b26f7
+	      {
0b26f7
+		result = 0;
0b26f7
+		break;
0b26f7
+	      }
0b26f7
+
0b26f7
+	    unsigned long int m = n;
0b26f7
+	    if (*endp == '-')
0b26f7
+	      {
0b26f7
+		l = endp + 1;
0b26f7
+		m = strtoul (l, &endp, 10);
0b26f7
+		if (l == endp)
0b26f7
+		  {
0b26f7
+		    result = 0;
0b26f7
+		    break;
0b26f7
+		  }
0b26f7
+	      }
0b26f7
+
0b26f7
+	    result += m - n + 1;
0b26f7
+
0b26f7
+	    l = endp;
0b26f7
+	    if (l < re && *l == ',')
0b26f7
+	      ++l;
0b26f7
+	  }
0b26f7
+	while (l < re && *l != '\n');
0b26f7
+
0b26f7
+      __close_nocancel_nostatus (fd);
0b26f7
+
0b26f7
+      if (result > 0)
0b26f7
+	return result;
0b26f7
+    }
0b26f7
+
0b26f7
+  cp = buffer_end;
0b26f7
+  re = buffer_end;
0b26f7
+
0b26f7
+  /* Default to an SMP system in case we cannot obtain an accurate
0b26f7
+     number.  */
0b26f7
+  result = 2;
0b26f7
+
0b26f7
+  fd = __open_nocancel ("/proc/stat", flags);
0b26f7
+  if (fd != -1)
0b26f7
+    {
0b26f7
+      result = 0;
0b26f7
+
0b26f7
+      while ((l = next_line (fd, buffer, &cp, &re, buffer_end)) != NULL)
0b26f7
+	/* The current format of /proc/stat has all the cpu* entries
0b26f7
+	   at the front.  We assume here that stays this way.  */
0b26f7
+	if (strncmp (l, "cpu", 3) != 0)
0b26f7
+	  break;
0b26f7
+	else if (isdigit (l[3]))
0b26f7
+	  ++result;
0b26f7
+
0b26f7
+      __close_nocancel_nostatus (fd);
0b26f7
+    }
0b26f7
+
0b26f7
+  return result;
0b26f7
 }
0b26f7
+libc_hidden_def (__get_nprocs)
0b26f7
+weak_alias (__get_nprocs, get_nprocs)
0b26f7
 
0b26f7
 
0b26f7
 /* On some architectures it is possible to distinguish between configured