diff --git a/main.c b/main.c index 945008ca6166f469ae68ac020d66b5a2f255cee7..5e6ac65873659e30ff01a359087cbe1d39809cfd 100644 --- a/main.c +++ b/main.c @@ -194,18 +194,6 @@ int main(int argc, char* argv[]) { * Runs cache simulator logic. */ void run_simulator(char* file_name, bool verbose) { - printf("\nrun_simulator()\n"); - printf(" file_name: %s\n", file_name); - printf(" s: %d\n", s); - printf(" S: %d\n", S); - printf(" e: %d\n", e); - printf(" E: %d\n", E); - printf(" b: %d\n", b); - printf(" B: %d\n", B); - printf(" m: %d\n", m); - printf(" M: %d\n", M); - printf(" verbose: %d\n", verbose); - // Open file. FILE* file_pointer = fopen(file_name, "r"); char* read_in_buffer = calloc(sizeof(char*), 255); @@ -214,8 +202,8 @@ void run_simulator(char* file_name, bool verbose) { while (fgets(read_in_buffer, 255, file_pointer)) { read_in_buffer = strtok(read_in_buffer, "\n"); if (read_in_buffer != NULL && strcmp(read_in_buffer, "") != 0) { - // printf(" line: %s\n", read_in_buffer); + // Set up line parsing values. char* full_line = calloc(255, sizeof(char)); memcpy(full_line, read_in_buffer, strlen(read_in_buffer)); char* line_operation = ""; @@ -225,7 +213,6 @@ void run_simulator(char* file_name, bool verbose) { // Parse values in line. char* token_1 = strtok(read_in_buffer, " "); while (token_1 != NULL) { - // printf(" token: %s", token_1); // Handle if operation not set. if (strcmp(line_operation, "") == 0) { @@ -244,13 +231,7 @@ void run_simulator(char* file_name, bool verbose) { token_1 = strtok(NULL, read_in_buffer); } - // printf(" line_operation: \"%s\"\n", line_operation); - // printf(" line_address: \"%s\"\n", line_address); - // printf(" line_size: \"%s\n", line_size); - // printf("\n\n"); handle_line(full_line, line_operation, line_address, line_size); - // printf("\n"); - free(full_line); } @@ -279,23 +260,13 @@ void handle_line(char* line, char* line_operation, char* line_address, char* lin // Start by validating line. char* char_pointer = line_address; for (int index = 0; index < strlen(line_address); index++) { - // char tempchar[1]; - // printf("tolower: %c\n", tolower(*char_pointer)); - // printf("atoi: %d\n", tolower(*char_pointer) - '0'); - // strcpy(tempchar, char_pointer); - // printf("atoi: %d\n", atoi(tempchar)); if ((tolower(*char_pointer) - '0') > 54) { - // printf("Updating index %d\n", index); line_address[index] = 'f'; } } - // printf("Updated line address: %s\n", line_address); // Convert to integers. long long address = strtoll(line_address, NULL, 16); - // int size = (int)strtol(line_size, NULL, 16) - 1; - // printf(" Address: %d\n", address); - // printf(" Size: %d\n", size); // Get Mask values. int block_mask = B - 1; @@ -304,21 +275,11 @@ void handle_line(char* line, char* line_operation, char* line_address, char* lin int set_offset = s; int tag_mask = (M - 1) ^ set_mask ^ block_mask; - // printf("block_mask: %d\n", block_mask); - // printf("block_offset: %d\n", block_offset); - // printf("set_mask: %d\n", set_mask); - // printf("set_offset: %d\n", set_offset); - // printf("tag_mask: %d\n", tag_mask); - // Use masks to get address chunks. // int block = address & block_mask; int set = (address & set_mask) >> block_offset; int tag = (address & tag_mask) >> (block_offset + set_offset); - // printf(" block: %d\n", block); - // printf(" set: %d\n", set); - // printf(" tag: %d\n", tag); - // check which of 3 operations apply. if (strcmp(line_operation, "L") == 0) { // Is load. @@ -354,19 +315,17 @@ void handle_line(char* line, char* line_operation, char* line_address, char* lin } +/** + * Handles instruction. + */ int process_line(char* line, int set, int tag, bool display_bool) { // Compare against all entries in the cache for match. for (int line_index = 0; line_index < E; line_index++) { - // printf("Set Index: %d line_index: %d\n", set, line_index); int valid_bit = cache->set[set].line[line_index].valid; int tag_bits = cache->set[set].line[line_index].tag; - // printf(" Set[%d][%d] valid bit: %d\n", set, line_index, valid_bit); - // printf(" Set[%d][%d] tag: %d\n", set, line_index, tag_bits); - // Check if match. if (valid_bit == true && tag_bits == tag) { - // printf("tag_bits: %d line_tag: %d\n", tag_bits, tag); // Match found. process_hit(line, set, line_index, display_bool); return 0; @@ -384,7 +343,6 @@ int process_line(char* line, int set, int tag, bool display_bool) { // Get last used index. for (int line_index = 0; line_index < E; line_index++) { // Check if value is equal to or greater than total possible lines. If so, use value. - // printf("line_index: %d last_use: %d\n", line_index, cache->set[set].line[line_index].last_use); if (cache->set[set].line[line_index].last_use >= E - 1) { process_eviction(line, set, line_index, tag, display_bool); @@ -397,6 +355,9 @@ int process_line(char* line, int set, int tag, bool display_bool) { } +/** + * Processes simulated cache for a hit. + */ void process_hit(char* line, int set_index, int line_index, bool display_bool) { hits += 1; @@ -409,6 +370,9 @@ void process_hit(char* line, int set_index, int line_index, bool display_bool) { } +/** + * Processes simulated cache for a miss. + */ void process_miss(char* line, int set_index, int line_index, int tag, bool display_bool) { misses += 1; @@ -418,13 +382,15 @@ void process_miss(char* line, int set_index, int line_index, int tag, bool displ } // Set cache values. - // printf(" New Tag: %d\n", tag); cache->set[set_index].line[line_index].valid = true; cache->set[set_index].line[line_index].tag = tag; update_last_recently_used(set_index, line_index); } +/** + * Processes simulated cache for an eviction. + */ void process_eviction(char* line, int set_index, int line_index, int tag, bool display_bool) { misses += 1; evictions += 1; @@ -435,21 +401,22 @@ void process_eviction(char* line, int set_index, int line_index, int tag, bool d } // Set cache values. - // printf(" New Tag: %d\n", tag); cache->set[set_index].line[line_index].valid = true; cache->set[set_index].line[line_index].tag = tag; update_last_recently_used(set_index, line_index); } +/** + * Updates cache line values according to "Least Recently Used" ordering. + * Farther from 0 means more recently used. + */ void update_last_recently_used(int set_index, int line_index) { // Update all lines in set. for (int index = 0; index < E; index++) { cache->set[set_index].line[index].last_use += 1; - // printf('line_index: %d new last_use: %d', index, cache->set[set_index].line[index].last_use); } // Reset current line index back to 0, as it was the last used. cache->set[set_index].line[line_index].last_use = 0; - // printf("curr line_index: %d new last_use: %d", line_index, cache->set[set_index].line[line_index].last_use); } diff --git a/main.py b/main.py index d18666dcb976102f01c72ccd7ee263e808173192..7013845202722007c95aa973e7f64ed46f6bb491 100644 --- a/main.py +++ b/main.py @@ -205,7 +205,7 @@ class CacheSimulator(): def process_line(self, line, parsed_data, main_bool): """ - Handle store instruction. Works as described in class video. + Handle instruction. :param line: Original line read in. :param parsed_data: Data parsed from line. :param main_bool: Bool to indicate if this is an initial cache line read in or a subquery from one. diff --git a/makefile b/makefile index 17ab4488527bfa9588cd99a5017f63d14c456538..f2dfbfd6534e8cbe45cf8eb739c03d620cd341b1 100644 --- a/makefile +++ b/makefile @@ -1,4 +1,11 @@ +# Date: 02-28-20 +# Class: CS 5541 +# Assignment: Cache Simulator +# Author: Brandon Rodriguez +# Email: bfp5870@wmich.edu + + # Tell makefile to use commands defined here, if file with same name exists. .PHONY: all compile run valgrind clean # Set default if none is specified. diff --git a/readme.md b/readme.md index 1b7e97086034a555729778a3eb120a2772564e40..58028dbbe7db7d1f522dfe7fbd9aede6101b3805 100644 --- a/readme.md +++ b/readme.md @@ -1,8 +1,10 @@ -# Python - Cache Simulator +# C / Python - Cache Simulator ## Language -Python +Implemented in both C and Python. + +The `run.sh` script runs C by default. To switch to Python, adjust lines 83 and 84 in the script. ## Description diff --git a/run.sh b/run.sh index c431cb641c149a2a9e732ae04e36eec05367a72e..434f445f48f985ef3993034f8aee143c93395dcc 100755 --- a/run.sh +++ b/run.sh @@ -1,5 +1,12 @@ #!/usr/bin/env bash ### + # Date: 02-28-20 + # Class: CS 5541 + # Assignment: Cache Simulator + # Author: Brandon Rodriguez + # Email: bfp5870@wmich.edu + # + # # Script to compile and run iloc Value Numbering project. ## @@ -68,14 +75,14 @@ function execute_single_file () { echo -e "${color_blue}Running input file on \"wmucachelab/csim-ref\". Runtime args:${color_reset}" echo -e "${color_blue} $@${color_reset}" ./wmucachelab/csim-ref $@ -# ./wmucachelab/csim-ref -v -s 1 -E 2 -b 1 -t $file # Run input file on compiled C code. echo "" echo -e "${color_blue}Running input file on Python code. Runtime args:${color_reset}" echo -e "${color_blue} $@${color_reset}" -# python3 main.py $@ - valgrind --leak-check=full ./a.out $@ + # python3 main.py $@ + ./a.out $@ + # valgrind --leak-check=full --show-leak-kinds=all ./a.out $@ # Clean up directory. make clean