1
0
mirror of https://sourceware.org/git/glibc.git synced 2025-12-24 17:51:17 +03:00

malloc: Add Huge Page support for mmap

With the morecore hook removed, there is not easy way to provide huge
pages support on with glibc allocator without resorting to transparent
huge pages.  And some users and programs do prefer to use the huge pages
directly instead of THP for multiple reasons: no splitting, re-merging
by the VM, no TLB shootdowns for running processes, fast allocation
from the reserve pool, no competition with the rest of the processes
unlike THP, no swapping all, etc.

This patch extends the 'glibc.malloc.hugetlb' tunable: the value
'2' means to use huge pages directly with the system default size,
while a positive value means and specific page size that is matched
against the supported ones by the system.

Currently only memory allocated on sysmalloc() is handled, the arenas
still uses the default system page size.

To test is a new rule is added tests-malloc-hugetlb2, which run the
addes tests with the required GLIBC_TUNABLE setting.  On systems without
a reserved huge pages pool, is just stress the mmap(MAP_HUGETLB)
allocation failure.  To improve test coverage it is required to create
a pool with some allocated pages.

Checked on x86_64-linux-gnu.

Reviewed-by: DJ Delorie <dj@redhat.com>
This commit is contained in:
Adhemerval Zanella
2021-08-16 15:08:27 -03:00
parent 6cc3ccc67e
commit 98d5fcb8d0
11 changed files with 207 additions and 15 deletions

View File

@@ -17,8 +17,10 @@
not, see <https://www.gnu.org/licenses/>. */
#include <intprops.h>
#include <dirent.h>
#include <malloc-hugepages.h>
#include <not-cancel.h>
#include <sys/mman.h>
unsigned long int
__malloc_default_thp_pagesize (void)
@@ -72,3 +74,128 @@ __malloc_thp_mode (void)
}
return malloc_thp_mode_not_supported;
}
static size_t
malloc_default_hugepage_size (void)
{
int fd = __open64_nocancel ("/proc/meminfo", O_RDONLY);
if (fd == -1)
return 0;
size_t hpsize = 0;
char buf[512];
off64_t off = 0;
while (1)
{
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;
}
__close_nocancel (fd);
return hpsize;
}
static inline int
hugepage_flags (size_t pagesize)
{
return MAP_HUGETLB | (__builtin_ctzll (pagesize) << MAP_HUGE_SHIFT);
}
void
__malloc_hugepage_config (size_t requested, size_t *pagesize, int *flags)
{
*pagesize = 0;
*flags = 0;
if (requested == 0)
{
*pagesize = malloc_default_hugepage_size ();
if (*pagesize != 0)
*flags = hugepage_flags (*pagesize);
return;
}
/* Each entry represents a supported huge page in the form of:
hugepages-<size>kB. */
int dirfd = __open64_nocancel ("/sys/kernel/mm/hugepages",
O_RDONLY | O_DIRECTORY, 0);
if (dirfd == -1)
return;
char buffer[1024];
while (true)
{
#if !IS_IN(libc)
# define __getdents64 getdents64
#endif
ssize_t ret = __getdents64 (dirfd, buffer, sizeof (buffer));
if (ret == -1)
break;
else if (ret == 0)
break;
bool found = false;
char *begin = buffer, *end = buffer + ret;
while (begin != end)
{
unsigned short int d_reclen;
memcpy (&d_reclen, begin + offsetof (struct dirent64, d_reclen),
sizeof (d_reclen));
const char *dname = begin + offsetof (struct dirent64, d_name);
begin += d_reclen;
if (dname[0] == '.'
|| strncmp (dname, "hugepages-", sizeof ("hugepages-") - 1) != 0)
continue;
size_t hpsize = 0;
const char *sizestr = dname + sizeof ("hugepages-") - 1;
for (int i = 0; sizestr[i] >= '0' && sizestr[i] <= '9'; i++)
{
hpsize *= 10;
hpsize += sizestr[i] - '0';
}
hpsize *= 1024;
if (hpsize == requested)
{
*pagesize = hpsize;
*flags = hugepage_flags (*pagesize);
found = true;
break;
}
}
if (found)
break;
}
__close_nocancel (dirfd);
}