diff mbox series

[3/3] malloc: Add arch-specific malloc_verify_thp_pagesize for Linux

Message ID 20210813210429.1147112-4-adhemerval.zanella@linaro.org
State Superseded
Headers show
Series malloc: improve THP effectiveness | expand

Checks

Context Check Description
dj/TryBot-apply_patch success Patch applied to master at the time it was sent
dj/TryBot-32bit success Build for i686

Commit Message

Adhemerval Zanella Aug. 13, 2021, 9:04 p.m. UTC
Not all architectures have Transparent Huge Page (THP) support enabled
by default, so this patch only adds support for the one that have
HAVE_ARCH_TRANSPARENT_HUGEPAGE defined.  Also, Linux THP only support
one Huge Page size, so malloc_verify_thp_pagesize() returns only the
default value for the architecture.

The x86, sparc, and riscv are straightforward since they only support
one possible value.  AArch64 and mips, which supports multiple pages
sizes, can use a direct map to the Large Page support.  PowerPC is
the only architecture where its THP size depends not only on the
configured page size, but also on which MMU is used.  For this case
the sysfs file is used instead.

Checked on x86_64-linux-gnu, aarch64-linux-gnu, and
powerpc64le-linux-gnu.
---
 sysdeps/unix/sysv/linux/aarch64/malloc-thp.h | 40 ++++++++++++++
 sysdeps/unix/sysv/linux/mips/malloc-thp.h    | 39 ++++++++++++++
 sysdeps/unix/sysv/linux/powerpc/malloc-thp.h | 56 ++++++++++++++++++++
 sysdeps/unix/sysv/linux/riscv/malloc-thp.h   | 32 +++++++++++
 sysdeps/unix/sysv/linux/s390/malloc-thp.h    | 33 ++++++++++++
 sysdeps/unix/sysv/linux/sparc/malloc-thp.h   | 36 +++++++++++++
 sysdeps/unix/sysv/linux/x86/malloc-thp.h     | 32 +++++++++++
 7 files changed, 268 insertions(+)
 create mode 100644 sysdeps/unix/sysv/linux/aarch64/malloc-thp.h
 create mode 100644 sysdeps/unix/sysv/linux/mips/malloc-thp.h
 create mode 100644 sysdeps/unix/sysv/linux/powerpc/malloc-thp.h
 create mode 100644 sysdeps/unix/sysv/linux/riscv/malloc-thp.h
 create mode 100644 sysdeps/unix/sysv/linux/s390/malloc-thp.h
 create mode 100644 sysdeps/unix/sysv/linux/sparc/malloc-thp.h
 create mode 100644 sysdeps/unix/sysv/linux/x86/malloc-thp.h
diff mbox series

Patch

diff --git a/sysdeps/unix/sysv/linux/aarch64/malloc-thp.h b/sysdeps/unix/sysv/linux/aarch64/malloc-thp.h
new file mode 100644
index 0000000000..e2e65446f2
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/aarch64/malloc-thp.h
@@ -0,0 +1,40 @@ 
+/* Transparent Huge Page support.  AArch64 implementation.
+   Copyright (C) 2021 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public License as
+   published by the Free Software Foundation; either version 2.1 of the
+   License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; see the file COPYING.LIB.  If
+   not, see <https://www.gnu.org/licenses/>.  */
+
+#ifndef _MALLOC_THP_H
+#define _MALLOC_THP_H
+
+#include <ldsodefs.h>
+#include <stdio.h>
+
+/* Return the prefered large page size for the request PAGESIZE.  The
+   requested value of 1 means the default size for the architecutre.
+   Returning 0 disables Large Parse usage.  */
+static inline size_t
+malloc_verify_thp_pagesize (size_t pagesize)
+{
+  /* AArch64 THP size depends of the default page size:
+      4k ->   2m
+     16k ->  32m
+     64k -> 512m  */
+  int page_shift = __builtin_ctzl (GLRO(dl_pagesize));
+  printf ("%s: page_shift=%d\n", __func__, page_shift);
+  return 1UL << ((page_shift - 3) * 2 + 3);
+}
+
+#endif /* _MALLOC_THP_H */
diff --git a/sysdeps/unix/sysv/linux/mips/malloc-thp.h b/sysdeps/unix/sysv/linux/mips/malloc-thp.h
new file mode 100644
index 0000000000..d8cdbd026d
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/mips/malloc-thp.h
@@ -0,0 +1,39 @@ 
+/* Transparent Huge Page support.  Generic implementation.
+   Copyright (C) 2021 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public License as
+   published by the Free Software Foundation; either version 2.1 of the
+   License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; see the file COPYING.LIB.  If
+   not, see <https://www.gnu.org/licenses/>.  */
+
+#ifndef _MALLOC_THP_H
+#define _MALLOC_THP_H
+
+#include <ldsodefs.h>
+
+/* Return the prefered large page size for the request PAGESIZE.  The
+   requested value of 1 means the default size for the architecture.  */
+static inline size_t
+malloc_verify_thp_pagesize (size_t pagesize)
+{
+  /* MIPS THP size depends of the default page size:
+      4k ->   2m
+      8k ->   8m
+     16k ->  32m
+     32k -> 128m
+     64k -> 512m   */
+  int page_shift = __builtin_ctzl (GLRO(dl_pagesize));
+  return 1UL << (page_shift + (page_shift - 3));
+}
+
+#endif /* _MALLOC_THP_H */
diff --git a/sysdeps/unix/sysv/linux/powerpc/malloc-thp.h b/sysdeps/unix/sysv/linux/powerpc/malloc-thp.h
new file mode 100644
index 0000000000..c3fcfa3386
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/powerpc/malloc-thp.h
@@ -0,0 +1,56 @@ 
+/* Transparent Huge Page support.  PowerPC implementation.
+   Copyright (C) 2021 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public License as
+   published by the Free Software Foundation; either version 2.1 of the
+   License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; see the file COPYING.LIB.  If
+   not, see <https://www.gnu.org/licenses/>.  */
+
+#ifndef _MALLOC_THP_H
+#define _MALLOC_THP_H
+
+#include <ldsodefs.h>
+#include <intprops.h>
+
+/* Return the prefered large page size for the request PAGESIZE.  The
+   requested value of 1 means the default size for the architecture.  */
+static inline size_t
+malloc_verify_thp_pagesize (size_t pagesize)
+{
+  /* PowerPC THP size depends of the default page size and which MMU hardware
+     is used.  So no easy way to statically map it, query the kernel
+     instead.  */
+  int fd = __open64_nocancel (
+    "/sys/kernel/mm/transparent_hugepage/hpage_pmd_size", O_RDONLY);
+  if (fd == -1)
+    return 0;
+
+  size_t hps = 0;
+
+  char str[INT_BUFSIZE_BOUND (size_t)];
+  ssize_t r = __read_nocancel (fd, str, sizeof (str));
+  if (r > 0)
+    for (ssize_t i = 0; i < r; i++)
+      {
+	if (str[i] == '\n')
+	  break;
+	hps *= 10;
+	hps += str[i] - '0';
+      }
+
+  __close_nocancel (fd);
+
+  return hps;
+}
+
+#endif /* _MALLOC_THP_H */
diff --git a/sysdeps/unix/sysv/linux/riscv/malloc-thp.h b/sysdeps/unix/sysv/linux/riscv/malloc-thp.h
new file mode 100644
index 0000000000..aa38ca6dd6
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/riscv/malloc-thp.h
@@ -0,0 +1,32 @@ 
+/* Transparent Huge Page support.  RISCV implementation.
+   Copyright (C) 2021 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public License as
+   published by the Free Software Foundation; either version 2.1 of the
+   License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; see the file COPYING.LIB.  If
+   not, see <https://www.gnu.org/licenses/>.  */
+
+#ifndef _MALLOC_THP_H
+#define _MALLOC_THP_H
+
+#include <ldsodefs.h>
+
+/* Return the prefered large page size for the request PAGESIZE.  The
+   requested value of 1 means the default size for the architecture.  */
+static inline size_t
+malloc_verify_thp_pagesize (size_t pagesize)
+{
+  return 1UL << 21;
+}
+
+#endif /* _MALLOC_THP_H */
diff --git a/sysdeps/unix/sysv/linux/s390/malloc-thp.h b/sysdeps/unix/sysv/linux/s390/malloc-thp.h
new file mode 100644
index 0000000000..be6c26aa3b
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/s390/malloc-thp.h
@@ -0,0 +1,33 @@ 
+/* Transparent Huge Page support.  Generic implementation.
+   Copyright (C) 2021 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public License as
+   published by the Free Software Foundation; either version 2.1 of the
+   License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; see the file COPYING.LIB.  If
+   not, see <https://www.gnu.org/licenses/>.  */
+
+#ifndef _MALLOC_THP_H
+#define _MALLOC_THP_H
+
+#include <ldsodefs.h>
+
+/* Return the prefered large page size for the request PAGESIZE.  The
+   requested value of 1 means the default size for the architecture.  */
+static inline size_t
+malloc_verify_thp_pagesize (size_t pagesize)
+{
+  /* s390 uses 1M for THP.  */
+  return 1UL << 20;
+}
+
+#endif /* _MALLOC_THP_H */
diff --git a/sysdeps/unix/sysv/linux/sparc/malloc-thp.h b/sysdeps/unix/sysv/linux/sparc/malloc-thp.h
new file mode 100644
index 0000000000..83f6fdc114
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/sparc/malloc-thp.h
@@ -0,0 +1,36 @@ 
+/* Transparent Huge Page support.  SPARC implementation.
+   Copyright (C) 2021 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public License as
+   published by the Free Software Foundation; either version 2.1 of the
+   License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; see the file COPYING.LIB.  If
+   not, see <https://www.gnu.org/licenses/>.  */
+
+#ifndef _MALLOC_THP_H
+#define _MALLOC_THP_H
+
+#include <ldsodefs.h>
+
+/* Return the prefered large page size for the request PAGESIZE.  The
+   requested value of 1 means the default size for the architecture.  */
+static inline size_t
+malloc_verify_thp_pagesize (size_t pagesize)
+{
+#ifdef __arch64__
+  return 1UL << 23;
+#else
+  return 1UL << 18;
+#endif
+}
+
+#endif /* _MALLOC_THP_H */
diff --git a/sysdeps/unix/sysv/linux/x86/malloc-thp.h b/sysdeps/unix/sysv/linux/x86/malloc-thp.h
new file mode 100644
index 0000000000..d94cb578c2
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/x86/malloc-thp.h
@@ -0,0 +1,32 @@ 
+/* Transparent Huge Page support.  Generic implementation.
+   Copyright (C) 2021 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public License as
+   published by the Free Software Foundation; either version 2.1 of the
+   License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; see the file COPYING.LIB.  If
+   not, see <https://www.gnu.org/licenses/>.  */
+
+#ifndef _MALLOC_THP_H
+#define _MALLOC_THP_H
+
+#include <ldsodefs.h>
+
+/* Return the prefered large page size for the request PAGESIZE.  The
+   requested value of 1 means the default size for the architecture.  */
+static inline size_t
+malloc_verify_thp_pagesize (size_t pagesize)
+{
+  return 1UL << 21;
+}
+
+#endif /* _MALLOC_THP_H */