Brick 1 partner Algorithm

Source: Internet
Author: User

When the coal mine in the house is relocated, it is about to be moved, and the pockets are occupied. The village is built on the second floor, so bricks are moved.

The price in Beijing is too expensive. To buy a house, you can only start one piece now. You can slowly move bricks, complain less, and move more bricks.

Record what you learned every day. Repeat and repeat.

I. Partner Algorithms

1. Partner System

The Linux kernel uses a memory paging model that is applicable to both 32-bit and 64-bit systems. For 32-bit systems, two-level page tables are sufficient, while in x86_64 systems, four-level page table, global page Directory, parent page Directory, intermediate page directory, and page table are used.

Each page table item points to a page box. in Linux, a page box of 4 kb is used as the standard memory allocation unit.

2. Partner algorithm-Based Memory Allocation

In Linux, the partner system is used to solve external fragments.

The partner system is based on 11 consecutive page box linked lists. The first linked list only stores all idle one page boxes, the second linked list stores only two consecutive pages that are idle, and so on. These 11 linked lists link consecutive memory blocks of different sizes to different linked lists, which saves the first time to traverse a long linked list, however, we can go straight to the continuous memory that best suits our needs.

Group all idle page boxes into 11 linked lists, each of which contains 1, 2, 4, 8, 16, 32, 64,128,256,512 and 1024 consecutive page boxes. The maximum number of requests to the 1024 page box corresponds to a 4 MB continuous ram block. The physical address of the first page of each block is an integer multiple of the block size. For example, the starting address of a block with a size of 16 page frames is a multiple of 16x212 (212 = 4096, which is the size of a regular page.

Working Process:

Assume that you want to request a block (1 MB) with 256 page frames ). The algorithm first checks whether there is an idle block in the linked list of the 256 page frame. Without such a block, the algorithm will find the next larger page block, that is, find an idle block in the linked list of the 512 page boxes. If such a block exists, the kernel divides the 256 page box into two equal parts, half of which is used to satisfy the request, and the other half is inserted into the linked list of the 256 page box. If no idle block is found in the block linked list of the 512 page frame, continue to find the larger block-the block of the 1024 page frame. If such a block exists, the kernel uses the 1024 page boxes of the 256 page boxes as the request, then, insert 768 entries from the remaining 512 page boxes to the linked list of the 512 page boxes, and then insert the last 256 entries into the linked list of the 256 page boxes. If the linked list of the 1024 page box is still empty, the algorithm will discard the concurrent error signal.

3. Memory release based on partner Algorithms

The release of memory is the inverse process of allocation, and can also be seen as the merger of partners. When a block is released, first check whether a partner exists in its corresponding free_area linked list. If no partner block exists, directly Insert the block to be released into the linked list header. If yes, remove it from the linked list, merge it into a large block, and then continue to find out whether the merged block has partners in the larger level linked list, until it cannot be merged or has been merged to the largest block 2 ^ 10.

The kernel tries to merge a pair of idle partner blocks with the size of B into a separate block with the size of 2B. (When a page block is released, two consecutive page blocks are automatically merged into a large page block)

Two blocks that meet the following conditions are called partners:
• The two blocks have the same size and are recorded as B.
• Their Physical addresses are continuous.
• The physical address of the first page is a multiple of 2 × B × 212.

4. Partner bitmap

The partner algorithm uses bitmap operations to describe two adjacent parts with one digit (0th and 1st are partners, and 2nd and 3rd are partners, but 1st and 2nd are not partners. This location code is called a partner location code.

If the value is 0, both parts are idle.

When the value is 1, it indicates that one of the two parts is busy (is both busy in the status of 1 ?)

Note: the block here is the unit block of the current level. Assume that the current group is 64 pages, and its partner block is 64 pages. That is, two consecutive 64 pages -- 128 pages require a bit.

5. Partner algorithm Deficiency

A. The requirements for merging are too strict: it can only be a part that meets the requirements of the Partnership. For example, Block 1 and block 2 cannot be merged.

B. Fragmentation problem: Algorithms for memory management all have this problem. Only one page in a continuous memory is occupied, so that the entire memory zone does not have the merging conditions.

C. Waste in the algorithm: The partner algorithm allocates the memory zone based on the power of 2. When you need 257 (2 ^ 8 + 1) pages, you have to apply for 2 ^ 9 pages. As a result, 255 pages were wasted.

D. Algorithm efficiency: the partner algorithm involves a lot of calculations, including linked list and bitmap operations. The overhead is still relatively large, if a partner block of 2 ^ n size is merged into a linked list queue of 2 ^ (n + 1, the chunks in the list of 2 ^ n sizes will be reduced due to the merge operation, but the system may immediately have requirements for the chunks, therefore, you must split the table from a 2 ^ (n + 1) linked list. Such a merge and instant split process is inefficient.

I have learned a lot. Coming tomorrow

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.