You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

152 lines
3.7 KiB

10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
  1. #include "cachelab.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <unistd.h>
  6. #include <getopt.h>
  7. typedef struct
  8. {
  9. unsigned tag;
  10. unsigned usedtime;
  11. } block;
  12. block *cache;
  13. int getOpt(int argc,char **argv,int *s,int *E,int *b,int *verbose,char *tracefile);
  14. void printHelpMenu();
  15. void cacheStateOut(char op,int type);
  16. void find(char op,unsigned addr,unsigned size,int time);
  17. int hit,miss,eviction;
  18. int verbose;
  19. int s,E,b;
  20. int main(int argc,char **argv)
  21. {
  22. FILE *fp;
  23. char tracefile[100];
  24. char op[10];
  25. unsigned addr,size;
  26. int t;
  27. getOpt(argc,argv,&s,&E,&b,&verbose,tracefile); // get option
  28. if(s<0||E<0||b<1){
  29. printf("Invalid Cache parameter\n\n");
  30. exit(1);
  31. }
  32. cache = (block *)malloc(sizeof(block)* E<<s);
  33. memset(cache,0,sizeof(block)* E<<s);
  34. fp = fopen (tracefile,"r");
  35. while(fscanf(fp,"%s%x,%d\n",op,&addr,&size) > 0){
  36. if(verbose)
  37. printf("%s %x,%d ",op,addr,size);
  38. switch(op[0]){
  39. case 'M': hit++;
  40. case 'L':
  41. case 'S': find(op[0],addr,size,++t);
  42. }
  43. }
  44. printSummary(hit, miss, eviction);
  45. free(cache);
  46. fclose(fp);
  47. return 0;
  48. }
  49. void find(char op, unsigned addr,unsigned size,int time){
  50. int i;
  51. unsigned tag = addr >>b >>s ;
  52. unsigned set_index = addr >> b &((1<<s) -1);
  53. block *cache_set = cache + E * set_index ; // set address
  54. block *eviction_block = cache_set; // LRU cacheline
  55. for(i = 0;i<E;i++){
  56. if(cache_set[i].usedtime>0 && cache_set[i].tag ==tag){ //hit
  57. cache_set[i].usedtime = time;
  58. hit++;
  59. if(verbose) cacheStateOut(op,0);
  60. return;
  61. }
  62. else if(!cache_set[i].usedtime){ // empty block
  63. miss++;
  64. cache_set[i].tag = tag;
  65. cache_set[i].usedtime = time;
  66. if(verbose) cacheStateOut(op,1);
  67. return;
  68. }
  69. else if(cache_set[i].usedtime < eviction_block->usedtime) // !=tag , current block is older
  70. eviction_block = cache_set+i;
  71. }
  72. miss ++;
  73. eviction ++;
  74. eviction_block->tag = tag; // replace sacrifice cacheline
  75. eviction_block->usedtime = time;
  76. if(verbose) cacheStateOut(op,2);
  77. return ;
  78. }
  79. int getOpt(int argc,char **argv,int *s,int *E,int *b,int *verbose,char *tracefile)
  80. {
  81. int oc;
  82. while((oc=getopt(argc,argv,"hvs:E:b:t:"))!=-1){
  83. switch(oc){
  84. case 'h': printHelpMenu();break; // print usage
  85. case 'v': *verbose=1;break;
  86. case 's': *s = atoi(optarg);break;
  87. case 'E': *E = atoi(optarg);break;
  88. case 'b': *b = atoi(optarg);break;
  89. case 't': strcpy(tracefile,optarg);break;
  90. default : printf("input error\n");break;
  91. }
  92. }
  93. return 0;
  94. }
  95. void cacheStateOut(char op,int type){
  96. switch(type){
  97. case 0: //hit
  98. switch(op){
  99. case 'L':
  100. case 'S':printf("hit\n");break;
  101. case 'M':printf("hit hit\n");break;
  102. }break;
  103. case 1: //miss
  104. switch(op){
  105. case 'L':
  106. case 'S':printf("miss\n");break;
  107. case 'M':printf("miss hit\n");break;
  108. }
  109. case 2: //eviction
  110. switch(op){
  111. case 'L':
  112. case 'S':printf("miss eviction\n");break;
  113. case 'M':printf("miss eviction hit\n");break;
  114. }break;
  115. }
  116. }
  117. void printHelpMenu(){
  118. printf("Usage: ./csim [-hv] -s <num> -E <num> -b <num> -t <file>\n");
  119. printf("Options:\n");
  120. printf("-h Print this help message.\n");
  121. printf("-v Optional verbose flag.\n");
  122. printf("-s <num> Number of set index bits.\n");
  123. printf("-E <num> Number of lines per set.\n");
  124. printf("-b <num> Number of block offset bits.\n");
  125. printf("-t <file> Trace file.\n\n\n");
  126. printf("Examples:\n");
  127. printf("linux> ./csim -s 4 -E 1 -b 4 -t traces/yi.trace\n");
  128. printf("linux> ./csim -v -s 8 -E 2 -b 4 -t traces/yi.trace\n");
  129. }
  130. void checkOptarg(char *curOptarg){
  131. if(curOptarg[0]=='-'){
  132. printf("./csim :Missing required command line argument\n");
  133. printHelpMenu();
  134. exit(0);
  135. }
  136. }