Memory Management Disclaimer: some slides are adopted from book - - PowerPoint PPT Presentation

memory management
SMART_READER_LITE
LIVE PREVIEW

Memory Management Disclaimer: some slides are adopted from book - - PowerPoint PPT Presentation

Memory Management Disclaimer: some slides are adopted from book authors slides with permission 1 Recap: Page Replacement On a page fault Step 1: allocate a free page frame If theres a free frame, use it If theres no free


slide-1
SLIDE 1

Memory Management

1

Disclaimer: some slides are adopted from book authors’ slides with permission

slide-2
SLIDE 2

Recap: Page Replacement

  • On a page fault

– Step 1: allocate a free page frame

  • If there’s a free frame, use it
  • If there’s no free frame, choose a victim frame and

evict it to disk (if necessary)  swap-out

– Step 2: bring the stored page on disk (if necessary) – Step 3: update the PTE (mapping and valid bit) – Step 4: restart the instruction

2

slide-3
SLIDE 3

Recap: Page Replacement Policies

  • FIFO

– Evict the oldest page first. Pros: fair; Cons: can throw out frequently used pages

  • Optimal

– Evict the page that will not be used for the longest period – Pros: optimal; Cons: you need to know the future

  • Random

– Randomly choose a page. Pros: simple. TLB commonly uses this method; Cons: unpredictable

  • LRU

– Look at the past history, choose the one that has not been used for the longest period. Pros: good performance; Cons: complex, requires h/w support

3

slide-4
SLIDE 4

Recap: Page Table Entry (PTE)

  • PTE format (architecture specific)

– Valid bit (V): whether the page is in memory – Modify bit (M): whether the page is modified – Reference bit (R): whether the page is accessed – Protection bits(P): readable, writable, executable

4

Page Frame No V M R P 20 bits 2 1 1 1

slide-5
SLIDE 5

Recap: Clock Algorithm

  • Key idea

– Replace an old page, not the

  • ldest page
  • Algorithm

– Step 1: advance the pointer by one – Step 2: check the reference bit of the page: 1  Used recently. Clear the bit and go to Step 1 0  Not used recently. Selected victim. End.

5

slide-6
SLIDE 6

Quiz.

Reference E D H B D E D A E B E Page #1 E E E B B B B A A A A Page #2 D D Page #3 H Mark X for a fault X X X

6

  • Complete the following with the FIFO

replacement policy

slide-7
SLIDE 7

Quiz.

7

  • Complete the following with the FIFO

replacement policy

Reference E D H B D E D A E B E Page #1 E E E B B B B A A A A Page #2 D D D * E E E * B B Page #3 H H H H D D D D E Mark X for a fault X X X X X X X X X

slide-8
SLIDE 8

Concepts to Learn

  • Thrashing
  • Memory-mapped I/O
  • Copy-on-Write (COW)
  • Memory allocator

8

slide-9
SLIDE 9

Recap: Performance of Demand Paging

  • Memory access time = 200 nanoseconds
  • Average page-fault service time = 8 milliseconds
  • EAT = (1 – p) x 200 + p (8 milliseconds)

= (1 – p) x 200 + p x 8,000,000 = 200 + p x 7,999,800

  • If one access out of 1,000 causes a page fault, then

EAT = 8.2 microseconds.  This is a slowdown by a factor of 40!!

  • If want performance degradation < 10 percent

– 220 > 200 + 7,999,800 x p 20 > 7,999,800 x p – p < .0000025 – < one page fault in every 400,000 memory accesses

9

slide-10
SLIDE 10

Thrashing

  • A processes is busy swapping pages in and out

– Don’t make much progress – Happens when a process do not have “enough” pages in memory – Very high page fault rate – Low CPU utilization (why?) – CPU utilization based admission control may bring more programs to increase the utilization  more page faults

10

slide-11
SLIDE 11

Thrashing

11

slide-12
SLIDE 12

Recap: Program Binary Sharing

  • Multiple instances of the same program

– E.g., 10 bash shells

12

Bash text Physical memory Bash #1 Bash #2

slide-13
SLIDE 13

Memory Mapped I/O

  • Idea: map a file on disk onto the memory space

13

slide-14
SLIDE 14

Memory Mapped I/O

  • Benefits: you don’t need to use read()/write() system

calls, just directly access data in the file via memory instructions

  • How it works?

– Just like demand paging of an executable file – What about writes?

  • Mark the modified (M) bit in the PTE
  • Write back the modified pages back to the original file

14

slide-15
SLIDE 15

Copy-on-Write (COW)

  • Fork() creates a copy of a parent process

– Copy the entire pages on new page frames?

  • If the parent uses 1GB memory, then a fork() call would

take a while

  • Then, suppose you immediately call exec(). Was it of

any use to copy the 1GB of parent process’s memory?

15

slide-16
SLIDE 16

Copy-on-Write

  • Better way: copy the page table of the parent

– Page table is much smaller (so copy is faster) – Both parent and child point to the exactly same physical page frames

16

parent child

slide-17
SLIDE 17

Copy-on-Write

  • What happens when the parent/child reads?
  • What happens when the parent/child writes?

– Trouble!!!

17

parent child

slide-18
SLIDE 18

Page Table Entry (PTE)

  • PTE format (architecture specific)

– Valid bit (V): whether the page is in memory – Modify bit (M): whether the page is modified – Reference bit (R): whether the page is accessed – Protection bits(P): readable, writable, executable

18

Page Frame No V M R P 20 bits 2 1 1 1

slide-19
SLIDE 19

Copy-on-Write

  • All pages are marked as read-only

19

parent child RO RO RO Page tbl RO RO RO Page tbl

slide-20
SLIDE 20

Copy-on-Write

  • Up on a write, a page fault occurs and the OS copies

the page on a new frame and maps to it with R/W protection setting

20

parent child RO RO RW Page tbl RO RO RO Page tbl

slide-21
SLIDE 21

User-level Memory Allocation

  • When a process actually

allocate a memory from the kernel?

– On a page fault – Allocate a page (e.g., 4KB)

  • What does malloc() do?

– Manage a process’s heap – Variable size objects in heap

21

slide-22
SLIDE 22

Kernel-level Memory Allocation

  • Page-level allocator

– Page frame allocation/free (fixed size) – Users: page fault handler, kernel-memory allocator

  • Kernel-memory allocator (KMA)

– Typical kernel object size << page size

  • File descriptor, inode, task_struct, …

– KMA  kernel-level malloc – In Linux: buddy allocator, SLAB

22

slide-23
SLIDE 23

Buddy Allocator

  • Allocate physically contiguous pages

– Satisfies requests in units sized as power of 2 – Request rounded up to next highest power of 2 – When smaller allocation needed than is available, current chunk split into two buddies of next-lower power of 2 – Quickly expand/shrink across the lists

23

4KB 8KB 16KB 32KB

slide-24
SLIDE 24

Buddy Allocator

  • Example

– Assume 256KB chunk available, kernel requests 21KB

24

32 A 32 Free 64 Free 128 Free 32 Free 64 Free 128 Free 32 Free 64 Free 128 Free 64 Free 128 Free 128 Free 256 Free

slide-25
SLIDE 25

Buddy Allocator

  • Example

– Free A

25

32 A 32 Free 64 Free 128 Free 32 Free 64 Free 128 Free 32 Free 64 Free 128 Free 64 Free 128 Free 128 Free 256 Free

slide-26
SLIDE 26

Virtual Memory Summary

  • MMU and address translation
  • Paging
  • Demand paging
  • Copy-on-write
  • Page replacement

26

slide-27
SLIDE 27

Quiz: Address Translation

27

2nd level

  • ffset

8 bits 8 bits

1st level

8 bits Virtual address format (24bits) Frame #

V

4 bits 3

Unused

1 Page table entry (8bit)

Addr +0 +1 +2 +3 +4 +5 +6 +7 +8 +A +B +C +D +E +F 0x000 31 0x010 0x020 41 .. 0x100 00 01 01 00 01 .. 0x200

Page-table base address = 0x100 Vaddr: 0x0703FE Paddr: 0x3FE Vaddr: 0x072370 Paddr: ??? Vaddr: 0x082370 Paddr: ???

slide-28
SLIDE 28

Quiz: Address Translation

28

2nd level

  • ffset

8 bits 8 bits

1st level

8 bits Virtual address format (24bits) Frame #

V

4 bits 3

Unused

1 Page table entry (8bit)

Addr +0 +1 +2 +3 +4 +5 +6 +7 +8 +A +B +C +D +E +F 0x000 31 0x010 0x020 41 .. 0x100 00 01 01 00 01 .. 0x200

Page-table base address = 0x100 Vaddr: 0x0703FE Paddr: 0x3FE Vaddr: 0x072370 Paddr: 0x470 Vaddr: 0x082370 Paddr: invalid