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.
This commit is contained in:
parent
927b7bd6a7
commit
189807e834
300
csim.c
300
csim.c
@ -3,25 +3,35 @@
|
|||||||
@file csim.c
|
@file csim.c
|
||||||
@author Adam Goldsmith
|
@author Adam Goldsmith
|
||||||
@author Jacob Komissar
|
@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:
|
Exit statuses:
|
||||||
0 - success
|
0 - success
|
||||||
1 - usage error
|
1 - usage error
|
||||||
-1 - file error
|
-1 - file error
|
||||||
|
*/
|
||||||
|
|
||||||
*/
|
/**** Headers ****/
|
||||||
/* Headers */
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <getopt.h>
|
#include <getopt.h>
|
||||||
#include "cachelab.h"
|
#include "cachelab.h"
|
||||||
|
|
||||||
/* Typedefs and structs */
|
|
||||||
|
/******** Typedefs and structs ********/
|
||||||
typedef unsigned char uint8_t;
|
typedef unsigned char uint8_t;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
int hits;
|
||||||
|
int misses;
|
||||||
|
int evictions;
|
||||||
|
} results;
|
||||||
|
|
||||||
/* Linked Line and related prototypes */
|
/* Linked Line and related prototypes */
|
||||||
typedef struct linked_line_s linked_line;
|
typedef struct linked_line_s linked_line;
|
||||||
struct linked_line_s {
|
struct linked_line_s {
|
||||||
@ -31,35 +41,41 @@ struct linked_line_s {
|
|||||||
linked_line* older;
|
linked_line* older;
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
int hits;
|
|
||||||
int misses;
|
|
||||||
int evictions;
|
|
||||||
} results;
|
|
||||||
|
|
||||||
|
/******** Macros ********/
|
||||||
/* Macros */
|
|
||||||
#define mprintf(...) if (VERBOSE) printf(__VA_ARGS__)
|
#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 print_usage(void);
|
||||||
void bad_usage_error(void);
|
void bad_usage_error(void);
|
||||||
long parse_int_arg(char *arg, char opt);
|
long parse_int_arg(char *arg, char opt);
|
||||||
long bits_to_size(int bits);
|
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_cache(long num_sets, long num_lines);
|
||||||
linked_line* make_set(linked_line* newer, long num_lines);
|
linked_line* make_set(linked_line* newer, long num_lines);
|
||||||
void free_cache(linked_line** cache, long num_sets);
|
void free_cache(linked_line** cache, long num_sets);
|
||||||
void free_set(linked_line* line);
|
void free_set(linked_line* line);
|
||||||
|
/* Cache-viewing functions */
|
||||||
void print_cache(linked_line** cache, long num_sets);
|
void print_cache(linked_line** cache, long num_sets);
|
||||||
void print_set(linked_line* line);
|
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);
|
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);
|
linked_line* find_line(linked_line* set_head, long tag);
|
||||||
void increment_result_counters(char result, results* score);
|
//linked_line* move_to_head(linked_line* new_head, linked_line* old_head);
|
||||||
|
|
||||||
/* Global variables */
|
|
||||||
uint8_t VERBOSE = 0; /**< If nonzero, mprintf will not print. Set in main() if -v flag is given. */
|
|
||||||
|
|
||||||
|
|
||||||
|
/** MAIN **/
|
||||||
int main(int argc, char* argv[])
|
int main(int argc, char* argv[])
|
||||||
{
|
{
|
||||||
results score;
|
results score;
|
||||||
@ -105,7 +121,6 @@ int main(int argc, char* argv[])
|
|||||||
bad_usage_error();
|
bad_usage_error();
|
||||||
}
|
}
|
||||||
/* End of argument parsing. */
|
/* 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);
|
linked_line** cache = make_cache(num_sets, num_lines);
|
||||||
|
|
||||||
@ -126,6 +141,7 @@ int main(int argc, char* argv[])
|
|||||||
char op = buffer[1];
|
char op = buffer[1];
|
||||||
long address = strtol(buffer+3, &end, 16);
|
long address = strtol(buffer+3, &end, 16);
|
||||||
long size = strtol(end+1, NULL, 16);
|
long size = strtol(end+1, NULL, 16);
|
||||||
|
|
||||||
if (*end != ',' || !(op == 'S' || op == 'L' || op == 'M')) {
|
if (*end != ',' || !(op == 'S' || op == 'L' || op == 'M')) {
|
||||||
printf("Invalid input file, last line:\n%s\n", buffer);
|
printf("Invalid input file, last line:\n%s\n", buffer);
|
||||||
return -1;
|
return -1;
|
||||||
@ -151,119 +167,9 @@ int main(int argc, char* argv[])
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void increment_result_counters(char result, results* score)
|
|
||||||
{
|
|
||||||
score->hits += (result == 'H');
|
|
||||||
score->misses += (result == 'M' || result == 'E');
|
|
||||||
score->evictions += (result == 'E');
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
linked_line** make_cache(long num_sets, long num_lines)
|
/************************** OPTION-PARSING FUNCTIONS **************************/
|
||||||
{
|
|
||||||
linked_line** cache = (linked_line**)calloc(num_sets, sizeof(linked_line*));
|
|
||||||
long ii;
|
|
||||||
for (ii = 0; ii < num_sets; ii++) {
|
|
||||||
cache[ii] = make_set(NULL, num_lines);
|
|
||||||
}
|
|
||||||
return cache;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
Creates a set by creating and linking a given number of lines.
|
|
||||||
@param newer A pointer to the previously-created element of the set.
|
|
||||||
@param num_lines The number of lines remaining to be added.
|
|
||||||
@return A pointer to the created line.
|
|
||||||
*/
|
|
||||||
linked_line* make_set(linked_line* newer, long num_lines) {
|
|
||||||
linked_line* current = (linked_line*)calloc(1, sizeof(linked_line));
|
|
||||||
current->newer = newer;
|
|
||||||
num_lines--;
|
|
||||||
if (num_lines) { // If not on the last line
|
|
||||||
current->older = make_set(current, num_lines);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
current->older = NULL;
|
|
||||||
}
|
|
||||||
return current;
|
|
||||||
}
|
|
||||||
|
|
||||||
void free_cache(linked_line** cache, long num_sets)
|
|
||||||
{
|
|
||||||
int ii;
|
|
||||||
for (ii = 0; ii < num_sets; ii++) {
|
|
||||||
free_set(cache[ii]);
|
|
||||||
}
|
|
||||||
free(cache);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
Frees a set by recursing through it, then freeing each element from oldest to newest.
|
|
||||||
@param line A pointer to the next line in the set.
|
|
||||||
@param num_lines The number
|
|
||||||
*/
|
|
||||||
void free_set(linked_line* line) {
|
|
||||||
if (line != NULL) {
|
|
||||||
free_set(line->older);
|
|
||||||
free(line);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void print_cache(linked_line** cache, long num_sets)
|
|
||||||
{
|
|
||||||
int ii;
|
|
||||||
for (ii = 0; ii < num_sets; ii++) {
|
|
||||||
print_set(cache[ii]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
|
||||||
long set = (address >> block_bits) & ((1 << set_bits) - 1);
|
|
||||||
linked_line* set_head = cache[set];
|
|
||||||
linked_line* line = find_line(set_head, tag);
|
|
||||||
|
|
||||||
if (!line->validity) out = 'M';
|
|
||||||
else if (line->tag == tag) out = 'H';
|
|
||||||
else out = 'E';
|
|
||||||
|
|
||||||
if(!(line == set_head)) {
|
|
||||||
line->newer->older = line->older;
|
|
||||||
if(line->older)
|
|
||||||
line->older->newer = line->newer;
|
|
||||||
line->newer = NULL;
|
|
||||||
line->older = set_head;
|
|
||||||
set_head->newer = line;
|
|
||||||
cache[set] = line;
|
|
||||||
}
|
|
||||||
|
|
||||||
line->tag = tag;
|
|
||||||
line->validity = 1;
|
|
||||||
|
|
||||||
return out;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
@returns the first line with a matching tag, the first invalid line,
|
|
||||||
or the first line without an older (i.e. the end of the set
|
|
||||||
*/
|
|
||||||
linked_line* find_line(linked_line* line, long tag)
|
|
||||||
{
|
|
||||||
if(line->tag == tag || !line->validity || !line->older)
|
|
||||||
return line;
|
|
||||||
else
|
|
||||||
return find_line(line->older, tag);
|
|
||||||
}
|
|
||||||
|
|
||||||
void print_usage(void)
|
void print_usage(void)
|
||||||
{
|
{
|
||||||
@ -325,3 +231,131 @@ long bits_to_size(int bits)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/***************************** SUB-MAIN FUNCTIONS *****************************/
|
||||||
|
|
||||||
|
void increment_result_counters(char result, results* score)
|
||||||
|
{
|
||||||
|
score->hits += (result == 'H');
|
||||||
|
score->misses += (result == 'M' || result == 'E');
|
||||||
|
score->evictions += (result == 'E');
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/********************* 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*));
|
||||||
|
long ii;
|
||||||
|
for (ii = 0; ii < num_sets; ii++) {
|
||||||
|
cache[ii] = make_set(NULL, num_lines);
|
||||||
|
}
|
||||||
|
return cache;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Creates a set by creating and linking a given number of lines.
|
||||||
|
@param newer A pointer to the previously-created element of the set.
|
||||||
|
@param num_lines The number of lines remaining to be added.
|
||||||
|
@return A pointer to the created line.
|
||||||
|
*/
|
||||||
|
linked_line* make_set(linked_line* newer, long num_lines) {
|
||||||
|
linked_line* current = (linked_line*)calloc(1, sizeof(linked_line));
|
||||||
|
current->newer = newer;
|
||||||
|
num_lines--;
|
||||||
|
if (num_lines) { // If not on the last line
|
||||||
|
current->older = make_set(current, num_lines);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
current->older = NULL;
|
||||||
|
}
|
||||||
|
return current;
|
||||||
|
}
|
||||||
|
|
||||||
|
void free_cache(linked_line** cache, long num_sets)
|
||||||
|
{
|
||||||
|
int ii;
|
||||||
|
for (ii = 0; ii < num_sets; ii++) {
|
||||||
|
free_set(cache[ii]);
|
||||||
|
}
|
||||||
|
free(cache);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Frees a set by recursing through it, then freeing each element from oldest to newest.
|
||||||
|
@param line A pointer to the next line in the set.
|
||||||
|
@param num_lines The number
|
||||||
|
*/
|
||||||
|
void free_set(linked_line* line) {
|
||||||
|
if (line != NULL) {
|
||||||
|
free_set(line->older);
|
||||||
|
free(line);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/************************** CACHE VIEWING FUNCTIONS **************************/
|
||||||
|
|
||||||
|
void print_cache(linked_line** cache, long num_sets)
|
||||||
|
{
|
||||||
|
int ii;
|
||||||
|
for (ii = 0; ii < num_sets; ii++) {
|
||||||
|
print_set(cache[ii]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void print_set(linked_line* line)
|
||||||
|
{
|
||||||
|
if (line != NULL) {
|
||||||
|
print_set(line->older);
|
||||||
|
printf("%p %x.%lx\t\t%p:%p\n", (void *) line, line->validity,
|
||||||
|
line->tag, (void *) line->newer, (void *) line->older);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*************************** 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;
|
||||||
|
long set = (address >> block_bits) & ((1 << set_bits) - 1);
|
||||||
|
linked_line* set_head = cache[set];
|
||||||
|
linked_line* line = find_line(set_head, tag);
|
||||||
|
|
||||||
|
if (!line->validity) out = 'M';
|
||||||
|
else if (line->tag == tag) out = 'H';
|
||||||
|
else out = 'E';
|
||||||
|
|
||||||
|
if(!(line == set_head)) {
|
||||||
|
line->newer->older = line->older;
|
||||||
|
if(line->older)
|
||||||
|
line->older->newer = line->newer;
|
||||||
|
line->newer = NULL;
|
||||||
|
line->older = set_head;
|
||||||
|
set_head->newer = line;
|
||||||
|
cache[set] = line;
|
||||||
|
}
|
||||||
|
|
||||||
|
line->tag = tag;
|
||||||
|
line->validity = 1;
|
||||||
|
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
@returns the first line with a matching tag, the first invalid line,
|
||||||
|
or the first line without an older (i.e. the end of the set
|
||||||
|
*/
|
||||||
|
linked_line* find_line(linked_line* line, long tag)
|
||||||
|
{
|
||||||
|
if(line->tag == tag || !line->validity || !line->older)
|
||||||
|
return line;
|
||||||
|
else
|
||||||
|
return find_line(line->older, tag);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user