Quote:
Originally Posted by
CrendKing
I'm involved in a project that several independent processes need to access a huge file (about 3.5G) in random pattern. The access frequency is high, so fseek and fread might be a bad choice for performance consider. My requirements are: 1) All file data are better to be in memory before any process accesses. 2) Also, assume the file is immutable, it is better to have only one copy of the data in memory in all time.
I can come up two different approaches. Approach One, every process mmap the file with MAP_SHARED flag. As mmap turns the file data into memory mapping, the file data accesses can be considered as memory accesses. However, the first time a specific location of the memory mapping is read, the kernel still goes to read the file, thus have some performance issue.
There's no avoiding the fact that data needs to be read from disk
sometime. If you know certain specific bits of it that will be accessed more frequently than the rest, you might streamline that a bit by locking those into memory in advance; you could also warn the kernel before you use it with posix_madvise if your system has it.
Quote:
Approach Two, start a "host" process before all other processes, which creates a shared memory objects using shm_open and copy the file data to it. Subsequent process mmap the shm with MAP_SHARED flag. Since all data are available in memory, this approach should have better performance and scalability.
I don't see how this would be an improvement. If you have enough memory, your kernel can cache it in the first place without all this duplication -- triplication, in this case, since your kernel will be trying to cache it despite you creating your own caches.
Quote:
For sequential read, usually Approach Three is the fastest, and Approach Two is the slowest. Although the difference between three is tiny, it shows that the access through mmap + shm has significant overhead.
Indeed, since you're using double the memory to do more or less the same thing.
Quote:
1) Are my experiments enough to show that shm_open has high overhead, which cancels its advantage of copying from memory, not disk?
It's not that shm_open has high overhead, it's that you're doing more work for little to no gain -- copying from disk cache to memory instead of using cached disk through mmap.
Quote:
4) If the answer to 3) is false, what is the cost that mmap pays in exchange of much better performance? AFAIK, there is no free lunch.
mmap can only deal with memory in blocks of page size -- that is to say, 4K for many systems. The minimum read you can do is therefore 4K. I think this limitation carries into disk cache in general though so it may not be relevant.
Get enough memory and most of what you need will stay in disk cache.