Operating System Three easy pieces Remzi H ArpaciDusseau

  • Slides: 16
Download presentation
Operating System : Three easy pieces Remzi H. Arpaci-Dusseau Andrea C. Arpaci-Dusseau Chapter. 17

Operating System : Three easy pieces Remzi H. Arpaci-Dusseau Andrea C. Arpaci-Dusseau Chapter. 17 Free-Space Management Minkyu Kang(mkkang@dcslab. snu. ac. kr) School of Computer Science and Engineering Seoul National University

Outline § We see how to manage free memory through allocating mechanisms § Splitting

Outline § We see how to manage free memory through allocating mechanisms § Splitting § Coalescing § When memory is allocated, fragmentation problems can occur § Internal Fragmentation § External Fragmentation § We several free-space managing strategies for a fast, spaceefficient, scalable allocator § Basic strategies § Segregated Lists § Buddy Allocation § We see free-space managing strategies of the Linux system Operating System : Three easy pieces 2

Allocating Mechanisms § Splitting and Coalescing Heap Request Return 0 x 0000 Free 15

Allocating Mechanisms § Splitting and Coalescing Heap Request Return 0 x 0000 Free 15 B 0 x 000 A 0 x 0014 5 B 0 x 001 E Free list head addr : 0 x 0000 size : 30 B 10 B Operating System : Three easy pieces addr : 0 x 0019 0 x 0014 NULL size: : 10 B 5 B size Free Used Free NULL 3

Allocating Mechanisms § Growing the heap 1. The heap runs out of space 2.

Allocating Mechanisms § Growing the heap 1. The heap runs out of space 2. Process requests sbrk Heap 0 x 0000 Free 3. OS finds free physical pages 4. OS maps them into the address space of the requesting process 0 x 000 A Used 0 x 0014 0 x 001 E 0 x 0028 Operating System : Three easy pieces Free

Allocating Mechanisms § Header head § Location • Before the handed-out chunk of memory

Allocating Mechanisms § Header head § Location • Before the handed-out chunk of memory § Structure • Pointer - Hptr : point the header using for fast deallocation • Value - Size : track the size of allocated regions - Next : embedding a free list - Check : check the integrity ptr free(0 x 0008) Size: : 4088 100 Size Check 0 x 04 D 2 Next : : 0 x 00 D 0 0 x 0008 . . . Size : 100 Size : : 3980 Check 0 x 04 D 2. 0 x 006 C Next. : . 0 x 0000. . . Size : 3872. Next. : . 0 x 0000 0 x 00 D 8 . . . Operating System : Three easy pieces 5

Fragmentation Problem § The free-space gets chopped into little pieces of different sizes §

Fragmentation Problem § The free-space gets chopped into little pieces of different sizes § Internal fragmentation § Requested memory < Allocated memory 4 KB § Paging to implement virtual memory . . . Process A 4 KB 3 KB 1 KB § External fragmentation § Requested memory < Total free memory Process A (but not continuous) § Segmentation to implement virtual memory 7 KB 1 KB Process B 7 KB 1 KB Operating System : Three easy pieces 6

Free-Space Managing Strategies § Basic Strategies 0 x 0000 § Best fit • Find

Free-Space Managing Strategies § Basic Strategies 0 x 0000 § Best fit • Find the smallest chunk and return the requested amount 0 x 000 A Used • Pros : reducing wasted space • Cons : a heavy performance penalty (exhaustive search) 0 x 0014 15 30 15 § Worst fit • Find the largest chunk and return the requested amount • Pros : keeping the remaining large chunk 10 0 x 0032 • Cons : a heavy performance penalty (exhaustive search) Used 0 x 0050 0 x 0064 Operating System : Three easy pieces 15 20 5 7

Free-Space Managing Strategies § Basic Strategies 0 x 0000 § First fit • Find

Free-Space Managing Strategies § Basic Strategies 0 x 0000 § First fit • Find the first block that is big enough and return the requested amount • Pros : the advantage of speed 0 x 000 A 10 Used 0 x 0014 • Cons : splintering of the beginning of the list 15 30 15 § Next fit • Find the block from the list where location was looking last 0 x 0032 • Pros : the advantage of speed, avoiding splintering of the beginning of the list • Cons : scattering the remaining space last Used 0 x 0050 0 x 0064 Operating System : Three easy pieces 15 20 5 8

Free-Space Managing Strategies § Basic Strategies Space Efficiency Performance How to mitigate this trade

Free-Space Managing Strategies § Basic Strategies Space Efficiency Performance How to mitigate this trade off? Operating System : Three easy pieces 9

Free-Space Managing Strategies § Segregated Lists § Keep a separate list just to manage

Free-Space Managing Strategies § Segregated Lists § Keep a separate list just to manage objects of particular size 2 B … 4 B … 8 B … 16 B … 32 B … 64 B … 128 B … . . . Operating System : Three easy pieces . . . 10

Free-Space Managing Strategies § Slab allocator § Allocate a number of object caches for

Free-Space Managing Strategies § Slab allocator § Allocate a number of object caches for kernel objects that are likely to be requested frequently § Pros : less internal fragmentation, serving requests quickly Cache 32 B 64 B Slabs_full … Cache 128 KB Slabs_free Slabs_partial slab Operating System : Three easy pieces slab 11

Free-Space Managing Strategies § Buddy Allocation § When a request for memory is made,

Free-Space Managing Strategies § Buddy Allocation § When a request for memory is made, the kernel splits free-space § When the requested block is return, the kernel coalesces two spaces § Pros : less external fragmentation, serving requests quickly Return Request 7 KB 64 KB 32 KB 16 KB 8 KB Operating System : Three easy pieces 12

Linux System : Slab Allocator § Data type § Object caches : struct kmem_cache

Linux System : Slab Allocator § Data type § Object caches : struct kmem_cache § Slab caches : struct kmem_list 3 § System call § kmem_cache_create : Create a slab cache § kmem_cache_alloc : Allocate slabs of the cache § kmem_cache_free : Free slabs of the cache Struct slab Struct kmem_cache List List … Struct kmem_list 3 Slabs_partial Nodelists Slabs_full … Slabs_free Operating System : Three easy pieces … 13

Linux System : Slab Allocator § Usage Statistics § cat /proc/slabinfo # name <active_objs>

Linux System : Slab Allocator § Usage Statistics § cat /proc/slabinfo # name <active_objs> <num_objs> <objsize> <objperslab> <pagesperslab> : tunables <limit> <batchcount> <sharedfactor> : slabdata <active_slabs> <num_slabs> <sharedavail> - Slabs for allocating memory about struct requested frequently mm_struct 864 896 36 8 : tunables 0 0 0 : slabdata 24 24 0 files_cache 425 640 25 4 : tunables 0 0 0 : slabdata 17 17 0 signal_cache 913 960 1088 30 8 : tunables 0 0 0 : slabdata 32 32 0 sighand_cache 519 585 2112 15 8 : tunables 0 0 0 : slabdata 39 39 0 task_struct 333 400 6144 5 8 : tunables 0 0 0 : slabdata 80 80 0 … - Slabs for allocating common memory about kmalloc dma-kmalloc-8192 dma-kmalloc-4096 dma-kmalloc-2048 dma-kmalloc-1024 … … kmalloc-8192 kmalloc-4096 kmalloc-2048 kmalloc-1024 0 0 185 412 610 2177 0 0 8192 4 8 : tunables 0 0 0 : slabdata 4096 8 8 : tunables 0 0 0 : slabdata 2048 16 8 : tunables 0 0 0 : slabdata 1024 32 8 : tunables 0 0 0 : slabdata 0 0 0 188 8192 4 8 : tunables 0 0 0 : slabdata 47 47 0 472 4096 8 8 : tunables 0 0 0 : slabdata 59 59 0 752 2048 16 8 : tunables 0 0 0 : slabdata 47 47 0 2528 1024 32 8 : tunables 0 0 0 : slabdata 79 79 0 kmem_cache_node 620 64 64 1 : tunables 0 0 0 : slabdata 10 10 kmem_cache 288 256 32 2 : tunables 0 0 0 : slabdata 9 9 0 Operating System : Three easy pieces 0 14

Linux System : Buddy System § Data type Order Struct free_area 0 1 2

Linux System : Buddy System § Data type Order Struct free_area 0 1 2 3 4 5 6 7 8 9 9 MAX-ORDER-1 § Buddy : struct free_area § System call § free_pages_bootmem : find free page § free_one_page() : pass the page to buddy Free page blocks 2^0 page block 2^4 page block 2^(MAX-ORDER-1) page block § Usage Statistics cat /proc/buddyinfo Node 0, zone DMA 0 0 1 0 Node 0, zone DMA 32 2592 2447 8747 Node 0, zone Normal 4241 4711 14734 Node 1, zone Normal 2371 5330 17025 Operating System : Three easy pieces 2 1 1 8264 6693 14596 11886 23113 19782 0 1 1 3 4620 2627 884 159 18 4 8118 4236 1440 213 22 2 16272 12269 7300 2924 465 30 15

Summary § We see the allocating mechanisms and fragmentation problem § The goals of

Summary § We see the allocating mechanisms and fragmentation problem § The goals of allocator is a fast, space-efficient, scalable § We several free-space managing strategies § Basic Strategies § Segregated Lists § Buddy Allocation § The more we know about the exact workload presented to allocator, the more we could do to tune it. Operating System : Three easy pieces 16