Tuesday, October 22, 2013

Increasing file I/O performance with mmap

While I decided to start blogging "again", the first point which came to my mind is to blog about mmap.
mmap is the recent magic word which I learnt and I deeply wanted to tell to this all who see my blog ( If any :-) ).

mmap is a system call provided by linux kernel. In simple english, mmap actually _pretends_ the file system as a normal memory to the application. That said, application can start accessing the file as a really really long array. When you execute mmap with the set of argument [ I dont wanna talk about the semantics of mmap.. you have google and wiki gurus... they have much tell you about the sementics ] , mmap just returns a char* . From now on, all your activities you wanna do with file, just do with the char* arithmetic.

I hear your questions about, lseek . That is so simple. The pointer returned always be at the first offset from the start position which you have given in mmap declaration. [ If you have given 0 in the mmap declaration, the char * is equal to lseek with 0 ]. So all you have to do is char * + length of the bytes which you need to point at.

Remember, when you do mmap, the kernel does not actually read the file. Only when you "ask" the kernel to read, i.e when you "use" that pointer, the paging happens. i.e OS actually reads the disk and populates the contents of the file. This whole process happens just like that so that you wont feel any difference.

So what are the advantages of using  mmap ?


  1. If you are dealing with large fat files i.e files with GBs, typical file I/O read / write does not help you. You need to have compiled your application with LFS support [ Large File System ] support. Else your lseek / fopen etc will fail.
  2. For 64bit application, all your 32bit file handling wont help.
  3. You have a really big file and you are sporadically searching something in file at random locations .
For all the above cases (atleast) you need to depend on mmap.

Next post, I will talk about tips and tricks of using mmap...

No comments:

Post a Comment