Memory Paging Management for Linux

Source: Internet
Author: User

Architect must read: Linux memory paging management Source: Linux enthusiasts Id:linuxhub memory is the main memory of the computer. Memory opens up the process space for the process, allowing the process to save data in it. I'll go into the details of memory management from the physical nature of memory, especially the concept of virtual memory and memory paging. Memory simply means that memory is a data shelf. Memory has a minimal storage unit, most of which is a byte. Memory addresses are used to number the data order for each byte. Therefore, the memory address illustrates the location of the data in memory. The memory address starts at 0 and increases by 1 each time. This linearly increasing memory address is called a linear address (linear addresses). For convenience, we use hexadecimal numbers to represent memory addresses, such as 0x00000003,0x1a010cb0。 The "0x" here is used to denote hexadecimal. Followed by "0x" is the hexadecimal number that is used as the memory address. There is an upper limit on the number of memory addresses. The scope of the address space is directly related to the number of bits in the address bus. The CPU uses address bus to describe to the memory the address to which the data is to be accessed. The Intel 32-bit 80386 CPU, for example, has 32 pins that can transmit address information. Each pin corresponds to one bit. If the pin is high-voltage, then this bit is 1. If the voltage is low, then this bit is 0. 32-bit voltage high and low information through the address bus to the memory of 32 pins, the memory can convert the voltage high and low information into a 32-bit binary number, so that the CPU wants to know which location of the data. In hexadecimal notation, the 32-bit address space is from 0x00000000 to 0xFFFFFFFF. Memory storage unit uses random read memory (RAM, random Access memory). The so-called "random read" refers to the memory read time and the location of the data is independent. In contrast, much of the memory's read time is related to where the data resides. Take the tape, we want to listen to one of the songs, we must turn the tape. If that song is the first song, you can play it right away. If that song happens to be the last, it takes a long time for us to get to the point where it can be played. We already know that processes need to invoke data from different locations in memory. If the data read time and location related, the computer is difficult to control the running time of the process. Therefore, the characteristic of random reading is that memory becomes the key factor of main memory. Memory provides storage space that, in addition to meeting the operational requirements of the kernel, can often support running processes. Even if the process requires more space than the memory space, the memory space can be offset by a small amount of expansion. In other words, the memory capacity is equal to the amount of data in the computer's running state. The disadvantage of memory is that data cannot be persisted. Once a power outage occurs, the data in memory disappears. Therefore, even if the computer has a memory such as the main memory, or the need for a hard disk such as external memory to provide a durable storage space. One of the main tasks of virtual memory memory is to store data about the process. We've seen the program segments, global data, stacks, and heaps of process space before, and these storage structures play a key role in running the process. Interestingly, although processes and memory are so closely related, the process does not directly access memory. Under Linux, a process cannot read and write directly to data in memory where the address is 0x1. The address that can be accessed in a process can only be a virtual memory address. The operating system translates the virtual memory address into a real memory address. This method of memory management is called virtual memory. Each process has its own set of virtual memory addresses that are used toGive yourself the process space number. The data for the process space is also incremented in bytes. Functionally, the virtual memory address is similar to the physical memory address, which provides the location index for the data. The virtual memory addresses of the processes are independent of each other. As a result, two process spaces can have the same virtual memory address, such as 0x10001000. The virtual memory address and physical memory address have a certain correspondence, as shown in 1. An operation on a virtual memory address of a process that is translated by the CPU into a specific memory address. Figure 1 The corresponding application for the virtual memory address and physical memory address knows nothing about the physical memory address. It is possible to read and write data only through virtual memory addresses. The memory address expressed in the program is also the virtual memory address. The operation of the process on the virtual memory address is translated by the operating system into a physical memory address. Because the process of translation is solely the responsibility of the operating system, the application can have no knowledge of the physical memory address throughout the process. Therefore, the memory address expressed in the C program is the virtual memory address.  For example, in C, you can use the following command to print the variable address: int V=0;printf ("%p", (void*) &v); Essentially, the virtual memory address deprives the application of the right to have free access to the physical memory address. The process's access to physical memory must be reviewed by the operating system. Therefore, the operating system that grasps the memory correspondence, also grasps the application accesses the memory the gate. With virtual memory addresses, the operating system can guarantee the independence of process space. As long as the operating system corresponds the process space of two processes to different memory regions, two process space becomes two small kingdoms of "Laosi". Two processes cannot tamper with each other's data, and the likelihood of a process error is greatly reduced. On the other hand, with virtual memory addresses, memory sharing becomes simple. The operating system can map the same physical memory area to more than one process space. This way, no data replication is required, and multiple processes can see the same data. The mapping of the kernel and shared libraries is done this way. In each process space, the first part of the virtual memory address corresponds to the space reserved to the kernel in physical memory. This allows all processes to share the same set of kernel data. The same is true for shared libraries. For any shared library, the computer needs to be loaded into physical memory only once, so that multiple processes can be used together by manipulating the corresponding relationships. The shared memory in an IPO also depends on the virtual memory address. The separation of memory paging virtual memory address and physical memory address brings convenience and security to the process. However, the translation of virtual memory addresses and physical memory addresses can also consume additional computer resources. In multi-tasking modern computers, virtual memory addresses have become a must-have design. Then the operating system must be aware of how the virtual memory address can be translated efficiently. The simplest way to record correspondence is to record the correspondence in a single table. In order for the translation to be fast enough, the table must be loaded in memory. However, this kind of recording method is surprisingly wasted. If the Raspberry Pi 1GB physical memory each byte has a corresponding record, then the light correspondence will be far more than the memory space. Because of the number of entries in the corresponding relationship, it takes a long time to search for a corresponding relationship. In that case, the Raspberry Pi will be paralyzed. Therefore, Linux uses paging (paging) to record the correspondence. The so-called paging is to manage memory in larger units (page). In Linux, the size of each page is usually 4KB. If you want to get the memory page size of the current Raspberry Pi, you can use the command: $getconf page_size to get the result, that is, the number of bytes in memory paging:4096The returned 4096 represents a memory page that can hold 4,096 bytes, or 4KB. Linux divides physical memory and process space into pages. Memory paging can greatly reduce the memory correspondence that you want to record. We've seen that there are too many corresponding records in bytes. If the physical memory and the address of the process space are divided into pages, the kernel only needs to record the corresponding relationship of the page, the related workload will be greatly reduced. Because the size of each page is 4,000 times times that of each byte. Therefore, the total number of pages in memory is only 1 per thousand of the total number of bytes. The corresponding relationship is also reduced to 1 per thousand of the original strategy. Paging enables the design of virtual memory addresses to be implemented. Whether it is a virtual page or a physical page, the addresses within a page are contiguous. In this case, a virtual page corresponds to a physical page, and the data in the page can correspond in order one by one. This means that the end part of the virtual memory address and the physical memory address should be exactly the same. In most cases, each page has 4,096 bytes. Since 4096 is 2 of 12 times, the correspondence of the last 12 bits of the address is naturally established. We call this part of the address an offset. The offset actually expresses the position of the byte within the page. The first part of the address is the page number. The operating system only needs to record the corresponding relationship of the page number. Fig. 2 The key of the multi-level paging table memory paging system in address translation process is the correspondence between the management process space page and the physical page. The operating system records the correspondence in the Paging table (page tables). This correspondence separates the upper layer of abstract memory from the underlying physical memory, giving Linux the flexibility to manage memory. Because each process has a set of virtual memory addresses, each process will have a paging table. To ensure query speed, the paging table is also stored in memory. There are many ways to implement a paging table, and the simplest kind of paging table is to record all correspondence in the same linear list, which is shown in the "Correspondence relationship" section of 2. This single continuous paging table needs to reserve a record location for each virtual page. But for any application process, the actual address used by the process space is quite limited. We remember that the process space would have stacks and heaps. Process space reserves the address for stack and heap growth, but stacks and heaps rarely fill the process space. This means that if you use a continuous paging table, many entries are not actually used. As a result, the paging table in Linux employs a multi-tiered data structure. A multi-tiered paging table reduces the space required. Let's look at a simplified paging design that illustrates the multi-tier paging table for Linux. We divide the address into two parts: page number and offset, and use a single-layer paging table to record the corresponding relationship of the page numbering part. For a multi-level paging table, the page numbering is further divided into two or more parts, and then the two or more layers of the paging table to record their correspondence, 3 shows. Figure 3 Multi-layer paging table in the example in Figure 3, the page number is divided into two levels. FirstLevel corresponds to the first 8-digit page number, denoted by 2 hexadecimal digits. The second level corresponds to the last 12-digit page number, with 3 hexadecimal numbers. A secondary table record has a corresponding physical page, that is, a real paging record is saved. The second level table has many sheets, each two level table paging record corresponds to the virtual address of the first 8 bits are the same. For example, the two level table 0x00, the first 8 bits of the record are 0x00. The process of translating an address crosses two levels. We take the first 8 digits of the address and find the corresponding record in the first level table. This record tells us where the target level two table is in memory. We then found the paging record in the level two table, through the last 12 bits of the virtual address, and finally found the physical address. A multi-level paging table is like dividing a complete telephone number into an area code. We have the telephone number in the same area and the corresponding name record in a small book. Then use a superior book to record the area code and the corresponding relationship between each small book. If an area code is not used, then we only need to mark the area code as empty on the parent's book. Similarly, the 0x01 record in the first-level paging table is empty, indicating that the virtual address segment beginning with 0x01 is unused and the corresponding level two table does not need to exist. It is through this means that a multi-level paging table occupies more space than a single-layer paging table. A multi-level paging table has another advantage. A single-level paging table must exist in contiguous memory space. The Level two table of the multi-layer paging table allows you to walk in different places of memory. In this way, the operating system can use fragmented space to store the paging table. It is also important to note that this simplifies many of the details of a multi-layer paging table. Up to 3 levels of paging tables in the latest Linux systems, and managed memory addresses are much longer than described in this chapter. However, the fundamentals of a multi-tier paging table are the same. In summary, we understand how memory is managed in pages. On the basis of paging, virtual memory and physical memory are decoupled, allowing the kernel to deeply participate in and monitor memory allocations. The security and stability of the application process is therefore greatly improved. Source: Vameiwww.cnblogs.com/vamei/p/9329278.html

Memory Paging Management for Linux

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.