Docsity

Virtualization Memory, Input/Output - Computer Organization and Design | CIS 371, Study notes of Computer Science

Toggle sidebar
bg1
CIS 371 (Roth/Martin): Virtual Memory & I/O 1
CIS 371
Computer Organization and Design
Unit 10: Virtual Memory & I/O
Rest of the Semester
•!Topics yet to cover
•!Virtual memory
•!Disks & I/O
•!Power & energy
•!Reliability
•!Data-level parallelism (SIMD, Vectors, and GPUs)
•!XBox 360 case study
•!Perennial problem:
•!Only three lectures remaining…
•!By design (we’re actually ā€œon scheduleā€)
•!Will try to hit the highlights, give some context
•!Try not to be overwhelmed
CIS 371 (Roth/Martin): Virtual Memory & I/O 2
CIS 371 (Roth/Martin): Virtual Memory & I/O 3
This Unit: Virtualization & I/O
•!The operating system (OS)
•!A super-application
•!Hardware support for an OS
•!Virtual memory
•!Page tables and address translation
•!TLBs and memory hierarchy issues
•!I/O
•!Magnetic storage (disks)
•!Solid-state storage (flash memory)
CPU Mem I/O
System software
App App App
CIS 371 (Roth/Martin): Virtual Memory & I/O 4
Readings
•!P&H
•!Virtual Memory: 5.4
•!I/O & Disks: 6.1-6.5, 6.8
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Virtualization Memory, Input/Output - Computer Organization and Design | CIS 371 and more Study notes Computer Science in PDF only on Docsity!

CIS 371 (Roth/Martin): Virtual Memory & I/O 1

CIS 371

Computer Organization and Design

Unit 10: Virtual Memory & I/O

Rest of the Semester

•! Topics yet to cover

-! Virtual memory -! Disks & I/O -! Power & energy -! Reliability -! Data-level parallelism (SIMD, Vectors, and GPUs) -! XBox 360 case study

•! Perennial problem:

-! Only three lectures remaining… -! By design (we’re actually ā€œon scheduleā€) -! Will try to hit the highlights, give some context -! Try not to be overwhelmed CIS 371 (Roth/Martin): Virtual Memory & I/O 2

This Unit: Virtualization & I/O

•! The operating system (OS)

-! A super-application -! Hardware support for an OS

•! Virtual memory

-! Page tables and address translation -! TLBs and memory hierarchy issues

•! I/O

-! Magnetic storage (disks) -! Solid-state storage (flash memory) Mem CPU I/O System software App App App

Readings

•! P&H

-! Virtual Memory: 5. -! I/O & Disks: 6.1-6.5, 6.

CIS 371 (Roth/Martin): Virtual Memory & I/O 5

A Computer System: Hardware

•! CPUs and memories

-! Connected by memory bus

•! I/O peripherals : storage, input, display, network, …

-! With separate or built-in DMA -! Connected by system bus (which is connected to memory bus) Memory Disk kbd

DMA DMA

display (^) NIC I/O ctrl Memory bus System (I/O) bus CPU/$ bridge CPU/$ CIS 371 (Roth/Martin): Virtual Memory & I/O 6

A Computer System: + App Software

•! Application software : computer must do something

Memory Disk kbd

DMA DMA

display (^) NIC I/O ctrl Memory bus System (I/O) bus CPU/$ bridge CPU/$ Application sofware

A Computer System: + OS

•! Operating System (OS): virtualizes hardware for apps

-! Abstraction : provides services (e.g., threads, files, etc.) +!Simplifies app programming model, raw hardware is nasty -! Isolation : gives each app illusion of private CPU, memory, I/O +!Simplifies app programming model +!Increases hardware resource utilization Memory Disk kbd

DMA DMA

display (^) NIC I/O ctrl Memory bus System (I/O) bus CPU/$ bridge CPU/$ OS Application Application Application Application

Operating System (OS) and User Apps

•! Sane system development requires a split

-! Hardware itself facilitates/enforces this split

•! Operating System (OS) : a super-privileged process

-! Manages hardware resource allocation/revocation for all processes -! Has direct access to resource allocation features -! Aware of many nasty hardware details -! Aware of other processes -! Talks directly to input/output devices (device driver software)

•! User-level apps : ignorance is bliss

-! Unaware of most nasty hardware details -! Unaware of other apps (and OS) -! Explicitly denied access to resource allocation features

CIS 371 (Roth/Martin): Virtual Memory & I/O 13

Virtualizing Main Memory

•! How do multiple apps (and the OS) share main memory?

-! Goal: each application thinks it has infinite memory

•! One app may want more memory than is in the system

-! App’s insn/data footprint may be larger than main memory -! Requires main memory to act like a cache -! With disk as next level in memory hierarchy (slow) -! Write-back, write-allocate, large blocks or ā€œpagesā€ -! No notion of ā€œprogram not fittingā€ in registers or caches (why?)

•! Solution:

-! Part #1: treat memory as a ā€œcacheā€ -! Store the overflowed blocks in ā€œswapā€ space on disk -! Part #2: add a level of indirection (address translation) CIS 371 (Roth/Martin): Virtual Memory & I/O 14

Virtual Memory (VM)

•! Programs use virtual addresses (VA)

•! 0…2N–

-! VA size also referred to as machine size -! E.g., 32-bit (embedded) or 64-bit (server)

•! Memory uses physical addresses (PA)

-! 0…2M–1 (typically M<N, especially if N=64) -! 2 M^ is most physical memory machine supports

•! VA!PA at page granularity (VP!PP)

-! By ā€œsystemā€ -! Mapping need not preserve contiguity -! VP need not be mapped to any PP -! Unmapped VPs live on disk (swap)

Disk Program Main Memory code heap stack

Virtual Memory (VM)

•! Virtual Memory (VM) :

-! Level of indirection (like register renaming) -! Application generated addresses are virtual addresses (VAs) -! Each process thinks it has its own 2N^ bytes of address space -! Memory accessed using physical addresses (PAs) -! VAs translated to PAs at some coarse granularity -! OS controls VA to PA mapping for itself and all other processes -! Logically: translation performed before every insn fetch, load, store -! Physically: hardware acceleration removes translation overhead …

OS

App … App VAs PAs (physical memory) OS controlled VA!PA mappings

VM is an Old Idea: Older than Caches

•! Original motivation: single-program compatibility

-! IBM System 370: a family of computers with one software suite +! Same program could run on machines with different memory sizes -! Prior, programmers explicitly accounted for memory size

•! But also: full-associativity + software replacement

-! Memory tmiss is high: extremely important to reduce %miss Parameter I$/D$ L2 Main Memory thit 2ns 10ns 30ns tmiss 10ns 30ns 10ms (10M ns) Capacity 8–64KB 128KB–2MB 64MB–64GB Block size 16–32B 32–256B 4+KB Assoc./Repl. 1–4, NMRU 4–16, NMRU Full, ā€œworking setā€

CIS 371 (Roth/Martin): Virtual Memory & I/O 17

Uses of Virtual Memory

•! More recently: isolation and multi-programming

-! Each app thinks it has 2N^ B of memory, its stack starts 0xFFFFFFFF,… -! Apps prevented from reading/writing each other’s memory -! Can’t even address the other program’s memory!

•! Protection

-! Each page with a read/write/execute permission set by OS -! Enforced by hardware

•! Inter-process communication.

-! Map same physical pages into multiple virtual address spaces -! Or share files via the UNIX mmap() call …

OS

App … App CIS 371 (Roth/Martin): Virtual Memory & I/O 18 Disk

Virtual Memory: The Basics

•! Programs use virtual addresses (VA)

-! VA size (N) aka machine size (e.g., Core 2 Duo: 48-bit)

•! Memory uses physical addresses (PA)

-! PA size (M) typically M<N, especially if N= -! 2 M^ is most physical memory machine supports

•! VA!PA at page granularity (VP!PP)

-! Mapping need not preserve contiguity -! VP need not be mapped to any PP -! Unmapped VPs live on disk (swap) or nowhere (if not yet touched) …

OS

App … App

Address Translation

•! VA!PA mapping called address translation

-! Split VA into virtual page number (VPN) & page offset (POFS) -! Translate VPN into physical page number (PPN) -! POFS is not translated -! VA!PA = [VPN, POFS]! [PPN, POFS]

•! Example above

-! 64KB pages! 16-bit POFS -! 32-bit machine! 32-bit VA! 16-bit VPN -! Maximum 256MB memory! 28-bit PA! 12-bit PPN virtual address[31:0] VPN[31:16] POFS[15:0] physical address[25:0] PPN[27:16] POFS[15:0] translate don’t touch

Address Translation Mechanics I

•! How are addresses translated?

-! In software (for now) but with hardware acceleration (a little later)

•! Each process allocated a page table (PT)

-! Software data structure constructed by OS -! Maps VPs to PPs or to disk (swap) addresses -! VP entries empty if page never referenced -! Translation is table lookup struct { int ppn; int is_valid, is_dirty, is_swapped; } PTE; struct PTE page_table[NUM_VIRTUAL_PAGES]; int translate(int vpn) { if (page_table[vpn].is_valid) return page_table[vpn].ppn; } PT vpn Disk(swap)

CIS 371 (Roth/Martin): Virtual Memory & I/O 25

Page-Level Protection

•! Page-level protection

-! Piggy-back page-table mechanism -! Map VPN to PPN + Read/Write/Execute permission bits -! Attempt to execute data, to write read-only data? -! Exception! OS terminates program -! Useful (for OS itself actually) struct { int ppn; int is_valid, is_dirty, is_swapped, permissions; } PTE; struct PTE page_table[NUM_VIRTUAL_PAGES]; int translate(int vpn, int action) { if (page_table[vpn].is_valid && !(page_table [vpn].permissions & action)) kill; … } (^) CIS 371 (Roth/Martin): Virtual Memory & I/O 26

Address Translation Mechanics II

•! Conceptually

-! Translate VA to PA before every cache access -! Walk the page table before every load/store/insn-fetch -! Would be terribly inefficient (even in hardware)

•! In reality

-! Translation Lookaside Buffer (TLB) : cache translations -! Only walk page table on TLB miss

•! Hardware truisms

-! Functionality problem? Add indirection (e.g., VM) -! Performance problem? Add cache (e.g., TLB)

Translation Lookaside Buffer

•! Translation lookaside buffer (TLB)

-! Small cache: 16–64 entries -! Associative (4+ way or fully associative) +! Exploits temporal locality in page table -! What if an entry isn’t found in the TLB? -! Invoke TLB miss handler VPN PPN VPN PPN VPN PPN ā€œtagā€ ā€œdataā€

CPU

D$

L

Main Memory

I$

TLB

VA PA

TLB

Serial TLB & Cache Access

•! ā€œPhysicalā€ caches

-! Indexed and tagged by physical addresses +! Natural, ā€œlazyā€ sharing of caches between apps/OS

•! VM ensures isolation (via physical addresses )

•! No need to do anything on context switches

•! Multi-threading works too

+! Cached inter-process communication works

-! Single copy indexed by physical address -! Slow: adds at least one cycle to thit

•! Note: TLBs are by definition virtual

-! Indexed and tagged by virtual addresses -! Flush across context switches -! Or extend with process id tags

CPU

D$

L

Main Memory

I$

TLB

VA PA

TLB

CIS 371 (Roth/Martin): Virtual Memory & I/O 29

Parallel TLB & Cache Access

•! What about parallel access?

-! What if (cache size) / (associativity)! page size -! Index bits same in virt. and physical addresses!

•! Access TLB in parallel with cache

-! Cache access needs tag only at very end +! Fast: no additional thit cycles +! No context-switching/aliasing problems -! Dominant organization used today

•! Example: Core 2, 4KB pages,

32KB, 8-way SA L1 data cache

-! Implication: associativity allows bigger caches

CPU

D$

L

Main Memory

TLB I$

VA TLB PA tag [31:12] index [11:5] [4:0] VPN [31:16] page offset [15:0] ? PPN[27:16] page offset [15:0] CIS 371 (Roth/Martin): Virtual Memory & I/O 30

TLB Organization

•! Like caches : TLBs also have ABCs

-! Capacity -! Associativity (At least 4-way associative, fully-associative common) -! What does it mean for a TLB to have a block size of two? -! Two consecutive VPs share a single tag -! Like caches : there can be L2 TLBs

•! Example: AMD Opteron

-! 32-entry fully-assoc. TLBs, 512-entry 4-way L2 TLB (insn & data) -! 4KB pages, 48-bit virtual addresses, four-level page table

•! Rule of thumb : TLB should ā€œcoverā€ L2 contents

-! In other words: (#PTEs in TLB) * page size! L2 size -! Why? Think about relative miss latency in each…

TLB Misses

•! TLB miss: translation not in TLB, but in page table

-! Two ways to ā€œfillā€ it, both relatively fast

•! Software-managed TLB : e.g., Alpha

-! Short (~10 insn) OS routine walks page table, updates TLB +! Keeps page table format flexible -! Latency: one or two memory accesses + OS call (pipeline flush)

•! Hardware-managed TLB : e.g., x

-! Page table root in hardware register, hardware ā€œwalksā€ table +! Latency: saves cost of OS call (pipeline flush) -! Page table format is hard-coded

Page Faults

•! Page fault : PTE not in TLB or page table

-!! page not in memory -! Starts out as a TLB miss, detected by OS/hardware handler

•! OS software routine :

-! Choose a physical page to replace -! ā€œWorking setā€ : refined LRU, tracks active page usage -! If dirty, write to disk -! Read missing page from disk -! Takes so long (~10ms), OS schedules another task -! Requires yet another data structure: frame map (why?) -! Treat like a normal TLB miss from here

CIS 371 (Roth/Martin): Virtual Memory & I/O 37

Disk Latency

•! Disk read/write latency has four components

-! Seek delay (tseek) : head seeks to right track -! Average of ~5ms - 15ms -! Less in practice because of shorter seeks) -! Rotational delay (trotation) : right sector rotates under head -! On average: time to go halfway around disk -! Based on rotation speed (RPM) -! 10,000 to 15,000 RPMs -! ~3ms -! Transfer time (ttransfer) : data actually being transferred -! Fast for small blocks -! Controller delay (tcontroller) : controller overhead (on either side) -! Fast (no moving parts)

•! tdisk = tseek + trotation + ttransfer + tcontroller

CIS 371 (Roth/Martin): Virtual Memory & I/O 38

Disk Latency Example

•! Example: time to read a 4KB chunk assuming…

-! 128 sectors/track, 512 B/sector, 6000 RPM, 10 ms tseek, 1 ms tcontroller -! 6000 RPM! 100 R/s! 10 ms/R! trotation = 10 ms / 2 = 5 ms -! 4 KB page! 8 sectors! ttransfer = 10 ms * 8/128 = 0.6 ms -! tdisk = tseek + trotation + ttransfer + tcontroller = 16.6 ms -! tdisk = 10 + 5 + 0.6 + 1 = 16.6 ms

Disk Bandwidth: Sequential vs Random

•! Disk is bandwidth-inefficient for page-sized transfers

-! Sequential vs random accesses

•! Random accesses:

-! One read each disk access latency (~10ms) -! Randomly reading 4KB pages -! 10ms is 0.01 seconds! 100 access per second -! 4KB * 100 access/sec! 400KB/second bandwidth

•! Sequential accesses:

-! Stream data from disk (no seeks) -! 128 sectors/track, 512 B/sector, 6000 RPM -! 64KB per rotation, 100 rotation/per sec -! 6400KB/sec! 6.4MB/sec

•! Sequential access is ~10x or more bandwidth than random

-! Still no where near the 1GB/sec to 10GB/sec of memory

Some (Old) Example Disks (Hitachi)

•! Flash : non-volatile CMOS storage

-! The ā€œnew diskā€: replacing disk in many Ultrastar Travelstar Microdrive Diameter 3.5ā€ 2.5ā€ 1.0ā€ Capacity 300 GB 40 GB 4 GB Cache 8 MB 2 MB 128KB RPM 10,000 RPM 4200 RPM 3600 RPM Seek 4.5 ms 12 ms 12 ms Sustained Data Rate 100 MB/s 40 MB/s 10 MB/s Cost $450 $120 $ Use Desktop Notebook some iPods

CIS 371 (Roth/Martin): Virtual Memory & I/O 41

Increasing Disk Performance

•! Software can help

-! More sequential seeks (layout files on disk intelligently)

•! Raw latency/bandwidth determined by technology

-! Mechanical disks (~5ms seek time), rotation time -! Higher RPMs help bandwidth (but not really latency)

•! Bandwidth is mostly determined by cost

-! ā€œYou can always buy bandwidthā€ -! Disk arrays : buy more disks, stripe data across multiple disks -! Increases both sequential and random access bandwidth -! Use the extra resources in parallel -! Yet another example of parallelism CIS 371 (Roth/Martin): Virtual Memory & I/O 42

Designing an I/O System for Bandwidth

•! Approach

-! Find bandwidths of individual components -! Configure components you can change… -! To match bandwidth of bottleneck component you can’t -! Caveat: real I/O systems modeled with simulation

•! Example parameters

-! 300 MIPS CPU, 150K insns per I/O operation, random reads -! I/O bus: 200 MB/s Disk controllers: 30 MB/s, up to 7 disks each -! Workload has multiple tasks to keep CPUs busy during I/O

•! Determine

-! What is the access time for the random access of a specific size? -! 4KB, 64KB, 256KB -! What is the maximum sustainable I/O rate for random accesses? -! How many disk controllers and disks does it require?

Disk Access Time Calculation

•! Time to read a random chunk assuming…

-! 1024 sectors/track, 512 B/sector, 12000 RPM -! tseek is 6.5 ms, tcontroller is 0.375 ms

•! First, calculate trotation

-! 12000 RPM! 200 R/s! 5 ms/R! trotation = 5 ms / 2 = 2.5 ms

•! Next, calculate ttransfer

-! 4KB read! 8 sectors! ttransfer = 5 ms * 8/1024 = 0.039 ms -! 64KB read! 128 sectors! ttransfer = 5 ms * 128/1024 = 0.625 ms -! 256KB read! 512 sectors! ttransfer = 5 ms * 512/1024 = 2.5 ms

•! Finally, tdisk = tseek + trotation + ttransfer + tcontroller

-! tdisk for 4KB = 6.5 + 2.5 + 0.039 + 0.375 = 9.4 ms -! tdisk for 64KB = 6.5 + 2.5 + 0.625 + 0.375 = 10 ms -! tdisk for 256KB = 6.5 + 2.5 + 2.5 + 0.375 = 11.9 ms

Calculation for 4KB Random Reads

•! First: determine I/O rates of components we can’t change

-! CPU: (300M insn/s) / (150K Insns/IO) = 2000 IO/s -! I/O bus: (200MB/s) / ( 4K B/IO) = 51200 IO/s -! Peak I/O rate determined by cpu: 2000 IO/s -! 2000 IO/s * 4KB per IO = 7.8MB/s

•! Second: configure remaining components to match rate

-! Disk: 9.4 ms is 0.0094 sec, so each disk is 106 IO/s & 0.41MB/sec -! How many disks? -! (2000 IO/s) / (106 IO/s) = 19 disks -! How many controllers? -! At 0.4 MB/sec per disk, 75 disks max for each 30MB/s controller -! But each controller can only support 7 disks… -! Thus, for 19 disks, we need 3 controllers