buddy system final

28
Implementation of Buddy System 1

Upload: techmx

Post on 19-Nov-2014

5.071 views

Category:

Technology


3 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Buddy system final

Implementation of Buddy System

1

Page 2: Buddy system final

Introduction Why Buddy System About Buddy System Types of Buddy System Implementation in Linux Pros and Cons Conclusion

Agenda

Page 3: Buddy system final

According to Donald Knuth, the buddy system was invented in 1963 by Harry Markowitz, who won the 1990 Nobel Memorial Prize in Economics.

It was first described by Kenneth C. Knowlton(published 1965).

Now a days Linux uses the buddy system to manage allocation of memory, possibly because it is allocating many structures which are already powers of two, like frames.

History

Page 4: Buddy system final

The buddy memory allocation technique is a  memory allocation algorithm that divides memory into partitions to try to satisfy a memory request as suitably as possible.

This system makes use of splitting memory into halves to try to give a best-fit.

Compared to the more complex memory allocation techniques that some modern operating systems use, buddy memory allocation is relatively easy to implement.

It supports limited but efficient splitting and coalescing of memory blocks.

INTRODUCTION

Page 5: Buddy system final

A fixed partitioning scheme limits the number of active processes and may use space inefficiently if there is a poor match between available partition size and process size

A dynamic partitioning scheme is more complex to maintain and includes the overhead of compaction.

An interesting compromise of fixed and dynamic partitioning is the buddy system.

Why Buddy System?

Page 6: Buddy system final

The buddy system(binary) allows a single allocation block to be split, to form two blocks half the size of the parent block. These two blocks are known as 'buddies'.

Part of the definition of a 'buddy' is that the buddy of block B must be the same size as B, and must be adjacent in memory (so that it is possible to merge them later).

The other important property of buddies, stems from the fact that in the buddy system, every block is at an address in memory which is exactly divisible by its size.

So all the 16-byte blocks are at addresses which are multiples of 16; all the 64K blocks are at addresses which are multiples of 64K... and so on.

What are Buddies….?

Page 7: Buddy system final

There are number of buddy systems, proposed by researcher, which are capable of reducing execution time and increase memory utilization.

Four Types of Buddy System Binary buddy system Fibonacci buddy system Weighted buddy system Tertiary buddy system

TYPES OF BUDDY SYSTEM

Page 8: Buddy system final

These three Buddy Systems are similar in the design of the algorithm, the major difference is the sizes of the memory blocks.

It also differs in memory utilization and execution time.

In some situations, one buddy system looks good, may not be good in other situation.

It simply lies on the requests for memory which causes external and internal fragmentation higher at some situations.

How it Differs?

Page 9: Buddy system final

In binary buddy system the memory block of 2m is into two equal parts of 2m-1.

It satisfies the following recurrence relation Li = Li-1+ Li-1

BINARY BUDDY SYSTEM

8

4

2 2

4

2 2

Page 10: Buddy system final

The memory consists of a collection of blocks of consecutive memory, each of which is a power of two in size.

Each block is marked either occupied or free, depending on whether it is allocated to the user.

For each block we also know its size . The system provides two operations for supporting

dynamic memory allocation: 1. Allocate (2k): Finds a free block of size 2k, marks it as

occupied, and returns a pointer to it. 2. Deallocate (B): Marks the previously allocated block

B as free and may merge it with others to form a larger free block.

Binary Buddy System

Page 11: Buddy system final

The buddy system maintains a list of the free blocks of each size (called a free list), so that it is easy to find a block of the desired size, if one is available.

If no block of the requested size is available, Allocate searches for the first nonempty list for blocks of at least the size requested.

In either case, a block is removed from the free list. This process of finding a large enough free block will

indeed be the most difficult operation for us to perform quickly.

Allocation in Binary Buddy System

Page 12: Buddy system final

If the found block is larger than the requested size, say 2k instead of the desired 2i, then the block is split in half, making two blocks of size 2k−1.

If this is still too large (k − 1 > i),then one of the blocks of size 2k−1 is split in half.

This process is repeated until we have blocks of size 2k−1, 2k−2, . . . , 2i+1, 2i, and 2i.

Then one of the blocks of size 2i is marked as occupied and returned to the user.

The others are added to the appropriate free lists. Each block B1 was created by splitting another block

into two halves, call them B1 (Buddy of B2) and B2(Buddy of B1).

Page 13: Buddy system final

Now when a block is deallocated, the buddy system checks whether the block can be merged with any others or more precisely whether we can undo any splits that were performed to make this block.

The merging process checks whether the buddy of a deallocated block is also free, in which case the two blocks are merged;

then it checks whether the buddy of the resulting block is also free, in which case they are merged; and so on.

Deallocation of Binary Buddy System

Page 14: Buddy system final

Thus it is crucial for performance purposes to know, given a block address, the size of the block and whether it is occupied.

This is usually done by storing a block header in the first few bits of the block.

More precisely, we use headers in which the first bit is the occupied bit , and the remaining bits specify the size of the block.

Eg) To determine whether the buddy of a block is free, we compute the buddy’s address, look at the first bit at this address, and also check that the two sizes match.

Block Header In Buddy System

Page 15: Buddy system final

Example:Let us consider 1-Mbyte of memory is allocated using

Buddy System. Show the Binary tree form and list form for the following :

Request 100k(A)Request 240k(B)Request 64k(C)Request 256k(D)Release BRelease ARequest 75kRelease CRelease ERelease D.

Page 16: Buddy system final

1MB

(A)100 28 128 256 512

(A)100 28 128 (B)240 16 512

(A)100 28 (C)64

64 (B)240 16 512

(A)100 28 (C)64

64 (B)240 16 (D)256 256

(A)100 28 (C)64

64 256 (D)256 256

128 (C)64

64 256 (D)256 256

(E)75 53 (C)64

64 256 (D)256 256

(E)75 53 128 256 (D)256 256

512 (D)256 256

1MB

Page 17: Buddy system final

A=128 C=64 64 256 D=256 256

Unused memory

Used memory

Page 18: Buddy system final

Hirschberg taking Knuth's suggestion has designed a Fibonacci buddy system with block sizes which are Fibonacci numbers.

It satisfies the following recurrence relation :Li=Li-1 + Li-2.

0, 1,1, 2, 3, 5, 8,13, 21, 34, 55, 144, 233,377, 610, 987, 1597, 2582…

FIBONACCI BUDDY SYSTEM

610

233

144 89144233

377

Page 19: Buddy system final

The address calculation for the binary and weighted buddy systems is straight forward, but the original procedure for the Fibonacci buddy system was either limited to a small, fixed number of block sizes or a time consuming computation.

Problem: Show the results of the following sequence in a

figure using Fibonacci Buddy system:Request 60 (A); Request 135 (B); Request 70 (C); Return A; Request 20 (D);Return B; Return C; Return D.

Fibonacci Buddy System

Page 20: Buddy system final

377 Byte Block 377(144+233)Request 60 (A) 55 A= 89 233

Request 135 (B) 55 A=89 89 B=144

Request 70 (C) 55 A=89 C=89 B=144

Return A 144 C=89 B=144

Request 60 (D) 55 D=89 C=89 B=144

Return B 55 D=89 C=89 144

Return D 144 C=89 144

Return C 377

Fibonacci Buddy System

Page 21: Buddy system final

377

144 + 233

(55+89)+ (89+144)

128K

64K 55 D=89 C=89 144

Fibonacci Buddy System

Page 22: Buddy system final

In weighted buddy system the memory block of size 2k+2 is split in 3.(2k ) and 2k sized blocks.

Further 3.(2k) is split in two 2k+1 and 2k sized blocks.

Weighted buddy system

64

1632

1648

824

412

412 348 1

Page 23: Buddy system final

In tertiary buddy system blocks of size 2k is split into blocks of three different sizes .

These blocks in turn are split into two different ways depending on the size of the blocks to be split .

Blocks of size 2k are split into three blocks of sizes 2k-

1 ,3.2k-3 and 2k-3 . Blocks of size 3. 2k-3 are split into the blocks of sizes

2k-2 and 2k-3 . It decreases the amount of internal fragmentation by

allowing more block sizes .

Tertiary buddy system

Page 24: Buddy system final

Li = Li – 1 + Li - 3 + Lβ(i) ◦ Where β is any function over positive integer with

βi <i.

Tertiary Binary System

12

8

16

1

8

4 3

4

16

24

32

64

48268

Page 25: Buddy system final

In LINUX, Buddy System in Contiguous Page Frame Allocation. All page frames are grouped into 10 lists of blocks that containing groups

of 1, 2, 4, 8, 16, 32, 64, 128, 256, and 512 contiguous page frames, respectively

The address of the first page frame of a block is a multiple of the group size

For eg, a request for 128 then First checks for a free block in the 128 list If no free block, it then looks in the 256 list for a free block If it finds a block, the kernel allocates 128 of the 256 page frames and puts

the remaining 128 into the 128 list If no block it looks at the next larger list, allocating it and dividing the

block similarly If no block can be allocated an error is reported

Implementation in LINUX

Page 26: Buddy system final

Less external fragmentation. Search for a block of the right size is cheaper than,

best fit because we need only find the first available block on the block list for blocks of size 2k;

Merging adjacent free blocks is easy. In buddy systems, the cost to allocate and free a

block of memory is low compared to that of best-fit or first-fit algorithms.

Advantages of Buddy System

Page 27: Buddy system final

It allows internal fragmentation. For example, a request for 515k will require a block

of size 1024k. In consequence, such an approach gives a waste of 509 k.

Splitting and merging adjacent areas is a recurrent operation and thus very unpredictable and inefficient.

The another drawback of the buddy system is the time required to fragment and coalesce blocks.

Disadvantages of Buddy System

Page 28: Buddy system final

Thank You!