Browse Source

Reordered functions.

Moved functions into labelled sections.
Made function definition order consistent with prototype order.
Todo: Add section descriptions, break out additional functions, add doc comments.
master
Jacob 6 years ago
parent
commit
189807e834
  1. 200
      csim.c

200
csim.c

@ -3,25 +3,35 @@ @@ -3,25 +3,35 @@
@file csim.c
@author Adam Goldsmith
@author Jacob Komissar
@date 2016-04-25, 2016-04-26, 2016-04-27
@date 2016-04-25, 2016-04-26, 2016-04-27, 2016-04-28
@TODO ADD CORRECT NAME COMMENT AT TOP OF FILE
Cachelab: csim.c
Simulates a cache with arbitrary sets, associativity, and block size.
Reads from a valgrind trace, and outputs the number of hits, misses, and evictions.
With the verbose option (-v), outputs details for each operation.
Exit statuses:
0 - success
1 - usage error
-1 - file error
Exit statuses:
0 - success
1 - usage error
-1 - file error
*/
*/
/* Headers */
/**** Headers ****/
#include <stdlib.h>
#include <stdio.h>
#include <getopt.h>
#include "cachelab.h"
/* Typedefs and structs */
/******** Typedefs and structs ********/
typedef unsigned char uint8_t;
typedef struct {
int hits;
int misses;
int evictions;
} results;
/* Linked Line and related prototypes */
typedef struct linked_line_s linked_line;
struct linked_line_s {
@ -31,35 +41,41 @@ struct linked_line_s { @@ -31,35 +41,41 @@ struct linked_line_s {
linked_line* older;
};
typedef struct {
int hits;
int misses;
int evictions;
} results;
/* Macros */
/******** Macros ********/
#define mprintf(...) if (VERBOSE) printf(__VA_ARGS__)
/* General Prototypes */
/******** Global variables ********/
static uint8_t VERBOSE = 0; /**< If nonzero, mprintf will not print. Set in main() if -v flag is given. */
/******** General Prototypes ********/
/* Option-parsing functions */
void print_usage(void);
void bad_usage_error(void);
long parse_int_arg(char *arg, char opt);
long bits_to_size(int bits);
/* Sub-main functions */
void increment_result_counters(char result, results* score);
/* Cache creation/deletion functions */
linked_line** make_cache(long num_sets, long num_lines);
linked_line* make_set(linked_line* newer, long num_lines);
void free_cache(linked_line** cache, long num_sets);
void free_set(linked_line* line);
/* Cache-viewing functions */
void print_cache(linked_line** cache, long num_sets);
void print_set(linked_line* line);
/* Cache access functions */
char cache_access(linked_line** cache, long address, int set_bits, int block_bits, long num_lines);
linked_line* find_line(linked_line* set_head, long tag);
void increment_result_counters(char result, results* score);
/* Global variables */
uint8_t VERBOSE = 0; /**< If nonzero, mprintf will not print. Set in main() if -v flag is given. */
//linked_line* move_to_head(linked_line* new_head, linked_line* old_head);
/** MAIN **/
int main(int argc, char* argv[])
{
results score;
@ -105,7 +121,6 @@ int main(int argc, char* argv[]) @@ -105,7 +121,6 @@ int main(int argc, char* argv[])
bad_usage_error();
}
/* End of argument parsing. */
//probably broken: printf("Arguments: s=%hhu, E=%u, b=%hhu, t=%s, v=%hhu, 2^s=%d, 2^b=%u\n", set_index_bits, num_lines, block_bits, filename, VERBOSE, num_sets, block_size);
linked_line** cache = make_cache(num_sets, num_lines);
@ -126,6 +141,7 @@ int main(int argc, char* argv[]) @@ -126,6 +141,7 @@ int main(int argc, char* argv[])
char op = buffer[1];
long address = strtol(buffer+3, &end, 16);
long size = strtol(end+1, NULL, 16);
if (*end != ',' || !(op == 'S' || op == 'L' || op == 'M')) {
printf("Invalid input file, last line:\n%s\n", buffer);
return -1;
@ -151,6 +167,72 @@ int main(int argc, char* argv[]) @@ -151,6 +167,72 @@ int main(int argc, char* argv[])
return 0;
}
/************************** OPTION-PARSING FUNCTIONS **************************/
void print_usage(void)
{
printf("Usage: ./csim [-hv] -s <number> -E <number> -b <number> -t <file>\n"
"Options:\n"
" -h Print this help.\n"
" -v Display trace info.\n"
" -s <number> The number of set index bits. Must be a positive integer.\n"
" -E <number> Associativity (lines per set). Must be a positive integer.\n"
" -b <number> The number of block bits. Must be a positive integer.\n"
" -t <file> Name of the file to read a valgrind trace from.");
}
/**
Prints the summarized usage and exits with status 1.
*/
void bad_usage_error(void)
{
printf("Usage: ./csim [-hv] -s <number> -E <number> -b <number> -t <file>\n");
exit(1);
}
/**
Parses a string into a size_t.
@param arg The string to parse.
@param opt The option being parsed. If 'E', the output can not be 0.
@return The parsed long integer if the argument was an integer.
@note Exits if argument is invalid.
*/
long parse_int_arg(char* arg, char opt)
{
char* end;
long i = strtol(arg, &end, 0);
if (*end != '\0' || (!i && opt=='E')) { // associativity can not be zero
printf("Invalid argument \"%s\" for option -%c\n", arg, opt);
bad_usage_error();
}
else if (i < 0) {
printf("Argument %ld for option %c too small (must be greater than 0)", i, opt);
exit(1);
}
return i;
}
/**
Calculates a size using a given number of bits.
@param bits The power of 2 to use.
@note Exits if number of bits is larger than a size_t.
*/
long bits_to_size(int bits)
{
int max_shift = 8*sizeof(int);
if (bits > max_shift-1) {
printf("Argument %hhd too large (-s and -b must be less than %hhd)", bits, max_shift);
exit(1);
}
return (size_t)1 << bits;
}
/***************************** SUB-MAIN FUNCTIONS *****************************/
void increment_result_counters(char result, results* score)
{
score->hits += (result == 'H');
@ -159,6 +241,9 @@ void increment_result_counters(char result, results* score) @@ -159,6 +241,9 @@ void increment_result_counters(char result, results* score)
}
/********************* CACHE CREATION/DELETION FUNCTIONS *********************/
linked_line** make_cache(long num_sets, long num_lines)
{
linked_line** cache = (linked_line**)calloc(num_sets, sizeof(linked_line*));
@ -209,6 +294,8 @@ void free_set(linked_line* line) { @@ -209,6 +294,8 @@ void free_set(linked_line* line) {
}
}
/************************** CACHE VIEWING FUNCTIONS **************************/
void print_cache(linked_line** cache, long num_sets)
{
int ii;
@ -221,11 +308,17 @@ void print_set(linked_line* line) @@ -221,11 +308,17 @@ void print_set(linked_line* line)
{
if (line != NULL) {
print_set(line->older);
printf("%p %x.%lx %p:%p\n", (void *) line, line->validity, line->tag, (void *) line->newer, (void *) line->older);
printf("%p %x.%lx\t\t%p:%p\n", (void *) line, line->validity,
line->tag, (void *) line->newer, (void *) line->older);
}
}
char cache_access(linked_line** cache, long address, int set_bits, int block_bits, long num_lines)
/*************************** CACHE ACCESS FUNCTIONS ***************************/
char cache_access(linked_line** cache, long address,
int set_bits, int block_bits, long num_lines)
{
char out;
long tag = address >> set_bits >> block_bits;
@ -265,63 +358,4 @@ linked_line* find_line(linked_line* line, long tag) @@ -265,63 +358,4 @@ linked_line* find_line(linked_line* line, long tag)
return find_line(line->older, tag);
}
void print_usage(void)
{
printf("Usage: ./csim [-hv] -s <number> -E <number> -b <number> -t <file>\n"
"Options:\n"
" -h Print this help.\n"
" -v Display trace info.\n"
" -s <number> The number of set index bits. Must be a positive integer.\n"
" -E <number> Associativity (lines per set). Must be a positive integer.\n"
" -b <number> The number of block bits. Must be a positive integer.\n"
" -t <file> Name of the file to read a valgrind trace from.");
}
/**
Prints the summarized usage and exits with status 1.
*/
void bad_usage_error(void)
{
printf("Usage: ./csim [-hv] -s <number> -E <number> -b <number> -t <file>\n");
exit(1);
}
/**
Parses a string into a size_t.
@param arg The string to parse.
@param opt The option being parsed. If 'E', the output can not be 0.
@return The parsed long integer if the argument was an integer.
@note Exits if argument is invalid.
*/
long parse_int_arg(char* arg, char opt)
{
char* end;
long i = strtol(arg, &end, 0);
if (*end != '\0' || (!i && opt=='E')) { // associativity can not be zero
printf("Invalid argument \"%s\" for option -%c\n", arg, opt);
bad_usage_error();
}
else if (i < 0) {
printf("Argument %ld for option %c too small (must be greater than 0)", i, opt);
exit(1);
}
return i;
}
/**
Calculates a size using a given number of bits.
@param bits The power of 2 to use.
@note Exits if number of bits is larger than a size_t.
*/
long bits_to_size(int bits)
{
int max_shift = 8*sizeof(int);
if (bits > max_shift-1) {
printf("Argument %hhd too large (-s and -b must be less than %hhd)", bits, max_shift);
exit(1);
}
return (size_t)1 << bits;
}