The Buddy System Algorithm

目录

1. Why need the Buddy System Algorithm

2. What's the Buddy System Algorithm

2.1 how to find buddy

3. Code implementation with comments

4. buddy system algorithm in linux kernel


1. Why need the Buddy System Algorithm

Memory consists of contiguous page frames, and kernel need to manage these page frames. Kernel need a robust and efficient strategy for allocating groups of contiguous page frames. In doing so, kernel need deal with the well-known external fragmentation. To address external fragmentation, kernel try to avoid as much as possible to split large free block to satisfy a request for a smaller one (otherwise, externel fragmentation happens if split a large free block for a smaller block request when there is smaller block can satisfy this request). The algorithm is the Buddy System Algorithm.

2. What's the Buddy System Algorithm

The buddy system algorithm (ULK p312),

1. groups all free page frames into several lists (shown in upper picture), each list has several blocks, each block has 2^n page frames. So if we group all page frames into 4 lists (L0, L1, L2, L3), then each list consists of blocks with 1, 2, 4, 8 page frames, that is each block in list L2 has 2^2 (4) page frames.

2. when allocating, assume we need a block of 2 page frames. It first check whether a list (L1) with blocks of 2 page frames has a free block, if yes, return this block. If no, it checks whether a list (L2) with blocks of 4 pages frames has a free block; if still no, it check whether a list (L3) with blocks of 8 pages frames has a free block. If no, return error for no memory. If yes, it return first 2 page frames of the block, then add next 2 page frames to the list (L1) with blocks of 2 page frames, and add the last 4 page frames to the list (L2) with blocks of 4 page frames.

As upper picture shows, at first only L3 has 2 blocks of 2^3 page frames, L0,L1,L2 all has no blocks. So if we need a block of 2 page frames, we first check L1 and find out no block available, then check L2, then L3. Since block in L3 is larger than requested, we split it into several smaller blocks, as picture shows, page frame 0/1 is returned; 2/3 is moved to L1, 4-7 is moved to L2. Future request for block of 2 page frames can be fulfilled directly by L1 since L1 now has a block.

3. when free, assume we free a block of 2 page frames. It first put this block to list (L1) with blocks of 2 page frames. Now the name 'buddy' comes into play. It then checks if we can merge this block with another block (buddy) of same list, into a larger block with 4 page frames. If no, nothing to be done. If yes, it remove these 2 blocks from L1, and insert to L2 as a single block. It continues merge blocks to larger block until the largest block.

Assuming we free block of page frame 0/1 which is requested as shown by last picture, we first put it in L1 and find out that block with page frame 2/3 is a buddy, so merge these 2 blocks into a larger one, then remove it from L1 and insert it to L2. In L2, we find out that block with page frame 4-7 is a buddy, so merge these 2 blocks into a larger one and remove it from L2 and insert it to L3.

2.1 how to find buddy

Maybe the hard part is how to find a buddy when return block of page frames to buddy system. Like uppper picture shows, assume L0 has 2 blocks of pf 0 & pf 1, now we need return/free block of pf 1. So which one is buddy of pf 1? pf 0 or pf 2? The rule is the starting address of new merged block should be a multiple of block size (and of course the buddy block need locate at contiguous address as pf 1). Assume each page frame has size 1 for simplicity (it is actually 4k normally). So the address of each page frame is 0, 1, and 2. And L0 has block of size 1 (2^0), L1 has block of size 2 (2^1). For pf1, if we choose pf 0 as buddy to form a larger block, then the starting address of new block is 0 which is the address of pf 0. Address 0 is a multiple of new block size 2, so pf 0 is a valid buddy. If we choose pf2, the new starting addresss is 1 which is the address of pf 1, clearly 1 is not a multiple of new block size 2, so pf2 is not a valid buddy.

3. Code implementation with comments

code for split large block into smaller ones

size = 1 << curr_order;
while (curr_order > order) {
    area--;
    curr_order--;
    
	/* this is how to split large block into smaller one */
    size >>= 1;
    buddy = page + size;
	
    /* insert buddy as first element in the list */
    list_add(&buddy->lru, &area->free_list);
    
    area->nr_free++;
    buddy->private = curr_order;
    SetPagePrivate(buddy);
}

code for merge blocks into larger one

while (order < 10) {
    /* this is how to find buddy */
    buddy_idx = page_idx ^ (1 << order);
    buddy = base + buddy_idx;
    if (!page_is_buddy(buddy, order))
        break;
		
    list_del(&buddy->lru);
    zone->free_area[order].nr_free--;
    ClearPagePrivate(buddy);
    buddy->private = 0;
	
	/* new block's start address */
    page_idx &= buddy_idx;
    order++;
}

4. buddy system algorithm in linux kernel

In linux kernel, memory is divided into several nodes due to access time by cpu, each node is divide into several zones due to some hardware limitation. Each zone has its own buddy system. (need a picture to better illustrate it)

《C++编程实例100篇》是一本深入实践、极具价值的编程教程,它针对C++编程语言提供了丰富的实例,旨在帮助读者更好地理解和掌握C++的各项特性与编程技巧。这本书的经典之处在于它将理论与实践相结合,通过100个精心设计的编程实例,覆盖了C++的各个核心领域,包括基础语法、面向对象编程、模板、异常处理、STL(标准模板库)等。 我们来探讨C++的基础语法。C++是C语言的增强版,它保留了C语言的高效性和灵活性,并引入了类、对象和继承等面向对象编程概念。基础语法包括变量声明、数据类型、运算符、控制结构(如if语句、for循环、while循环)、函数的定义和调用等。在实例中,你可能会遇到如何编写简单的程序,如计算两个数的和,或者实现一个简单的猜数字游戏。 C++的面向对象编程是其一大特色。通过类和对象,你可以构建复杂的软件系统。类是对象的蓝图,它定义了对象的属性和行为。实例化一个类,就是创建一个具体的对象。继承允许你创建新的类,这些类从现有的类派生,共享其属性和方法,同时可以添加新的功能。多态性是面向对象的另一个关键特性,它使得不同类型的对象可以对同一消息作出不同的响应。这些概念在实例中会以各种形式展现,例如设计一个图形界面的类层次,或实现一个简单的模拟游戏。 接下来是模板,C++的模板功能让代码更加通用,可以处理不同类型的数据。模板分为函数模板和类模板,前者可以创建泛型函数,后者可以创建泛型类。通过模板,你可以编写出高效且灵活的代码,比如实现一个通用的排序算法。 异常处理是C++中用于处理程序运行时错误的机制。当程序出现异常情况时,可以抛出一个异常,然后在适当的点捕获并处理这个异常。这使得代码能够优雅地处理错误,而不是让程序崩溃。实例中可能会有涉及文件操作或网络通信时可能出现的异常处理示例。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值