[Reprint] operation and maintenance required reading: Linux memory paging management

Operation and maintenance required reading: Linux memory paging management

The memory is a main memory of the computer. Memory for the process to open up the process space, so that the process in which to save the data. My memory of the physical characteristics of the departure from, deep into the details of memory management, in particular, to understand the concept of virtual memory and memory paging.

▉ memory

Simply put, memory is a data shelf. Memory has a smallest unit of storage, most of them are one byte. Memory to the data sequence number with each byte memory address (memory address). Thus, the described memory address location of data in memory. Memory addresses start at zero, increments 1. This linear increase in a linear address called a memory address (linear address). For convenience, we use hexadecimal numbers to represent a memory address, such as 0x00000003,0x1A010CB0. Where "0x" is used to indicate hexadecimal. "0x" followed behind, as is the memory address hexadecimal number.

No memory address has an upper limit. Directly related to the number of bits of the address space range and address bus (address bus) is. The CPU address bus wants to access the memory address of the data described. To Intel's 32-bit 80386 CPU type, for example, this CPU has a 32-pin address information can be transmitted. Each corresponds to a pin. If the voltage is high on pin, then this bit is 1. If the voltage is low, then this one is 0.32 high and low voltage memory spread information through the address bus 32-pin, high and low voltage memory will be able to convert information into a binary number 32, which CPU you want to know which is the position of the data. Hexadecimal, is a 32-bit address space from 0x00000000 to 0xFFFFFFFF.

A memory storage unit with random access memory (RAM, Random Access Memory). So-called "random access" refers to reading time regardless of where the data and memory. On the other hand, a lot of time to read a memory location and the data related to. Take the tape, we want to hear a song in which you must rotate the belt. If that song is the first song, then you can play immediately. If that song happens to be the last one, we fast-forward to a location that can be played on takes a long time. We already know that the process needs to call data from different locations in memory. If the data read time and location relevant, it is difficult to run a computer time control of the process. Thus, the characteristics of random access memory is the main memory to become the key factor.

Provide memory storage space, in addition to meet the operational requirements of the kernel, but also usually processes running in support. Even more than the space required to process memory space, memory space can also be compensated by a small amount of expansion. In other words, the amount of data storage capacity of memory, and the operating state of the computer considerably. The disadvantage is that the memory can not be permanently stored data. Once the power failure, data in memory disappears. Thus, even though a computer with a memory so that main memory or external memory such as a hard disk required to provide persistent storage.

▉ virtual memory

One of the main tasks of memory, is the data storage process. We have previously seen block the process space, global data, stack and heap, and the key role of these storage structures in these processes run in the play. Interestingly, although the relationship is so close processes and memory, but the process can not directly access the memory. Under Linux, the process can not be directly read and write data memory address location 0x1. Address process can access only the virtual memory address (virtual memory address). OS virtual memory address will be translated into real memory addresses. This memory management, known as virtual memory (virtual memory).

Each process has its own set of virtual memory addresses, used to own process space number. The same process space of data, in bytes, followed by an increase. From the function that is similar to virtual memory addresses and physical memory addresses are indexed to provide location data. Process virtual memory address independent of each other. Therefore, the two processes can have the same space of virtual memory address, such as 0x10001000. Physical memory addresses and virtual memory addresses have a certain relationship, as shown in FIG. Operations on a process virtual memory address translation will be operated in pairs with a specific CPU memory address.

And a virtual memory address corresponding to a physical memory address in FIG.

Applications, ignorant of the physical memory address. It is only possible to read and write data through the virtual memory address. Memory address expressed in the program, are also virtual memory address. The operation of the process virtual memory address, the operating system will be translated to a physical memory address of the operation. Because of the translation process by the operating system is responsible for full, so the application can not know anything about the physical memory address in the whole process. Therefore, the memory address expressed in a C program, are virtual memory address. For example, in C language, the following command can be used to print variable address:

int v = 0; printf("%p", (void*)&v);

In essence, the virtual memory address denied the right to free access to physical memory addresses applications. Process access to physical memory, the operating system must be reviewed. Therefore, control of the operating system memory corresponding relationship, but also master the application accesses memory gate. With virtual memory address, the operating system can guarantee the independence of the process space. As long as the operating system to process space of two processes correspond to different memory areas, let the two process space into two small kingdom "never in contact with" the. Two processes can not tamper with each other's data, the possibility of error on the process greatly reduced.

On the other hand, we have a virtual memory address, memory sharing becomes simple. The operating system may correspond to the same physical memory area to a plurality of process space. In this way, without any data replication, multiple processes can see the same data. Mapping the kernel and shared libraries is carried out in this way. Each process space, originally part of the virtual memory addresses are mapped to physical memory space reserved for the kernel. In this way, all the processes can share the same set of core data. The situation is similar to shared libraries. For any shared library, the computer needs to be loaded into physical memory only once, it can be manipulated by correspondence, to make common use of multiple processes. IPO in shared memory, also relies on virtual memory address.

▉ memory paging

Separating virtual memory addresses and physical memory addresses, to the process to bring convenience and safety. But the translation of virtual memory addresses and physical memory addresses, the computer will consume additional resources. In modern computer multi-tasking, virtual memory address has become an essential design. Then, the operating system must consider carefully how to efficiently translate the virtual memory address.

Record correspondence between the easiest way, is to correspondence between the records in a table. To make translation speed fast enough, the table must be loaded in memory. However, this recording method surprisingly wasted. If Raspberry Pi each byte 1GB of physical memory has a corresponding record, then alone will far exceed the correspondence between the memory space. As the correspondence between the number of entries, the search time required corresponds to a long relationship. In this case, make the raspberry pie paralyzed.

Thus, Linux uses a paging (Paging) way to record a correspondence relationship. The so-called paging, is to the larger size of the unit page (page) to manage memory. In Linux, usually the size of each page is 4KB. If you want to get the current memory page size raspberry pie, you can use the command:

$getconf PAGE_SIZE

The results obtained, i.e. the number of bytes of memory page:

4096

4096 returned to represent each memory page can store 4096 bytes, 4KB. Linux physical memory space and process are divided into pages.

Memory paging, can greatly reduce the memory corresponding relationship to be recorded. We have seen, in bytes of the corresponding record too much. If the physical memory address space and process are divided into pages, the kernel only needs to record correspondence between the pages of the related workload will be greatly reduced. Since the page size is 4000 times per byte. Thus, the total number of pages in memory but the total number of bytes 1/4000. Correspondence between the original strategy also reduced to 1/4000. Paged virtual memory address so that the design has been possible.

Whether it is a virtual page, or physical page address within one page are continuous. In this case, a virtual page and a physical page corresponding to up, the data in the page may correspond in sequence. This means that the end portion of the virtual memory addresses and physical memory addresses should be identical. In most cases, each page has 4096 bytes. Since 4096 is the 12 th power of 2, so the address of the last 12 of the correspondence between the natural establishment. We call this part of the address is called an offset (offset). In practice the offset byte positions expressed in the page. The first part of the address is a page number. Operating system only need to record correspondence between the page number.

Figure 2 address translation process

▉ multi-level page table

The key memory paging system, that correspondence management process space pages and physical pages. Operating system recorded in the corresponding relationship table page (page table) in. This correspondence between the physical memory so that the upper and lower separate abstract memory, allowing the flexibility to Linux memory management. Since each process there will be a set of virtual memory addresses, each process has a page table. In order to ensure the query speed, the page table will be stored in memory. There are many page table implementation, the simplest one is to put all the page table corresponding to the same linear relationship record in the list, i.e., "the correspondence relationship" in section 2 shown in FIG.

This single continuous page table, need to set aside each virtual page of a record position. But for any application process, its process address space actually used are quite limited. We also recall process will stack and heap space. Address space reserved for the process stack and heap growth, but rarely fill up the stack and heap process space. This means that if a continuous paging tables, many entries are not really used. Thus, the page table in Linux, the use of multi-layer data structure. The multilayer page table space required can be reduced.

我们来看一个简化的分页设计,用以说明Linux的多层分页表。我们把地址分为了页编号和偏移量两部分,用单层的分页表记录页编号部分的对应关系。对于多层分页表来说,会进一步分割页编号为两个或更多的部分,然后用两层或更多层的分页表来记录其对应关系,如图3所示。

图3 多层分页表

在图3的例子中,页编号分成了两级。第一级对应了前8位页编号,用2个十六进制数字表示。第二级对应了后12位页编号,用3个十六进制编号。二级表记录有对应的物理页,即保存了真正的分页记录。二级表有很多张,每个二级表分页记录对应的虚拟地址前8位都相同。比如二级表0x00,里面记录的前8位都是0x00。翻译地址的过程要跨越两级。我们先取地址的前8位,在一级表中找到对应记录。该记录会告诉我们,目标二级表在内存中的位置。我们再在二级表中,通过虚拟地址的后12位,找到分页记录,从而最终找到物理地址。

多层分页表就好像把完整的电话号码分成区号。我们把同一地区的电话号码以及对应的人名记录同通一个小本子上。再用一个上级本子记录区号和各个小本子的对应关系。如果某个区号没有使用,那么我们只需要在上级本子上把该区号标记为空。同样,一级分页表中0x01记录为空,说明了以0x01开头的虚拟地址段没有使用,相应的二级表就不需要存在。正是通过这一手段,多层分页表占据的空间要比单层分页表少了很多。

多层分页表还有另一个优势。单层分页表必须存在于连续的内存空间。而多层分页表的二级表,可以散步于内存的不同位置。这样的话,操作系统就可以利用零碎空间来存储分页表。还需要注意的是,这里简化了多层分页表的很多细节。最新Linux系统中的分页表多达3层,管理的内存地址也比本章介绍的长很多。不过,多层分页表的基本原理都是相同。

 

*声明:推送内容及图片来源于网络,部分内容会有所改动,版权归原作者所有,如来源信息有误或侵犯权益,请联系我们删除或授权事宜。

- END -

Guess you like

Origin www.cnblogs.com/jinanxiaolaohu/p/11646584.html