Create huge page shared memory for IPC in linux

Issue

I have the following huge page create source code in linux:

int iTotalByte = sizeof(datafeed)* ARRAYSIZE ;
conf =  (datafeed*) mmap(0, iTotalByte , (PROT_READ | PROT_WRITE), MAP_PRIVATE|MAP_ANONYMOUS|MAP_POPULATE|MAP_HUGETLB , -1  , 0) ;
if(conf  == MAP_FAILED)
{
    printf(" mmap error ....\n")  ;
    exit( 0 ) ;
} 

and it works well, numastat -m will see the huge page table how many MB the application create.

The following is the source I create shared memory used for IPC:

int shm_fd;
if((shm_fd = shm_open(THE_FILE, (O_CREAT | O_EXCL | O_RDWR),
                   (S_IREAD | S_IWRITE))) > 0 ) {
    ; /* We are the first instance */
}
else if((shm_fd = shm_open(THE_FILE, (O_CREAT | O_RDWR),
                    (S_IREAD | S_IWRITE))) < 0)
{
    printf("Could not create shm object. %s\n", strerror(errno));
    exit( 0 ) ;
}
int iTotalByte = sizeof(datafeed)*ARRAYSIZE ;
ftruncate(shm_fd, iTotalByte );
conf =  (datafeed*) mmap(0, iTotalByte , (PROT_READ | PROT_WRITE), MAP_SHARED, shm_fd, 0) ;
if(conf  == MAP_FAILED)
{
    printf(" mmap error ....\n")  ;
    exit( 0 ) ;
}

This source will create a shared memory THE_FILE used for IPC in /dev/shm/, many processes can do IPC through THE_FILE shared memory.

I wonder if there exist a method that I can mmap shared memory to
/dev/shm/ and it is huge page at the same time ?! means I like
this hugepages memory to be used for IPC among processes,
not just used for threads in the same process.

Edit :

https://lwn.net/Articles/375098/

http://lxr.free-electrons.com/source/Documentation/vm/hugetlbpage.txt?v=2.6.32

have the sample code work as expected .

https://lwn.net/Articles/374424/

https://lwn.net/Articles/375096/

https://lwn.net/Articles/376606/

https://lwn.net/Articles/378641/

https://lwn.net/Articles/379748/

also help a lot for understanding huge pages .

Solution

Given you’ve done the right thing setting up pages, page sizes etc.

sysctl vm.nr_hugepages=1024

checking it with:

cat /proc/meminfo | grep Huge
AnonHugePages:         0 kB
HugePages_Total:    1024
HugePages_Free:      986
HugePages_Rsvd:      261
HugePages_Surp:        0
Hugepagesize:       2048 kB

Put something like this in your /etc/fstab:

hugetlbfs   /mnt/hugepages  hugetlbfs   gid=2000,uid=2000   0   0

Then use /mnt/hugepages in place of /dev/shm and you’re doing IPC through shared memory backed by 2M superpages.

Answered By – Hal

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published