Ken Birman. What’s the goal? Each program you build consists of Code you wrote Pre-existing libraries your code accesses In early days, the balance.

Slides:



Advertisements
Similar presentations
Memory.
Advertisements

Part IV: Memory Management
Chapter 6: Memory Management
Operating Systems Lecture Notes Memory Management Matthew Dailey Some material © Silberschatz, Galvin, and Gagne, 2002.
Memory Management Chapter 7.
Allocating Memory.
Ken Birman. 2 Reminders We’ve focused on demand paging Each process is allocated  pages of memory As a process executes it references pages On a miss.
1 CSE 380 Computer Operating Systems Instructor: Insup Lee University of Pennsylvania, Fall 2002 Lecture Note: Memory Management.
1 1 Lecture 4 Structure – Array, Records and Alignment Memory- How to allocate memory to speed up operation Structure – Array, Records and Alignment Memory-
Multiprocessing Memory Management
CS 104 Introduction to Computer Science and Graphics Problems
Memory Management. 2 How to create a process? On Unix systems, executable read by loader Compiler: generates one object file per source file Linker: combines.
Memory Management 1 CS502 Spring 2006 Memory Management CS-502 Spring 2006.
CS-3013 & CS-502, Summer 2006 Memory Management1 CS-3013 & CS-502 Summer 2006.
Memory Management Chapter 5.
Thrashing and Memory Management
Memory Management Five Requirements for Memory Management to satisfy: –Relocation Users generally don’t know where they will be placed in main memory May.
Virtual Memory II: Thrashing Working Set Algorithm Dynamic Memory Management.
Layers and Views of a Computer System Operating System Services Program creation Program execution Access to I/O devices Controlled access to files System.
Basics of Operating Systems March 4, 2001 Adapted from Operating Systems Lecture Notes, Copyright 1997 Martin C. Rinard.
Operating System Chapter 7. Memory Management Lynn Choi School of Electrical Engineering.
CS 346 – Chapter 8 Main memory –Addressing –Swapping –Allocation and fragmentation –Paging –Segmentation Commitment –Please finish chapter 8.
Dynamically Linked Libraries. 2 What’s the goal? Each program you build consists of –Code you wrote –Pre-existing libraries your code accesses In early.
Review of Memory Management, Virtual Memory CS448.
8.4 paging Paging is a memory-management scheme that permits the physical address space of a process to be non-contiguous. The basic method for implementation.
1 Memory Management Chapter Basic memory management 4.2 Swapping (εναλλαγή) 4.3 Virtual memory (εικονική/ιδεατή μνήμη) 4.4 Page replacement algorithms.
Operating Systems Memory management. Memory Management List of Topics 1. Memory Management 2. Memory In Systems Design 3. Binding Times 4. Introduction.
CIS250 OPERATING SYSTEMS Memory Management Since we share memory, we need to manage it Memory manager only sees the address A program counter value indicates.
Memory Management – Page 1 of 49CSCI 4717 – Computer Architecture Memory Management Uni-program – memory split into two parts –One for Operating System.
Chapter 8 – Main Memory (Pgs ). Overview  Everything to do with memory is complicated by the fact that more than 1 program can be in memory.
1 Address Translation Memory Allocation –Linked lists –Bit maps Options for managing memory –Base and Bound –Segmentation –Paging Paged page tables Inverted.
Operating Systems CMPSC 473 Virtual Memory Management (4) November – Lecture 22 Instructor: Bhuvan Urgaonkar.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Memory: Relocation.
1 Memory Management Chapter 7. 2 Memory Management Subdividing memory to accommodate multiple processes Memory needs to be allocated to ensure a reasonable.
1 Memory Management Basics. 2 Program P Basic Memory Management Concepts Address spaces Physical address space — The address space supported by the hardware.
By Teacher Asma Aleisa Year 1433 H.   Goals of memory management  To provide a convenient abstraction for programming.  To allocate scarce memory.
Operating Systems Lecture 14 Segments Adapted from Operating Systems Lecture Notes, Copyright 1997 Martin C. Rinard. Zhiqing Liu School of Software Engineering.
1 Memory Management Chapter 7. 2 Memory Management Subdividing memory to accommodate multiple processes Memory needs to be allocated to ensure a reasonable.
Processes and Virtual Memory
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Memory Management Overview.
NETW3005 Memory Management. Reading For this lecture, you should have read Chapter 8 (Sections 1-6). NETW3005 (Operating Systems) Lecture 07 – Memory.
CS 241 Discussion Section (12/1/2011). Tradeoffs When do you: – Expand Increase total memory usage – Split Make smaller chunks (avoid internal fragmentation)
Chapter 8: Main Memory. 8.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 22, 2005 Memory and Addressing It all starts.
COMP091 – Operating Systems 1 Memory Management. Memory Management Terms Physical address –Actual address as seen by memory unit Logical address –Address.
Chapter 7 Memory Management Eighth Edition William Stallings Operating Systems: Internals and Design Principles.
Memory Management. 2 How to create a process? On Unix systems, executable read by loader Compiler: generates one object file per source file Linker: combines.
Memory Management Chapter 5 Advanced Operating System.
Chapter 7: Main Memory CS 170, Fall Program Execution & Memory Management Program execution Swapping Contiguous Memory Allocation Paging Structure.
Memory Management.
Dynamic Memory Allocation
PA1 is out Best by Feb , 10:00 pm Enjoy early
Chapter 8: Main Memory.
Memory Management Lectures notes from the text supplement by Siberschatz and Galvin Modified by B.Ramamurthy 11/12/2018.
Operating System Concepts
Optimizing Malloc and Free
Last Time We’ve focused on demand paging
Memory Management Lectures notes from the text supplement by Siberschatz and Galvin Modified by B.Ramamurthy Chapter 8 11/24/2018.
Memory Management Lectures notes from the text supplement by Siberschatz and Galvin Modified by B.Ramamurthy Chapter 9 12/1/2018.
Lecture 3: Main Memory.
CSE 451: Operating Systems Autumn 2005 Memory Management
Operating System Chapter 7. Memory Management
CSE 451: Operating Systems Autumn 2003 Lecture 10 Paging & TLBs
CSE451 Virtual Memory Paging Autumn 2002
CSE 451: Operating Systems Autumn 2003 Lecture 9 Memory Management
Memory Management Lectures notes from the text supplement by Siberschatz and Galvin Modified by B.Ramamurthy Chapter 9 4/5/2019.
CSE 451: Operating Systems Autumn 2003 Lecture 10 Paging & TLBs
CSE 451: Operating Systems Autumn 2003 Lecture 9 Memory Management
COMP755 Advanced Operating Systems
CSE 542: Operating Systems
Working Set Algorithm Thrashing
Presentation transcript:

Ken Birman

What’s the goal? Each program you build consists of Code you wrote Pre-existing libraries your code accesses In early days, the balance was “mostly your code” and libraries were small But by now, libraries can be immense! 3

Some libraries The formatted I/O library The windowing subsystem library Scientific computing libraries Specialized end-to-end communication libraries doing things like Encoding data into XML Implementing web security Implementing RPC 4

Picture? By early 1980’s, if we accounted for memory in use on a typical computer, we found that Everything needed virtual memory Main “use” of memory was for libraries Dynamically linked libraries emerged as a response to this observation 5

Basic idea First, let multiple processes share a single copy of each library Thinking: Why have multiple copies of identical code loaded at the same time? Next, don’t even load a library until it gets used Thinking: Perhaps the application is linked to some libraries for “obscure” reasons and usually doesn’t even access those routines! 6

Issues? We need to look at: How this can be implemented Overheads it will incur at runtime Impact this has on the size and form of page tables 7

Implementation Recall that compiler transforms each part of a program or library into several kinds of segments: 8 Your code Compiled instructions (Linux directive:.text) Initialized data (.data) Zeroed data (.bss)

At runtime… A process will end up consisting of your main routine(s) and the segments to which it was linked 9 Code bss Data Main program Libraries Code bss Data Code bss Data ++ Primary stack Heap Thread stack

Memory layout for different programs will differ Point is that segment sizes differ… E.g. you and I both use the window library But your program is huge and mine is small Even when we share a library, the code segment won’t be at the same place in memory for each of us! How can we compile code for this case? 10

The basic issue Comes down to how machine instructions are represented load _Xmin,r2 Loop:load-(r0),r1 addr1,r2,r1 store r1,(r2)+ add $-5,r3 jnz Loop 11 Where was the constant -5 stored in memory? _Xmin is in the data segment. Each copy of the library has its own data segment at a different place in memory! Does the linker “fix” the address to which we jump at link time?

Position independent code (PIC) Idea is to compile our code so that if we have a register containing the base of the data segment for each library, it won’t have any references to actual addresses in it Instead of _Xmin, compiler generates something more like _XminOffset(R6) Assumes that R6 is loaded with appropriate base address! 12

What about the jump? Here, compiler can generate PC-relative addressing Instead ofjnz Loop… …. jnz-64(PC) This kind of code is a little slower hence you usually have to TELL the compiler or it won’t do this 13

Managing PIC code Once we generate PIC code and data segments, each process needs a table One entry per code segment It tells where the data segment for that code segment is located To call the code segment Push old value of Rb (base register) to the stack Load appropriate value from table Call the procedure in question Pop previous value of Rb back from stack 14

“Call the procedure” Notice that even the call needs to be done as an indirection! In C: Suppose that code_seg_base is the base of the code segment for a function that returns an integer, and we want to call a procedure at offset 0x200 in the code segment We call: res = (*(code_seg_base+0x200))(args) Assumes code_seg_base is declared like this: int (*code_seg_base)(); 15

So… Compile each library using PIC When loading the program Put the code segment anywhere, but remember where you put it (code_seg_base) Make a fresh, private copy of the data segment – same size as the “original” copy, but private for this process. Remember base address for use during procedure calls Initialize it from the original version Allocate and zero a suitable BSS region 16

In Linux… This results in a table, potentially visible to debugger, in Linux called _ _ DYNAMIC Entries consist of Name of the file containing the library code Status bits: 0 if not yet loaded, 1 if loaded Code base address Data segment base address On first access “Map” the file into memory, remembering base addr Malloc and initialize copy of data segment Update the entry for this segment accordingly 17

Mapping a file Everyone is used to the normal file system interface File open, seek, read, write But Linix and Windows also support memory mapping of files Base_addr = mmap(“file-name”, …) This creates a window in memory and you can directly access the file contents at that address range! Moreover, different processes can share a file Arguments tell mmap how to set up permissions 18

Summary So… Compile with PIC directive to compiler But also need to decide where the library will be placed in the file system Now, compile application program and tell it that the windowing library is a DLL It needs to know the file name, e.g. /lib/xxx.so Resulting executable is tiny… and will link to the DLL at runtime 19

DLLs are popular! Even Microsoft DOS had them In DOS, they sometimes put multiple DLLs at the same base address Requires them to swap A out if B gets used, and vice versa, but makes memory footprint of programs smaller Very widely used now… almost universal 20

Consequence for page table? A single process may be linked to a LOT of segments Suppose: 10 libraries and 30 threads You’ll have 2 segments per library Plus approximately five for the main process Plus 30 for lightweight thread stacks … a total of 55 segments! And these are spread “all over the place” with big gaps between them (why?) 21

Issue Page table might be huge Covers an enormous range of addresses And has big holes in it One approach: page the page table Costs get high Better approach: an inverted page table 22

Inverted page table Must be implemented by hardware Idea is this: Instead of having one page table entry per virtual memory page, have one PTE per physical memory page It tells which process and which virtual page is currently resident in this physical page The O/S keeps “remaining” PTE’s for non-resident virtual pages in some other table 23

Benefits? With an inverted page table, we have an overhead of precisely one PTE per physical page of memory CPU needs to be able to search this quickly Turns out to be identical to what TLB already was doing (an associative lookup) So can leverage existing hardware to solve this problem 24

Summary All modern systems use DLLs heavily You can build your own Just need to understand how to tell the compiler what you are doing (for PIC, and to agree on the name for the DLL files) Only some computers support inverted page tables Others typically have a two-level paging scheme that pages the page table 25

26 Dynamic Memory Management Notice that the O/S kernel can manage memory in a fairly trivial way: All memory allocations are in units of “pages” And pages can be anywhere in memory… so a simple free list is the only data structure needed But for variable-sized objects, we need a heap: Used for all dynamic memory allocations malloc/free in C, new/delete in C++, new/garbage collection in Java Is a very large array allocated by OS, managed by program

27 Allocation and deallocation What happens when you call: int *p = (int *)malloc(2500*sizeof(int)); Allocator slices a chunk of the heap and gives it to the program free(p); Deallocator will put back the allocated space to a free list Simplest implementation: Allocation: increment pointer on every allocation Deallocation: no-op Problems: lots of fragmentation heap (free memory) current free position allocation

28 Memory allocation goals Minimize space Should not waste space, minimize fragmentation Minimize time As fast as possible, minimize system calls Maximizing locality Minimize page faults cache misses And many more Proven: impossible to construct “always good” memory allocator

29 Memory Allocator What allocator has to do: Maintain free list, and grant memory to requests Ideal: no fragmentation and no wasted time What allocator cannot do: Control order of memory requests and frees A bad placement cannot be revoked Main challenge: avoid fragmentation a malloc(20)? b

30 Impossibility Results Optimal memory allocation is NP-complete for general computation Given any allocation algorithm,  streams of allocation and deallocation requests that defeat the allocator and cause extreme fragmentation

31 Best Fit Allocation Minimum size free block that can satisfy request Data structure: List of free blocks Each block has size, and pointer to next free block Algorithm: Scan list for the best fit

32 Best Fit gone wrong Simple bad case: allocate n, m (m<n) in alternating orders, free all the m’s, then try to allocate an m+1. Example: If we have 100 bytes of free memory Request sequence: 19, 21, 19, 21, 19 Free sequence: 19, 19, 19 Wasted space: 57!

33 A simple scheme Each memory chunk has a signature before and after Signature is an int +ve implies the a free chunk -ve implies that the chunk is currently in use Magnitude of chunk is its size So, the smallest chunk is 3 elements: One each for signature, and one for holding the data

34 Which chunk to allocate? Maintain a list of free chunks Binning, doubly linked lists, etc Use best fit or any other strategy to determine page For example: binning with best-fit What if allocated chunk is much bigger than request? Internal fragmentation Solution: split chunks Will not split unless both chunks above a minimum size What if there is no big-enough free chunk? sbrk or mmap Possible page fault

35 What happens on free? Identify size of chunk returned by user Change sign on both signatures (make +ve) Combine free adjacent chunks into bigger chunk Worst case when there is one free chunk before and after Recalculate size of new free chunk Update the signatures Don’t really need to erase old signatures

36 Example Initially one chunk, split and make signs negative on malloc p = malloc(2 * sizeof (int));

37 Example q gets 4 words, although it requested for p = malloc(2 * sizeof (int)); q = malloc(3 * sizeof (int));

38 Design features Which free chunks should service request Ideally avoid fragmentation… requires future knowledge Split free chunks to satisfy smaller requests Avoids internal fragmentation Coalesce free blocks to form larger chunks Avoids external fragmentation

39 Buddy-Block Scheme Invented by Donald Knuth, very simple Idea: Work with memory regions that are all powers of 2 times some “smallest” size 2 k times b Round each request up to have form b*2 k

Buddy-Block Scheme Buddy Block in Action P = malloc(290) … round up to 2 8 = 512 Q = malloc (111) …. Round up to 2 7 = 128 Z = malloc(750) …. Round up to 2 9 = 1024 Free(Q) … consolidate

41 Buddy Block Scheme Keep a free list for each block size (each k) When freeing an object, combine with adjacent free regions if this will result in a double-sized free object Basic actions on allocation request: If request is a close fit to a region on the free list, allocate that region. If request is less than half the size of a region on the free list, split the next larger size of region in half If request is larger than any region, double the size of the heap (this puts a new larger object on the free list)

42 How to get more space? In Unix, system call sbrk() Used by malloc if heap needs to be expanded Notice that heap only grows on “one side” /* add nbytes of valid virtual address space */ void *get_free_space(unsigned nbytes) { void *p; if(!(p = sbrk(nbytes))) error(“virtual memory exhausted”); return p; }

43 Malloc & OS memory management Relocation OS allows easy relocation (change page table) Placement decisions permanent at user level Size and distribution OS: small number of large objects Malloc: huge number of small objects heap code data stack

Summary Modern OS includes a variety of memory allocators Best of all is the one used for paging but this is because pages are “interchangable” For other purposes, we use fancier structures, like Knuth’s buddy-block scheme Some applications also include their own free-list managers, to avoid cost of malloc/free for typical objects