Computer Systems Lab Matteo Corti Informatikdienste, ETH Z urich - - PowerPoint PPT Presentation

computer systems lab
SMART_READER_LITE
LIVE PREVIEW

Computer Systems Lab Matteo Corti Informatikdienste, ETH Z urich - - PowerPoint PPT Presentation

Computer Systems Lab Matteo Corti Informatikdienste, ETH Z urich 2007-03-19 Matteo Corti (Informatikdienste, ETH Z urich) Computer Systems Lab 2007-03-19 1 / 24 Introduction Course schedule lecture: Monday 4:15 5:00 p.m. (RZ


slide-1
SLIDE 1

Computer Systems Lab

Matteo Corti

Informatikdienste, ETH Z¨ urich

2007-03-19

Matteo Corti (Informatikdienste, ETH Z¨ urich) Computer Systems Lab 2007-03-19 1 / 24

slide-2
SLIDE 2

Introduction

Course schedule

  • lecture: Monday 4:15 – 5:00 p.m. (RZ F21)
  • assignments: in teams, time to be arranged individually

Contact Matteo Corti Mathias Payer SOW E16 RZ H7

matteo.corti@id.ethz.ch mathias.payer@inf.ethz.ch

Resources

  • Homepage:

http://www.lst.inf.ethz.ch/teaching/lectures/ss07/2100/index.html

Matteo Corti (Informatikdienste, ETH Z¨ urich) Computer Systems Lab 2007-03-19 2 / 24

slide-3
SLIDE 3

Structure of the course

Projects

  • Topics related to operating systems
  • Practical focus

Organization

  • programming assignments (labs)
  • teams (max 2 students) are allowed
  • final grade based on the labs

Environment

  • Unix/C

Matteo Corti (Informatikdienste, ETH Z¨ urich) Computer Systems Lab 2007-03-19 3 / 24

slide-4
SLIDE 4

Course schedule (tentative)

Date Lecture Project 2007-03-19 Memory allocation malloc lab 2007-03-26 2007-04-02 Scheduling 2007-04-09 Easter Monday scheduler lab 2007-04-16 Sechsel¨ auten 2007-04-23 File systems 2007-04-30 filesystem lab 2007-05-07 Networking 2007-05-14 proxy lab 2007-05-21 Distributed systems 2007-05-28 Whit Monday distributed lab 2007-06-04 Distributed systems 2007-06-11 Optional labs 2007-06-18 TBD Optional labs

Matteo Corti (Informatikdienste, ETH Z¨ urich) Computer Systems Lab 2007-03-19 4 / 24

slide-5
SLIDE 5

Suggested literature

  • R. E. Bryant and D. O’Hallaron

Computer Systems — A Programmer’s Perspective Prentice Hall, Upper Saddle River, NJ, 2003.

Matteo Corti (Informatikdienste, ETH Z¨ urich) Computer Systems Lab 2007-03-19 5 / 24

slide-6
SLIDE 6

Project 1: Memory allocation

Summary Implement a memory allocator: malloc, free, realloc Goals

  • Learn how to manage storage allocation.
  • Good exercise to master pointers

Environment

  • Unix like system
  • C

Resources

  • Computer Systems — A Programmer’s Perspective, Chapter 10.9

(Dynamic Memory Allocation), pages 730–755.

Matteo Corti (Informatikdienste, ETH Z¨ urich) Computer Systems Lab 2007-03-19 6 / 24

slide-7
SLIDE 7

The Heap

Area of a process’s (virtual) memory where which is dynamically allocated. Example (Unix)

  • grows upwards
  • the brk pointer marks the top of

the heap

User stack Heap Uninitialized data (.bss) Initialized data (.data) Program text (.text) Memory mapped region for shared libraries top of the heap (brk)

Matteo Corti (Informatikdienste, ETH Z¨ urich) Computer Systems Lab 2007-03-19 7 / 24

slide-8
SLIDE 8

Memory Management

The memory manager (user space) maintains a process’s heap: a collection of blocks (contiguous chunks of memory) which are either allocated or free. Allocation is explicit (e.g., malloc in C or new in Java). Deallocation can be explict (e.g., free in C) or implicit (garbage collection in Java). Issues The memory manager needs to:

  • distinguish block boundaries
  • distinguish between free and allocated blocks

Matteo Corti (Informatikdienste, ETH Z¨ urich) Computer Systems Lab 2007-03-19 8 / 24

slide-9
SLIDE 9

Memory allocation: Strategies

8 16 8 32 32 16 16 32 16 8 8 8 64 32 8 16 8 16 8 32

  • first-fit: use the first free block which is big enough
  • best-fit: take smallest fitting block
  • worst-fit: take biggest available block
  • quick-fit: best-fit but multiple free-lists (one per block size)

Matteo Corti (Informatikdienste, ETH Z¨ urich) Computer Systems Lab 2007-03-19 9 / 24

slide-10
SLIDE 10

Memory allocation: Bitmaps

The simplest (but often slowest) way to manage memory blocks is to mark in a bitmap if a block is free or allocated.

0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

8 16 8 32 32 16 16 32 16 8 8 8 64 32 8 16 8 16 8 32

Space For a 4GB memory space and 4KB words a 128MB table is necessary to store the free/allocated information. Speed Slow scan to find a free block

Matteo Corti (Informatikdienste, ETH Z¨ urich) Computer Systems Lab 2007-03-19 10 / 24

slide-11
SLIDE 11

Memory allocation: Dynamic data structures

Free blocks are stored in a dynamic data structure (ordered list,

  • rdered tree, . . .).

8 16 8 32 32 16 16 32 16 8 8 8 64 32 8 16 8 16 8 free_list 32

Lists are usually ordered by allocation address to allow a faster merging of free blocks.

8 8 16

Matteo Corti (Informatikdienste, ETH Z¨ urich) Computer Systems Lab 2007-03-19 11 / 24

slide-12
SLIDE 12

Memory allocation: Dynamic data structures

Free blocks are stored in a dynamic data structure (ordered list,

  • rdered tree, . . .).

8 16 8 32 32 16 16 32 16 8 8 8 64 32 8 16 8 16 8 free_list 32

Lists are usually ordered by allocation address to allow a faster merging of free blocks.

8 8 16

Matteo Corti (Informatikdienste, ETH Z¨ urich) Computer Systems Lab 2007-03-19 11 / 24

slide-13
SLIDE 13

Memory allocation: Dynamic data structures

Free blocks are stored in a dynamic data structure (ordered list,

  • rdered tree, . . .).

8 16 8 32 32 16 16 32 16 8 8 8 64 32 8 16 8 16 8 free_list 32

Lists are usually ordered by allocation address to allow a faster merging of free blocks.

16

Matteo Corti (Informatikdienste, ETH Z¨ urich) Computer Systems Lab 2007-03-19 11 / 24

slide-14
SLIDE 14

Memory allocation: Lists

Space Linking information as well as some flags (i.e., free or allocated) has to be stored in the block. Speed Depending on the chosen structure operations can be implemented efficiently.

Matteo Corti (Informatikdienste, ETH Z¨ urich) Computer Systems Lab 2007-03-19 12 / 24

slide-15
SLIDE 15

Memory allocation: Space utilization

External fragmentation Unused space among the blocks. Depends on the allocation strategy. Internal fragmentation Unused space inside the blocks. Depends on the possible block sizes (granularity). Data structures Space used to maintain data structures cannot be allocated (bitmaps, pointers, flags, . . .)

Matteo Corti (Informatikdienste, ETH Z¨ urich) Computer Systems Lab 2007-03-19 13 / 24

slide-16
SLIDE 16

Memory allocation: Buddy System

An example of allocation strategy:

  • Each block has a size of 2k
  • The system maintains a list for each size
  • Block allocation:
  • Find a block which fits
  • If necessary split
  • Put the remaining part in the right list
  • Free:
  • Mark the block as free
  • If is possible merge with a free neighbor

The buddy system is used in several operating systems (Windows, Linux, Oberon, . . .).

Matteo Corti (Informatikdienste, ETH Z¨ urich) Computer Systems Lab 2007-03-19 14 / 24

slide-17
SLIDE 17

Memory allocation: Buddy System (example)

Free

8 16 8 16 8 32 8 16 32

Matteo Corti (Informatikdienste, ETH Z¨ urich) Computer Systems Lab 2007-03-19 15 / 24

slide-18
SLIDE 18

Memory allocation: Buddy System (example)

Free

8 16 8 8 16 8 8 16 32 32 8 8 16 32

Matteo Corti (Informatikdienste, ETH Z¨ urich) Computer Systems Lab 2007-03-19 15 / 24

slide-19
SLIDE 19

Memory allocation: Buddy System (example)

Free

16 16 16 8 8 16 32 32 16 8 8 16 32 8 16 32

Matteo Corti (Informatikdienste, ETH Z¨ urich) Computer Systems Lab 2007-03-19 15 / 24

slide-20
SLIDE 20

Memory allocation: Buddy System (example)

Free

8 16 32 32 8 32 32 32 32 16 8 8 16 16 8 16 32

Matteo Corti (Informatikdienste, ETH Z¨ urich) Computer Systems Lab 2007-03-19 15 / 24

slide-21
SLIDE 21

Memory allocation: Buddy System (example)

Free

8 16 32 32 8 32 32 32 32 16 8 8 16 16 8 16 32

Allocate (8)

32 32 32 32 16 8

Matteo Corti (Informatikdienste, ETH Z¨ urich) Computer Systems Lab 2007-03-19 15 / 24

slide-22
SLIDE 22

Memory allocation: Buddy System (example)

Free

8 16 32 32 8 32 32 32 32 16 8 8 16 16 8 16 32

Allocate (8)

32 16 16 16 32 32 16 32 16 8

Matteo Corti (Informatikdienste, ETH Z¨ urich) Computer Systems Lab 2007-03-19 15 / 24

slide-23
SLIDE 23

Memory allocation: Buddy System (example)

Free

8 16 32 32 8 32 32 32 32 16 8 8 16 16 8 16 32

Allocate (8)

8 16 8 8 16 32 32 32 8 16 32 16 8 16 32

Matteo Corti (Informatikdienste, ETH Z¨ urich) Computer Systems Lab 2007-03-19 15 / 24

slide-24
SLIDE 24

Memory allocation: Buddy System (example)

Free

8 16 32 32 8 32 32 32 32 16 8 8 16 16 8 16 32

Allocate (8)

8 16 8 16 32 32 32 32 8 8 16 32 8 16 16 8 16 32

Matteo Corti (Informatikdienste, ETH Z¨ urich) Computer Systems Lab 2007-03-19 15 / 24

slide-25
SLIDE 25

Memory allocation: Slabs

Disadvantages of the buddy system:

  • internal fragmentation (max 50%)
  • bad distribution of the block addresses (bad caches

performance) Solaris (J. Bonwick 1994) introduced the concept of a slab allocator:

  • based on the idea that processes are likely to request a lot of
  • bjects of the same size: these objects can be kept and reused
  • slabs are collections of objects of the same size
  • sizes (and addresses) are not geometrically distributed

Used by AmigaOS (4), Linux (≥ 2.2) and Solaris (≥ 2.4)

  • J. Bonwick

The Slab Allocator: An Object-Caching Kernel Memory Allocator

  • Proc. 1994 USENIX Annual Tech. Conf
  • pp. 87–98, June 1994, Boston, MA.

Matteo Corti (Informatikdienste, ETH Z¨ urich) Computer Systems Lab 2007-03-19 16 / 24

slide-26
SLIDE 26

Lab: Task

Write a dynamic memory allocator, i.e., implement:

  • malloc
  • free
  • realloc

Goals of the lab

  • understand how a real memory allocator works
  • evaluate different design strategies
  • a nice exercise for low level C programming

Matteo Corti (Informatikdienste, ETH Z¨ urich) Computer Systems Lab 2007-03-19 17 / 24

slide-27
SLIDE 27

Lab: Task

void *mm malloc(size t size)

  • returns a pointer to a memory block of at least size bytes.
  • 8 byte aligned
  • if no allocation is possible returns NULL

void mm free(void *ptr)

  • frees the block pointed by ptr
  • if ptr is not the first address of an allocated block the behavior is

undefined.

Matteo Corti (Informatikdienste, ETH Z¨ urich) Computer Systems Lab 2007-03-19 18 / 24

slide-28
SLIDE 28

Lab: Task

void *mm realloc(void *ptr, size t size)

  • if ptr is NULL: mm malloc(size)
  • if size is 0: mm free(ptr)
  • returns a pointer to a new block of size size and with the same

content of the block pointed by ptr (up to the minimum of the old and new size).

Matteo Corti (Informatikdienste, ETH Z¨ urich) Computer Systems Lab 2007-03-19 19 / 24

slide-29
SLIDE 29

Lab: Provided framework

We provide one of the simple possible implementations:

  • malloc: sequentially allocate a block increasing the top of heap

pointer (brk).

  • free: do nothing
  • realloc: allocate a new block with the requested size

Although this solution is formally correct and very fast it has a very bad space utilization.

Matteo Corti (Informatikdienste, ETH Z¨ urich) Computer Systems Lab 2007-03-19 20 / 24

slide-30
SLIDE 30

Lab: Design

Before beginning think about the design of your memory allocator:

  • Choose the allocation strategy: first fit, best fit, . . .
  • Choose a data structure(s): ordered list, binary tree, . . .
  • Think on how to mark the blocks: in general how to store the

size of the block, the free tag and the eventual pointers to manage the data structure (e.g., next pointer for a list, left, right for tree, . . .)

  • Think about the API of the functions to manage your structure

(flexibility)

  • Choose a time to perform coalescing (merging): immediate

(when putting a block in the free list) or deferred (e.g., when allocation fails)?

Matteo Corti (Informatikdienste, ETH Z¨ urich) Computer Systems Lab 2007-03-19 21 / 24

slide-31
SLIDE 31

Lab: Design

How to choose the correct strategy?

  • analyze the needs (space, speed, predictability)
  • analyze the environment (experimentally)
  • test different strategies

Matteo Corti (Informatikdienste, ETH Z¨ urich) Computer Systems Lab 2007-03-19 22 / 24

slide-32
SLIDE 32

Lab: Block format

Most allocators store information (allocated/free, size, . . .) in the blocks themselves. Example:

payload ptr size flags padding (optional) size linking information flags payload

Linking:

  • implicit: blocks are traversed using the size field only.
  • explicit: linking information is embedded in the block.

Matteo Corti (Informatikdienste, ETH Z¨ urich) Computer Systems Lab 2007-03-19 23 / 24

slide-33
SLIDE 33

Lab: Block format (footers)

Implicit allocators have the problem that to perform coalescing they need information about the previous block.

payload ptr size flags padding (optional) flags size

Knuth [K73] suggested to put a copy of the block header at the end of the block (footer) When freeing a block an implicit allocator can then check the header of the next block and the footer of the previous one. Footers are not necessary for allocated blocks if we store the allocated/free information in the header of the next block.

  • D. E. Knuth

The Art of Computer Programming Addison Wesley, 1973 Chapter 2 of Volume 1, pages 228–463.

Matteo Corti (Informatikdienste, ETH Z¨ urich) Computer Systems Lab 2007-03-19 24 / 24