Created
May 3, 2020 21:58
-
-
Save kostikbel/4e7caeee46b5d9b95d1ff1f791368732 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* $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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment