int main(int argc, char** argv){ if(argc != 3){ std::cerr << "USAGE: ./cache_simulator <input_file_name> <output-file-name>" << std::endl; exit(1); } std::ofstream outfile(argv[2]); int cache_line_size = -1, cache_size = -1, associativity = -1; std::string policy = ""; Cache_simulator direct_mapped(DIRECT_MAPPED, argv[1]); cache_line_size = 32; int cache_size_array[] = {1024, 4096, 16384, 32768}; for(int i = 0; i < 4; i++){ outfile << direct_mapped.simulate(cache_size_array[i], cache_line_size, associativity, policy); outfile << " " << direct_mapped.get_total_referenced() << " "; } outfile << std::endl; Cache_simulator set_associative(SET_ASSOCIATIVE, argv[1]); int associativity_array[] = {2, 4, 8, 16}; cache_size = 16384; for(int i = 0; i < 4; i++){ outfile << set_associative.simulate(cache_size, cache_line_size, associativity_array[i], policy); outfile << " " << set_associative.get_total_referenced() << " "; } outfile << std::endl; Cache_simulator fully_associative(FULLY_ASSOCIATIVE, argv[1]); policy = "LRU"; outfile << fully_associative.simulate(cache_size, cache_line_size, associativity, policy); outfile << " " << fully_associative.get_total_referenced() << std::endl; policy = "HOT-COLD"; outfile << fully_associative.simulate(cache_size, cache_line_size, associativity, policy); outfile << " " << fully_associative.get_total_referenced() << std::endl; Cache_simulator set_associative_no_alloc_write_miss(SET_ASSOCIATIVE_NO_ALLOC_ON_WRITE_MISS, argv[1]); for(int i = 0; i < 4; i++){ outfile << set_associative_no_alloc_write_miss.simulate(cache_size, cache_line_size, associativity_array[i], policy); outfile << " " << set_associative_no_alloc_write_miss.get_total_referenced() << " "; } outfile << std::endl; Cache_simulator set_associative_nextline_prefetching(SET_ASSOCIATIVE_NEXTLINE_PREFETCHING, argv[1]); for(int i = 0; i < 4; i++){ outfile << set_associative_nextline_prefetching.simulate(cache_size, cache_line_size, associativity_array[i], policy); outfile << " " << set_associative_nextline_prefetching.get_total_referenced() << " "; } outfile << std::endl; Cache_simulator prefetch_on_miss(PREFETCH_ON_MISS, argv[1]); for(int i = 0; i < 4; i++){ outfile << prefetch_on_miss.simulate(cache_size, cache_line_size, associativity_array[i], policy); outfile << " " << prefetch_on_miss.get_total_referenced() << " "; } outfile << std::endl; //Extra credit Cache_simulator test(FIFO, argv[1]); std::cout << "Extra credit: FIFO replacement\n" << "Cache hit: " << test.simulate(16*1024, 32, -1, "") << ", total referenced: " << test.get_total_referenced() << std::endl; outfile.close(); return 0; }
int main(){ errno_t errorCode =fopen_s(&output,"Arxeio Dedomenwn","w+"); int flush_count=0; long int random_num; int ram_bits; int cache_bits; int i=0; int j=0; int k=0; int counter=0; double checkbase_double; int checkbase_int; long int rem[50]; char entoli[4]={'R','W','M','F'}; int random_num2; checkbase_double=log((double)word_size_bytes)/log((double)2); checkbase_int=checkbase_double; // ============ERROR HANDLING============ checkbase_double=log((double)word_size_bytes)/log((double)2); checkbase_int=checkbase_double; if(checkbase_double/checkbase_int != 1) { printf("ERROR!: The word size is not a power of 2\n"); //Not a power of 2 error system("pause"); return 1; } if(cache_size_bytes>ram_size_bytes) { printf("ERROR!: The size of Cache MUST NOT exceed the size of RAM\n"); //cache size exceeds ram size error system("pause"); return 1; } if(ram_size_bytes<=0) { printf("ERROR!: The size of Ram MUST NOT be 0 or below\n"); //cache size exceeds ram size error system("pause"); return 1; } if(cache_size_bytes<=0) { printf("ERROR!: The size of Cache MUST NOT be 0 or below\n"); //cache size exceeds ram size error system("pause"); return 1; } if(num_of_flush>=ram_size*NUM_MUL) { printf("ERROR!: The amount of flushes MUST NOT exceed the amount of references\n"); //amount of flushed exceeds # references error system("pause"); return 1; } checkbase_double=log((double)ram_size_bytes)/log((double)2); checkbase_int=checkbase_double; if(checkbase_double/checkbase_int != 1) { printf("ERROR!: The size of RAM is not a power of 2\n"); //Not a power of 2 error system("pause"); return 1; } checkbase_double=log((double)cache_size_bytes)/log((double)2); checkbase_int=checkbase_double; if(checkbase_double/checkbase_int != 1) { printf("ERROR!: The size of Cache is not a power of 2\n"); //Not a power of 2 error system("pause"); return 1; } checkbase_double=log((double)words_in_block)/log((double)2); checkbase_int=checkbase_double; if(checkbase_double/checkbase_int != 1) { printf("ERROR!: The amount of words in each block is not a power of 2\n"); //Not a power of 2 error system("pause"); return 1; } if(words_in_block<=0) { printf("ERROR!: The amount of words in each block is a negative number\n"); //Not a power of 2 error system("pause"); return 1; } checkbase_double=log((double)num_of_ways)/log((double)2); checkbase_int=checkbase_double; if(checkbase_double/checkbase_int != 1) { printf("ERROR!: The amount of ways is not a power of 2\n"); //Not a power of 2 error system("pause"); return 1; } if(num_of_ways<=0) { printf("ERROR!: The amount of ways is a negative number or zero\n"); //Not a power of 2 error system("pause"); return 1; } ram_bits=log((double)ram_size)/log((double)vasi_2); //calculation of Ram Bits (idiotita logarithmwn => log2(num)=log10(num)/log10(2)) cache_bits=log((double)cache_size)/log((double)vasi_2); //calculation of Cache Bits printf("Size of RAM : %d Bytes => %d Entries\n",ram_size_bytes,ram_size); //Print Messages for info printf("Size of Cache : %d Bytes => %d Entries \n",cache_size_bytes,cache_size); printf("# Bits of RAM Address is : %d \n",ram_bits); switch (type_of_cache) //Prints the type of Cache { case 0:{ printf("Type of Cache: Direct Mapped\n"); break; } case 1:{ printf("Type of Cache: Fully Assosiative\n"); break; } case 2:{ printf("Type of Cache: %d-Way Assosiative\n",num_of_ways); break; } } printf("Each block has %d word (Word Size: %d Byte)\n",words_in_block,word_size_bytes); //Prints the amount and size of words in block srand((int)time(NULL)); for (i = 0; i <(NUM_MUL*ram_size); i++) //Data File generator { counter=counter+1; //counter of flushes random_num=rand() % ((int)(ram_size)-1); //generate random number for reference dec2bin(random_num,rem); //transform number to binary random_num2=rand()%3; //random number for command if((num_of_flush!=0)&&((counter)>=((NUM_MUL*ram_size)/num_of_flush))) //controls the frequency of flushes { random_num2=rand()%4; if(random_num2==3) { counter=0; fprintf(output,"%c",entoli[random_num2]); //prints the letter that matches the command fprintf(output,"\n"); continue; } } else{random_num2=rand()%3;} fprintf(output,"%c ",entoli[random_num2]); if(random_num2!=3) { for(j=ram_bits-1;j>=0;j--) //we print the binary backwords because it comes from the function in backwords { fprintf(output,"%ld",rem[j]);} //printing address in file fprintf(output,"\t (%d)",random_num); } fprintf(output,"\n"); } printf("Arxeio Dedomenwn has been generated!\n"); //message that says that the file has been generated fclose(output); switch (type_of_cache) //switch that calls the function that has been selected { case 0:{ direct_mapped(ram_bits,cache_bits); break; } case 1:{ fully_assosiative(ram_bits,cache_bits); break; } case 2:{ n_way(ram_bits,cache_bits); break; } } printf("Arxeio Dedomenwn has been parsed and Arxeio Eksodou has been generated!\n"); //Finall output file has been generated fclose(fp); fclose(input); system("pause"); return 0; }//end