Hugetlbfs and Shared Memory


 
Thread Tools Search this Thread
Operating Systems Linux Hugetlbfs and Shared Memory
# 1  
Old 05-25-2010
Hugetlbfs and Shared Memory

I am trying to add support for hugepages into an existing application. I have done a lot of online research, but I still have a few questions that I haven't been able get answers to. Hopefully someone here can help!

(1) My application requires that all mapped memory segments be shared. The existing application, which does not use hugepages, uses shm_open() and mmap() to map the shared memory. Since shm_open() automatically maps into the /dev/shm filepath and hugepages must be mapped into the mount filepath (/mnt on my system), it seems that I cannot use shm_open() for hugepages. Is this correct?

(2) If I cannot use shm_open() for hugepages, is there any way to use open() and mmap() and still get shared memory? Are objects mapped to the hugepage mount filepath shared or can they be?

(3) If I use open() instead of shm_open() but use the shared memory filepath (/dev/shm) does that make it shared?
# 2  
Old 05-27-2010
Quote:
Originally Posted by ms_farenheit
I am trying to add support for hugepages into an existing application. I have done a lot of online research, but I still have a few questions that I haven't been able get answers to. Hopefully someone here can help!

(1) My application requires that all mapped memory segments be shared. The existing application, which does not use hugepages, uses shm_open() and mmap() to map the shared memory. Since shm_open() automatically maps into the /dev/shm filepath and hugepages must be mapped into the mount filepath (/mnt on my system), it seems that I cannot use shm_open() for hugepages. Is this correct?
That is correct. Since the use of hugepages can in some cases change memory semantics drastically(getpagesize() becomes no longer accurate for all memory!), general-purpose system calls won't create them by default. There's some modified, experimental systems out there that create hugepages by default in some places, google published a paper on it but it currently eludes me; but you'd have to know exactly what you were doing with your system to avoid potential problems.
Quote:
(2) If I cannot use shm_open() for hugepages, is there any way to use open() and mmap() and still get shared memory? Are objects mapped to the hugepage mount filepath shared or can they be?
If you map them as MAP_SHARED they should be.
Quote:
(3) If I use open() instead of shm_open() but use the shared memory filepath (/dev/shm) does that make it shared?
If independent processes map the same areas of any mappable file with mmap's MAP_SHARED flag, the address space is effectively shared, as specified by man mmap:
Code:
       MAP_SHARED Share this mapping.  Updates to the mapping are  visible  to
                  other  processes that map this file, and are carried through
                  to the underlying  file.   The  file  may  not  actually  be
                  updated until msync(2) or munmap() is called.

/dev/shm(aka tmpfs) and hugetlbfs are just kernel extensions to the filesystem that allow mmap to map and share new kinds of memory without otherwise changing the behavior of the mmap call or requiring new and strange system calls; instead of some special new create_explicit_memory_segment system call, just create ordinary files in a special place... I suspect shm_open ultimately just calls open() and mmap() on Linux.

That said, I am not intimately familiar with the hugepage system yet, and there may be corner cases/gotchas I'm not yet aware of. You might try using libhugetlbfs or at least examining its operation.

Last edited by Corona688; 05-27-2010 at 12:54 PM..
# 3  
Old 05-27-2010
Thanks. That pretty much confirmed what I thought, but it's nice to have some assurance. I am fairly new to Linux and kind of had to jump right into it.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Shared library with acces to shared memory.

Hello. I am new to this forum and I would like to ask for advice about low level POSIX programming. I have to implement a POSIX compliant C shared library. A file will have some variables and the shared library will have some functions which need those variables. There is one special... (5 Replies)
Discussion started by: iamjag
5 Replies

2. AIX

shared memory

1.How to know wich process is using the shared memory? 2.How to flush (release) the process from the shared memory? (1 Reply)
Discussion started by: pchangba
1 Replies

3. UNIX for Advanced & Expert Users

Shared Memory

Hi, Using ipcs we can see shared memory, etc.. details. How can I add/remove shared memory(command name)? Thanks, Naga:cool: (2 Replies)
Discussion started by: Nagapandi
2 Replies

4. Programming

Shared memory for shared library

I am writing a shared library in Linux (but compatible with other UNIXes) and I want to allow multiple instances to share a piece of memory -- 1 byte is enough. What's the "best" way to do this? I want to optimize for speed and portability. Obviously, I'll have to worry about mutual exclusion. (0 Replies)
Discussion started by: otheus
0 Replies

5. Programming

Shared memory in shared library

I need to create a shared library to access an in memory DB. The DB is not huge, but big enough to make it cumbersome to carry around in every single process using the shared library. Luckily, it is pretty static information, so I don't need to worry much about synchronizing the data between... (12 Replies)
Discussion started by: DreamWarrior
12 Replies

6. Programming

memory sharing - not shared memory -

hi, this is the problem: i want to swap a linked list between 4 processes (unrelated), is there any way i can do that just by sending a pointer to a structure? //example typedef struct node { int x; char c; struct node *next; } node; or i should send the items ( x,c ) by... (9 Replies)
Discussion started by: elzalem
9 Replies

7. Programming

help with shared memory

what i want to do is have an int that can been written into by 2 processes but my code doesn't seem to work. #include <sys/types.h> #include <sys/ipc.h> #include <sys/sem.h> #include <sys/shm.h> #include<stdio.h> #define KEY1 (1492) int main() { int shmid; volatile int * addr;... (6 Replies)
Discussion started by: ddx08
6 Replies

8. Linux

all about shared memory

Hi all :confused: , I am new to unix.I have been asked to implement shared memory in user's mode.What does this mean?What is the difference it makes in kernel mode and in users mode?What are the advantages of this impemenation(user's mode)? And also i would like to know why exactly shared... (0 Replies)
Discussion started by: vijaya2006
0 Replies

9. UNIX for Advanced & Expert Users

Shared memory shortage but lots of unused memory

I am running HP-UX B.11.11. I'm increasing a parameter for a database engine so that it uses more memory to buffer the disk drive (to speed up performance). I have over 5GB of memory not being used. But when I try to start the DB with the increased buffer parameter I get told. "Not... (1 Reply)
Discussion started by: cjcamaro
1 Replies

10. Programming

Shared memory

Dear Reader, Is is necessary to attach / dettach the shared memory segments for write operations , if more than one program is accessing same shared memory segments.. I have used semaphore mutex and still I'm getting segmentation fault when I write to the segment when other program is already... (1 Reply)
Discussion started by: joseph_shibu
1 Replies
Login or Register to Ask a Question