Paging (continued) & Caching CS-3013 A-term 20081 Paging (continued) & Caching CS-3013 Operating...

26
Paging (continued ) & Caching CS-3013 A-term 200 8 1 Paging (continued) & Caching CS-3013 Operating Systems A-term 2008 (Slides include materials from Modern Operating Systems, 3 rd ed., by Andrew Tanenbaum and from Operating System Concepts, 7 th ed., by Silbershatz, Galvin, & Gagne)

Transcript of Paging (continued) & Caching CS-3013 A-term 20081 Paging (continued) & Caching CS-3013 Operating...

Page 1: Paging (continued) & Caching CS-3013 A-term 20081 Paging (continued) & Caching CS-3013 Operating Systems A-term 2008 (Slides include materials from Modern.

Paging (continued) & Caching

CS-3013 A-term 2008 1

Paging (continued) &Caching

CS-3013 Operating SystemsA-term 2008

(Slides include materials from Modern Operating Systems, 3rd ed., by Andrew Tanenbaum and from Operating System Concepts, 7th ed., by Silbershatz, Galvin, & Gagne)

Page 2: Paging (continued) & Caching CS-3013 A-term 20081 Paging (continued) & Caching CS-3013 Operating Systems A-term 2008 (Slides include materials from Modern.

Paging (continued) & Caching

CS-3013 A-term 2008 2

Review – Paging

• Partition virtual memory into small fixed size units called pages

• Typically 512-8192 bytes

• Translate every memory address on-the-fly from virtual to physical address

• Allow process to execute, even if not all pages are in physical memory

Page 3: Paging (continued) & Caching CS-3013 A-term 20081 Paging (continued) & Caching CS-3013 Operating Systems A-term 2008 (Slides include materials from Modern.

Paging (continued) & Caching

CS-3013 A-term 2008 3

Definition — Page Fault

• Trap when process attempts to reference a location in virtual memory whose virtual address cannot be translated

• I.e., valid bit in PTE set to false

• I.e., reference violates protection bits in PTE

• OS takes appropriate action, restarts process at point of fault

Page 4: Paging (continued) & Caching CS-3013 A-term 20081 Paging (continued) & Caching CS-3013 Operating Systems A-term 2008 (Slides include materials from Modern.

Paging (continued) & Caching

CS-3013 A-term 2008 4

PTE Structure

• Valid bit gives state of this PTE– says whether or not a virtual address is valid – in memory and VA range– If not set, page might not be in memory or may not even exist!

• Reference bit says whether the page has been accessed– it is set by hardware whenever a page has been read or written to

• Modify bit says whether or not the page is dirty– it is set by hardware during every write to the page

• Protection bits control which operations are allowed– read, write, execute, etc.

• Page frame number (PFN) determines the physical page– physical page start address

• Other bits dependent upon machine architecture

page frame numberprotMRV

202111

Page 5: Paging (continued) & Caching CS-3013 A-term 20081 Paging (continued) & Caching CS-3013 Operating Systems A-term 2008 (Slides include materials from Modern.

Paging (continued) & Caching

CS-3013 A-term 2008 5

Paging – Advantages

• Easy to allocate physical memory• pick any free frame

• No external fragmentation• All frames are equal

• Minimal internal fragmentation• Bounded by page/frame size

• Easy to swap out pages (called pageout)• Size is usually a multiple of disk blocks• PTEs may contain info that help reduce disk traffic

• Processes can run with not all pages swapped in

Page 6: Paging (continued) & Caching CS-3013 A-term 20081 Paging (continued) & Caching CS-3013 Operating Systems A-term 2008 (Slides include materials from Modern.

Paging (continued) & Caching

CS-3013 A-term 2008 6

Definitions & Recurring Themes

• Temporal Locality – locations referenced recently tend to be referenced again soon

• Spatial Locality – locations near recent references tend to be referenced soon

• Working set – the set of pages that a process needs to run without frequent page faults

• Thrashing – excessive page faulting due to insufficient frames to support working set

Page 7: Paging (continued) & Caching CS-3013 A-term 20081 Paging (continued) & Caching CS-3013 Operating Systems A-term 2008 (Slides include materials from Modern.

Paging (continued) & Caching

CS-3013 A-term 2008 7

Paging Issues

• #1 — Page Tables can consume large amounts of space– If PTE is 4 bytes, and use 4KB pages, and have 32 bit VA space

4MB for each process’s page table• Stored in main memory!

– What happens for 64-bit logical address spaces?

• #2 — Performance Impact– Converting virtual to physical address requires multiple operations

to access memory • Read Page Table Entry from memory!• Get page frame number• Construct physical address • Assorted protection and valid checks

– Without fast hardware support, requires multiple memory accesses and a lot of work per logical address

Page 8: Paging (continued) & Caching CS-3013 A-term 20081 Paging (continued) & Caching CS-3013 Operating Systems A-term 2008 (Slides include materials from Modern.

Paging (continued) & Caching

CS-3013 A-term 2008 8

Paging Issues

• #1 — Page Tables can consume large amounts of space– If PTE is 4 bytes, and use 4KB pages, and have 32 bit VA space

4MB for each process’s page table• Stored in main memory!

– What happens for 64-bit logical address spaces?

• #2 — Performance Impact– Converting virtual to physical address requires multiple operations

to access memory • Read Page Table Entry from memory!• Get page frame number• Construct physical address • Assorted protection and valid checks

– Without fast hardware support, requires multiple memory accesses and a lot of work per logical address

Two-level or inverted

page tables

Translation Lookaside

Buffer (TLB)

Page 9: Paging (continued) & Caching CS-3013 A-term 20081 Paging (continued) & Caching CS-3013 Operating Systems A-term 2008 (Slides include materials from Modern.

Paging (continued) & Caching

CS-3013 A-term 2008 9

Paging Tricks

• Shared Virtual Memory• Pages of two or more processes point to same page

frames

• E.g. shared libraries

• Copy-on-write• Processes share virtual memory

• Writable pages marked read-only

• On page fault, make separate writable copies

Page 10: Paging (continued) & Caching CS-3013 A-term 20081 Paging (continued) & Caching CS-3013 Operating Systems A-term 2008 (Slides include materials from Modern.

Paging (continued) & Caching

CS-3013 A-term 2008 10

Definition — Backing Storage

• A place on external storage medium where copies of virtual pages are kept

• Usually a hard disk (locally or on a server)

• Place from which contents of pages are read after page faults

• Place where contents of pages are written if page frames need to be re-allocated

Page 11: Paging (continued) & Caching CS-3013 A-term 20081 Paging (continued) & Caching CS-3013 Operating Systems A-term 2008 (Slides include materials from Modern.

Paging (continued) & Caching

CS-3013 A-term 2008 11

Backing Storage

• Executable code and constants:–• The loadable image file produced by compiler

• Working memory (stack, heap, static data)• Special swapping file (or partition) on disk

• Persistent application data• Application data files on local or server disks

Page 12: Paging (continued) & Caching CS-3013 A-term 20081 Paging (continued) & Caching CS-3013 Operating Systems A-term 2008 (Slides include materials from Modern.

Paging (continued) & Caching

CS-3013 A-term 2008 12

Definition — Mapping

• Mapping (a part of) virtual memory to a file =

• Connecting blocks of the file to the virtual memory pages so that– Page faults in (that part of) virtual memory

resolve to the blocks of the file– Dirty pages in (that part of) virtual memory are

written back to the blocks of the file

Page 13: Paging (continued) & Caching CS-3013 A-term 20081 Paging (continued) & Caching CS-3013 Operating Systems A-term 2008 (Slides include materials from Modern.

Paging (continued) & Caching

CS-3013 A-term 2008 13

Warning — Memory-mapped Files

• Tempting idea:–– Instead of standard I/O calls (read(), write(),

etc.) map file starting at virtual address X– Access to “X + n” refers to file at offset n– Use paging mechanism to read file blocks into

physical memory on demand … – … and write them out as needed

• Has been tried many times• Rarely works very well in practice

Page 14: Paging (continued) & Caching CS-3013 A-term 20081 Paging (continued) & Caching CS-3013 Operating Systems A-term 2008 (Slides include materials from Modern.

Paging (continued) & Caching

CS-3013 A-term 2008 14

Warning — Memory-mapped Files

• Tempting idea:–– Instead of standard I/O calls (read(), write(),

etc.) map file starting at virtual address X– Access to “X + n” refers to file at offset n– Use paging mechanism to read file blocks into

physical memory on demand … – … and write them out as needed

• Has been tried many times• Rarely works well in practice

Hard to program; even

Harder to manage performance

Page 15: Paging (continued) & Caching CS-3013 A-term 20081 Paging (continued) & Caching CS-3013 Operating Systems A-term 2008 (Slides include materials from Modern.

Paging (continued) & Caching

CS-3013 A-term 2008 15

Virtual Memory Organization

0x00000000

0xFFFFFFFF

Virtual

address space

program code(text)

static data

heap(dynamically allocated)

stack(dynamically allocated)

PC

SP

From an

earlier to

pic

Page 16: Paging (continued) & Caching CS-3013 A-term 20081 Paging (continued) & Caching CS-3013 Operating Systems A-term 2008 (Slides include materials from Modern.

Paging (continued) & Caching

CS-3013 A-term 2008 16

Virtual Memory Organization

program code& constants

static data

heap(dynamically allocated)

stack(dynamically allocated)

PC

SP

Mapped to read-only executable file

Map to writable swap file

Map to writable swap file

Map to writable swap file

Unmapped, may be dynamically mapped

guard page (never mapped)

Page 17: Paging (continued) & Caching CS-3013 A-term 20081 Paging (continued) & Caching CS-3013 Operating Systems A-term 2008 (Slides include materials from Modern.

Paging (continued) & Caching

CS-3013 A-term 2008 17

thread 3 stack

Virtual Memory Organization (continued)

program code& constants

static data

heap

thread 1 stack

PC (T2)

SP (T2)thread 2 stack

SP (T1)

SP (T3)

PC (T3)

guard page (never mapped)

guard page

guard page

Mapped to read-only executable file

Map to writable swap file

Unmapped, may be dynamically mapped

Map to writable swap file

Map to writable swap file

Page 18: Paging (continued) & Caching CS-3013 A-term 20081 Paging (continued) & Caching CS-3013 Operating Systems A-term 2008 (Slides include materials from Modern.

Paging (continued) & Caching

CS-3013 A-term 2008 18

Virtual Memory Organization (continued)

• Each area of process VM is its own segment (or sequence of segments)

• Executable code & constants – fixed size

• Shared libraries

• Static data – fixed size

• Heap – open-ended

• Stacks – each open-ended

• Other segments as needed– E.g., symbol tables, loader information, etc.

Page 19: Paging (continued) & Caching CS-3013 A-term 20081 Paging (continued) & Caching CS-3013 Operating Systems A-term 2008 (Slides include materials from Modern.

Paging (continued) & Caching

CS-3013 A-term 2008 19

Virtual Memory – a Major OS Abstraction

• Processes execute in virtual memory, not physical memory

• Virtual memory may be very large• Much larger than physical memory

• Virtual memory may be organized in interesting & useful ways

• Open-ended segments

• Virtual memory is where the action is!• Physical memory is just a cache of virtual memory

Page 20: Paging (continued) & Caching CS-3013 A-term 20081 Paging (continued) & Caching CS-3013 Operating Systems A-term 2008 (Slides include materials from Modern.

Paging (continued) & Caching

CS-3013 A-term 2008 20

Reading Assignment

• Tanenbaum– §§ 3.1-3.2 (previous topic – Memory

Management)

– § 3.3 (this topic on Paging)

Page 21: Paging (continued) & Caching CS-3013 A-term 20081 Paging (continued) & Caching CS-3013 Operating Systems A-term 2008 (Slides include materials from Modern.

Paging (continued) & Caching

CS-3013 A-term 2008 21

Related topic – Caching

Page 22: Paging (continued) & Caching CS-3013 A-term 20081 Paging (continued) & Caching CS-3013 Operating Systems A-term 2008 (Slides include materials from Modern.

Paging (continued) & Caching

CS-3013 A-term 2008 22

Definition — Cache

• A small subset of active items held in fast storage while most of the items are in much larger, slower storage– Virtual Memory

• Very large, mostly stored on (slow) disk

• Small working set in (fast) RAM during execution

– Page tables• Very large, mostly stored in (slow) RAM

• Small working set stored in (fast) TLB registers

Page 23: Paging (continued) & Caching CS-3013 A-term 20081 Paging (continued) & Caching CS-3013 Operating Systems A-term 2008 (Slides include materials from Modern.

Paging (continued) & Caching

CS-3013 A-term 2008 23

Caching is Ubiquitous in Computing

• Transaction processing• Keep records of today’s departures in RAM while records of

future flights are on disk

• Program execution• Keep the bytes near the current program counter in on-chip

memory while rest of program is in RAM

• File management• Keep disk maps of open files in RAM while retaining maps of

all files on disk

• Game design• Keep nearby environment in cache of each character

• …

Page 24: Paging (continued) & Caching CS-3013 A-term 20081 Paging (continued) & Caching CS-3013 Operating Systems A-term 2008 (Slides include materials from Modern.

Paging (continued) & Caching

CS-3013 A-term 2008 24

Caching issues

• When to put something in the cache• What to throw out to create cache space for new

items• How to keep cached item and stored item in sync

after one or the other is updated• How to keep multiple caches in sync across

processors or machines• Size of cache needed to be effective• Size of cache items for efficiency• …

Page 25: Paging (continued) & Caching CS-3013 A-term 20081 Paging (continued) & Caching CS-3013 Operating Systems A-term 2008 (Slides include materials from Modern.

Paging (continued) & Caching

CS-3013 A-term 2008 25

Caching issues

• When to put something in the cache• What to throw out to create cache space for new

items• How to keep cached item and stored item in sync

after one or the other is updated• How to keep multiple caches in sync across

processors or machines• Size of cache needed to be effective• Size of cache items for efficiency• …

This is a very

important list!

Page 26: Paging (continued) & Caching CS-3013 A-term 20081 Paging (continued) & Caching CS-3013 Operating Systems A-term 2008 (Slides include materials from Modern.

Paging (continued) & Caching

CS-3013 A-term 2008 26

Questions?