Why mmap still increase disk read?

171 Views Asked by At

For Ubuntu 20.04, I use mmap map a disk file(about 4G). The file changed by day, so I have to mmap another file day by day.
And I found sometimes(not always), the disk read increase fast although mmap done.
Why the mmap not load all the file to memory?

1

There are 1 best solutions below

2
On

In order to understand the problem, fistly you need to understand what happened if you call mmap().

mmap() can be used to map virtual memory to physical memory, and it can also map file to own virtual memory. The function stack is like ksys_mmap_pgoff()->vm_mmap_pgoff()->do_mmap_pgoff()->do_mmap(). After this, we will construct the user space memory map, but it only apply a new area in the virtual memory, we haven't touched the physical memory. When processes schedule to this process and we want to address this physical memory, then it triggers page fault interrupt and call do_page_fault()->find_vma()->handle_mm_fault() to really get in touch with physical memory, for different file system we will use different function like ext3_filemap_fault().

Now let's get back into the question.mmap() will cause disk read, but we cannot be sure when it will happen. It depends on when you used it and when CPU schedule to this process.

What's more, if you use free or other tools, you will find Linux OS have a design named Buffer and Cache.

buffers    Memory used by kernel buffers (Buffers in /proc/meminfo)
cache      Memory used by the page cache and slabs (Cached and SReclaimable in /proc/meminfo)

This mechanism will help to accelecrate I/O read/write speed, and it may also cause the disk read increase when you use mmap().