├── README.md ├── bomb-lab ├── answer ├── assembly.asm ├── bomb ├── test ├── test.c └── test.o ├── buflab-handout ├── answer0 ├── answer1 ├── answer2 ├── answer3 ├── answer4 ├── asm.s ├── asm2.o ├── asm2.s ├── asm3.o ├── asm3.s ├── asm4.o ├── asm4.s ├── bufbomb ├── buflab.pdf ├── hex2raw ├── makecookie ├── tmpanswer2 └── tmpinput4 ├── cachelab-handout ├── .csim_results ├── Makefile ├── README ├── cachelab.c ├── cachelab.h ├── csim ├── csim-ref ├── csim.c ├── driver.py ├── test-csim ├── test-trans ├── test-trans.c ├── tracegen ├── tracegen.c ├── traces │ ├── dave.trace │ ├── long.trace │ ├── trans.trace │ ├── yi.trace │ └── yi2.trace ├── trans.c └── trans.o ├── datalab-handout ├── .bits.h.swp ├── Driverhdrs.pm ├── Driverlib.pm ├── Makefile ├── README ├── bits.c ├── bits.h ├── btest ├── btest.c ├── btest.h ├── decl.c ├── dlc ├── driver.pl ├── fshow ├── fshow.c ├── ishow ├── ishow.c └── tests.c ├── malloclab-handout ├── Makefile ├── README ├── clock.c ├── clock.h ├── config.h ├── fcyc.c ├── fcyc.h ├── fsecs.c ├── fsecs.h ├── ftimer.c ├── ftimer.h ├── mdriver.c ├── memlib.c ├── memlib.h ├── mm.c ├── mm.h ├── short1-bal.rep ├── short2-bal.rep └── traces │ ├── Makefile │ ├── README │ ├── amptjp-bal.rep │ ├── amptjp.rep │ ├── binary-bal.rep │ ├── binary.rep │ ├── binary2-bal.rep │ ├── binary2.rep │ ├── cccp-bal.rep │ ├── cccp.rep │ ├── checktrace.pl │ ├── coalescing-bal.rep │ ├── coalescing.rep │ ├── cp-decl-bal.rep │ ├── cp-decl.rep │ ├── expr-bal.rep │ ├── expr.rep │ ├── gen_binary.pl │ ├── gen_binary2.pl │ ├── gen_coalescing.pl │ ├── gen_random.pl │ ├── gen_realloc.pl │ ├── gen_realloc2.pl │ ├── random-bal.rep │ ├── random.rep │ ├── random2-bal.rep │ ├── random2.rep │ ├── realloc-bal.rep │ ├── realloc.rep │ ├── realloc2-bal.rep │ ├── realloc2.rep │ ├── short1-bal.rep │ ├── short1.rep │ ├── short2-bal.rep │ └── short2.rep ├── proxylab-handout ├── Makefile ├── README ├── csapp.c ├── csapp.h └── proxy.c └── shlab-handout ├── Makefile ├── README ├── myint ├── myint.c ├── myspin ├── myspin.c ├── mysplit ├── mysplit.c ├── mystop ├── mystop.c ├── sdriver.pl ├── tags ├── trace01.txt ├── trace02.txt ├── trace03.txt ├── trace04.txt ├── trace05.txt ├── trace06.txt ├── trace07.txt ├── trace08.txt ├── trace09.txt ├── trace10.txt ├── trace11.txt ├── trace12.txt ├── trace13.txt ├── trace14.txt ├── trace15.txt ├── trace16.txt ├── tsh ├── tsh.c ├── tshref └── tshref.out /README.md: -------------------------------------------------------------------------------- 1 | csapp-lab 2 | ========= 3 | -------------------------------------------------------------------------------- /bomb-lab/answer: -------------------------------------------------------------------------------- 1 | Public speaking is very easy. 2 | 1 2 6 24 120 720 3 | 0 q 777 4 | 9 austinpowers 5 | opekma 6 | 4 2 6 3 1 5 7 | 1001 8 | -------------------------------------------------------------------------------- /bomb-lab/bomb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zyearn/csapp-lab-2e/baca11bded68642413b1b7e0ab57f00497033a8a/bomb-lab/bomb -------------------------------------------------------------------------------- /bomb-lab/test: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zyearn/csapp-lab-2e/baca11bded68642413b1b7e0ab57f00497033a8a/bomb-lab/test -------------------------------------------------------------------------------- /bomb-lab/test.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int g = 2; 4 | 5 | int main() 6 | { 7 | int* a = &g; 8 | printf("a = %x\n", a); 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /bomb-lab/test.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zyearn/csapp-lab-2e/baca11bded68642413b1b7e0ab57f00497033a8a/bomb-lab/test.o -------------------------------------------------------------------------------- /buflab-handout/answer0: -------------------------------------------------------------------------------- 1 | 00 00 00 00 00 00 00 00 2 | 00 00 00 00 00 00 00 00 3 | 00 00 00 00 00 00 00 00 4 | 00 00 00 00 00 00 00 00 5 | 00 00 00 00 83 8d 04 08 6 | -------------------------------------------------------------------------------- /buflab-handout/answer1: -------------------------------------------------------------------------------- 1 | 00 00 00 00 00 00 00 00 2 | 00 00 00 00 00 00 00 00 3 | 00 00 00 00 00 00 00 00 4 | 00 00 00 00 00 00 00 00 5 | 00 00 00 00 38 8d 04 08 6 | 00 00 00 00 d4 9f c2 50 7 | -------------------------------------------------------------------------------- /buflab-handout/answer2: -------------------------------------------------------------------------------- 1 | a1 e4 c1 04 08 a3 ec c1 2 | 04 08 68 eb 8c 04 08 c3 3 | 00 00 00 00 00 00 00 00 4 | 00 00 00 00 00 00 00 00 5 | 00 00 00 00 e0 36 68 55 6 | -------------------------------------------------------------------------------- /buflab-handout/answer3: -------------------------------------------------------------------------------- 1 | 68 e5 8d 04 08 c3 00 00 2 | 00 00 00 00 00 00 00 00 3 | 00 00 00 00 00 00 00 00 4 | 00 00 00 00 00 00 00 00 5 | 00 00 00 00 e0 36 68 55 6 | -------------------------------------------------------------------------------- /buflab-handout/answer4: -------------------------------------------------------------------------------- 1 | 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 2 | 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 3 | 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 4 | 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 5 | 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 6 | 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 7 | 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 8 | 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 9 | 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 10 | 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 11 | 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 12 | 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 13 | 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 14 | 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 15 | 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 16 | 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 17 | 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 18 | 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 19 | 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 20 | 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 21 | 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 22 | 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 23 | 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 24 | 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 25 | 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 26 | 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 27 | 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 28 | 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 29 | 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 30 | 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 31 | 90 90 90 90 90 90 90 90 90 90 90 90 8d 6c 24 28 32 | a1 e4 c1 04 08 68 93 8c 04 08 c3 90 90 90 90 90 33 | 00 00 00 00 70 35 68 55 34 | -------------------------------------------------------------------------------- /buflab-handout/asm2.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zyearn/csapp-lab-2e/baca11bded68642413b1b7e0ab57f00497033a8a/buflab-handout/asm2.o -------------------------------------------------------------------------------- /buflab-handout/asm2.s: -------------------------------------------------------------------------------- 1 | mov (0x804c1e4),%eax 2 | mov %eax,(0x804c1ec) 3 | push $0x8048ceb 4 | ret 5 | -------------------------------------------------------------------------------- /buflab-handout/asm3.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zyearn/csapp-lab-2e/baca11bded68642413b1b7e0ab57f00497033a8a/buflab-handout/asm3.o -------------------------------------------------------------------------------- /buflab-handout/asm3.s: -------------------------------------------------------------------------------- 1 | push $0x8048de5 2 | ret 3 | -------------------------------------------------------------------------------- /buflab-handout/asm4.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zyearn/csapp-lab-2e/baca11bded68642413b1b7e0ab57f00497033a8a/buflab-handout/asm4.o -------------------------------------------------------------------------------- /buflab-handout/asm4.s: -------------------------------------------------------------------------------- 1 | lea 0x28(%esp), %ebp 2 | mov 0x804c1e4, %eax 3 | push $0x8048c93 4 | ret 5 | -------------------------------------------------------------------------------- /buflab-handout/bufbomb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zyearn/csapp-lab-2e/baca11bded68642413b1b7e0ab57f00497033a8a/buflab-handout/bufbomb -------------------------------------------------------------------------------- /buflab-handout/buflab.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zyearn/csapp-lab-2e/baca11bded68642413b1b7e0ab57f00497033a8a/buflab-handout/buflab.pdf -------------------------------------------------------------------------------- /buflab-handout/hex2raw: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zyearn/csapp-lab-2e/baca11bded68642413b1b7e0ab57f00497033a8a/buflab-handout/hex2raw -------------------------------------------------------------------------------- /buflab-handout/makecookie: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zyearn/csapp-lab-2e/baca11bded68642413b1b7e0ab57f00497033a8a/buflab-handout/makecookie -------------------------------------------------------------------------------- /buflab-handout/tmpanswer2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zyearn/csapp-lab-2e/baca11bded68642413b1b7e0ab57f00497033a8a/buflab-handout/tmpanswer2 -------------------------------------------------------------------------------- /buflab-handout/tmpinput4: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zyearn/csapp-lab-2e/baca11bded68642413b1b7e0ab57f00497033a8a/buflab-handout/tmpinput4 -------------------------------------------------------------------------------- /cachelab-handout/.csim_results: -------------------------------------------------------------------------------- 1 | 265189 21775 21743 2 | -------------------------------------------------------------------------------- /cachelab-handout/Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # Student makefile for Cache Lab 3 | # 4 | CC = gcc 5 | CFLAGS = -g -Wall -Werror -std=c99 6 | 7 | all: csim test-trans tracegen 8 | # Generate a handin tar file each time you compile 9 | #-tar -cvf ${USER}-handin.tar csim.c trans.c 10 | 11 | csim: csim.c cachelab.c cachelab.h 12 | $(CC) $(CFLAGS) -o csim csim.c cachelab.c -lm 13 | 14 | test-trans: test-trans.c trans.o cachelab.c cachelab.h 15 | $(CC) $(CFLAGS) -o test-trans test-trans.c cachelab.c trans.o 16 | 17 | tracegen: tracegen.c trans.o cachelab.c 18 | $(CC) $(CFLAGS) -O0 -o tracegen tracegen.c trans.o cachelab.c 19 | 20 | trans.o: trans.c 21 | $(CC) $(CFLAGS) -O0 -c trans.c 22 | 23 | # 24 | # Clean the src dirctory 25 | # 26 | clean: 27 | rm -rf *.o 28 | rm -f csim 29 | rm -f test-trans tracegen 30 | rm -f trace.all trace.f* 31 | rm -f .csim_results .marker 32 | -------------------------------------------------------------------------------- /cachelab-handout/README: -------------------------------------------------------------------------------- 1 | This is the handout directory for the CS:APP Cache Lab. 2 | 3 | ************************ 4 | Running the autograders: 5 | ************************ 6 | 7 | Before running the autograders, compile your code: 8 | linux> make 9 | 10 | Check the correctness of your simulator: 11 | linux> ./test-csim 12 | 13 | Check the correctness and performance of your transpose functions: 14 | linux> ./test-trans -M 32 -N 32 15 | linux> ./test-trans -M 64 -N 64 16 | linux> ./test-trans -M 61 -N 67 17 | 18 | Check everything at once (this is the program that your instructorruns): 19 | linux> ./driver.py 20 | 21 | ****** 22 | Files: 23 | ****** 24 | 25 | # You will modifying and handing in these two files 26 | csim.c Your cache simulator 27 | trans.c Your transpose function 28 | 29 | # Tools for evaluating your simulator and transpose function 30 | Makefile Builds the simulator and tools 31 | README This file 32 | driver.py* The driver program, runs test-csim and test-trans 33 | cachelab.c Required helper functions 34 | cachelab.h Required header file 35 | csim-ref* The executable reference cache simulator 36 | test-csim* Tests your cache simulator 37 | test-trans.c Tests your transpose function 38 | tracegen.c Helper program used by test-trans 39 | traces/ Trace files used by test-csim.c 40 | -------------------------------------------------------------------------------- /cachelab-handout/cachelab.c: -------------------------------------------------------------------------------- 1 | /* 2 | * cachelab.c - Cache Lab helper functions 3 | */ 4 | #include 5 | #include 6 | #include 7 | #include "cachelab.h" 8 | #include 9 | 10 | trans_func_t func_list[MAX_TRANS_FUNCS]; 11 | int func_counter = 0; 12 | 13 | /* 14 | * printSummary - Summarize the cache simulation statistics. Student cache simulators 15 | * must call this function in order to be properly autograded. 16 | */ 17 | void printSummary(int hits, int misses, int evictions) 18 | { 19 | printf("hits:%d misses:%d evictions:%d\n", hits, misses, evictions); 20 | FILE* output_fp = fopen(".csim_results", "w"); 21 | assert(output_fp); 22 | fprintf(output_fp, "%d %d %d\n", hits, misses, evictions); 23 | fclose(output_fp); 24 | } 25 | 26 | /* 27 | * initMatrix - Initialize the given matrix 28 | */ 29 | void initMatrix(int M, int N, int A[N][M], int B[M][N]) 30 | { 31 | int i, j; 32 | srand(time(NULL)); 33 | for (i = 0; i < N; i++){ 34 | for (j = 0; j < M; j++){ 35 | // A[i][j] = i+j; /* The matrix created this way is symmetric */ 36 | A[i][j]=rand(); 37 | B[j][i]=rand(); 38 | } 39 | } 40 | } 41 | 42 | void randMatrix(int M, int N, int A[N][M]) { 43 | int i, j; 44 | srand(time(NULL)); 45 | for (i = 0; i < N; i++){ 46 | for (j = 0; j < M; j++){ 47 | // A[i][j] = i+j; /* The matrix created this way is symmetric */ 48 | A[i][j]=rand(); 49 | } 50 | } 51 | } 52 | 53 | /* 54 | * correctTrans - baseline transpose function used to evaluate correctness 55 | */ 56 | void correctTrans(int M, int N, int A[N][M], int B[M][N]) 57 | { 58 | int i, j, tmp; 59 | for (i = 0; i < N; i++){ 60 | for (j = 0; j < M; j++){ 61 | tmp = A[i][j]; 62 | B[j][i] = tmp; 63 | } 64 | } 65 | } 66 | 67 | 68 | 69 | /* 70 | * registerTransFunction - Add the given trans function into your list 71 | * of functions to be tested 72 | */ 73 | void registerTransFunction(void (*trans)(int M, int N, int[N][M], int[M][N]), 74 | char* desc) 75 | { 76 | func_list[func_counter].func_ptr = trans; 77 | func_list[func_counter].description = desc; 78 | func_list[func_counter].correct = 0; 79 | func_list[func_counter].num_hits = 0; 80 | func_list[func_counter].num_misses = 0; 81 | func_list[func_counter].num_evictions =0; 82 | func_counter++; 83 | } 84 | -------------------------------------------------------------------------------- /cachelab-handout/cachelab.h: -------------------------------------------------------------------------------- 1 | /* 2 | * cachelab.h - Prototypes for Cache Lab helper functions 3 | */ 4 | 5 | #ifndef CACHELAB_TOOLS_H 6 | #define CACHELAB_TOOLS_H 7 | 8 | #define MAX_TRANS_FUNCS 100 9 | 10 | typedef struct trans_func{ 11 | void (*func_ptr)(int M,int N,int[N][M],int[M][N]); 12 | char* description; 13 | char correct; 14 | unsigned int num_hits; 15 | unsigned int num_misses; 16 | unsigned int num_evictions; 17 | } trans_func_t; 18 | 19 | /* 20 | * printSummary - This function provides a standard way for your cache 21 | * simulator * to display its final hit and miss statistics 22 | */ 23 | void printSummary(int hits, /* number of hits */ 24 | int misses, /* number of misses */ 25 | int evictions); /* number of evictions */ 26 | 27 | /* Fill the matrix with data */ 28 | void initMatrix(int M, int N, int A[N][M], int B[M][N]); 29 | 30 | /* The baseline trans function that produces correct results. */ 31 | void correctTrans(int M, int N, int A[N][M], int B[M][N]); 32 | 33 | /* Add the given function to the function list */ 34 | void registerTransFunction( 35 | void (*trans)(int M,int N,int[N][M],int[M][N]), char* desc); 36 | 37 | #endif /* CACHELAB_TOOLS_H */ 38 | -------------------------------------------------------------------------------- /cachelab-handout/csim: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zyearn/csapp-lab-2e/baca11bded68642413b1b7e0ab57f00497033a8a/cachelab-handout/csim -------------------------------------------------------------------------------- /cachelab-handout/csim-ref: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zyearn/csapp-lab-2e/baca11bded68642413b1b7e0ab57f00497033a8a/cachelab-handout/csim-ref -------------------------------------------------------------------------------- /cachelab-handout/csim.c: -------------------------------------------------------------------------------- 1 | /* 2 | * author : zjs 3 | * date : 2014.02.15 4 | */ 5 | 6 | #include "cachelab.h" 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #define MAXLINE 200 14 | 15 | void get_opt(int argc, char **argv); 16 | void do_init(); 17 | void do_deinit(); 18 | void do_L(void *addr, int size); 19 | void do_M(void *addr, int size); 20 | void do_S(void *addr, int size); 21 | int getset(void *addr); 22 | long gettag(void *addr); 23 | 24 | // data structure of one specific set entry 25 | // the size of array is the number of lines from command line, i.e. -E 1 26 | struct oneSet 27 | { 28 | int *v; 29 | clock_t *last_access_time; 30 | long *tag; 31 | }; 32 | 33 | int hits, misses, evicts; 34 | struct oneSet *setptr; // the global pointer to the cache, setptr[n] stands for the nth set entry 35 | int s = 0, E = 0, b = 0; 36 | char *file = NULL; 37 | long clocktime = 0; // used to record the access time 38 | 39 | int main(int argc, char **argv) 40 | { 41 | get_opt(argc, argv); 42 | do_init(); 43 | 44 | FILE *fp = fopen(file, "r"); 45 | if (fp == NULL) 46 | { 47 | fprintf(stderr, "open file error\n"); 48 | exit(0); 49 | } 50 | 51 | char op[MAXLINE]; 52 | void *addr; 53 | int size; 54 | char buf[MAXLINE]; 55 | while (fgets(buf, MAXLINE, fp) != NULL) 56 | { 57 | //printf("\nline read: %s\n", buf); 58 | sscanf(buf, "%s %p,%d", op, &addr, &size); 59 | //printf("op = %s, addr = %p, size = %d\n", op, addr, size); 60 | if (*op == 'L') 61 | { 62 | do_L(addr, size); 63 | } 64 | else if (*op == 'M') 65 | { 66 | do_M(addr, size); 67 | } 68 | else if (*op == 'S') 69 | { 70 | do_S(addr, size); 71 | } 72 | } 73 | 74 | do_deinit(); 75 | printSummary(hits, misses, evicts); 76 | return 0; 77 | } 78 | 79 | // get the program parameter using getopt library 80 | void get_opt(int argc, char **argv) 81 | { 82 | int c; 83 | 84 | while ((c = getopt(argc, argv, "s:E:b:t:")) != -1) 85 | { 86 | switch (c) 87 | { 88 | case 's': 89 | s = atoi(optarg); 90 | break; 91 | case 'E': 92 | E = atoi(optarg); 93 | break; 94 | case 'b': 95 | b = atoi(optarg); 96 | break; 97 | case 't': 98 | file = optarg; 99 | break; 100 | default: 101 | printf("illegal opt\n"); 102 | exit(0); 103 | } 104 | } 105 | } 106 | 107 | // using malloc to initialize data structure 108 | void do_init() 109 | { 110 | int S = (1 << s); 111 | if ( S <= 0) 112 | { 113 | fprintf(stderr, "S is nonpositive\n"); 114 | exit(0); 115 | } 116 | setptr = (struct oneSet*)malloc(sizeof(struct oneSet) * S); 117 | 118 | for (int ind = 0; ind < S; ++ind) 119 | { 120 | setptr[ind].v = (int *)malloc(sizeof(int) * E); 121 | setptr[ind].last_access_time = (clock_t *)malloc(sizeof(clock_t) * E); 122 | setptr[ind].tag = (long *)malloc(sizeof(long) * E); 123 | 124 | for(int Eind = 0 ; Eind < E; Eind++) 125 | { 126 | setptr[ind].v[Eind] = 0; 127 | setptr[ind].last_access_time[Eind] = 0; 128 | setptr[ind].tag[Eind] = 0; 129 | } 130 | } 131 | } 132 | 133 | // explicitly return the space back to heap 134 | void do_deinit() 135 | { 136 | int S = (1 << s); 137 | 138 | setptr = (struct oneSet*)malloc(sizeof(struct oneSet) * S); 139 | for (int ind = 0; ind < S; ++ind) 140 | { 141 | free(setptr[ind].v); 142 | free(setptr[ind].last_access_time); 143 | free(setptr[ind].tag); 144 | } 145 | 146 | free(setptr); 147 | } 148 | 149 | // do a LOAD operation 150 | void do_L(void *addr, int size) 151 | { 152 | int setnum = getset(addr); 153 | printf("setnum = %d, ", setnum); 154 | struct oneSet *this_set = &setptr[setnum]; 155 | 156 | int index; 157 | int full = 1; 158 | int empty_item = 0; // if not full, keep track of the empty item 159 | int last_item = 0; // if full, keep track of the evict item 160 | int last_time = this_set->last_access_time[0]; 161 | 162 | for (index = 0; index < E; index++) 163 | { 164 | // find, update the access time 165 | if (this_set->v[index] == 1 && gettag(addr) == this_set->tag[index]) 166 | { 167 | this_set->last_access_time[index] = ++clocktime; 168 | break; 169 | } 170 | // not valid, then this entry is considered empty which means cache is not full 171 | else if (this_set->v[index] == 0) 172 | { 173 | full = 0; 174 | empty_item = index; 175 | } 176 | // valid but tag not equal 177 | else 178 | { 179 | // keep track of the LRU item, ready for eviction 180 | if (this_set->last_access_time[index] < last_time) 181 | { 182 | last_item = index; 183 | last_time = this_set->last_access_time[index]; 184 | } 185 | } 186 | } 187 | 188 | if (index == E) // miss 189 | { 190 | misses++; 191 | if (full) //evict 192 | { 193 | this_set->tag[last_item] = gettag(addr); 194 | this_set->last_access_time[last_item] = ++clocktime; 195 | evicts++; 196 | } 197 | else 198 | { 199 | this_set->v[empty_item] = 1; 200 | this_set->tag[empty_item] = gettag(addr); 201 | this_set->last_access_time[empty_item] = ++clocktime; 202 | } 203 | } 204 | else // hit 205 | { 206 | hits++; 207 | } 208 | 209 | } 210 | 211 | //the difference between S and M is that S only load when miss, while M always load first then store 212 | void do_S(void *addr, int size) 213 | { 214 | int setnum = getset(addr); 215 | printf("setnum = %d, ", setnum); 216 | struct oneSet *this_set = &setptr[setnum]; 217 | 218 | int index; 219 | for (index = 0; index < E; ++index) 220 | { 221 | if (this_set->v[index] == 1 && gettag(addr) == this_set->tag[index]) //find 222 | { 223 | this_set->last_access_time[index] = ++clocktime; 224 | break; 225 | } 226 | } 227 | 228 | if (index == E) // store miss 229 | { 230 | do_L(addr, size); // if miss, then load 231 | } 232 | else // store hit 233 | { 234 | hits++; 235 | } 236 | } 237 | 238 | // do a MODIFY operation, which do S afer L 239 | void do_M(void *addr, int size) 240 | { 241 | int setnum = getset(addr); 242 | printf("setnum = %d, ", setnum); 243 | 244 | do_L(addr, size); 245 | do_S(addr, size); 246 | } 247 | 248 | // get the set number from the address 249 | int getset(void *addr) 250 | { 251 | return (int)( ((long)addr >> b) & ((1 << s) - 1) ); 252 | } 253 | 254 | // get the tag number from the address 255 | long gettag(void *addr) 256 | { 257 | return (long)((long)addr >> (s + b)); 258 | } 259 | -------------------------------------------------------------------------------- /cachelab-handout/driver.py: -------------------------------------------------------------------------------- 1 | #!/usr//bin/python 2 | # 3 | # driver.py - The driver tests the correctness of the student's cache 4 | # simulator and the correctness and performance of their transpose 5 | # function. It uses ./test-csim to check the correctness of the 6 | # simulator and it runs ./test-trans on three different sized 7 | # matrices (32x32, 64x64, and 61x67) to test the correctness and 8 | # performance of the transpose function. 9 | # 10 | import subprocess; 11 | import re; 12 | import os; 13 | import sys; 14 | import optparse; 15 | 16 | # 17 | # computeMissScore - compute the score depending on the number of 18 | # cache misses 19 | # 20 | def computeMissScore(miss, lower, upper, full_score): 21 | if miss <= lower: 22 | return full_score 23 | if miss >= upper: 24 | return 0 25 | 26 | score = (miss - lower) * 1.0 27 | range = (upper- lower) * 1.0 28 | return round((1 - score / range) * full_score, 1) 29 | 30 | # 31 | # main - Main function 32 | # 33 | def main(): 34 | 35 | # Configure maxscores here 36 | maxscore= {}; 37 | maxscore['csim'] = 27 38 | maxscore['transc'] = 1 39 | maxscore['trans32'] = 8 40 | maxscore['trans64'] = 8 41 | maxscore['trans61'] = 10 42 | 43 | # Parse the command line arguments 44 | p = optparse.OptionParser() 45 | p.add_option("-A", action="store_true", dest="autograde", 46 | help="emit autoresult string for Autolab"); 47 | opts, args = p.parse_args() 48 | autograde = opts.autograde 49 | 50 | # Check the correctness of the cache simulator 51 | print "Part A: Testing cache simulator" 52 | print "Running ./test-csim" 53 | p = subprocess.Popen("./test-csim", 54 | shell=True, stdout=subprocess.PIPE) 55 | stdout_data = p.communicate()[0] 56 | 57 | # Emit the output from test-csim 58 | stdout_data = re.split('\n', stdout_data) 59 | for line in stdout_data: 60 | if re.match("TEST_CSIM_RESULTS", line): 61 | resultsim = re.findall(r'(\d+)', line) 62 | else: 63 | print "%s" % (line) 64 | 65 | # Check the correctness and performance of the transpose function 66 | # 32x32 transpose 67 | print "Part B: Testing transpose function" 68 | print "Running ./test-trans -M 32 -N 32" 69 | p = subprocess.Popen("./test-trans -M 32 -N 32 | grep TEST_TRANS_RESULTS", 70 | shell=True, stdout=subprocess.PIPE) 71 | stdout_data = p.communicate()[0] 72 | result32 = re.findall(r'(\d+)', stdout_data) 73 | 74 | # 64x64 transpose 75 | print "Running ./test-trans -M 64 -N 64" 76 | p = subprocess.Popen("./test-trans -M 64 -N 64 | grep TEST_TRANS_RESULTS", 77 | shell=True, stdout=subprocess.PIPE) 78 | stdout_data = p.communicate()[0] 79 | result64 = re.findall(r'(\d+)', stdout_data) 80 | 81 | # 61x67 transpose 82 | print "Running ./test-trans -M 61 -N 67" 83 | p = subprocess.Popen("./test-trans -M 61 -N 67 | grep TEST_TRANS_RESULTS", 84 | shell=True, stdout=subprocess.PIPE) 85 | stdout_data = p.communicate()[0] 86 | result61 = re.findall(r'(\d+)', stdout_data) 87 | 88 | # Compute the scores for each step 89 | csim_cscore = map(int, resultsim[0:1]) 90 | trans_cscore = int(result32[0]) * int(result64[0]) * int(result61[0]); 91 | miss32 = int(result32[1]) 92 | miss64 = int(result64[1]) 93 | miss61 = int(result61[1]) 94 | trans32_score = computeMissScore(miss32, 300, 600, maxscore['trans32']) * int(result32[0]) 95 | trans64_score = computeMissScore(miss64, 1300, 2000, maxscore['trans64']) * int(result64[0]) 96 | trans61_score = computeMissScore(miss61, 2000, 3000, maxscore['trans61']) * int(result61[0]) 97 | total_score = csim_cscore[0] + trans32_score + trans64_score + trans61_score 98 | 99 | # Summarize the results 100 | print "\nCache Lab summary:" 101 | print "%-22s%8s%10s%12s" % ("", "Points", "Max pts", "Misses") 102 | print "%-22s%8.1f%10d" % ("Csim correctness", csim_cscore[0], 103 | maxscore['csim']) 104 | 105 | misses = str(miss32) 106 | if miss32 == 2**31-1 : 107 | misses = "invalid" 108 | print "%-22s%8.1f%10d%12s" % ("Trans perf 32x32", trans32_score, 109 | maxscore['trans32'], misses) 110 | 111 | misses = str(miss64) 112 | if miss64 == 2**31-1 : 113 | misses = "invalid" 114 | print "%-22s%8.1f%10d%12s" % ("Trans perf 64x64", trans64_score, 115 | maxscore['trans64'], misses) 116 | 117 | misses = str(miss61) 118 | if miss61 == 2**31-1 : 119 | misses = "invalid" 120 | print "%-22s%8.1f%10d%12s" % ("Trans perf 61x67", trans61_score, 121 | maxscore['trans61'], misses) 122 | 123 | print "%22s%8.1f%10d" % ("Total points", total_score, 124 | maxscore['csim'] + 125 | maxscore['trans32'] + 126 | maxscore['trans64'] + 127 | maxscore['trans61']) 128 | 129 | # Emit autoresult string for Autolab if called with -A option 130 | if autograde: 131 | autoresult="%.1f:%d:%d:%d" % (total_score, miss32, miss64, miss61) 132 | print "\nAUTORESULT_STRING=%s" % autoresult 133 | 134 | 135 | # execute main only if called as a script 136 | if __name__ == "__main__": 137 | main() 138 | 139 | -------------------------------------------------------------------------------- /cachelab-handout/test-csim: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zyearn/csapp-lab-2e/baca11bded68642413b1b7e0ab57f00497033a8a/cachelab-handout/test-csim -------------------------------------------------------------------------------- /cachelab-handout/test-trans: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zyearn/csapp-lab-2e/baca11bded68642413b1b7e0ab57f00497033a8a/cachelab-handout/test-trans -------------------------------------------------------------------------------- /cachelab-handout/test-trans.c: -------------------------------------------------------------------------------- 1 | /* 2 | * test-trans.c - Checks the correctness and performance of all of the 3 | * student's transpose functions and records the results for their 4 | * official submitted version as well. 5 | */ 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include "cachelab.h" 15 | #include // fir WEXITSTATUS 16 | #include // for INT_MAX 17 | 18 | /* Maximum array dimension */ 19 | #define MAXN 256 20 | 21 | /* The description string for the transpose_submit() function that the 22 | student submits for credit */ 23 | #define SUBMIT_DESCRIPTION "Transpose submission" 24 | 25 | /* External function defined in trans.c */ 26 | extern void registerFunctions(); 27 | 28 | /* External variables defined in cachelab-tools.c */ 29 | extern trans_func_t func_list[MAX_TRANS_FUNCS]; 30 | extern int func_counter; 31 | 32 | /* Globals set on the command line */ 33 | static int M = 0; 34 | static int N = 0; 35 | 36 | /* The correctness and performance for the submitted transpose function */ 37 | struct results { 38 | int funcid; 39 | int correct; 40 | int misses; 41 | }; 42 | static struct results results = {-1, 0, INT_MAX}; 43 | 44 | /* 45 | * eval_perf - Evaluate the performance of the registered transpose functions 46 | */ 47 | void eval_perf(unsigned int s, unsigned int E, unsigned int b) 48 | { 49 | int i,flag; 50 | unsigned int len, hits, misses, evictions; 51 | unsigned long long int marker_start, marker_end, addr; 52 | char buf[1000], cmd[255]; 53 | char filename[128]; 54 | 55 | registerFunctions(); 56 | 57 | /* Open the complete trace file */ 58 | FILE* full_trace_fp; 59 | FILE* part_trace_fp; 60 | 61 | /* Evaluate the performance of each registered transpose function */ 62 | 63 | for (i=0; i trace.tmp", M, N,i); 72 | flag=WEXITSTATUS(system(cmd)); 73 | if (0!=flag) { 74 | printf("Validation error at function %d! Run ./tracegen -M %d -N %d -F %d for details.\nSkipping performance evaluation for this function.\n",flag-1,M,N,i); 75 | continue; 76 | } 77 | 78 | /* Get the start and end marker addresses */ 79 | FILE* marker_fp = fopen(".marker", "r"); 80 | assert(marker_fp); 81 | fscanf(marker_fp, "%llx %llx", &marker_start, &marker_end); 82 | fclose(marker_fp); 83 | 84 | 85 | func_list[i].correct=1; 86 | 87 | /* Save the correctness of the transpose submission */ 88 | if (results.funcid == i ) { 89 | results.correct = 1; 90 | } 91 | 92 | full_trace_fp = fopen("trace.tmp", "r"); 93 | assert(full_trace_fp); 94 | 95 | 96 | /* Filtered trace for each transpose function goes in a separate file */ 97 | sprintf(filename, "trace.f%d", i); 98 | part_trace_fp = fopen(filename, "w"); 99 | assert(part_trace_fp); 100 | 101 | /* Locate trace corresponding to the trans function */ 102 | flag = 0; 103 | while (fgets(buf, 1000, full_trace_fp) != NULL) { 104 | 105 | /* We are only interested in memory access instructions */ 106 | if (buf[0]==' ' && buf[2]==' ' && 107 | (buf[1]=='S' || buf[1]=='M' || buf[1]=='L' )) { 108 | sscanf(buf+3, "%llx,%u", &addr, &len); 109 | 110 | /* If start marker found, set flag */ 111 | if (addr == marker_start) 112 | flag = 1; 113 | 114 | /* Valgrind creates many spurious accesses to the 115 | stack that have nothing to do with the students 116 | code. At the moment, we are ignoring all stack 117 | accesses by using the simple filter of recording 118 | accesses to only the low 32-bit portion of the 119 | address space. At some point it would be nice to 120 | try to do more informed filtering so that would 121 | eliminate the valgrind stack references while 122 | include the student stack references. */ 123 | if (flag && addr < 0xffffffff) { 124 | fputs(buf, part_trace_fp); 125 | } 126 | 127 | /* if end marker found, close trace file */ 128 | if (addr == marker_end) { 129 | flag = 0; 130 | fclose(part_trace_fp); 131 | break; 132 | } 133 | } 134 | } 135 | fclose(full_trace_fp); 136 | 137 | /* Run the reference simulator */ 138 | printf("Step 2: Evaluating performance (s=%d, E=%d, b=%d)\n", s, E, b); 139 | char cmd[255]; 140 | sprintf(cmd, "./csim-ref -s %u -E %u -b %u -t trace.f%d > /dev/null", 141 | s, E, b, i); 142 | system(cmd); 143 | 144 | /* Collect results from the reference simulator */ 145 | FILE* in_fp = fopen(".csim_results","r"); 146 | assert(in_fp); 147 | fscanf(in_fp, "%u %u %u", &hits, &misses, &evictions); 148 | fclose(in_fp); 149 | func_list[i].num_hits = hits; 150 | func_list[i].num_misses = misses; 151 | func_list[i].num_evictions = evictions; 152 | printf("func %u (%s): hits:%u, misses:%u, evictions:%u\n", 153 | i, func_list[i].description, hits, misses, evictions); 154 | 155 | /* If it is transpose_submit(), record number of misses */ 156 | if (results.funcid == i) { 157 | results.misses = misses; 158 | } 159 | } 160 | 161 | } 162 | 163 | /* 164 | * usage - Print usage info 165 | */ 166 | void usage(char *argv[]){ 167 | printf("Usage: %s [-h] -M -N \n", argv[0]); 168 | printf("Options:\n"); 169 | printf(" -h Print this help message.\n"); 170 | printf(" -M Number of matrix rows (max %d)\n", MAXN); 171 | printf(" -N Number of matrix columns (max %d)\n", MAXN); 172 | printf("Example: %s -M 8 -N 8\n", argv[0]); 173 | } 174 | 175 | /* 176 | * sigsegv_handler - SIGSEGV handler 177 | */ 178 | void sigsegv_handler(int signum){ 179 | printf("Error: Segmentation Fault.\n"); 180 | printf("TEST_TRANS_RESULTS=0:0\n"); 181 | fflush(stdout); 182 | exit(1); 183 | } 184 | 185 | /* 186 | * sigalrm_handler - SIGALRM handler 187 | */ 188 | void sigalrm_handler(int signum){ 189 | printf("Error: Program timed out.\n"); 190 | printf("TEST_TRANS_RESULTS=0:0\n"); 191 | fflush(stdout); 192 | exit(1); 193 | } 194 | 195 | /* 196 | * main - Main routine 197 | */ 198 | int main(int argc, char* argv[]) 199 | { 200 | char c; 201 | 202 | while ((c = getopt(argc,argv,"M:N:h")) != -1) { 203 | switch(c) { 204 | case 'M': 205 | M = atoi(optarg); 206 | break; 207 | case 'N': 208 | N = atoi(optarg); 209 | break; 210 | case 'h': 211 | usage(argv); 212 | exit(0); 213 | default: 214 | usage(argv); 215 | exit(1); 216 | } 217 | } 218 | 219 | if (M == 0 || N == 0) { 220 | printf("Error: Missing required argument\n"); 221 | usage(argv); 222 | exit(1); 223 | } 224 | 225 | if (M > MAXN || N > MAXN) { 226 | printf("Error: M or N exceeds %d\n", MAXN); 227 | usage(argv); 228 | exit(1); 229 | } 230 | 231 | /* Install SIGSEGV and SIGALRM handlers */ 232 | if (signal(SIGSEGV, sigsegv_handler) == SIG_ERR) { 233 | fprintf(stderr, "Unable to install SIGALRM handler\n"); 234 | exit(1); 235 | } 236 | 237 | if (signal(SIGALRM, sigalrm_handler) == SIG_ERR) { 238 | fprintf(stderr, "Unable to install SIGALRM handler\n"); 239 | exit(1); 240 | } 241 | 242 | /* Time out and give up after a while */ 243 | alarm(120); 244 | 245 | /* Check the performance of the student's transpose function */ 246 | eval_perf(5, 1, 5); 247 | 248 | /* Emit the results for this particular test */ 249 | if (results.funcid == -1) { 250 | printf("\nError: We could not find your transpose_submit() function\n"); 251 | printf("Error: Please ensure that description field is exactly \"%s\"\n", 252 | SUBMIT_DESCRIPTION); 253 | printf("\nTEST_TRANS_RESULTS=0:0\n"); 254 | } 255 | else { 256 | printf("\nSummary for official submission (func %d): correctness=%d misses=%d\n", 257 | results.funcid, results.correct, results.misses); 258 | printf("\nTEST_TRANS_RESULTS=%d:%d\n", results.correct, results.misses); 259 | } 260 | return 0; 261 | } 262 | -------------------------------------------------------------------------------- /cachelab-handout/tracegen: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zyearn/csapp-lab-2e/baca11bded68642413b1b7e0ab57f00497033a8a/cachelab-handout/tracegen -------------------------------------------------------------------------------- /cachelab-handout/tracegen.c: -------------------------------------------------------------------------------- 1 | /* 2 | * tracegen.c - Running the binary tracegen with valgrind produces 3 | * a memory trace of all of the registered transpose functions. 4 | * 5 | * The beginning and end of each registered transpose function's trace 6 | * is indicated by reading from "marker" addresses. These two marker 7 | * addresses are recorded in file for later use. 8 | */ 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include "cachelab.h" 16 | #include 17 | 18 | /* External variables declared in cachelab.c */ 19 | extern trans_func_t func_list[MAX_TRANS_FUNCS]; 20 | extern int func_counter; 21 | 22 | /* External function from trans.c */ 23 | extern void registerFunctions(); 24 | 25 | /* Markers used to bound trace regions of interest */ 26 | volatile char MARKER_START, MARKER_END; 27 | 28 | static int A[256][256]; 29 | static int B[256][256]; 30 | static int M; 31 | static int N; 32 | 33 | 34 | int validate(int fn,int M, int N, int A[N][M], int B[M][N]) { 35 | int C[M][N]; 36 | memset(C,0,sizeof(C)); 37 | correctTrans(M,N,A,C); 38 | for(int i=0;i 11 | #include "cachelab.h" 12 | 13 | int is_transpose(int M, int N, int A[N][M], int B[M][N]); 14 | 15 | /* 16 | * transpose_submit - This is the solution transpose function that you 17 | * will be graded on for Part B of the assignment. Do not change 18 | * the description string "Transpose submission", as the driver 19 | * searches for that string to identify the transpose function to 20 | * be graded. 21 | */ 22 | char transpose_submit_desc[] = "Transpose submission"; 23 | void transpose_submit(int M, int N, int A[N][M], int B[M][N]) 24 | { 25 | } 26 | 27 | /* 28 | * You can define additional transpose functions below. We've defined 29 | * a simple one below to help you get started. 30 | */ 31 | 32 | /* 33 | * trans - A simple baseline transpose function, not optimized for the cache. 34 | */ 35 | char trans_desc[] = "Simple row-wise scan transpose"; 36 | void trans(int M, int N, int A[N][M], int B[M][N]) 37 | { 38 | int i, j, tmp; 39 | 40 | for (i = 0; i < N; i++) { 41 | for (j = 0; j < M; j++) { 42 | tmp = A[i][j]; 43 | B[j][i] = tmp; 44 | } 45 | } 46 | 47 | } 48 | 49 | /* 50 | * registerFunctions - This function registers your transpose 51 | * functions with the driver. At runtime, the driver will 52 | * evaluate each of the registered functions and summarize their 53 | * performance. This is a handy way to experiment with different 54 | * transpose strategies. 55 | */ 56 | void registerFunctions() 57 | { 58 | /* Register your solution function */ 59 | registerTransFunction(transpose_submit, transpose_submit_desc); 60 | 61 | /* Register any additional transpose functions */ 62 | registerTransFunction(trans, trans_desc); 63 | 64 | } 65 | 66 | /* 67 | * is_transpose - This helper function checks if B is the transpose of 68 | * A. You can check the correctness of your transpose by calling 69 | * it before returning from the transpose function. 70 | */ 71 | int is_transpose(int M, int N, int A[N][M], int B[M][N]) 72 | { 73 | int i, j; 74 | 75 | for (i = 0; i < N; i++) { 76 | for (j = 0; j < M; ++j) { 77 | if (A[i][j] != B[j][i]) { 78 | return 0; 79 | } 80 | } 81 | } 82 | return 1; 83 | } 84 | 85 | -------------------------------------------------------------------------------- /cachelab-handout/trans.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zyearn/csapp-lab-2e/baca11bded68642413b1b7e0ab57f00497033a8a/cachelab-handout/trans.o -------------------------------------------------------------------------------- /datalab-handout/.bits.h.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zyearn/csapp-lab-2e/baca11bded68642413b1b7e0ab57f00497033a8a/datalab-handout/.bits.h.swp -------------------------------------------------------------------------------- /datalab-handout/Driverhdrs.pm: -------------------------------------------------------------------------------- 1 | # 2 | # This file contains configuration variables for drivers. 3 | # It was generated by genhdrs.pl. Do not modify it. 4 | # 5 | package Driverhdrs; 6 | 7 | $LAB = "datalab"; 8 | $SERVER_NAME = "changeme.ics.cs.cmu.edu"; 9 | $SERVER_PORT = 8081; 10 | $COURSE_NAME = "csapp"; 11 | $AUTOGRADE_TIMEOUT = 0; 12 | 1; 13 | -------------------------------------------------------------------------------- /datalab-handout/Driverlib.pm: -------------------------------------------------------------------------------- 1 | ############################################################### 2 | # Driverlib.pm - A package of helper functions for Perl drivers 3 | # 4 | # Copyright (c) 2005 David R. O'Hallaron, All rights reserved. 5 | ############################################################### 6 | 7 | package Driverlib; 8 | 9 | use Socket; 10 | 11 | # Autogenerated header file with lab-specific constants 12 | use lib "."; 13 | use Driverhdrs; 14 | 15 | require Exporter; 16 | @ISA = qw(Exporter); 17 | @EXPORT = qw( 18 | driver_post 19 | ); 20 | 21 | use strict; 22 | 23 | ##### 24 | # Public functions 25 | # 26 | 27 | # 28 | # driver_post - This is the routine that a driver calls when 29 | # it needs to transmit an autoresult string to the result server. 30 | # 31 | sub driver_post ($$) { 32 | my $userid = shift; # User id for this submission 33 | my $result = shift; # Autoresult string 34 | my $autograded = shift; # Set if called by an autograder 35 | 36 | # Echo the autoresult string to stdout if the driver was called 37 | # by an autograder 38 | if ($autograded) { 39 | print "\n"; 40 | print "AUTORESULT_STRING=$result\n"; 41 | return; 42 | } 43 | 44 | # If the driver was called with a specific userid, then submit 45 | # the autoresult string to the result server over the Internet. 46 | if ($userid) { 47 | my $status = submitr($Driverhdrs::SERVER_NAME, 48 | $Driverhdrs::SERVER_PORT, 49 | $Driverhdrs::COURSE_NAME, 50 | $userid, 51 | $Driverhdrs::LAB, 52 | $result); 53 | 54 | # Print the status of the transfer 55 | if (!($status =~ /OK/)) { 56 | print "$status\n"; 57 | print "Did not send autoresult string to the result server.\n"; 58 | exit(1); 59 | } 60 | print "Success: Sent autoresult string for $userid to the result server.\n"; 61 | } 62 | } 63 | 64 | 65 | ##### 66 | # Private functions 67 | # 68 | 69 | # 70 | # submitr - Sends an autoresult string to the result server 71 | # 72 | sub submitr ($$$$$$) { 73 | my $hostname = shift; 74 | my $port = shift; 75 | my $course = shift; 76 | my $userid = shift; 77 | my $lab = shift; 78 | my $result = shift; 79 | 80 | my $internet_addr; 81 | my $enc_result; 82 | my $paddr; 83 | my $line; 84 | my $http_version; 85 | my $errcode; 86 | my $errmsg; 87 | 88 | # Establish the connection to the server 89 | socket(SERVER, PF_INET, SOCK_STREAM, getprotobyname('tcp')); 90 | $internet_addr = inet_aton($hostname) 91 | or die "Could not convert $hostname to an internet address: $!\n"; 92 | $paddr = sockaddr_in($port, $internet_addr); 93 | connect(SERVER, $paddr) 94 | or die "Could not connect to $hostname:$port:$!\n"; 95 | 96 | select((select(SERVER), $| = 1)[0]); # enable command buffering 97 | 98 | # Send HTTP request to server 99 | $enc_result = url_encode($result); 100 | print SERVER "GET /$course/submitr.pl/?userid=$userid&lab=$lab&result=$enc_result&submit=submit HTTP/1.0\r\n\r\n"; 101 | 102 | # Get first HTTP response line 103 | $line = ; 104 | chomp($line); 105 | ($http_version, $errcode, $errmsg) = split(/\s+/, $line); 106 | if ($errcode != 200) { 107 | return "Error: HTTP request failed with error $errcode: $errmsg"; 108 | } 109 | 110 | # Read the remaining HTTP response header lines 111 | while ($line = ) { 112 | if ($line =~ /^\r\n/) { 113 | last; 114 | } 115 | } 116 | 117 | # Read and return the response from the result server 118 | $line = ; 119 | chomp($line); 120 | 121 | close SERVER; 122 | return $line; 123 | 124 | } 125 | 126 | # 127 | # url_encode - Encode text string so it can be included in URI of GET request 128 | # 129 | sub url_encode ($) { 130 | my $value = shift; 131 | 132 | $value =~s/([^a-zA-Z0-9_\-.])/uc sprintf("%%%02x",ord($1))/eg; 133 | return $value; 134 | } 135 | 136 | # Always end a module with a 1 so that it returns TRUE 137 | 1; 138 | 139 | -------------------------------------------------------------------------------- /datalab-handout/Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # Makefile that builds btest and other helper programs for the CS:APP data lab 3 | # 4 | CC = gcc 5 | CFLAGS = -O -Wall -m32 6 | LIBS = -lm 7 | 8 | all: btest fshow ishow 9 | 10 | btest: btest.c bits.c decl.c tests.c btest.h bits.h 11 | $(CC) $(CFLAGS) $(LIBS) -o btest bits.c btest.c decl.c tests.c 12 | 13 | fshow: fshow.c 14 | $(CC) $(CFLAGS) -o fshow fshow.c 15 | 16 | ishow: ishow.c 17 | $(CC) $(CFLAGS) -o ishow ishow.c 18 | 19 | # Forces a recompile. Used by the driver program. 20 | btestexplicit: 21 | $(CC) $(CFLAGS) $(LIBS) -o btest bits.c btest.c decl.c tests.c 22 | 23 | clean: 24 | rm -f *.o btest fshow ishow *~ 25 | 26 | 27 | -------------------------------------------------------------------------------- /datalab-handout/README: -------------------------------------------------------------------------------- 1 | *********************** 2 | The CS:APP Data Lab 3 | Directions to Students 4 | *********************** 5 | 6 | Your goal is to modify your copy of bits.c so that it passes all the 7 | tests in btest without violating any of the coding guidelines. 8 | 9 | 10 | ********* 11 | 0. Files: 12 | ********* 13 | 14 | Makefile - Makes btest, fshow, and ishow 15 | README - This file 16 | bits.c - The file you will be modifying and handing in 17 | bits.h - Header file 18 | btest.c - The main btest program 19 | btest.h - Used to build btest 20 | decl.c - Used to build btest 21 | tests.c - Used to build btest 22 | tests-header.c- Used to build btest 23 | dlc* - Rule checking compiler binary (data lab compiler) 24 | driver.pl* - Driver program that uses btest and dlc to autograde bits.c 25 | Driverhdrs.pm - Header file for optional "Beat the Prof" contest 26 | fshow.c - Utility for examining floating-point representations 27 | ishow.c - Utility for examining integer representations 28 | 29 | *********************************************************** 30 | 1. Modifying bits.c and checking it for compliance with dlc 31 | *********************************************************** 32 | 33 | IMPORTANT: Carefully read the instructions in the bits.c file before 34 | you start. These give the coding rules that you will need to follow if 35 | you want full credit. 36 | 37 | Use the dlc compiler (./dlc) to automatically check your version of 38 | bits.c for compliance with the coding guidelines: 39 | 40 | unix> ./dlc bits.c 41 | 42 | dlc returns silently if there are no problems with your code. 43 | Otherwise it prints messages that flag any problems. Running dlc with 44 | the -e switch: 45 | 46 | unix> ./dlc -e bits.c 47 | 48 | causes dlc to print counts of the number of operators used by each function. 49 | 50 | Once you have a legal solution, you can test it for correctness using 51 | the ./btest program. 52 | 53 | ********************* 54 | 2. Testing with btest 55 | ********************* 56 | 57 | The Makefile in this directory compiles your version of bits.c with 58 | additional code to create a program (or test harness) named btest. 59 | 60 | To compile and run the btest program, type: 61 | 62 | unix> make btest 63 | unix> ./btest [optional cmd line args] 64 | 65 | You will need to recompile btest each time you change your bits.c 66 | program. When moving from one platform to another, you will want to 67 | get rid of the old version of btest and generate a new one. Use the 68 | commands: 69 | 70 | unix> make clean 71 | unix> make btest 72 | 73 | Btest tests your code for correctness by running millions of test 74 | cases on each function. It tests wide swaths around well known corner 75 | cases such as Tmin and zero for integer puzzles, and zero, inf, and 76 | the boundary between denormalized and normalized numbers for floating 77 | point puzzles. When btest detects an error in one of your functions, 78 | it prints out the test that failed, the incorrect result, and the 79 | expected result, and then terminates the testing for that function. 80 | 81 | Here are the command line options for btest: 82 | 83 | unix> ./btest -h 84 | Usage: ./btest [-hg] [-r ] [-f [-1|-2|-3 ]*] [-T