public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Adhemerval Zanella <adhemerval.zanella@linaro.org>
To: libc-alpha@sourceware.org
Subject: [PATCH 2/2] linux: Use procutils_read_file on malloc-hugepages
Date: Tue, 12 Sep 2023 10:15:06 -0300	[thread overview]
Message-ID: <20230912131506.2571612-3-adhemerval.zanella@linaro.org> (raw)
In-Reply-To: <20230912131506.2571612-1-adhemerval.zanella@linaro.org>

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

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;
 }
 
-- 
2.34.1


  parent reply	other threads:[~2023-09-12 13:15 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-12 13:15 [PATCH 0/2] Use procutils on getsysstats and malloc-hugepages Adhemerval Zanella
2023-09-12 13:15 ` [PATCH 1/2] linux: Use procutils_read_file on getsysstats Adhemerval Zanella
2023-09-12 13:15 ` Adhemerval Zanella [this message]
2023-09-12 13:35   ` [PATCH 2/2] linux: Use procutils_read_file on malloc-hugepages 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

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

  git send-email \
    --in-reply-to=20230912131506.2571612-3-adhemerval.zanella@linaro.org \
    --to=adhemerval.zanella@linaro.org \
    --cc=libc-alpha@sourceware.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).