[Linux Basics] - Principle Analysis of Linux Memory Mapping mmap

I have always been vague about the concept of memory-mapped files. I don’t know what is the difference between it and virtual memory, and the word mapping is also very confusing. Today I finally figured it out. . . Below, I first explain my understanding of the word mapping, and then distinguish a few confusing concepts, after which, what is memory mapping is very clear.


principle

First of all, the word "mapping" has the same meaning as the "one-to-one mapping" in mathematics class, which is to establish a one-to-one correspondence, here mainly refers to the location of the file on the hard disk and the size of the logical address space of the process The one-to-one correspondence between the same areas is shown in process 1 in Figure 1. This correspondence is purely a logical concept and does not exist physically, because the logical address space of the process itself does not exist. In the process of memory mapping, there is no actual data copy. The file is not loaded into the memory, but logically put into the memory. Specific to the code, the relevant data structure (struct address_space) is established and initialized. This process It is implemented by the system call mmap(), so the efficiency of establishing memory mapping is very high.

                                                                           Figure 1. Principle of memory mapping

Since the establishment of the memory map does not carry out the actual data copy, how can the process finally directly access the files on the hard disk through the memory operation? It depends on several related processes after memory mapping.

mmap() will return a pointer ptr, which points to an address in the logical address space of the process, so that in the future, the process does not need to call read or write to read and write the file, but only needs to operate the file through ptr. But what ptr points to is a logical address. To manipulate the data in it, the logical address must be converted to a physical address through the MMU, as shown in the process 2 of Figure 1. This process has nothing to do with memory mapping.

As mentioned earlier, the establishment of the memory map does not actually copy the data. At this time, the MMU cannot find the physical address corresponding to ptr in the address mapping table, that is, if the MMU fails, a page fault interrupt will be generated. The interrupt response function will look for the corresponding page in swap. If it cannot find the page (that is, the file has never been read into the memory), it will read the file from the hard disk through the mapping relationship established by mmap() To the physical memory, as shown in process 3 in Figure 1. This process has nothing to do with memory mapping.

If you find that the physical memory is not enough while copying data, the temporarily unused physical pages will be swapped to the hard disk through the virtual memory mechanism (swap), as shown in process 4 in Figure 1. This process also has nothing to do with memory mapping.

effectiveness

From the code level, to read a file from the hard disk into the memory, the data must be copied through the file system, and the data copy operation is implemented by the file system and the hardware driver. In theory, the efficiency of copying data is the same. But accessing files on the hard disk through memory mapping is more efficient than read or write system calls. Why? The reason is that read() is a system call in which data is copied. It first copies the content of the file from the hard disk to a buffer in the kernel space, as shown in Process 1 in Figure 2, and then copies the data to the user space, as shown in the figure In process 2, in this process, two data copies are actually completed; and mmap() directly maps the file to the user space, so the interrupt handling function directly copies the file from the hard disk to the user space according to this mapping relationship. Only one data copy was made. Therefore, the efficiency of memory mapping is higher than that of read/write.

                                                                  Figure 2. Principle of read system call

The following program operates on a file named "mmap_test" on the hard disk through the two methods of read and mmap. There are 10,000 integers in the file. The program uses different methods to read them twice, add 1, and then Write back to the hard disk. By comparison, it can be seen that the time consumed by read is nearly two to three times that of mmap.

Guess you like

Origin blog.csdn.net/u014674293/article/details/106388822