Page MenuHomeFreeBSD
Paste P384

largepage shm API example
ActivePublic

Authored by kib on May 3 2020, 10:04 PM.
Tags
None
Referenced Files
F6454209: raw.txt
May 3 2020, 10:04 PM
Subscribers
None
/* $Id: hugetlb2.c,v 1.4 2020/05/03 16:58:27 kostik Exp kostik $ */
#include <sys/param.h>
#include <sys/fcntl.h>
#include <sys/filio.h>
#include <sys/mman.h>
#include <sys/syscall.h>
#include <err.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
static int pn;
static size_t pagesize[10];
static const u_int cnt = 5;
static void
test_sp(int idx)
{
char *addr;
struct shm_largepage_conf slc;
size_t i, sz;
int error, fd;
printf("Mapping %#zx x %d (idx %d) superpages\n", pagesize[idx],
cnt, idx);
sz = pagesize[idx] * cnt;
fd = syscall(SYS_shm_open2, SHM_ANON, O_CREAT | O_RDWR, 0666,
SHM_LARGEPAGE, (void *)NULL);
if (fd == -1)
err(1, "shm_open2");
memset(&slc, 0, sizeof(slc));
slc.domain = -1;
slc.psind = idx;
slc.alloc_policy = SHM_LARGEPAGE_ALLOC_DEFAULT;
error = ioctl(fd, FIOSHMLPGCNF, &slc);
if (error == -1)
err(1, "FIOSHMLPGCNF");
error = ftruncate(fd, sz);
if (error == -1)
err(1, "ftruncate");
addr = mmap(NULL, sz, PROT_READ | PROT_WRITE,
MAP_LARGEPAGE | MAP_SHARED, fd, 0);
if (addr == MAP_FAILED)
err(1, "mmap MAP_LARGEPAGE");
printf("Mapped at %p\n", addr);
for (i = 0; i < sz; addr++, i++)
*addr = 0x13;
printf("Done\n");
error = munmap(addr, sz);
if (error == -1)
err(1, "munmap");
}
int
main(void)
{
int i;
pn = getpagesizes(pagesize, nitems(pagesize));
printf("Supported page sizes:");
for (i = 0; i < pn; i++)
printf(" %#zx", pagesize[i]);
printf("\n");
for (i = 0; i < pn; i++) {
if (pagesize[i] == (size_t)getpagesize())
continue;
test_sp(i);
}
}