Memory management outline q Concepts q Swapping q

  • Slides: 44
Download presentation
Memory management: outline q Concepts q Swapping q Paging o Multi-level paging o TLB

Memory management: outline q Concepts q Swapping q Paging o Multi-level paging o TLB & inverted page tables Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels, & Jihad El-Sana 1

Memory size/requirements are growing… 1951: the UNIVAC computer: 1000 72 -bit words! 1971: the

Memory size/requirements are growing… 1951: the UNIVAC computer: 1000 72 -bit words! 1971: the Cray 1 supercomputer: About 200 K memory gates! 1983: the IBM XT: 640 KB “should be enough for everybody…” 2012: today's laptops: 4 GB-8 GB Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels, & Jihad El-Sana 2

Our requirements from memory q An indispensible resource q Variation on Parkinson’s law: “Programs

Our requirements from memory q An indispensible resource q Variation on Parkinson’s law: “Programs expand to fill the memory available to hold them” q Ideally programmers want memory that is o fast o non volatile o large o cheap Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels, & Jihad El-Sana 3

The Memory Hierarchy q Memory hierarchy o Hardware registers: very small amount of very

The Memory Hierarchy q Memory hierarchy o Hardware registers: very small amount of very fast volatile memory o Cache: small amount of fast, expensive, volatile memory o Main memory: medium amount of medium-speed, medium price, volatile memory o Disk: large amount of slow, cheap, non-volatile memory q The memory manager is the part of the OS that handles main memory and transfers between it and secondary storage (disk) Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels, & 4

Mono-programming memory management q Mono-programming systems require a simple memory manager o User types

Mono-programming memory management q Mono-programming systems require a simple memory manager o User types a command o System loads program to main memory and executes it o System displays prompt, waits for new command ROM Device Drivers User Program Operating System in RAM MS DOS memory organization Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels, & Jihad El-Sana 5

Multi-programming Motivation n processes, each spending a fraction p of their time waiting for

Multi-programming Motivation n processes, each spending a fraction p of their time waiting for I/O, gives a probability pn of all processes waiting for I/O simultaneously CPU utilization = 1 - pn This calculation is simplistic Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels, & Jihad El-Sana 6

Memory/efficiency tradeoff q Assume each process takes 200 k and so does the operating

Memory/efficiency tradeoff q Assume each process takes 200 k and so does the operating system q Assume there is 1 Mb of memory available and that p=0. 8 q space for 4 processes 60% cpu utilization q Another 1 Mb enables 9 processes 87% cpu utilization Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels, & Jihad El-Sana 7

Memory management: outline q Concepts q Swapping q Paging o Multi-level paging o TLB

Memory management: outline q Concepts q Swapping q Paging o Multi-level paging o TLB & inverted page tables Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels, & Jihad El-Sana 8

Swapping: schematic view Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels, & Jihad

Swapping: schematic view Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels, & Jihad El-Sana 9

Swapping q Bring a process in its entirety, run it, and then write back

Swapping q Bring a process in its entirety, run it, and then write back to backing store (if required) q Backing store – fast disk large enough to accommodate copies of all memory images for all processes; must provide direct access to these memory images. q Major part of swap time is transfer time; total transfer time is proportional to the amount of memory swapped. This time can be used to run another process q Creates holes in memory (fragmentation), memory compaction may be required q No need to allocate swap space for memory-resident processes (e. g. Daemons) q Not used much anymore (but still interesting…) Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels, & Jihad El-Sana 10

Multiprogramming with Fixed Partitions (OS/360 MFT) q How to organize main memory? q How

Multiprogramming with Fixed Partitions (OS/360 MFT) q How to organize main memory? q How to assign processes to partitions? q Separate queues vs. single queue Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels, & Jihad El-Sana 11

Allocating memory - growing segments Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels,

Allocating memory - growing segments Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels, & Jihad El-Sana 12

Memory Allocation - Keeping Track (bitmaps; linked lists) Operating Systems, 2013, Meni Adler, Michael

Memory Allocation - Keeping Track (bitmaps; linked lists) Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels, & Jihad El-Sana 13

Swapping in Unix (prior to 3 BSD) q When is swapping done? o o

Swapping in Unix (prior to 3 BSD) q When is swapping done? o o o Kernel runs out of memory a fork system call – no space for child process a brk system call to expand a data segment a stack becomes too large A swapped-out process becomes ready q Who is swapped? o o a suspended process with “highest” priority (in) a process which consumed much CPU (out) q How much space is swapped? use holes and first-fit (more on this later) Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels, & Jihad El-Sana 14

Binding of Instructions and Data to Memory Address binding of instructions and data to

Binding of Instructions and Data to Memory Address binding of instructions and data to memory addresses can happen at three different stages q Compile time: If memory location known a priori, absolute code can be generated; must recompile code if starting location changes (e. g. , MS/DOS . com programs) q Load time: Must generate relocatable code if memory location is not known at compile time q Execution time: Binding delayed until run-time if the process can be moved during its execution from one memory segment to another. Need hardware support for address maps (e. g. , base and limit registers or virtual memory support) Which of these binding-types dictates that a process be swapped back from disk to same location? Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels, & Jihad El-Sana 15

Dynamic Linking q Linking postponed until execution time q A small piece of code,

Dynamic Linking q Linking postponed until execution time q A small piece of code, stub, is used to locate the appropriate memory-resident library routine q Stub replaces itself with the address of the routine, and calls the routine q Operating system makes sure the routine is mapped to processes' memory address q Dynamic linking is particularly useful for libraries (e. g. , Windows DLLs) Do DLLs save space in main memory or in disk? Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels, & Jihad El-Sana 16

Strategies for Memory Allocation q First fit – do not search too much. .

Strategies for Memory Allocation q First fit – do not search too much. . q Next fit - start search from last location fit q Best fit - a drawback: generates small holes fit q Worst fit - solves the above problem, badly fit q Quick fit - several queues of different sizes fit Main problem of such memory allocation – Fragmentation Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels, & Jihad El-Sana 17

Fragmentation q External Fragmentation – total memory space exists to satisfy a request, but

Fragmentation q External Fragmentation – total memory space exists to satisfy a request, but it is not contiguous q Internal Fragmentation – allocated memory may be slightly larger than requested memory; this size difference is memory internal to a partition, but not being used q Reduce external fragmentation by compaction o Shuffle memory contents to place all free memory together in one large block o Compaction is possible only if relocation is dynamic, and is done at execution time Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels, & Jihad El-Sana 18

Memory Compaction 0 0 Operating system 300 k P 1 500 k P 2

Memory Compaction 0 0 Operating system 300 k P 1 500 k P 2 600 k 400 K 800 k P 1 600 k P 3 1000 k P 1 500 k P 2 600 k P 4 1200 k P 2 400 K 1000 k P 3 Operating system 300 k 500 k P 2 0 Operating system 300 k P 1 500 k 600 k 0 900 K P 3 1200 k 300 K 1500 k 900 K P 4 900 K 1900 k 2100 k P 4 1900 k 200 K 2100 k original allocation 2100 k moved 600 K Figure 8. 11 2100 k moved 400 K P 3 moved 200 K Comparison of some different ways to compact memory Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels, & Jihad El-Sana 19

The Buddy Algorithm q An example scheme – the Buddy algorithm (Knuth 1973): o

The Buddy Algorithm q An example scheme – the Buddy algorithm (Knuth 1973): o Separate lists of free holes of sizes of powers of two o For any request, pick the 1 st large enough hole and halve it recursively o Relatively little external fragmentation (as compared with other simple algorithms) o Freed blocks can only be merged with neighbors of their own size. This is done recursively Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels, & Jihad El-Sana 20

The Buddy Algorithm Memory 0 128 k 256 k 384 k 512 k 640

The Buddy Algorithm Memory 0 128 k 256 k 384 k 512 k 640 k 768 k 896 k 1 Initially Request 70 A Request 35 A B 64 Request 80 A B 64 C Return A 128 B 64 Request 60 128 B Return B 128 64 Return D Return C 1 M Holes 128 256 512 3 128 512 3 C 128 512 4 D C 128 512 4 C 128 512 3 1024 1 Fig. 3 -9. The buddy algorithm. The horizontal axis represents memory addresses. The numbers are the sizes of unallocated blocks of memory in K. The letters represent allocated blocks of memory. Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels, & Jihad El-Sana 21

Logical vs. Physical Address Space q The concept of a logical address space that

Logical vs. Physical Address Space q The concept of a logical address space that is bound to a separate physical address space is central to modern memory management o Logical address – generated by the CPU; also referred to as virtual address o Physical address – address seen by the memory unit q Logical and physical addresses are the same in compile-time and load-time address-binding schemes; logical (virtual) and physical addresses differ in execution-time address-binding schemes Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels, & Jihad El-Sana 22

Memory management: outline q Concepts q Swapping q Paging o Multi-level paging o TLB

Memory management: outline q Concepts q Swapping q Paging o Multi-level paging o TLB & inverted page tables Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels, & Jihad El-Sana 23

Paging and Virtual Memory q Support an address space that is independent of physical

Paging and Virtual Memory q Support an address space that is independent of physical memory q Only part of a program may be in memory: program size may be larger than physical memory q 232 addresses for a 32 bit (address bus) machine - virtual addresses q can be achieved by segmenting the executable (using segment registers), or by dividing memory using another method q Paging - Divide physical memory into fixed-size Paging blocks (page-frames) page-frames q Allocate to processes non-contiguous memory chunks – disregarding holes Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels, & Jihad El-Sana 24

Memory Management Unit q Hardware unit within the CPU q It maps virtual address

Memory Management Unit q Hardware unit within the CPU q It maps virtual address in a process space to physical addresses q The translation is done before the address is put on the bus (to access the memory). Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels, & Jihad El-Sana 25

Memory-Management Unit (MMU) q The user program o Deals with logical addresses o Not

Memory-Management Unit (MMU) q The user program o Deals with logical addresses o Not aware of the real physical addresses mov A, 1000 ; virtual (logical) address is ; sent to the MMU – which maps ; this to the physical (real) address mov A, 9192 Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels, & Jihad El-Sana 26

Paging Page table 64 K: virtual space 32 K: physical space Must choose the

Paging Page table 64 K: virtual space 32 K: physical space Must choose the size of the pages: determines size of the page table Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels, & Jihad El-Sana 27

Operation of the MMU Map incoming virtual address to physical address. Virtual page =

Operation of the MMU Map incoming virtual address to physical address. Virtual page = 2 is used as an index into the page table Incoming virtual address (8196) Present/ absent bit 12 -bit offset copied directly from virtual to physical address Outgoing physical address (24580) Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels, & Jihad El-Sana 28

Page Faults: Pages In/Out q When MMU tries to access a page which is

Page Faults: Pages In/Out q When MMU tries to access a page which is not currently loaded in RAM – a hardware trap is raised: PAGE FAULT. q This allows the kernel to handle the swapping of one currently loaded page with the requested page. q The kernel maintains copies of the pages in disk. Up to full process virtual memory for each process in swap disk. q Issues: o Select page to be evicted (Eviction strategy) o Should evicted page be written to disk to save modifications? Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels 29

Pages: blocks of virtual addresses Page frames: refer to physical memory segments Page Table

Pages: blocks of virtual addresses Page frames: refer to physical memory segments Page Table Entries (PTE) contain (per page): q Page frame number (physical address) q Present/absent (valid )bit q Dirty (modified) bit q Referenced (accessed) bit q Protection q Caching disable/enable Caching page frame number Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels, & Jihad 30

Linux Memory Management q All memory addresses generated by a program get passed through

Linux Memory Management q All memory addresses generated by a program get passed through an address translation mechanism in the processor. q The addresses are converted from a process-specific virtual address to a physical memory address. This is referred to as virtual memory. Operating Systems, 2013, Meni Adler, Michael Elhadad, 31

Page size vs. Page-table size Tradeoffs q A logical address of 32 -bits (4

Page size vs. Page-table size Tradeoffs q A logical address of 32 -bits (4 GB) can be divided into: o 1 K pages and 4 M entries table o 4 K pages and 1 M entries table q Large pages – a smaller number of pages, but higher internal fragmentation q Smaller pages – larger tables (also waste of space) Large tables, and we need ONE PER PROCESS! Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels, & Jihad El-Sana 32

Page table considerations q Can be very large (1 M pages for 32 bits,

Page table considerations q Can be very large (1 M pages for 32 bits, 4 K page size) q Must be fast (every instruction needs it) q One extreme will have it all in hardware - fast registers that hold the page table and are loaded with each process - too expensive for the above size q The other extreme has it all in main memory (using a page table base register – ptbr - to point to it) - each memory reference ptbr during instruction translation is doubled. . . q Possible solution: to avoid keeping complete page tables in memory - make them multilevel, and avoid making multiple memory references per instruction by caching We do paging on the page-table itself! Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels, & Jihad El- 33

Two-Level Page-Table Scheme Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels, & Jihad

Two-Level Page-Table Scheme Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels, & Jihad El-Sana 34

Two-Level Paging Example q A logical address (on 32 -bit machine with 4 K

Two-Level Paging Example q A logical address (on 32 -bit machine with 4 K page size) is divided into: o a page number consisting of 20 bits. o a page offset consisting of 12 bits. q Since the page table itself is paged, the page number is further divided into: o a 10 -bit page number. o a 10 -bit page offset. q Thus, a logical address has the following structure: page number p 1 10 page offset p 2 d 10 12 Where p 1 is an index into the top-level (outer) page table, and p 2 is an index into the selected second-level page table Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels, & Jihad El-Sana 35

Two-Level Paging: Motivation q Two-level paging helps because most of the time a process

Two-Level Paging: Motivation q Two-level paging helps because most of the time a process does not need ALL of its virtual memory space. q Example: A process in a 32 bit machine uses o 4 MB of stack o 4 MB of code segment o 4 MB of heap q Only 12 MB effectively used out of 4 GB – only 3 pages of pages needed (out of 1024) Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels 36

Two-Level Paging Example (cont’d) 1023 Top-level page table page number p 1 10 p

Two-Level Paging Example (cont’d) 1023 Top-level page table page number p 1 10 p 2 10 page offset d 12 1023 4 3 2 1 0 5 4 3 2 0 4095 1023 5 4 3 2 0 Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels, & Jihad El-Sana 37

Translation Lookaside Buffer (TLB): Associative memory for minimizing redundant memory accesses • TLB resides

Translation Lookaside Buffer (TLB): Associative memory for minimizing redundant memory accesses • TLB resides in MMU • Most accesses are to a small set of pages high hit rate Locality of reference Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels, & Jihad 38

Notes about TLB q TLB is an associative memory q Typically, inside the MMU

Notes about TLB q TLB is an associative memory q Typically, inside the MMU q With a large enough hit-ratio, the extra accesses to page tables are rare q Only a complete virtual address (all levels) can be counted as a hit q with multi-processing, TLB must be cleared on context switch - wasteful. . o Possible solution: add a field to the associative memory to hold process ID and change in context switch. q TLB management may be done by hardware or OS Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels, & Jihad El- 39

Inverted page tables q Regular page tables impractical for 64 -bit address space 4

Inverted page tables q Regular page tables impractical for 64 -bit address space 4 K page size / 252 pages x 8 bytes 30 M GB page tables! q Inverted page table – sorted by (physical) page frames and not by virtual pages 1 GB of RAM & 4 K page size / 256 K entries 2 MB table q A single inverted page table used for all processes currently in memory q Each entry stores which process/virtual-page maps to it q A hash table is used to avoid linear search for every virtual page q In addition to the hash table, TLB registers are used to store recently used page table entries Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels, & Jihad El- 40

Inverted Page Table Architecture Table Search must take pid into account Operating Systems, 2013,

Inverted Page Table Architecture Table Search must take pid into account Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels, & Jihad El-Sana 41

Inverted Table with Hashing Mapped physical memory Virtual page number Hash function Index into

Inverted Table with Hashing Mapped physical memory Virtual page number Hash function Index into the hash anchor table Inverted page table Hash anchor table The inverted page table contains one PTE for every page frame in memory, making it densely packed compared to the hierarchical page table. It is indexed by a hash of the virtual page number. Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels, & Jihad El- 42

Inverted Table with Hashing q The hash function points into the anchor hash table.

Inverted Table with Hashing q The hash function points into the anchor hash table. q Each entry in the anchor table is the first link in a list of pointers to the inverted table. q Each list ends with a Nil pointer. q On every memory call the page is looked up in the relevant list. q TLB still used to prevent search in most cases Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels, & Jihad El- 43

Shared Pages Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels, & Jihad El-Sana

Shared Pages Operating Systems, 2013, Meni Adler, Michael Elhadad, Amnon Meisels, & Jihad El-Sana 44