The Buddy System
Linux kernel paging model adopted for both 32-bit and 64-bit system, for 32-bit systems, two page table sufficient, whereas in x86_64 system uses four page table. Page four tables are as follows:
- Page Global Directory (Page Global Directory)
- Parent directory page (Page Upper Directory)
- Intermediate directory page (Page Middle Directory)
- Page table (Page Table)
Page Global directory contains several pages address the parent directory, page parent directory in turn contains the address of the intermediate directory of several pages, and page number of intermediate directory also contains the address of the page table, each page table entry points to a page frame. Linux used of 4KB page frames as a standard memory allocation unit.
In practical applications, often you need to allocate a set of consecutive frames, and frequent application and release of contiguous page frames of different sizes, will inevitably lead to a dispersed plurality of small free page frames allocated memory block in the page frame. Thus, even if the page frame is free, others need to allocate contiguous page frame applications are difficult to meet.
To avoid this situation, Linux kernel introduced the buddy system algorithm (buddy system). All the free page frame list are grouped into 11 blocks, each block contains the size of each list 1,2,4,8,16,32,64,128,256,512 1024 consecutive page frames and page frames block . 1024 can apply the maximum consecutive frames, corresponding to the size of the contiguous memory 4MB. Physical page frame address of the first block of each page frame is an integer multiple of the block size.
Suppose you want to apply for a block of 256 page frames, start to find a free block list 256 page box, and if not, go to the list box on page 512 of the find, find the page box will be divided into two blocks 256 page frames of the block, one assigned to the application, a further 256 moves to the list page frames. If still no free block list 512 page box, continue to look to 1024 pages list box, if still no error is returned.
Page frame block release will initiate two consecutive page frames blocks are merged into a larger block page frame.
Buddy algorithm advantages and disadvantages:
1) Although the partner memory algorithms on memory fragmentation issues have done very well, but this algorithm, a small block tend to impede a merger chunk, a system for allocating memory block size is random , a memory only a small memory block is not released, two large side can not be merged.
2) algorithm has some waste, buddy algorithm is based on a power of 2 size allocated memory block, of course, do so for a reason, namely to avoid the demolition of a large block of memory is too broken, more importantly, the allocation and release quickly. But he also had a negative side, if the required memory size is not a power of 2, there will be a waste of part of the page. Sometimes very serious. Such as the original block is 1024, applied for 16 blocks, 600 blocks and then apply it to apply not, as it has been split.
3) In the split and merge linked list and involve more bitmap operations, the cost is quite large.
Buddy (defined partner):
Here is the concept of partnership to meet the following three conditions are called partners:
1) two identical block sizes;
2) two successive block address;
3) two blocks must be separated from the bulk of the same;
Buddy principle of allocation algorithm:
If the system requires four (2 memory block 2) of the page size, the algorithm is to free_area [2] find, if there is the free block list, directly off therefrom and dispensed. If the two parts do not, the algorithm looks up the array along free_area [. 3], if free_area [. 3] has a free block, it is picked from the list, into equal-sized, the first four pages as one block inserted free_area [2], 4 pages dispensed, free_area [3] is also not, then look up, if free_area [4] there, it will be 16 (2 2 2 2) split into two pages, before the half hanging free_area [3] the head of the list, the other half of the 8 pages is divided into two halves, the first half linked free_area [2] in the list, the latter half dispensed. If free_area [. 4] and no, then repeat the above process, know free_area reach the final array, is discarded if it is not assigned.
Release principle Buddy algorithm:
The reverse process is freeing memory allocated, the process can also be seen as a merger partner. When a block is released, its corresponding first test in the list whether a partner exists, if no partner block to be released directly into the linked list head block; if so, the list from the partner off, combined into a chunk, then after the merger continue to study a larger block in the list if there are partners exist until it merged or been merged into the largest block (2 2 2 2 2 2 2 2 2 pages).
slab mechanism
slab is a memory allocation mechanism Linux operating system. Its work is for some of the regular allocation and release of objects, such as process descriptor and so on, the size of these objects are generally small, if the direct use of the buddy system to allocate and release, not only will cause a lot of internal fragmentation, and processing speed too slow. The slab allocator is managed based on the object, the object of the same type into one category (e.g., the process is a class descriptor), whenever an object to apply such, a slab allocator allocates a slab of this size from the list the unit went out, and when to be released, will save it back in the list, instead of returning directly to the partner system, so as to avoid these in the debris. The slab allocator does not discard the object has been allocated, but released and store them in memory. When later have to request a new object, you can get directly from memory rather than repeat initialization.
Linux's slab can have three states:
- Full: All objects slab is marked to use.
- Empty: All objects in the slab are marked as free.
- Part: slab, some objects are marked as used, and some are marked as free.
First, the slab allocator allocates partially free slab. If not, it is allocated from empty slab. If not, physically contiguous pages allocated from a new slab, and assign it a Cache, then re-allocate space slab.
Compared with conventional memory management mode, slab cache allocator provides a number of advantages.
1, the kernel is usually dependent on the allocation of small objects, they will be numerous distribution within the system life cycle.
2, slab allocator cache to provide this functionality through an object similar to the size of the cache, thus avoiding the common problem of fragmentation.
3, slab dispenser further supports common initialization object, thereby avoiding for the same purpose to repeat an object is initialized.
4, slab dispenser may also support hardware cache alignment and coloring, which allows different cache objects occupy the same cache line, thereby improving cache utilization and better performance.