CS Advanced Operating Systems Structures and Implementation Lecture 13. Two-Level Scheduling (Con t) Segmentation/Paging/Virtual Memory

Similar documents
CS162 Operating Systems and Systems Programming Lecture 12. Address Translation. Page 1

Midterm 1. Administrivia. Virtualizing Resources. CS162 Operating Systems and Systems Programming Lecture 12. Address Translation

Page 1. Goals for Today" Virtualizing Resources" Important Aspects of Memory Multiplexing" CS162 Operating Systems and Systems Programming Lecture 20

Page 1. Goals for Today" Important Aspects of Memory Multiplexing" Virtualizing Resources" CS162 Operating Systems and Systems Programming Lecture 9

CS162 Operating Systems and Systems Programming Lecture 12. Protection (continued) Address Translation

CS162 Operating Systems and Systems Programming Lecture 13. Caches and TLBs. Page 1

CS162 Operating Systems and Systems Programming Lecture 12. Protection (continued) Address Translation

Operating Systems (1DT020 & 1TT802) Lecture 9 Memory Management : Demand paging & page replacement. Léon Mugwaneza

CS162 Operating Systems and Systems Programming Lecture 14. Caching and Demand Paging

Page 1. Review: Address Segmentation " Review: Address Segmentation " Review: Address Segmentation "

CS162 Operating Systems and Systems Programming Lecture 14. Caching (Finished), Demand Paging

Paging! 2/22! Anthony D. Joseph and Ion Stoica CS162 UCB Fall 2012! " (0xE0)" " " " (0x70)" " (0x50)"

Introduction to Operating Systems

10/7/13! Anthony D. Joseph and John Canny CS162 UCB Fall 2013! " (0xE0)" " " " (0x70)" " (0x50)"

CS162 Operating Systems and Systems Programming Lecture 11. Protection: Address Spaces. Page 1. Review

CS162 Operating Systems and Systems Programming Lecture 10 Caches and TLBs"

Recall: Paging. Recall: Paging. Recall: Paging. CS162 Operating Systems and Systems Programming Lecture 13. Address Translation, Caching

CS162 Operating Systems and Systems Programming Lecture 14. Caching and Demand Paging

CS162 Operating Systems and Systems Programming Lecture 13. Address Translation (con t) Caches and TLBs

Page 1. Review: Last Time" Goals for Today" Round-Robin Discussion" CS162 Operating Systems and Systems Programming Lecture 8

Page 1. CS162 Operating Systems and Systems Programming Lecture 14. Caching and Demand Paging

Page 1. CS194-3/CS16x Introduction to Systems. Lecture 10. Protection: Address Spaces. Goals for Today

Memory Management Topics. CS 537 Lecture 11 Memory. Virtualizing Resources

More on Address Translation. CS170 Fall T. Yang Some of slides from UCB CS162 by Kubiatowicz

CS 162 Operating Systems and Systems Programming Professor: Anthony D. Joseph Spring Lecture 13: Address Translation

Main Memory Yi Shi Fall 2017 Xi an Jiaotong University

Main Memory: Address Translation

PROCESS VIRTUAL MEMORY. CS124 Operating Systems Winter , Lecture 18

CS 318 Principles of Operating Systems

Chapter 8: Memory-Management Strategies

Memory Management. Disclaimer: some slides are adopted from book authors slides with permission 1

Recall: Address Space Map. 13: Memory Management. Let s be reasonable. Processes Address Space. Send it to disk. Freeing up System Memory

Chapter 8: Main Memory

CHAPTER 8 - MEMORY MANAGEMENT STRATEGIES

Virtual Memory. Daniel Sanchez Computer Science & Artificial Intelligence Lab M.I.T. April 12, 2018 L16-1

CSE 120 Principles of Operating Systems

CS252 S05. Main memory management. Memory hardware. The scale of things. Memory hardware (cont.) Bottleneck

15 Sharing Main Memory Segmentation and Paging

Chapter 8: Main Memory. Operating System Concepts 9 th Edition

Virtual Memory. Daniel Sanchez Computer Science & Artificial Intelligence Lab M.I.T. November 15, MIT Fall 2018 L20-1

16 Sharing Main Memory Segmentation and Paging

CPS 104 Computer Organization and Programming Lecture 20: Virtual Memory

Chapter 8: Memory- Management Strategies. Operating System Concepts 9 th Edition

CS399 New Beginnings. Jonathan Walpole

Memory Hierarchy Requirements. Three Advantages of Virtual Memory

CPS104 Computer Organization and Programming Lecture 16: Virtual Memory. Robert Wagner

Chapter 8: Main Memory

Page 1. Goals for Today" TLB organization" CS162 Operating Systems and Systems Programming Lecture 11. Page Allocation and Replacement"

CS162 - Operating Systems and Systems Programming. Address Translation => Paging"

memory management Vaibhav Bajpai

Address spaces and memory management

Memory management. Last modified: Adaptation of Silberschatz, Galvin, Gagne slides for the textbook Applied Operating Systems Concepts

Chapter 8 Memory Management

Virtual Memory: From Address Translation to Demand Paging

Principles of Operating Systems

Topic 18 (updated): Virtual Memory

How to create a process? What does process look like?

Virtual Memory. Kevin Webb Swarthmore College March 8, 2018

CHAPTER 8: MEMORY MANAGEMENT. By I-Chen Lin Textbook: Operating System Concepts 9th Ed.

John Wawrzynek & Nick Weaver

Memory Management. Dr. Yingwu Zhu

CSE 120 Principles of Operating Systems Spring 2017

Memory Hierarchy Review

File Systems. OS Overview I/O. Swap. Management. Operations CPU. Hard Drive. Management. Memory. Hard Drive. CSI3131 Topics. Structure.

Lecture 4: Memory Management & The Programming Interface

Administrivia. Lab 1 due Friday 12pm. We give will give short extensions to groups that run into trouble. But us:

Memory Management. Disclaimer: some slides are adopted from book authors slides with permission 1

Memory management. Requirements. Relocation: program loading. Terms. Relocation. Protection. Sharing. Logical organization. Physical organization

Chapter 7: Main Memory. Operating System Concepts Essentials 8 th Edition

CS 31: Intro to Systems Virtual Memory. Kevin Webb Swarthmore College November 15, 2018

Topics: Memory Management (SGG, Chapter 08) 8.1, 8.2, 8.3, 8.5, 8.6 CS 3733 Operating Systems

Multi-Process Systems: Memory (2) Memory & paging structures: free frames. Memory & paging structures. Physical memory

Chapter 5B. Large and Fast: Exploiting Memory Hierarchy

CS307: Operating Systems

Lecture 13: Address Translation

CS162 Operating Systems and Systems Programming Lecture 11 Page Allocation and Replacement"

Reducing Hit Times. Critical Influence on cycle-time or CPI. small is always faster and can be put on chip

Chapter 8: Memory- Management Strategies. Operating System Concepts 9 th Edition

Chapter 8: Memory- Management Strategies

Embedded Systems Dr. Santanu Chaudhury Department of Electrical Engineering Indian Institute of Technology, Delhi

Another View of the Memory Hierarchy. Lecture #25 Virtual Memory I Memory Hierarchy Requirements. Memory Hierarchy Requirements

Chapter 9 Memory Management

CS 153 Design of Operating Systems Winter 2016

Virtual Memory. Virtual Memory

Operating Systems. 09. Memory Management Part 1. Paul Krzyzanowski. Rutgers University. Spring 2015

CS 61C: Great Ideas in Computer Architecture. Lecture 23: Virtual Memory. Bernhard Boser & Randy Katz

Operating Systems. Memory Management. Lecture 9 Michael O Boyle

Topic 18: Virtual Memory

Chapter 8 & Chapter 9 Main Memory & Virtual Memory

Memory Management. Reading: Silberschatz chapter 9 Reading: Stallings. chapter 7 EEL 358

18-447: Computer Architecture Lecture 16: Virtual Memory

CSE 560 Computer Systems Architecture

EECS 470. Lecture 16 Virtual Memory. Fall 2018 Jon Beaumont

CS 61C: Great Ideas in Computer Architecture. Lecture 23: Virtual Memory

6 - Main Memory EECE 315 (101) ECE UBC 2013 W2

Processes and Virtual Memory Concepts

Virtual Memory. CS61, Lecture 15. Prof. Stephen Chong October 20, 2011

Virtual Memory 1. Virtual Memory

Virtual Memory. Today. Segmentation Paging A good, if common example

Outline. V Computer Systems Organization II (Honors) (Introductory Operating Systems) Advantages of Multi-level Page Tables

Transcription:

Goals for Today CS194-24 Advanced Operating Systems Structures and Implementation Lecture 13 Two-Level Scheduling (Con t) Segmentation/Paging/ Memory March 18 th, 2013 Prof. John Kubiatowicz http://inst.eecs.berkeley.edu/~cs194-24 Two-Level Scheduling/Tessellation (Con t) Segmentation/Paging/ Memory (Review) Interactive is important! Ask Questions! Note: Some slides and/or pictures in the following are adapted from slides 2013 Lec 13.2 Recall: Example of DRF vs Asset vs CEEI Resources <1000 CPUs, 1000 GB> 2 users A: <2 CPU, 3 GB> and B: <5 CPU, 1 GB> 100% 50% 0% 100% 50% 0% 0% CPU Mem CPU Mem CPU Mem a) DRF b) Asset Fairness 100% 50% c) CEEI User A User B Monolithic CPU and Resource Scheduling Recall: Two Level Scheduling: Control vs Data Plane Resource Allocation And Distribution Two-Level Scheduling Application Specific Scheduling Split monolithic scheduling into two pieces: Course-Grained Resource Allocation and Distribution to Cells» Chunks of resources (CPUs, Memory Bandwidth, QoS to Services)» Ultimately a hierarchical process negotiated with service providers Fine-Grained (User-Level) Application-Specific Scheduling» Applications allowed to utilize their resources in any way they see fit» Performance Isolation: Other components of the system cannot interfere with Cells use of resources Lec 13.3 Lec 13.4

Recall: Adaptive Resource-Centric Computing (ARCC) Resource Allocation (Control Plane) Partitioning and Distribution Resource Assignments Running System (Data Plane) Application1 Channel GUI Service QoS-aware Scheduler Block Service QoS-aware Scheduler Network Service QoS-aware Scheduler Observation and Modeling Channel Performance Reports Application2 Resource Allocation Goal: Meet the QoS requirements of a software component (Cell) Behavior tracked through applicationspecific heartbeats and systemlevel monitoring Dynamic exploration of performance space to find operation points Complications: Many cells with conflicting requirements Finite Resources Hierarchy of resource ownership Context-dependent resource availability Stability, Efficiency, Rate of Convergence, Execution Execution Execution Execution Execution Modeling Modeling Models Adaptation Evaluation Modeling and Models State Adaptation Evaluation Modeling and Models State Adaptation Evaluation Modeling and Models Models State Adaptation Evaluationand State Adaptation Evaluationand State Resource Discovery, Access Control, Advertisement Lec 13.5 Lec 13.6 Performance-Aware Convex Optimization for Resource Allocation (PACORA) Express problem as an optimization with constraints Maximize throughput, performance, battery life, etc. Tune for QoS Measure performance as a function of resources for each application Create performance model Refine performance function from application history Combine resource-value functions and QoS requirements to make resource decisions Application is given enough resources for QoS Turn off resources that don t improve the system Developers don t need to understand resources Basic Solution to Impedance Mismatch problem 7 Lec 13.7 + Tackling Multiple Requirements: Express as Convex Optimization Problem 8 Continuously minimize using the penalty of the system (subject to restrictions on the total amount of resources) Penalty 1 Penalty 2 Penalty i Penalty Function Runtime 1 Runtime 2 Resource-Value Function Runtime 1 (r (0,1),, r (n-1,1) ) Runtime 2 (r (0,2),, r (n-1,2) ) Runtime Runtime i (r (0,i),, r (n-1,i) ) i Lec 13.8 Speech Graph Stencil

Sibling Broker Parent Broker Local Broker Brokering Service: The Hierarchy of Ownership Discover Resources in Domain Devices, Services, Other Brokers Resources self-describing? Allocate and Distribute Resources to Cells that need them Solve Impedance-mismatch problem Dynamically optimize execution Hand out Service-Level Child Agreements (SLAs) to Cells Broker Deny admission to Cells when violates existing agreements Complete hierarchy Throughout world graph of applications Lec 13.9 Spatial Partition: Performance isolation Each partition receives a vector of basic resources» A number HW threads» Chunk of physical memory» A portion of shared cache» A fraction of memory BW» Shared fractions of services Space-Time Partitioning Cell Space Time Partitioning varies over time Fine-grained multiplexing and guarantee of resources» Resources are gang-scheduled Controlled multiplexing, not uncontrolled virtualization Partitioning adapted to the system s needs Lec 13.10 Communication-Avoiding Gang Scheduling Multiplexer Gang Scheduling Algorithm Core 0 Efficient Space-Time Partitioning Multiplexer Gang Scheduling Algorithm Core 1 Multiplexer Gang Scheduling Algorithm Core 2 Multiplexer Gang Scheduling Algorithm Core 3 Supports a variety of Cell types with low overhead Cross between EDF (Earliest Deadline First) and CBS (Constant Bandwidth Server) Multiplexers do not communicate because they use synchronized clocks with sufficiently high precision Not Muxed Identical or Consistent Schedules Lec 13.11 Adaptive, Second-Level Preemptive Scheduling Framework PULSE: Preemptive User-Level SchEduling Framework for adaptive, premptive schedulers: Dedicated access to processor resources Timer Callback and Event Delivery User-level virtual memory mapping User-level device control Auxiliary Scheduler: Interface with policy service Runs outstanding scheduler contexts past synchronization points when resizing happens 2nd-level Schedulers not aware of existence of the Auxiliary Scheduler, but receive resize events A number of adaptive schedulers have already been built: Round-Robin, EDF, CBS, Speed Balancing Application Adaptive Preemptive Scheduler Auxiliary Scheduler PULSE Preemptive User- Level SchEduling Framework Lec 13.12

Example: Tesselation GUI Service GUI Service Architecture QoS-aware Scheduler Operate on user-meaningful actions E.g. draw frame, move window Service time guarantees (soft real-time) Differentiated service per application E.g. text editor vs video Performance isolation from other applications Lec 13.13 Lec 13.14 Nano-X/ Linux Composite Resource with Greatly Improved QoS Nano-X/ Tess GuiServ(1)/ Tess GuiServ(2)/ GuiServ(4)/ Tess Tess Lec 13.15 Policy Service Cell Creation and Resizing Requests From Users Partition Mapping and Multiplexing Layer Partition Mechanism Layer Architecture of Tessellation OS STRG Validator Resource Planner Partition Implementation Network Bandwidth Admission Control Minor ACK/NACK Changes Space-Time Resource Graph (STRG) TPM/ Crypto All system resources Cell group with fraction of resources Cell Cell Cell Cell Cache/ Local Store Major Change Request ACK/NACK QoS Enforcement Memory Resource Allocation And Adaptation Mechanism Offline Models and Behavioral Parameters (Current Resources) Partition Multiplexing Channel Authenticator Cores NICs Global Policies/ User Policies and Preferences Online Performance Monitoring, Model Building, and Prediction Tessellation Kernel (Trusted) Performance Counters Partitionable (and Trusted) Hardware Cell #1 Cell #2 Cell #3 User/System Partition #1 Partition #2 Partition #3 Lec 13.16

Administrivia Moved code deadline for Lab2 to Saturday Design documents can be turned in on Sunday Don t forget to do Group evaluations! Still working on grading the exam! I ve posted the exam for those of your who want to look at it Attendance at sections and design reviews is not optional! We will be taking attendance on all future Friday sections lack of attendance will be counted against you Communication and meetings with members of your group is also not optional I expect that every group should meet at least 3 times (once a week) during the course of a project If anyone is having trouble getting together with your groups, please talk to me How to Evaluate a Scheduling algorithm? Deterministic modeling takes a predetermined workload and compute the performance of each algorithm for that workload Queueing models Mathematical approach for handling stochastic workloads Implementation/Simulation: Build system which allows actual algorithms to be run against actual data. Most flexible/general. Lec 13.17 Lec 13.18 A Final Word On Scheduling When do the details of the scheduling policy and fairness really matter? When there aren t enough resources to go around When should you simply buy a faster computer? (Or network link, or expanded highway, or ) One approach: Buy it when it will pay for itself in improved response time» Assuming you re paying for worse response time in reduced productivity, customer angst, etc» Might think that you should buy a faster X when X is utilized 100%, but usually, response time goes to infinity as utilization 100% An interesting implication of this curve: Response time Utilization Most scheduling algorithms work fine in the linear portion of the load curve, fail otherwise Argues for buying a faster X when hit knee of curve 100% Lec 13.19 izing Resources Reality: Different Processes/Threads share the same hardware Need to multiplex CPU (Just finished: scheduling) Need to multiplex use of Memory (Today) Need to multiplex disk and devices (later in term) Why worry about memory sharing? The complete working state of a process and/or kernel is defined by its data in memory (and registers) Consequently, cannot just let different threads of control use the same memory» Physics: two different pieces of data cannot occupy the same locations in memory Probably don t want different threads to even have access to each other s memory (protection) Lec 13.20

Important Aspects of Memory Multiplexing Controlled overlap: Separate state of threads should not collide in physical memory. Obviously, unexpected overlap causes chaos! Conversely, would like the ability to overlap when desired (for communication) Translation: Ability to translate accesses from one address space (virtual) to a different one (physical) When translation exists, processor uses virtual addresses, physical memory uses physical addresses Side effects:» Can be used to avoid overlap» Can be used to give uniform view of memory to programs Protection: Prevent access to private memory of other processes» Different pages of memory can be given special behavior (Read Only, Invisible to user programs, etc).» Kernel data protected from User programs» Programs protected from themselves Lec 13.21 Binding of Instructions and Data to Memory Binding of instructions and data to addresses: Choose addresses for instructions and data from the standpoint of the processor data1: dw 32 start: lw r1,0(data1) jal checkit loop: addi r1, r1, -1 bnz r1, r0, loop checkit: Could we place data1, start, and/or checkit at different addresses?» Yes» When? Compile time/load time/execution time Related: which physical memory locations hold particular instructions or data? 0x300 00000020 0x900 8C2000C0 0x904 0C000340 0x908 2021FFFF 0x90C 1420FFFF 0xD00 Lec 13.22 Multi-step Processing of a Program for Execution Preparation of a program for execution involves components at: Compile time (i.e. gcc ) Link/Load time (unix ld does link) Execution time (e.g. dynamic libs) Addresses can be bound to final values anywhere in this path Depends on hardware support Also depends on operating system Dynamic Libraries Linking postponed until execution Small piece of code, stub, used to locate the appropriate memoryresident library routine Stub replaces itself with the address of the routine, and executes routine Recall: Uniprogramming Uniprogramming (no Translation or Protection) Application always runs at same place in physical memory since only one application at a time Application can access any physical address Operating System Application 0xFFFFFFFF Valid 32-bit Addresses 0x00000000 Application given illusion of dedicated machine by giving it reality of a dedicated machine Of course, this doesn t help us with multithreading Lec 13.23 Lec 13.24

Multiprogramming (First Version) Multiprogramming without Translation or Protection Must somehow prevent address overlap between threads Operating System 0xFFFFFFFF Multiprogramming (Version with Protection) Can we protect programs from each other without translation? Operating System 0xFFFFFFFF Limit=Base+0x10000 Application2 0x00020000 Application2 0x00020000 Base=0x20000 Application1 0x00000000 Trick: Use Loader/Linker: Adjust addresses while program loaded into memory (loads, stores, jumps)» Everything adjusted to memory location of program» Translation done by a linker-loader» Was pretty common in early days With this solution, no protection: bugs in any program can cause other programs to crash or even the OS Lec 13.25 Application1 0x00000000 Yes: use two special registers Base and Limit to prevent user from straying outside designated area» If user tries to access an illegal address, cause an error During switch, kernel loads new base/limit from TCB» User not allowed to change base/limit registers Lec 13.26 Two Views of Memory with Dynamic Translation CPU Addresses MMU Addresses Untranslated read or write Two views of memory: View from the CPU (what program sees, virtual memory) View fom memory (physical memory) Translation box converts between the two views Translation helps to implement protection If task A cannot even gain access to task B s data, no way for A to adversely affect B With translation, every program can be linked/loaded into same region of user address space Overlap avoided through translation, not relocation Lec 13.27 Simple Segmentation: Base and Limit registers (CRAY-1) CPU Address Limit <? Base No: Error! Could use base/limit for dynamic address translation (often called segmentation ): Alter address of every load/store by adding base Generate error if address bigger than limit This gives program the illusion that it is running on its own dedicated machine, with memory starting at 0 Program gets continuous region of memory Addresses within program do not have to be relocated when program placed in different region of DRAM User may have multiple segments available (e.g x86) Loads and stores include segment ID in opcode: x86 Example: mov [es:bx],ax. + Address DRAM Lec 13.28

Issues with simple segmentation method More Flexible Segmentation process 6 process 5 process 6 process 5 process 6 process 5 process 9 process 6 process 5 process 9 1 1 1 4 process 2 process 10 2 OS Fragmentation problem Not every process is the same size Over time, memory space becomes fragmented Hard to do inter-process sharing Want to share code segments when possible Want to share memory between processes Helped by by providing multiple segments per process Need enough physical memory for every process OS OS OS Lec 13.29 Logical View: multiple separate segments Typical: Code, Data, Stack Others: memory sharing, etc Each segment is given region of contiguous memory Has a base and limit Can reside anywhere in physical memory 3 4 user view of memory space 2 2 3 physical memory space Lec 13.30 Implementation of Multi-Segment Model Intel x86 Special Registers Address Seg # Base0 Limit0 V Base1 Limit1 V Base2 Limit2 V Base3 Limit3 N Base4 Limit4 V Base5 Limit5 N Base6 Limit6 N Base7 Limit7 V > Error + Address Segment map resides in processor Segment number mapped into base/limit pair Base added to offset to generate physical address Error check catches offset out of range As many chunks of physical memory as entries Segment addressed by portion of virtual address However, could be included in instruction instead:» x86 Example: mov [es:bx],ax. What is V/N? Can mark segments as invalid; requires check as well Typical Segment Register Current Priority is RPL Of Code Segment (CS) 80386 Special Registers Lec 13.31 Lec 13.32

Seg Example: Four Segments (16 bit addresses) 15 14 13 Address Format 0x0000 0x4000 0x8000 0xC000 Address Space 0 Seg ID # Base Limit 0 (code) 0x4000 0x0800 1 (data) 0x4800 0x1400 2 (shared) 0xF000 0x1000 3 (stack) 0x0000 0x3000 0x0000 0x4000 0x4800 0x5C00 0xF000 Address Space Might be shared Space for Other Apps Shared with Other Apps Example of segment translation 0x240 main: la $a0, varx 0x244 jal strlen 0x360 strlen: li $v0, 0 ;count 0x364 loop: lb $t0, ($a0) 0x368 beq $r0,$t1, done 0x4050 varx dw 0x314159 Seg ID # Base Limit 0 (code) 0x4000 0x0800 1 (data) 0x4800 0x1400 2 (shared) 0xF000 0x1000 3 (stack) 0x0000 0x3000 Let s simulate a bit of this code to see what happens (PC=0x240): 1. Fetch 0x240. segment #? 0;? 0x240 address? Base=0x4000, so physical addr=0x4240 Fetch instruction at 0x4240. Get la $a0, varx Move 0x4050 $a0, Move PC+4 PC 2. Fetch 0x244. Translated to =0x4244. Get jal strlen Move 0x0248 $ra (return address!), Move 0x0360 PC 3. Fetch 0x360. Translated to =0x4360. Get li $v0,0 Move 0x0000 $v0, Move PC+4 PC 4. Fetch 0x364. Translated to =0x4364. Get lb $t0,($a0) Since $a0 is 0x4050, try to load byte from 0x4050 Translate 0x4050. segment #? 1;? 0x50 address? Base=0x4800, addr = 0x4850, Load Byte from 0x4850 $t0, Move PC+4 PC Lec 13.33 Lec 13.34 Observations about Segmentation address space has holes Segmentation efficient for sparse address spaces A correct program should never address gaps (except as mentioned in moment)» If it does, trap to kernel and dump core When it is OK to address outside valid range: This is how the stack and heap are allowed to grow For instance, stack takes fault, system automatically increases size of stack Need protection mode in segment table For example, code segment would be read-only Data and stack would be read-write (stores allowed) Shared segment could be read-only or read-write What must be saved/restored on context switch? Segment table stored in CPU, not in memory (small) Might store all of processes memory onto disk when switched (called swapping ) Lec 13.35 Schematic View of Swapping Extreme form of Context Switch: Swapping In order to make room for next process, some or all of the previous process is moved to disk» Likely need to send out complete segments This greatly increases the cost of context-switching Desirable alternative? Some way to keep only active portions of a process in memory at any one time Need finer granularity control over physical memory Lec 13.36

Paging: Memory in Fixed Size Chunks Problems with segmentation? Must fit variable-sized chunks into physical memory May move processes multiple times to fit everything Limited options for swapping to disk Fragmentation: wasted space External: free gaps between allocated chunks Internal: don t need all memory within allocated chunks Solution to fragmentation from segments? Allocate physical memory in fixed size chunks ( pages ) Every chunk of physical memory is equivalent» Can use simple vector of bits to handle allocation: 00110001110001101 110010» Each bit represents page of physical memory 1 allocated, 0 free Should pages be as big as our previous segments? No: Can lead to lots of internal fragmentation» Typically have small pages (1K-16K) Consequently: need multiple pages/segment Lec 13.37 Multiprogramming (Translation and Protection version 2) Problem: Run multiple applications in such a way that they are protected from one another Goals: Isolate processes and kernel from one another Allow flexible translation that:» Doesn t lead to fragmentation» Allows easy sharing between processes» Allows only part of process to be resident in physical memory (Some of the required) Hardware Mechanisms: General Address Translation» Flexible: Can fit physical chunks of memory into arbitrary places in users address space» Not limited to small number of segments» Think of this as providing a large number (thousands) of fixed-sized segments (called pages ) Dual Mode Operation» Protection base involving kernel/user distinction Lec 13.38 Address: How to Implement Paging? PageTablePtr page #0 page #1 page #2 PageTableSize > page #3 page #4 Access page #5 N Address Check Perm Access Error Error Page Table (One per process) Resides in physical memory Contains physical page and permission for each virtual page» Permissions include: Valid bits, Read, Write, etc address mapping from address copied to Address» Example: 10 bit offset 1024-byte pages page # is all remaining bits» Example for 32-bits: 32-10 = 22 bits, i.e. 4 million entries» page # copied from table into physical address Check Page Table bounds and permissions Address (Process A): What about Sharing? PageTablePtrA page #0 page #1 page #2 page #3 page #4 page #5 PageTablePtrB page #0 page #1 page #2 page #3 page #4 page #5 Address: Process B N N N Shared Page This physical page appears in address space of both processes Lec 13.39 Lec 13.40

Simple Page Table Discussion What needs to be switched on a context switch? Page table pointer and limit Simple Page Table Analysis Pros» Simple memory allocation» Easy to Share Con: What if address space is sparse?» E.g. on UNIX, code starts at 0, stack starts at (2 31-1).» With 1K pages, need 4 million page table entries! Con: What if table really big?» Not all pages used all the time would be nice to have working set of page table in memory How about combining paging and segmentation? Segments with pages inside them? Need some sort of multi-level translation Lec 13.41 What is in a Page Table Entry (PTE)? What is in a Page Table Entry (or PTE)? Pointer to next-level page table or to actual page Permission bits: valid, read-only, read-write, write-only Example: Intel x86 architecture PTE: Address same format previous slide (10, 10, 12-bit offset) Intermediate page tables called Directories Page Frame Number ( Page Number) Free (OS) 0 L D A 31-12 11-9 8 7 6 5 4 3 2 1 0 P: Present (same as valid bit in other architectures) W: Writeable U: User accessible PWT: Page write transparent: external cache write-through PCD: Page cache disabled (page cannot be cached) A: Accessed: page has been accessed recently D: Dirty (PTE only): page has been modified recently L: L=1 4MB page (directory only). Bottom 22 bits of virtual address serve as offset PWT PCD U W P Lec 13.42 Examples of how to exploit a PTE How do we use the PTE? Invalid PTE can imply different things:» Region of address space is actually invalid or» Page/directory is just somewhere else than memory Validity checked first» OS can use other (say) 31 bits for location info Usage Example: Demand Paging Keep only active pages in memory Place others on disk and mark their PTEs invalid Usage Example: Copy on Write UNIX fork gives copy of parent address space to child» Address spaces disconnected after child created How to do this cheaply?» Make copy of parent s page tables (point at same memory)» Mark entries in both sets of page tables as read-only» Page fault on write creates two copies Usage Example: Zero Fill On Demand New data pages must carry no information (say be zeroed) Mark PTEs as invalid; page fault on use gets zeroed page Often, OS creates zeroed pages in background Multi-level Translation: Segments + Pages What about a tree of tables? Lowest level page table memory still allocated with bitmap Higher levels often segmented Could have any number of levels. Example (top segment): Address: Seg # Base0 Limit0 V Base1 Limit1 V Base2 Limit2 V Base3 Limit3 N Base4 Limit4 V Base5 Limit5 N Base6 Limit6 N Base7 Limit7 V > page #0 page #1 page #2 page #3 page #4 page #5 Access Error Error What must be saved/restored on context switch? Contents of top-level segment registers (for this example) Pointer to top-level table (page table) N Address Check Perm Access Lec 13.43 Lec 13.44

Process A Process B What about Sharing (Complete Segment)? Seg # Base0 Limit0 V Base1 Limit1 V Base2 Limit2 V Base3 Limit3 N Base4 Limit4 V Base5 Limit5 N Base6 Limit6 N Base7 Limit7 V Seg # Base0 Limit0 V Base1 Limit1 V Base2 Limit2 V Base3 Limit3 N Base4 Limit4 V Base5 Limit5 N Base6 Limit6 N Base7 Limit7 V page #0 page #1 page #2 page #3 page #4 page #5 N Shared Segment Lec 13.45 Another common example: two-level page table 10 bits 10 bits 12 bits Address: P1 index P2 index Address: PageTablePtr 4 bytes Tree of Page Tables Tables fixed size (1024 entries) On context-switch: save single PageTablePtr register Valid bits on Page Table Entries Don t need every 2 nd -level table Even when exist, 2 nd -level tables 4 bytes can reside on disk if not in use 4KB Lec 13.46 Multi-level Translation Analysis Pros: Only need to allocate as many page table entries as we need for application» In other wards, sparse address spaces are easy Easy memory allocation Easy Sharing» Share at segment or page level (need additional reference counting) Cons: One pointer per page (typically 4K 16K pages today) Page tables need to be contiguous» However, previous example keeps tables to exactly one page in size Two (or more, if >2 levels) lookups per reference» Seems very expensive! Lec 13.47 Inverted Page Table With all previous examples ( Forward Page Tables ) Size of page table is at least as large as amount of virtual memory allocated to processes memory may be much less» Much of process space may be out on disk or not in use Hash Table Answer: use a hash table Called an Inverted Page Table Size is independent of virtual address space Directly related to amount of physical memory Very attractive option for 64-bit address spaces Cons: Complexity of managing hash changes Often in hardware! Lec 13.48

Recall: Caching Concept Cache: a repository for copies that can be accessed more quickly than the original Make frequent case fast and infrequent case less dominant Caching underlies many of the techniques that are used today to make computers fast Can cache: memory locations, address translations, pages, file blocks, file names, network routes, etc Only good if: Frequent case frequent enough and Infrequent case not too expensive Important measure: Average Access time = (Hit Rate x Hit Time) + (Miss Rate x Miss Time) Lec 13.49 Address: Why does caching matter for Memory? Seg # Base0 Limit0 V Base1 Limit1 V Base2 Limit2 V Base3 Limit3 N Base4 Limit4 V Base5 Limit5 N Base6 Limit6 N Base7 Limit7 V page #0 page #1 page #2 page #3 page #4 page #5 Access Error Cannot afford to translate on every access At least three DRAM accesses per actual DRAM access Or: perhaps I/O if page table partially on disk! Even worse: What if we are using caching to make memory access faster than DRAM access??? Solution? Cache translations! Translation Cache: TLB ( Translation Lookaside Buffer ) N > Address Check Perm Access Error Lec 13.50 CPU Caching Applied to Address Translation Address TLB Cached? Yes No Translate (MMU) Address Data Read or Write (untranslated) Question is one of page locality: does it exist? Instruction accesses spend a lot of time on the same page (since accesses sequential) Stack accesses have definite locality of reference Data accesses have less page locality, but still some Can we have a TLB hierarchy? Sure: multiple levels at different sizes/speeds Memory Lec 13.51 What Actually Happens on a TLB Miss? Hardware traversed page tables: On TLB miss, hardware in MMU looks at current page table to fill TLB (may walk multiple levels)» If PTE valid, hardware fills TLB and processor never knows» If PTE marked as invalid, causes Page Fault, after which kernel decides what to do afterwards Software traversed Page tables (like MIPS) On TLB miss, processor receives TLB fault Kernel traverses page table to find PTE» If PTE valid, fills TLB and returns from fault» If PTE marked as invalid, internally calls Page Fault handler Most chip sets provide hardware traversal Modern operating systems tend to have more TLB faults since they use translation for many things Examples:» shared segments» user-level portions of an operating system Lec 13.52

What happens on a Context Switch? Need to do something, since TLBs map virtual addresses to physical addresses Address Space just changed, so TLB entries no longer valid! Options? Invalidate TLB: simple but might be expensive» What if switching frequently between processes? Include ProcessID in TLB» This is an architectural solution: needs hardware What if translation tables change? For example, to move page from memory to disk or vice versa Must invalidate TLB entry!» Otherwise, might think that page is still in memory! Lec 13.53 What TLB organization makes sense? CPU TLB Cache Memory Needs to be really fast Critical path of memory access» In simplest view: before the cache» Thus, this adds to access time (reducing cache speed) Seems to argue for Direct Mapped or Low Associativity However, needs to have very few conflicts! With TLB, the Miss Time extremely high! This argues that cost of Conflict (Miss Time) is much higher than slightly increased cost of access (Hit Time) Thrashing: continuous conflicts between accesses What if use low order bits of page as index into TLB?» First page of code, data, stack may map to same entry» Need 3-way associativity at least? What if use high order bits as index?» TLB mostly unused for small programs Lec 13.54 TLB organization: include protection How big does TLB actually have to be? Usually small: 128-512 entries Not very big, can support higher associativity TLB usually organized as fully-associative cache Lookup is by Address Returns Address + other info What happens when fully-associative is too slow? Put a small (4-16 entry) direct-mapped cache in front Called a TLB Slice Example for MIPS R3000: Address Address Dirty Ref Valid Access ASID 0xFA00 0x0003 Y N Y R/W 34 0x0040 0x0010 N Y Y R 0 0x0041 0x0011 N Y Y R 0 Example: R3000 pipeline includes TLB stages MIPS R3000 Pipeline Inst Fetch Dcd/ Reg ALU / E.A Memory Write Reg TLB I-Cache RF Operation WB E.A. TLB D-Cache TLB 64 entry, on-chip, fully associative, software TLB fault handler Address Space ASID V. Page Number 6 20 12 0xx User segment (caching based on PT/TLB entry) 100 Kernel physical space, cached 101 Kernel physical space, uncached 11x Kernel virtual space Allows context switching among 64 user processes without TLB flush Lec 13.55 Lec 13.56

Reducing translation time further As described, TLB lookup is in serial with cache lookup: Address V page no. 10 offset Overlapping TLB & Cache Access Here is how this might work with a 4K cache: 32 TLB assoc lookup index 4K Cache 1 K TLB Lookup V Access Rights Machines with TLBs go one step further: they overlap TLB lookup with cache access. Works because offset available early PA P page no. offset 10 Address Lec 13.57 20 10 2 4 bytes page # disp 00 Hit/ Miss FN = FN Data Hit/ Miss What if cache size is increased to 8KB? Overlap not complete Need to do something else. See CS152/252 Another option: Caches Tags in cache are virtual addresses Translation only happens on cache misses Lec 13.58 Summary (1/2) Memory is a resource that must be shared Controlled Overlap: only shared when appropriate Translation: Change Addresses into Addresses Protection: Prevent unauthorized Sharing of resources Segment Mapping Segment registers within processor Segment ID associated with each access» Often comes from portion of virtual address» Can come from bits in instruction instead (x86) Each segment contains base and limit information» (rest of address) adjusted by adding base Page Tables Memory divided into fixed-sized chunks of memory page number from virtual address mapped through page table to physical page number of virtual address same as physical address Large page tables can be placed into virtual memory Multi-Level Tables address mapped to series of tables Permit sparse population of address space Inverted page table Size of page table related to physical memory size Lec 13.59 Summary (2/2) PTE: Page Table Entries Includes physical page number Control info (valid bit, writeable, dirty, user, etc) A cache of translations called a Translation Lookaside Buffer (TLB) Relatively small number of entries (< 512) Fully Associative (Since conflict misses expensive) TLB entries contain PTE and optional process ID On TLB miss, page table must be traversed If located PTE is invalid, cause Page Fault On context switch/change in page table TLB entries must be invalidated somehow TLB is logically in front of cache Thus, needs to be overlapped with cache access to be really fast Lec 13.60