Buddy system algorithm

Source: Internet
Author: User

1. Partnerships


Definition: A parent entity is divided into two child entities with the same attributes in different aspects. These two child entities are in a partnership. During the memory allocation process of the operating system, a memory block is often divided into two memory blocks of the same size. The two memory blocks of the same size are in a partnership. It meets three conditions:

  • The two blocks have the same size as 2 ^ K
  • Their physical addresses are continuous.
  • Split from the same big block

 

2. Implementation principle of the buddy algorithm


To facilitate page maintenance, multiple pages are made up of memory blocks, each of which has a square power page of 2. The square power index is called order. During memory operations, these memory blocks are often divided into two equal-sized blocks. We call them the partner blocks. 1 is used to represent their partnerships. When this bit is 1, it indicates that one of them is in use. When this bit is 0, it indicates that both pages are idle or in use, the system determines whether to use or allocate the page Block Based on the value 0 or 1. Each time the system allocates and recycles partner blocks, it performs an exclusive or operation on their partner bit and 1. In the beginning, both partner blocks were idle and their partner bits were 0. Later, one of them was used.
The difference is 1. Later, the difference is 0. Later, the difference is 1, later, the other part also recycles the difference or returns 0.

 

A simple example is provided to illustrate how the algorithm works. This example is based on the Linux partner algorithm structure. It groups 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.

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
The page box is divided into two equal parts, one half is used to meet the request, and the other half is inserted into the linked list of 256 page boxes. If no idle block is found in the block linked list of the 512 page box, continue to find the larger block-the block of the 1024 page box. 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.

The inverse process of the above process is the release process of the page block, which is also the origin of the algorithm name. 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. 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 x B x 212.

This algorithm is iterative. If it successfully merges the released blocks, it will try to merge 2B blocks to try to form larger blocks again.

Note: The example above does not involve any operations on the partner bitmap. This is to briefly describe the principle of the partner algorithm.

 

 

To achieve efficient partner algorithms, some basic operations are required:

  • Search for the partner page number in the specified order block linked list by page number

Note: The "partner" Page refers to the page of the serial number in the partner block corresponding to the block where a page is located. For example, the partner page corresponding to order 4 on page 40 is 40 + 16 = 56. This concept was created by the author mainly to illustrate the problem.

Linux uses this statement: buddy_idx = page_idx ^ (1 <order );

This statement returns page_idx and 1 <order or, actually, add or subtract 2 ^ order to or from page_idx (the gap between the two partner blocks is good in the order block linked list; 2 ^ order page boxes ), the addition or subtraction of page_idx depends on whether the bit of the XOR operation is 0 or 1. That is to say, if this bit is 0, the partner page of this page should be behind it (here it refers to the page number order), so we need to add 2 ^ order; and if this bit is 1, then the partner on this page should also be in front of it, so we need to subtract 2 ^ order. Is that true? In fact, the principle of memory alignment is used here. Have a good taste!

  • Search for the partner location of the corresponding order by page number

Two partner blocks are described by one partner bit. Therefore, each linked list only requires pages/(1 <(Order + 1) Partners (pages is the total number of page boxes ).

The query partner bit operation is buddy_bit_idx = page_idx> (Order + 1 ). This statement is easy to understand. In fact, 2 ^ (Order + 1) is the size of a pair of partner blocks in the order block linked list. Naturally, by dividing page_idx by this value, the partner offset is obtained.

 

  • Merge partner Blocks

The merge of the partner blocks is actually a page number alignment operation. page_idx & = buddy_idx indicates that the order bit of page_idx is set to 0. The above is the implementation of the Linux system. When I see this statement, I wonder why I didn't directly set it to 0 ~ If the order bits are set to 0, page_idx =
Page_idx)

The specific implementation of the Linux kernel buddy algorithm will not be explored here. You can refer to the following blog

Http://blog.csdn.net/yunsongice/article/details/5225155


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.