Caching in mmap

I am using a mmap call to read from a very large file using simple pointer arithmetic in C ++. The problem is that when I read small chunks of data (in KB order) multiple times, each read takes the same amount of time as the previous one. How do I know if the disk is being accessed to fulfill my request, or if the request is being made from main memory (page cache) on calls after the first.

+2


a source to share


2 answers


The problem is this: both reads were done from the cache. I think caching starts when the file is opened or mmapped before requesting data. To test this, I issued:

echo 3 > /proc/sys/vm/drop_caches



which flushes the cache, then if I run two iterations to get the same data, the first run (in my case) is 10 times slower than the second.

+3


a source


You will get the best cache performance if you use link locality... That is, if you are accessing variables that are close to each other in memory (for example, step by step in ascending order of the variables), and you are making those calls in time (ie, you are not doing many other memory accesses between by reading those elements) then you will get the best cache performance. If each read takes approximately the same amount of time, then it is most likely cached; unless things are served from the cache, which is usually indicated by multiple fast reads (cache hits) followed by a burst (cache miss) followed by faster reads. In almost all systems, a cache miss causes the chunk where the data is stored in the cache, so if you access neighboring variables (which are in the same block), they will be in the cache.



0


a source







All Articles