recitation 9 (nov. 8)
DESCRIPTION
Outline Virtual memory Lab 6 hints Reminders Lab 6: Get correctness points Exam 2: Nov. 16 (Next Tue). Minglong Shao [email protected] Office hours: Thursdays 5-6PM Wean Hall 1315. Recitation 9 (Nov. 8). Virtual memory (VM). One of the most important concepts in CS - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Recitation 9 (Nov. 8)](https://reader035.vdocuments.net/reader035/viewer/2022062807/5681501f550346895dbe03b5/html5/thumbnails/1.jpg)
Recitation 9 (Nov. 8)
Outline Virtual memory Lab 6 hints
Reminders Lab 6:
Get correctness points Exam 2:
Nov. 16 (Next Tue)
Minglong [email protected]
Office hours:Thursdays 5-6PMWean Hall 1315
![Page 2: Recitation 9 (Nov. 8)](https://reader035.vdocuments.net/reader035/viewer/2022062807/5681501f550346895dbe03b5/html5/thumbnails/2.jpg)
Virtual memory (VM)
One of the most important concepts in CS Why use virtual memory (VM)
Use RAM as a cache for disk Easier memory management Access protection
![Page 3: Recitation 9 (Nov. 8)](https://reader035.vdocuments.net/reader035/viewer/2022062807/5681501f550346895dbe03b5/html5/thumbnails/3.jpg)
Virtual memory
CPU
0:1:
N-1:
Memory
0:1:
P-1:
Page Table
Disk
VirtualAddresses
PhysicalAddresses
Page, page table, page hits, page faultsDemand paging
![Page 4: Recitation 9 (Nov. 8)](https://reader035.vdocuments.net/reader035/viewer/2022062807/5681501f550346895dbe03b5/html5/thumbnails/4.jpg)
Conceptual address translation
Higher bits of address (page number) mapped from virtual addr. to physical addr.
Lower bits (page offset) stay the same.
virtual page number (VPN) page offset virtual address
physical page number (PPN) page offset physical address
0p–1
address translation
pm–1
n–1 0p–1p
![Page 5: Recitation 9 (Nov. 8)](https://reader035.vdocuments.net/reader035/viewer/2022062807/5681501f550346895dbe03b5/html5/thumbnails/5.jpg)
Address translation through page table Translation
Separate (set of) page table(s) per process VPN forms index into page table (points to a page table entry)
virtual page number (VPN) page offset
virtual address
physical page number (PPN) page offset
physical address
0p–1pm–1
n–1 0p–1ppage table base register
if valid=0then pagenot in memory
valid physical page number (PPN)access
VPN acts astable index
virtual page number (VPN) page offset
virtual address
physical page number (PPN) page offset
physical address
0p–1pm–1
n–1 0p–1ppage table base register
if valid=0then pagenot in memory
valid physical page number (PPN)access
VPN acts astable index
![Page 6: Recitation 9 (Nov. 8)](https://reader035.vdocuments.net/reader035/viewer/2022062807/5681501f550346895dbe03b5/html5/thumbnails/6.jpg)
Integrating VM and cache
Most caches are physically addressed Perform address translation before cache lookup Another cache: Translation Lookaside Buffer
CPUTLB
LookupCache Main
Memory
VA PA miss
hit
data
Trans-lation
hit
miss
![Page 7: Recitation 9 (Nov. 8)](https://reader035.vdocuments.net/reader035/viewer/2022062807/5681501f550346895dbe03b5/html5/thumbnails/7.jpg)
Address translation with TLB
virtual addressvirtual page number page offset
physical address
n–1 0p–1p
valid physical page numbertag
valid tag data
data=
cache hit
tag byte offsetindex
=
TLB hit
TLB
Cache
. ..
![Page 8: Recitation 9 (Nov. 8)](https://reader035.vdocuments.net/reader035/viewer/2022062807/5681501f550346895dbe03b5/html5/thumbnails/8.jpg)
Example
Understand end-to-end addr. translation 20-bit virtual addresses 18-bit physical addresses Page size is 1024 bytes TLB is 2-way associative with 16 total entries
![Page 9: Recitation 9 (Nov. 8)](https://reader035.vdocuments.net/reader035/viewer/2022062807/5681501f550346895dbe03b5/html5/thumbnails/9.jpg)
Example: TLB and page table
![Page 10: Recitation 9 (Nov. 8)](https://reader035.vdocuments.net/reader035/viewer/2022062807/5681501f550346895dbe03b5/html5/thumbnails/10.jpg)
Part 1
A. Virtual address
B. Physical address
16 15 14 13 12 11 10 9 8 7 6 5 4 31718 2 1 019
VPN VPO
TLBT TLBI
16 15 14 13 12 11 10 9 8 7 6 5 4 317 2 1 0
PPN PPO
![Page 11: Recitation 9 (Nov. 8)](https://reader035.vdocuments.net/reader035/viewer/2022062807/5681501f550346895dbe03b5/html5/thumbnails/11.jpg)
Part 2
Virtual address 0x78E6 A. 078E6 = B. Address translation
C. Physical address
parameter Value Parameter Value
VPN 0x01E TLB hit? N
TLB Index 0x6 Page fault? N
TLB Tag 0x03 PPN 0x57
0000 0111 1000 1110 0110
01 0101 1100 1110 0110
![Page 12: Recitation 9 (Nov. 8)](https://reader035.vdocuments.net/reader035/viewer/2022062807/5681501f550346895dbe03b5/html5/thumbnails/12.jpg)
Part 2
Virtual address 0x04AA4 A. 04AA4 = 0000 0100 1010 1010 0100 B. Address translation
C. Physical address
01 1010 0010 1010 0100
parameter Value Parameter Value
VPN 0x012 TLB hit? Y
TLB Index 0x2 Page fault? N
TLB Tag 0x02 PPN 0x68
![Page 13: Recitation 9 (Nov. 8)](https://reader035.vdocuments.net/reader035/viewer/2022062807/5681501f550346895dbe03b5/html5/thumbnails/13.jpg)
End-to-end address translation
Section 10.6.4: a concrete example Read carefully and solve practice problem 10.4
Multi-level page table
![Page 14: Recitation 9 (Nov. 8)](https://reader035.vdocuments.net/reader035/viewer/2022062807/5681501f550346895dbe03b5/html5/thumbnails/14.jpg)
Malloc Lab (Lab 6)
Submit twice Checkpoint submission:
Only check correctness: 15 pts Final submission:
Correctness: 10 pts Performance: 60 pts
Interposition test: 5 pts Style: 5 pts
Start early!! Get correctness points this week
![Page 15: Recitation 9 (Nov. 8)](https://reader035.vdocuments.net/reader035/viewer/2022062807/5681501f550346895dbe03b5/html5/thumbnails/15.jpg)
Design options
Organize free blocks Implicit free list Explicit free list Segregate lists/search trees
Find free blocks First fit/next fit Blocks sorted by address with first fit Best fit
Large design space Step by step
![Page 16: Recitation 9 (Nov. 8)](https://reader035.vdocuments.net/reader035/viewer/2022062807/5681501f550346895dbe03b5/html5/thumbnails/16.jpg)
Example implementation
Section 10.9.12 Understand every line of the code
Implicit free list + immediate boundary tag coalescing + first fit
Code is available athttp://csapp.cs.cmu.edu/public/ics/code/vm/malloc.c
Use it as a starting point
![Page 17: Recitation 9 (Nov. 8)](https://reader035.vdocuments.net/reader035/viewer/2022062807/5681501f550346895dbe03b5/html5/thumbnails/17.jpg)
Block format
Header
Pointer returnedby malloc (Block Pointer (bp))
32 bits
32 bits
>= what user askedfor in malloc
Footer
Payload
![Page 18: Recitation 9 (Nov. 8)](https://reader035.vdocuments.net/reader035/viewer/2022062807/5681501f550346895dbe03b5/html5/thumbnails/18.jpg)
Header/footer format
Double word alignment Three lower-order bits of size always 0
Pack size and allocated bits into a single integer Size = 24 (0x18). Block is allocated
Header = 0 x18 | 0x1 = 0x19
0 0 asize
3 2 1 0 31
![Page 19: Recitation 9 (Nov. 8)](https://reader035.vdocuments.net/reader035/viewer/2022062807/5681501f550346895dbe03b5/html5/thumbnails/19.jpg)
Heap format
8|1 8|1 0|1
Double Word Alignment(8 bytes)
4 bytes Pad Prologue Epilogue
Allocated and Free Blocks
HDR FTR HDR FTR
![Page 20: Recitation 9 (Nov. 8)](https://reader035.vdocuments.net/reader035/viewer/2022062807/5681501f550346895dbe03b5/html5/thumbnails/20.jpg)
Very useful macros
#define WSIZE 4
#define DSIZE 8
#define CHUNKSIZE (1<<12)
#define OVERHEAD 8
![Page 21: Recitation 9 (Nov. 8)](https://reader035.vdocuments.net/reader035/viewer/2022062807/5681501f550346895dbe03b5/html5/thumbnails/21.jpg)
Very useful macros #define PACK(size, alloc) ((size) | (alloc)) #define GET(p) (*(size_t *)(p))
#define PUT(p, val) (*(size_t *)(p) = (val))
#define GET_SIZE(p) (GET(p) & ~0x7) #define GET_ALLOC(p) (GET(p) & 0x1)
![Page 22: Recitation 9 (Nov. 8)](https://reader035.vdocuments.net/reader035/viewer/2022062807/5681501f550346895dbe03b5/html5/thumbnails/22.jpg)
Very useful macros #define HDRP(bp)
((char *)(bp) - WSIZE)
#define FTRP(bp) ((char *)(bp) + GET_SIZE(HDRP(bp)) - DSIZE)
#define NEXT_BLKP(bp) ((char *)(bp) + GET_SIZE(((char *)(bp) - WSIZE)))
#define PREV_BLKP(bp) ((char *)(bp) - GET_SIZE(((char *)(bp) - DSIZE)))
![Page 23: Recitation 9 (Nov. 8)](https://reader035.vdocuments.net/reader035/viewer/2022062807/5681501f550346895dbe03b5/html5/thumbnails/23.jpg)
Initializing the heapint mm_init(void) {
if ((heap_listp = mem_sbrk(4*WSIZE)) == NULL)
return -1;
PUT(heap_listp, 0); PUT(heap_listp+WSIZE, PACK(OVERHEAD, 1));
PUT(heap_listp+DSIZE, PACK(OVERHEAD, 1)); PUT(heap_listp+WSIZE+DSIZE, PACK(0, 1));
heap_listp += DSIZE;
if (extend_heap(CHUNKSIZE/WSIZE) == NULL) return -1;
return 0; }
![Page 24: Recitation 9 (Nov. 8)](https://reader035.vdocuments.net/reader035/viewer/2022062807/5681501f550346895dbe03b5/html5/thumbnails/24.jpg)
Extending the heapstatic void *extend_heap(size_t words) {
char *bp; size_t size;
size = (words % 2) ? (words+1)*WSIZE : words*WSIZE;
if ((int)(bp = mem_sbrk(size)) < 0) return NULL;
PUT(HDRP(bp), PACK(size, 0)); PUT(FTRP(bp), PACK(size, 0)); PUT(HDRP(NEXT_BLKP(bp)), PACK(0, 1));
return coalesce(bp); }
![Page 25: Recitation 9 (Nov. 8)](https://reader035.vdocuments.net/reader035/viewer/2022062807/5681501f550346895dbe03b5/html5/thumbnails/25.jpg)
Mallocvoid *mm_malloc(size_t size) {
size_t asize, extendsize; char *bp;
if (size <= 0) return NULL; if (size <= DSIZE)
asize = DSIZE+OVERHEAD; else
asize = DSIZE*((size+(OVERHEAD)+(DSIZE-1))/DSIZE);
if ((bp = find_fit(asize)) != NULL) { place(bp, asize); return bp;
}extendsize = MAX(asize,CHUNKSIZE); if ((bp = extend_heap(extendsize/WSIZE)) == NULL)
return NULL; place(bp, asize); return bp;
}
![Page 26: Recitation 9 (Nov. 8)](https://reader035.vdocuments.net/reader035/viewer/2022062807/5681501f550346895dbe03b5/html5/thumbnails/26.jpg)
Finding first fitstatic void *find_fit(size_t asize) {
void *bp; for (bp = heap_listp;
GET_SIZE(HDRP(bp)) > 0; bp = NEXT_BLKP(bp))
if (!GET_ALLOC(HDRP(bp)) && (asize <= GET_SIZE(HDRP(bp))))
return bp;
return NULL;}
![Page 27: Recitation 9 (Nov. 8)](https://reader035.vdocuments.net/reader035/viewer/2022062807/5681501f550346895dbe03b5/html5/thumbnails/27.jpg)
Mallocvoid *mm_malloc(size_t size) {
size_t asize, extendsize; char *bp;
if (size <= 0) return NULL; if (size <= DSIZE)
asize = DSIZE+OVERHEAD; else
asize = DSIZE*((size+(OVERHEAD)+(DSIZE-1))/DSIZE);
if ((bp = find_fit(asize)) != NULL) { place(bp, asize); return bp;
}extendsize = MAX(asize,CHUNKSIZE); if ((bp = extend_heap(extendsize/WSIZE)) == NULL)
return NULL; place(bp, asize); return bp;
}
![Page 28: Recitation 9 (Nov. 8)](https://reader035.vdocuments.net/reader035/viewer/2022062807/5681501f550346895dbe03b5/html5/thumbnails/28.jpg)
Placing a block in a free blockstatic void place(void *bp, size_t asize) {
size_t csize = GET_SIZE(HDRP(bp));
if ((csize - asize) >= (DSIZE + OVERHEAD)) {PUT(HDRP(bp), PACK(asize, 1));PUT(FTRP(bp), PACK(asize, 1)); bp = NEXT_BLKP(bp); PUT(HDRP(bp), PACK(csize-asize, 0)); PUT(FTRP(bp), PACK(csize-asize, 0));
} else {
PUT(HDRP(bp), PACK(csize, 1)); PUT(FTRP(bp), PACK(csize, 1));
} }
![Page 29: Recitation 9 (Nov. 8)](https://reader035.vdocuments.net/reader035/viewer/2022062807/5681501f550346895dbe03b5/html5/thumbnails/29.jpg)
Free
void mm_free(void *bp) {
size_t size = GET_SIZE(HDRP(bp));
PUT(HDRP(bp), PACK(size, 0));
PUT(FTRP(bp), PACK(size, 0));
coalesce(bp);
}
![Page 30: Recitation 9 (Nov. 8)](https://reader035.vdocuments.net/reader035/viewer/2022062807/5681501f550346895dbe03b5/html5/thumbnails/30.jpg)
Coalesce: called by mm_free() & extend_heap()
static void *coalesce(void *bp) { size_t prev_alloc = GET_ALLOC(FTRP(PREV_BLKP(bp))); size_t next_alloc = GET_ALLOC(HDRP(NEXT_BLKP(bp))); size_t size = GET_SIZE(HDRP(bp));
if (prev_alloc && next_alloc) { return bp; }else if (prev_alloc && !next_alloc) { …… }else if (!prev_alloc && next_alloc) {
size += GET_SIZE(HDRP(PREV_BLKP(bp))); PUT(FTRP(bp), PACK(size, 0)); PUT(HDRP(PREV_BLKP(bp)), PACK(size, 0));
bp = PREV_BLKP(bp); }
else { …… }return bp;
}