Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 10 Memory management Nov. 17, 2015 Kyu Ho Park.

Similar presentations


Presentation on theme: "Lecture 10 Memory management Nov. 17, 2015 Kyu Ho Park."— Presentation transcript:

1 Lecture 10 Memory management Nov. 17, 2015 Kyu Ho Park

2 Memory Management  Ideally programmers want memory that is  large  fast  non volatile  Memory hierarchy  small amount of fast, expensive memory – cache  some medium-speed, medium price main memory  storage class memory using SSD  gigabytes of slow, cheap disk storage  Memory manager handles the memory hierarchy

3 Basic Memory Management Monoprogramming without Swapping or Paging Three simple ways of organizing memory - an operating system with one user process

4 Multiprogramming with Fixed Partitions  Fixed memory partitions  separate input queues for each partition  single input queue

5 Relocation and Protection  Cannot be sure where program will be loaded in memory  address locations of variables, code routines cannot be absolute  must keep a program out of other processes’ partitions  Use base and limit values  address locations added to base value to map to physical addr  address locations larger than limit value is an error

6 Dynamic relocation using a relocation register CPU logical address relocation register 11000 + memory physical address 123 11123 MMU

7 HW address protection with base and limit registers CPU trap to operating system monitor-addressing error address memory base base + limit ≥< yes no

8 8 Memory Management with Bit Maps  Part of memory with 5 processes, 3 holes  tick marks show allocation units  shaded regions are free  Corresponding bit map  Same information as a list

9 Contiguous Allocation  Main memory usually into two partitions:  Resident operating system, usually held in low memory with interrupt vector  User processes then held in high memory  Relocation registers used to protect user processes from each other, and from changing operating-system code and data  Base register contains value of smallest physical address  Limit register contains range of logical addresses – each logical address must be less than the limit register  MMU maps logical address dynamically

10 Contiguous Allocation (Cont.)  Multiple-partition allocation  Hole – block of available memory; holes of various size are scattered throughout memory  When a process arrives, it is allocated memory from a hole large enough to accommodate it  Operating system maintains information about: a) allocated partitions b) free partitions (hole) OS process 5 process 8 process 2 OS process 5 process 2 OS process 5 process 2 OS process 5 process 9 process 2 process 9 process 10

11 Dynamic Storage-Allocation Problem  First-fit: Allocate the first hole that is big enough  Best-fit: Allocate the smallest hole that is big enough; must search entire list, unless ordered by size  Produces the smallest leftover hole  Worst-fit: Allocate the largest hole; must also search entire list  Produces the largest leftover hole How to satisfy a request of size n from a list of free holes First-fit and best-fit better than worst-fit in terms of speed and storage utilization

12 Fragmentation  External Fragmentation – total memory space exists to satisfy a request, but it is not contiguous  Internal Fragmentation – allocated memory may be slightly larger than requested memory; this size difference is memory internal to a partition, but not being used  Reduce external fragmentation by compaction  Shuffle memory contents to place all free memory together in one large block  Compaction is possible only if relocation is dynamic, and is done at execution time  I/O problem  Latch job in memory while it is involved in I/O  Do I/O only into OS buffers

13 13 Virtual Memory Paging (1) The position and function of the MMU

14 14 Paging (2) The relation between virtual addresses and physical memory addres- ses given by page table

15 Address Translation Scheme  Address generated by CPU is divided into:  Page number (p) – used as an index into a page table which contains base address of each page in physical memory  Page offset (d) – combined with base address to define the physical memory address that is sent to the memory unit  For given logical address space 2 m and page size 2 n page number page offset p d m - n n

16 Paging Hardware f1111…..1111 page table CPU physical address pd fd p f f0000…..0000 f physical address logical address

17 Paging Model of Logical and Physical Memory page 0 page 1 page 2 page 3 frame number logical memory 01 14 23 37 page table 0 1page 0 2 3page 2 4page 1 5 6 7page 3 physical address

18 Implementation of Page Table  Page table is kept in main memory  Page-table base register (PTBR) points to the page table  Page-table length register (PRLR) indicates size of the page table  In this scheme every data/instruction access requires two memory accesses. One for the page table and one for the data/instruction.  The two memory access problem can be solved by the use of a special fast-lookup hardware cache called associative memory or translation look-aside buffers (TLBs)  Some TLBs store address-space identifiers (ASIDs) in each TLB entry – uniquely identifies each process to provide address-space protection for that process

19 Memory Protection  Memory protection implemented by associating protection bit with each frame  Valid-invalid bit attached to each entry in the page table:  “valid” indicates that the associated page is in the process’ logical address space, and is thus a legal page  “invalid” indicates that the page is not in the process’ logical address space

20 Valid (v) or Invalid (i) Bit In A Page Table page 0 page 1 page 2 page 3 page 4 page 5 00000 02v 13v 24v 37v 48v 59v 60i 70i page table 0 1 2page 0 3page 1 4page 2 5 6 7page 3 8page 4 9page 5 page n frame number valid-invalid bit 10,468 12,287 …

21 Shared Pages  Shared code  One copy of read-only (reentrant) code shared among processes (i.e., text editors, compilers, window systems).  Shared code must appear in same location in the logical address space of all processes  Private code and data  Each process keeps a separate copy of the code and data  The pages for the private code and data can appear anywhere in the logical address space

22 Shared Pages Example ed 1 ed 2 ed 3 data 1 process P 1 0 1data 1 2data 3 3ed 1 4ed 2 5 6ed 3 7data 2 8 9 10 11 page table for P 1 3 4 6 1 ed 1 ed 2 ed 3 data 2 process P 2 page table for P 2 3 4 6 7 ed 1 ed 2 ed 3 data 3 process P 3 page table for P 3 3 4 6 2

23 Two-Level Paging Example  A logical address (on 32-bit machine with 1K page size) is divided into:  a page number consisting of 22 bits  a page offset consisting of 10 bits  Since the page table is paged, the page number is further divided into:  a 12-bit page number  a 10-bit page offset  Thus, a logical address is as follows: where p i is an index into the outer page table, and p 2 is the displacement within the page of the outer page table page number page offset pipi p2p2 d 12 10

24 Address-Translation Scheme p1p1 p2p2 d p2p2 page of page table p1p1 outer page table d logical address

25 Three-level Paging Scheme 2nd outer pageouter pageinner pageoffset p1p1 p2p2 p3p3 d 3210 12 outer pageinner pageoffset p1p1 p2p2 d 421012

26 Paging Hardware With TLB logical address pd CPU page table f p page numbe r frame numbe s TLB miss TLB fd physical address physical memory TLB hit

27 27 Page Replacement Algorithms  Page fault forces choice  which page must be removed  make room for incoming page  Modified page must first be saved  unmodified just overwritten  Better not to choose an often used page  will probably need to be brought back in soon

28 28 Optimal Page Replacement Algorithm  Replace page needed at the farthest point in future  Optimal but unrealizable  Estimate by …  logging page use on previous runs of process  although this is impractical

29 29 Not Recently Used Page Replacement Algorithm  Each page has Reference bit, Modified bit  bits are set when page is referenced, modified  Pages are classified 1. not referenced, not modified 2. not referenced, modified 3. referenced, not modified 4. referenced, modified  NRU removes page at random  from lowest numbered non empty class

30 30 FIFO Page Replacement Algorithm  Maintain a linked list of all pages  in order they came into memory  Page at beginning of list replaced  Disadvantage  page in memory the longest may be often used

31 31 Second Chance Page Replacement Algorithm  Operation of a second chance  pages sorted in FIFO order  Page list if fault occurs at time 20, A has R bit set (numbers above pages are loading times)

32 32 The Clock Page Replacement Algorithm When a page fault occurs, the page the hand is pointing to is inspected. The action taken depends on the R bit: R = 0 : Evict the page R = 1 : Clear R and advance hand

33 33 Least Recently Used (LRU)  Assume pages used recently will used again soon  throw out page that has been unused for longest time  Must keep a linked list of pages  most recently used at front, least at rear  update this list every memory reference !!  Alternatively keep counter in each page table entry  choose page with lowest value counter  periodically zero the counter

34 34 Page Size  Overhead due to page table and internal fragmentation  Where  s = average process size in bytes  p = page size in bytes  e = page entry page table space internal fragmentation Optimized when

35 35 Memory Management in Linux

36 36 Memory Address Logical address segment + offset (displacement) Linear address (Virtual address) a single 32-bit unsigned integer Physical address 32-bit or 36-bit unsigned integer

37 Computer Engineering Research Laboratory, EECS, KAIST 37 Regular paging Two-level scheme  to reduce the amount of RAM required for per-process Page Tables. The physical address of the Page Directory in use is stored in a control register cr3. The 32 bits of a linear addr Directory : 10 bits Table : 10 bits Offset : 12 bits Translation Tables : Page Directory Page Table

38 Computer Engineering Research Laboratory, EECS, KAIST 38 Extended Paging Page frames  4MB Do not use Page Tables and save memory and preserve TLB entries. By setting the Page Size flag of a Page Directory entry. The 32 bits of Linear addr Directory : 10 bits Offset : 22 bits Page Directory entries The Page Size flag must be set. Only the 10 msb of the 20-bit physical address field are significant.

39 Computer Engineering Research Laboratory, EECS, KAIST 39 Paging in Linux Starting with version 2.6.11, a four-level paging model has been adopted.

40 40 UMA CC MM Bus Bank

41 41 C Bus C MM CC MM Bank NUMA

42 42 Bank and Node Bank: a set of memories whose access time are same. UMA has one bank UNMA ha several banks. The bank is called ‘node’ in Linux( /include/linux/mmzone.h). A node is represented by struct pglist_data. The bank of UMA is represented by struct pglist_data contig_page_data. The banks of NUMA are represented by struct pglist_data *pgdat_list[]. typedef struct pglist_data { struct zone node_zones[MAX_NR_ZONES]; struct zonelist node_zone_lists[GFP_ZONE_TYPES]; … int node_id; } pg_data_t;

43 43 zone(/include/linux/mmzone.h) Linux partitions the physical memory of every memory node into 3 zones. ZONE_DMA -page frames of memory below 16MB. -for old ISA bus. ZONE_NORMAL -page frames of memory at and above 16MB and below 896MB. ZOME_HIGHMEM -Page frames of memory at and above 896 MB.

44 44 ZONE_HIGH ZONE_NORMAL ZONE_DMA KERNEL SPACE USER SPACE Physical Address Space Virtual Address Space 4GB 3GB 896MB 16MB 0 0 Physical and Virtual Address Space

45 kernel stack kernel vmalloc area kernel bss kernel data kernel text 0xfffffff 3GB bleak kernel page table process stack process heap process bss process data process text end edata etext 0x00000000 process 1 page table Virtual Space of Users’ Processes and Kernel 1G 3G

46 46 Zone struct zone{ … unsigned long min_slab_pages; struct free_areafree_area[MAX_ORDER]; … struct pglist_data*zone_pgdat; struct page*zone_mem_map; … } We can get the zone information of current system, $cat /proc/zoneinfo.

47 47 page frame( /include/linux/mm_types.h) page frame: a page of a physical memory is called ‘ a page frame’ and the page frames are managed at each zone. page frame data structure: struct page. struct page{ page_flags_t flags; … }; page are accessed through mem_map.

48 48 pg_data_t node_zones ZONE_DMA ZONE_NORMAL ZONE_HIGHMEM zone_mem_map struct page Relations among Nodes, Zones and Pages

49 49 Buddy Allocator and Slab Allocator Fragmemtation: External Fragmentation – total memory space exists to satisfy a request, but it is not contiguous. Linux Solution: Buddy Allocator. Internal Fragmentation – allocated memory may be slightly larger than requested memory; this size difference is memory internal to a partition, but not being used. Linux Solution: Slab Allocator.

50 50 0 1 2 3 4 5 6 7 8 9 MAX_ORDER free_area_t zone_free_area order Free page blocks 2 4 page sized blocks 2 0 page sized blocks 2 MAX_ORDER_1 page sized blocks Buddy Allocator Free Page Block Management

51 51 0 1 2 3 4 5 6 7 8 9 MAX_ORDER free_area_1 zone_free_area order 1 x 2 4 block Requesting Process 2 x 2 3 block 2 x 2 2 block Allocating Physical Pages

52 52 Zone and Buddy Allocator Zone allocator Per-CPU page frame cache Buddy system Per-CPU page frame cache Buddy system Per-CPU page frame cache Buddy system Zone_DMA Memory Zone Zone_NORMAL Memory Zone Zone_HIGHMEM Memory Zone

53 53 Slab Allocator object slab (full) cache_cache kmem_cache cache slabs_full slabs_free slabs_partial kmem_cache_node slab (full) slab (full) slab (free) slab (partial) slab (partial) ….... slab object ….

54 54 Interfacing with the Buddy Allocator Slab allocator must ask the physical page allocator for its pages. Two interfaces are provides: kmem_getpages(); kmem_freepages();

55 Virtual Memory /*sample's*/ #include int a=1; int b=2; main(){ int sum; sum=a+b; printf(“a=%d”, “b=%d”, “sum=%d\n”, a,b,sum); } Text:sum=a+b; Printf(“a=%d”,………..); Data(global variables) a,b bss Stack:(local variables) sum kernel Heap:(Allocated dynamically by malloc()/mfree()) 0 3GB 4GB Kernel space User space Address view of a.out

56 a.out ELF header phdr1 [program header 1] phdr2 phdr3 ……. phdrN section1 section2 section3 …….. sectionN Sample.c compiler ELF format: (executable and linking format defined at /include/linux/elf.h)

57 virtual memory 4 GB 3 GB 12 KB 0 KB kernel stack 4K 28K NP 20K NP 12K NP T2 D1 S1 T1 physical memory n K n-1K 32 K 28 K 24 K 20 K 16 K 12 K 8 K 4 K 0 K data text Page table Page Table

58 mm_struct

59 Virtual Memory Management mm_struct: struct vm_area_struct *mmap; unsigned long env_end; unsigned long start_code; task_struct: struct mm_struct *mm; vm_area_struct unsigned long vm_end; unsigned long vm_start; struct vm_area_struct *vm_next; vm_area_struct unsigned long vm_end; unsigned long vm_start; struct vm_area_struct*vm_next; data text kernel 4GB 3GB

60 60 BIOS comm area kernel code image DMA region mem_map Direct Mapping vmalloc kmap Fixed mapping 3GB 4GB 1MB 8MB 16MB 896MB Virtual Memory Kernel Virtual Address Space

61 Homework#4 Task 2 The standard functions for the dynamic allocation of memory are as follows: #include void *malloc(size_t size); void *calloc(size_t nmemb, size_t size); void *realloc(void *ptr, size_t size); void free(void *ptr);, where size: the number of bytes to be allocated, nmemb: number of blocks where the block size is ‘size’. Task2: Explain those 4 functions how to use them and make your own program using calloc(). Due: Nov. 24, 2015, Submit to Joo Kyung Ro eu8189@kaist.ac.kr.eu8189@kaist.ac.kr

62 Memory Mapped File Memory Mapped I/O: Input/Output Devices or contents of files are mapped into the virtual memory area to increase the access speed or for the convenience of users. The related file is called the Memory Mapped File.

63 mmap(), munmap() : map or unmap files or devices into memory #include void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset);,where addr: the starting address for the mapping, length: the length of the mapping, prot: PROT_EXEC,PROT_READ, PROT_WRITE,PROT_NONE, PROC_NONE: Pages may not be accessed.

64 text data process file(filedes) mapping offset addr length mmap( )

65

66 munmap() #include int munmap(void *addr, size_t length); text data stack process addr file(filedes) offset length adjusted mapping unmapped area


Download ppt "Lecture 10 Memory management Nov. 17, 2015 Kyu Ho Park."

Similar presentations


Ads by Google