What I did:
Enable huge page with root (my system supports 1MB huge page)
$ echo 20 > /proc/sys/vm/nr_hugepages
Mount huge page filesystem to /mnt/hugepages
$ mount -t hugetlbfs nodev /mnt/hugepages
Create a file in huge page filesystem
$ touch /mnt/hugepages/hello
Then map a huge page using
mmap
to address 0 as shown in the code below#define FILE_NAME "/mnt/hugepages/hello" #define PROTECTION (PROT_READ | PROT_WRITE) // page flag #define LENGTH (1024*1024*1024) // huge page size #define FLAGS (MAP_SHARED) //page flag #define ADDR (void *) (0x0UL) //starting address of the page fd = open(FILE_NAME, O_CREAT | O_RDWR, 0755); if (fd < 0) { // perror("Open failed"); exit(1); } // allocate a buffer using huge pages buf = mmap(ADDR, LENGTH, PROTECTION, FLAGS, fd, 0); if (buf == MAP_FAILED) { perror("mmap"); unlink(FILE_NAME); exit(1); }
The program outputs:
mmap: Cannot allocate memory