I ran the program with root priviledge but it keeps complaining that mmap cannot allocate memory. Code snippet is below:
#define PROTECTION (PROT_READ | PROT_WRITE)
#define LENGTH (4*1024)
#ifndef MAP_HUGETLB
#define MAP_HUGETLB 0x40000
#endif
#define ADDR (void *) (0x0UL)
#define FLAGS (MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB)
int main (int argc, char *argv[]){
...
// allocate a buffer with the same size as the LLC using huge pages
buf = mmap(ADDR, LENGTH, PROTECTION, FLAGS, 0, 0);
if (buf == MAP_FAILED) {
perror("mmap");
exit(1);
}
...}
Hardware: I have 8G RAM. Processor is ivybridge
Uname output:
Linux mymachine 3.13.0-43-generic #72-Ubuntu SMP Mon Dec 8 19:35:06 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
EDIT 1: The output of perror
mmap: Cannot allocate memory
Also added one line to print errno
printf("something is wrong: %d\n", errno);
But the output is:
something is wrong: 12
EDIT 2: The huge tlb related information from /proc/meminfo
HugePages_Total: 0
HugePages_Free: 0
HugePages_Rsvd: 0
HugePages_Surp: 0
Hugepagesize: 2048 kB
Well, as Documentation/vm/hugetlspage.txt suggested, do
echo 20 > /proc/sys/vm/nr_hugepages
solved the problem. Tested on ubuntu 14.04. Check Why I can't map memory with mmap also.