Advanced Memory Management Multiple processes Paging Segmentation with





























































- Slides: 61
Advanced Memory Management • Multiple processes – Paging – Segmentation with Paging – Virtual Memory • Related topics Operating Systems – a Spiral Approach – Elmasri, Carrick & Levine – Copyright 2010 11. 1
What is the problem? • Multiprocessing causes external fragmentation • Compaction wastes resources • Solution – break RAM into fixed parts • Do not need to be contiguous • Map from logical to physical spaces • Need hardware help Operating Systems – a Spiral Approach – Elmasri, Carrick & Levine – Copyright 2010 11. 2
Paging • Physical Memory divided into frames – typically 4 KB – size dictated by hardware • Logical space divided into pages – same size as frames • CPU generates logical addresses • MMU maps to physical addresses • Page Table holds the map Operating Systems – a Spiral Approach – Elmasri, Carrick & Levine – Copyright 2010 11. 3
Logical Addresses • Running process generates • Hardware treats as 2 parts: – displacement in the page – d – number of the page – p • Displacement unchanged • Map page number to frame number – through the page table Operating Systems – a Spiral Approach – Elmasri, Carrick & Levine – Copyright 2010 11. 4
Dynamic Page Mapping CPU Page Table Address Register Logical address Physical address p d f d + p Memory f Page Table in RAM Operating Systems – a Spiral Approach – Elmasri, Carrick & Levine – Copyright 2010 11. 5
Mapping • Page table address register • Page table length register or fixed length table • Mapping transparent to program • Pages can be anywhere • No external fragmentation • Still internal fragmentation Operating Systems – a Spiral Approach – Elmasri, Carrick & Levine – Copyright 2010 11. 6
Dynamic Page Mapping Logical Address Space A B C D E F G H Page Table Per Process 3 5 7 1 4 2 6 8 D F A E B G C H Physical Address Space Operating Systems – a Spiral Approach – Elmasri, Carrick & Levine – Copyright 2010 11. 7
Dual Memory Access • • RAM access must hit page table 1 st Access memory twice! Unacceptable Solution - cache the page table Translation Lookaside Buffer - TLB – aka content addressable memory - CAM – aka associative memory – lookup in parallel by content – returns frame number Operating Systems – a Spiral Approach – Elmasri, Carrick & Levine – Copyright 2010 11. 8
TLB CPU Logical address Physical address p d f d Memory TLB p p f f Page Table in RAM Operating Systems – a Spiral Approach – Elmasri, Carrick & Levine – Copyright 2010 11. 9
Effective Access Time Memory reference may be in TLB - TLB hit – may not be - TLB miss A = % hits - hit ratio M = Memory access time E = TLB lookup time Effective memory access time - EAT = A (E + M) + (1 – A)(2 M) Operating Systems – a Spiral Approach – Elmasri, Carrick & Levine – Copyright 2010 11. 10
Effective Access Time • Want TLB high hit >. 99999 • TLB lookup may not be parallel with page table access. EAT becomes EAT = A (E + M) + (1 – A)(E + 2 M) • All jobs have same logical space context switch must flush TLB Operating Systems – a Spiral Approach – Elmasri, Carrick & Levine – Copyright 2010 11. 11
Memory Access Control • Relocation register – used with limit register • Page table needs limits – Fixed table size • Valid Bit • Turned on for pages in limit – Variable table size • Page Table Limit Register Operating Systems – a Spiral Approach – Elmasri, Carrick & Levine – Copyright 2010 11. 12
Page Access Protection • Page entries can contain other bits – checked by hardware • Read Only • Execute Only • Compiler/ Assembler/ Linker – must put things on page boundaries Operating Systems – a Spiral Approach – Elmasri, Carrick & Levine – Copyright 2010 11. 13
Page Table Size • Page tables get huge – Also sparse (see why later) • Have to be contiguous • Memory management problem back • Page the page tables! Operating Systems – a Spiral Approach – Elmasri, Carrick & Levine – Copyright 2010 11. 14
Two-level Page Table Logical address Physical address f d p 1 p 2 d CPU Page Table Address Register Memory p 2 f p 1 First-Level Page Table Operating Systems – a Spiral Approach – Elmasri, Carrick & Levine – Copyright 2010 Second-Level Page Table 11. 15
Multi-level Page Tables • • Can go up to 4 levels Can require 5 memory references! TLB still saves us TLB sometimes managed by software – interrupt on cache miss Operating Systems – a Spiral Approach – Elmasri, Carrick & Levine – Copyright 2010 11. 16
Inverted Page Table • • • Memory was very expensive Still in descendant systems Rarely used in new designs Must store process number Use hashing to find entry TLB makes performance OK Operating Systems – a Spiral Approach – Elmasri, Carrick & Levine – Copyright 2010 11. 17
Inverted Page Table Memory Physical address Logical address p d CPU f d i i p Process ID Search i p Operating Systems – a Spiral Approach – Elmasri, Carrick & Levine – Copyright 2010 11. 18
Multiple page sizes • Older designs used fixed page size • Some now allow for different sizes – 2 sizes – one large and one small • large for OS Kernel • small for applications – Many sizes – not as common • may be used for segmentation with paging Operating Systems – a Spiral Approach – Elmasri, Carrick & Levine – Copyright 2010 11. 19
Single process paging • • Paging once used with single process OS Program larger than physical memory Paged on demand from drum storage Current designs with multiprocessing Operating Systems – a Spiral Approach – Elmasri, Carrick & Levine – Copyright 2010 11. 20
Segmentation • Alternative to paging • Programmer friendly – Modules, tables, objects, stack, … • Each gets separate address space • Compilers (etc. ) must be aware Operating Systems – a Spiral Approach – Elmasri, Carrick & Levine – Copyright 2010 11. 21
Process Segments 4 - Subroutine A Original Program 5 - Subroutine B Code Segment Data Segment Stack Segment Heap Segment 3 - Stack Segment 1 - Code Segment Heap Segment 2 - Data Segment Language Library Main Memory Operating Systems – a Spiral Approach – Elmasri, Carrick & Levine – Copyright 2010 11. 22
Segmentation • Segment table maps to physical RAM – segment # =►start of segment – offset within segment • Segments can be read only, etc • Still has external fragmentation • Still has internal fragmentation Operating Systems – a Spiral Approach – Elmasri, Carrick & Levine – Copyright 2010 11. 23
Segment Table CPU Logical address s d Physical address Data Segment -> data -> code -> stack Memory + Subroutine A Subroutine B Code Segment -> Subroutine A -> Subroutine B Operating Systems – a Spiral Approach – Elmasri, Carrick & Levine – Copyright 2010 Stack Segment 11. 24
Segmentation with Paging • Segmentation by compilers, etc. not visible to programmer • Can page the segments • Still have fine control • Get rid of external fragmentation Operating Systems – a Spiral Approach – Elmasri, Carrick & Levine – Copyright 2010 11. 25
Segmentation with Paging Memory Logical address s CPU p d Physical address -> data -> code -> stack -> Subr. A Segment Table -> Subr. B Per Process + p f Page Table per Segment Operating Systems – a Spiral Approach – Elmasri, Carrick & Levine – Copyright 2010 11. 26
Segmentation with Linear Addressing Memory Logical address CPU s p Physical address f d p d f -> data -> code -> stack -> Subr. A Segment Table -> Subr. B Per Process Page Table per Process Operating Systems – a Spiral Approach – Elmasri, Carrick & Levine – Copyright 2010 11. 27
Demand Paging • Programs don’t use RAM randomly • Locality of reference – Temporal – Spatial • 80/20 rule – 20 % of the program – does 80 % of the work Operating Systems – a Spiral Approach – Elmasri, Carrick & Levine – Copyright 2010 11. 28
Demand Paging • Don’t load things until referenced • Can run programs > physical RAM • Change the Valid bit meaning – now means page not in RAM – page fault interrupt – first insure page # is valid – if so, load the page, update table • AKA Virtual Memory Operating Systems – a Spiral Approach – Elmasri, Carrick & Levine – Copyright 2010 11. 29
Pages Not In RAM 1 2 3 4 5 6 7 8 A B C D E Logical Address Space Valid bit End of Logical Space 2 0 7 0 0 0 1 0 0 0 Page Table in RAM Operating Systems – a Spiral Approach – Elmasri, Carrick & Levine – Copyright 2010 Not in RAM Not in address space A C 1 2 3 4 5 6 7 8 Physical Address Space 11. 30
EAT with Demand Paging Component Relative Speed TLB lookup 1 nanosecond Memory access 100 nanoseconds Disk write (dirty page) 20 milliseconds Disk read 20 milliseconds • EAT is dominated by the disk • Can burn lots of CPU to avoid a disk hit Operating Systems – a Spiral Approach – Elmasri, Carrick & Levine – Copyright 2010 11. 31
The Working Set Working set Page reference string: 12157163716427 Operating Systems – a Spiral Approach – Elmasri, Carrick & Levine – Copyright 2010 1 12 12 1257 1567 1367 1467 1246 2467 1 2 3 4 5 6 7 8 9 10 11 12 13 14 Event Number 11. 32
Page Replacement • What if out of frames? • Have to discard one • May have pages no longer used Working Set - those in use • How to identify unneeded? • Least Recently Used page Operating Systems – a Spiral Approach – Elmasri, Carrick & Levine – Copyright 2010 11. 33
Optimal page replacement • Dump the page whose next use is furthest in the future • Future is unknown • Optimum, so • Used for comparison Operating Systems – a Spiral Approach – Elmasri, Carrick & Levine – Copyright 2010 11. 34
Least Recently Used • • Would be our best guess Impractical to track Can approximate Need hardware assistance – Page reference bit in page table Operating Systems – a Spiral Approach – Elmasri, Carrick & Levine – Copyright 2010 11. 35
Clock Algorithm Page reference bit Valid bit R/O 1 0 • Periodically clear the reference bit - aka access bit or use bit • Look for pages in RAM (valid) but not referenced Operating Systems – a Spiral Approach – Elmasri, Carrick & Levine – Copyright 2010 11. 36
Reference Counter • As reference bits are cleared, • Shift into a counter one for each page • Smallest counter referenced least recently Operating Systems – a Spiral Approach – Elmasri, Carrick & Levine – Copyright 2010 11. 37
Page Reference Bit & Counter Page reference bit Reference count Valid bit 1 1 0 0 1 1 1 1 Shift right when reference bit is cleared Operating Systems – a Spiral Approach – Elmasri, Carrick & Levine – Copyright 2010 11. 38
Dirty Pages • Yet another hardware bit • Indicates page changed in RAM • Clear when – page read in – page written to swap file – code never modified • Hardware will set on page write Operating Systems – a Spiral Approach – Elmasri, Carrick & Levine – Copyright 2010 11. 39
Second Chance Algorithm • Search page table for unreferenced • Skip referenced but clear the reference bit • Continue looking • Wrap at end of page table Operating Systems – a Spiral Approach – Elmasri, Carrick & Levine – Copyright 2010 11. 40
More Strategies • Disk 1 to 10 K times slower than RAM • Can compute a lot to save 1 read • Dirty pages expensive have to write old & read new • Avoid dirty pages Operating Systems – a Spiral Approach – Elmasri, Carrick & Levine – Copyright 2010 11. 41
Enhanced 2 nd Chance • Use Dirty and Referenced bits • Up to 4 passes through table • Prefer – – unreferenced and clean, then unreferenced and dirty, then • aka Not Used Recently (NUR) or Not Recently Used (NRU) Operating Systems – a Spiral Approach – Elmasri, Carrick & Levine – Copyright 2010 11. 42
Local vs. Global Replacement • When taking a frame, – take it from any process (global), or – only from faulting process (local) • Local punishes good programs Operating Systems – a Spiral Approach – Elmasri, Carrick & Levine – Copyright 2010 11. 43
How Many Frames? • Hardware minimum – block move – 6 possible pages • Could be arbitrary – only 1 job? – fill RAM – no page fault interrupts Operating Systems – a Spiral Approach – Elmasri, Carrick & Levine – Copyright 2010 11. 44
How Many Frames? • Equal for all jobs – database <> calculator • Proportional to program size – consider a word processor • opening a thesis or • opening a party invitation? Operating Systems – a Spiral Approach – Elmasri, Carrick & Levine – Copyright 2010 11. 45
Page Limit Balancing Max. Below Page fault rate minimum - remove frames Frames allocated Page fault rate Above Page fault rate maximum add frames 0 0 Page fault frequency Operating Systems – a Spiral Approach – Elmasri, Carrick & Levine – Copyright 2010 Max. 11. 46
Working Set Again • For any job at time T the working set (WS) is a minimum • Measure over short, fixed intervals • < WS available - job produces many faults called Thrashing • Excessive disk I/O Operating Systems – a Spiral Approach – Elmasri, Carrick & Levine – Copyright 2010 11. 47
System Thrashing • • No one job may be thrashing System WS is Σ (WS for all jobs) System WS > available RAM System starts thrashing – Appears to freeze – Excessive disk I/O Operating Systems – a Spiral Approach – Elmasri, Carrick & Levine – Copyright 2010 11. 48
System Thrashing • You have probably seen this • No long term scheduler on PCs – You click - the OS starts it • Disk going like crazy swapping pages • Nothing is happening • Try to kill a few jobs Operating Systems – a Spiral Approach – Elmasri, Carrick & Levine – Copyright 2010 11. 49
Page Locking • Don’t dump page with I/O pending • Solution – Lock the pages – int mlock (const void *addr, size_t len) – int munlock (const void *addr, size_t len) Operating Systems – a Spiral Approach – Elmasri, Carrick & Levine – Copyright 2010 11. 50
Page Cleaning • Using dirty pages expensive • Clean pages with idle time – background job • Clean free frame pool – use while dirty page is cleaned – then return cleaned page to pool Operating Systems – a Spiral Approach – Elmasri, Carrick & Levine – Copyright 2010 11. 51
Not Really Transparent Following code run on arrays of double precision floating point numbers Silicon Graphics system MIPS R 4000 processor 1 M Byte cache Operating Systems – a Spiral Approach – Elmasri, Carrick & Levine – Copyright 2010 11. 52
Matrix Multiply for (i = 0; i < 500; i++) for (j = 0; j < 500; j++) for (k = 0; k < 500; k++) x [i] [j] = x [i] [j] + y [i] [k] * z [k] [j]; • running time was 77. 2 seconds Operating Systems – a Spiral Approach – Elmasri, Carrick & Levine – Copyright 2010 11. 53
The Problem • Array stored with row elements contiguous • Innermost loop varies row subscript • Each reference to a different page The Solution • Exchange inner and outer loops Operating Systems – a Spiral Approach – Elmasri, Carrick & Levine – Copyright 2010 11. 54
Revised Multiply for (k = 0; k < 500; k++) for (j = 0; j < 500; j++) for (i = 0; i < 500; i++) x [i] [j] = x [i] [j] + y [i] [k] * z [k] [j]; • run time now 44. 2 seconds Operating Systems – a Spiral Approach – Elmasri, Carrick & Levine – Copyright 2010 11. 55
Shared Memory • Code is read (execute) only • Can be shared between processes – done by system • Processes can share data pages – Normally locked out – Must ask the OS for help Operating Systems – a Spiral Approach – Elmasri, Carrick & Levine – Copyright 2010 11. 56
shmget() key_t key; /* key to be passed to shmget() */ int shmflg; /* shmflg to be passed to shmget() */ int shmid; /* return value from shmget() */ int size; /* size to be passed to shmget() */ shm_id = shmget(IPC_PRIVATE, size, shmflg); if (shm_id < 0) { printf("shmget errorn"); exit(1); } Operating Systems – a Spiral Approach – Elmasri, Carrick & Levine – Copyright 2010 11. 57
Copy On Write • Forked process can share memory pages – done by OS, not by process • Writes by processes should not be seen • Both page tables point to same pages – page table entries are marked read only • If a process writes to a page – interrupt • OS makes a new copy of page – marks both page tables read/write Operating Systems – a Spiral Approach – Elmasri, Carrick & Levine – Copyright 2010 11. 58
Memory Mapped Files • • • Job asks OS to map file into RAM Uses array addressing - not I/O calls OS maps logical address space Does not read in the file Page faults cause blocks to be read Multiple jobs can access the same RAM at the same time Operating Systems – a Spiral Approach – Elmasri, Carrick & Levine – Copyright 2010 11. 59
XP Prefetch Files Tracks page faults by programs • tracks for several minutes at startup • writes results to a prefetch file • Sorts file as background task • At next launch, fetches pages together – reads multiple blocks – moves heads around less Operating Systems – a Spiral Approach – Elmasri, Carrick & Levine – Copyright 2010 11. 60
Symbian Memory Management Logical Physical Thread A 2 Data 29 Thread A 1 Data 21 Process A 9 Thread B 1 Data 43 Process B 17 The running process is always mapped here. a) Process B is running Operating Systems – a Spiral Approach – Elmasri, Carrick & Levine – Copyright 2010 b) Process A is running 11. 61