diff mbox series

[2/2] linux: Use procutils_read_file on malloc-hugepages

Message ID 20230912131506.2571612-3-adhemerval.zanella@linaro.org
State New
Headers show
Series Use procutils on getsysstats and malloc-hugepages | expand

Commit Message

Adhemerval Zanella Sept. 12, 2023, 1:15 p.m. UTC
Checked on x86_64-linux-gnu.
---
 sysdeps/aarch64/Makefile                   |  5 +-
 sysdeps/unix/sysv/linux/Makefile           |  6 ++
 sysdeps/unix/sysv/linux/malloc-hugepages.c | 68 ++++++++++------------
 3 files changed, 40 insertions(+), 39 deletions(-)

Comments

Andreas Schwab Sept. 12, 2023, 1:35 p.m. UTC | #1
On Sep 12 2023, Adhemerval Zanella wrote:

> +  *(size_t*) arg = hpsize;

Spacing.

> +  /* If procfs is not accessible of if huge page field is not present,

s/of/or/
diff mbox series

Patch

diff --git a/sysdeps/aarch64/Makefile b/sysdeps/aarch64/Makefile
index 6a9559e5f5..6c80ee8479 100644
--- a/sysdeps/aarch64/Makefile
+++ b/sysdeps/aarch64/Makefile
@@ -67,5 +67,8 @@  sysdep_routines += __mtag_tag_zero_region \
 endif
 
 ifeq ($(subdir),malloc)
-sysdep_malloc_debug_routines = __mtag_tag_zero_region __mtag_tag_region
+sysdep_malloc_debug_routines += \
+  __mtag_tag_zero_region \
+  __mtag_tag_region \
+  # sysdep_malloc_debug_routines
 endif
diff --git a/sysdeps/unix/sysv/linux/Makefile b/sysdeps/unix/sysv/linux/Makefile
index 063719bae6..d4ab43c8f2 100644
--- a/sysdeps/unix/sysv/linux/Makefile
+++ b/sysdeps/unix/sysv/linux/Makefile
@@ -637,6 +637,12 @@  CFLAGS-mq_send.c += -fexceptions
 CFLAGS-mq_receive.c += -fexceptions
 endif
 
+ifeq ($(subdir),malloc)
+sysdep_malloc_debug_routines += \
+  procutils \
+  # sysdep_malloc_debug_routines
+endif
+
 ifeq ($(subdir),nscd)
 sysdep-CFLAGS += -DHAVE_EPOLL -DHAVE_INOTIFY -DHAVE_NETLINK
 CFLAGS-gai.c += -DNEED_NETLINK
diff --git a/sysdeps/unix/sysv/linux/malloc-hugepages.c b/sysdeps/unix/sysv/linux/malloc-hugepages.c
index 2f316474c1..cec034e07f 100644
--- a/sysdeps/unix/sysv/linux/malloc-hugepages.c
+++ b/sysdeps/unix/sysv/linux/malloc-hugepages.c
@@ -16,10 +16,12 @@ 
    License along with the GNU C Library; see the file COPYING.LIB.  If
    not, see <https://www.gnu.org/licenses/>.  */
 
-#include <intprops.h>
 #include <dirent.h>
+#include <intprops.h>
 #include <malloc-hugepages.h>
+#include <string.h>
 #include <not-cancel.h>
+#include <procutils.h>
 #include <sys/mman.h>
 
 unsigned long int
@@ -78,51 +80,41 @@  __malloc_thp_mode (void)
   return malloc_thp_mode_not_supported;
 }
 
-static size_t
-malloc_default_hugepage_size (void)
+static int
+proc_meminfo_parse (const char *s, void *arg)
 {
-  int fd = __open64_nocancel ("/proc/meminfo", O_RDONLY);
-  if (fd == -1)
+  enum { HUGEPAGESIZELEN = sizeof ("Hugepagesize:") - 1 };
+  if (strncmp (s, "Hugepagesize:", HUGEPAGESIZELEN) != 0)
     return 0;
 
-  size_t hpsize = 0;
+  /* The default huge page size is in the form:
+     Hugepagesize:       NUMBER kB  */
+  s += HUGEPAGESIZELEN;
 
-  char buf[512];
-  off64_t off = 0;
-  while (1)
+  size_t hpsize = 0;
+  for (int i = 0; (s[i] >= '0' && s[i] <= '9') || s[i] == ' '; i++)
     {
-      ssize_t r = __pread64_nocancel (fd, buf, sizeof (buf) - 1, off);
-      if (r < 0)
-	break;
-      buf[r] = '\0';
-
-      /* If the tag is not found, read the last line again.  */
-      const char *s = strstr (buf, "Hugepagesize:");
-      if (s == NULL)
-	{
-	  char *nl = strrchr (buf, '\n');
-	  if (nl == NULL)
-	    break;
-	  off += (nl + 1) - buf;
-	  continue;
-	}
-
-      /* The default huge page size is in the form:
-	 Hugepagesize:       NUMBER kB  */
-      s += sizeof ("Hugepagesize: ") - 1;
-      for (int i = 0; (s[i] >= '0' && s[i] <= '9') || s[i] == ' '; i++)
-	{
-	  if (s[i] == ' ')
-	    continue;
-	  hpsize *= 10;
-	  hpsize += s[i] - '0';
-	}
-      hpsize *= 1024;
-      break;
+      if (s[i] == ' ')
+	continue;
+
+      if (INT_MULTIPLY_WRAPV (hpsize, 10, &hpsize)
+	  || INT_ADD_WRAPV (hpsize, s[i] - '0', &hpsize))
+	return -1;
     }
+  if (INT_MULTIPLY_WRAPV (hpsize, 1024, &hpsize))
+    return -1;
 
-  __close_nocancel (fd);
+  *(size_t*) arg = hpsize;
+  return 1;
+}
 
+static size_t
+malloc_default_hugepage_size (void)
+{
+  size_t hpsize = 0;
+  /* If procfs is not accessible of if huge page field is not present,
+     the 'hpsize' argument is not changed.  */
+  __procutils_read_file ("/proc/meminfo", proc_meminfo_parse, &hpsize);
   return hpsize;
 }