📄 bocs.c
字号:
/*
* File: bocs.c
* Author: Ben Juurlink
* Mnenomic: "Ben's Own Cache Simulator"
* History:
* March 2001:
* Original version
* October 2002:
* Revised for the course "System Programming in C"
*/
#include <stdio.h>
#include <stdlib.h>
#include "getopt.h"
#include "config.h"
#include "cache.h"
#include "misc.h"
#include "victim.h"
void
print_usage(void) /*add the victim option*/
{
printf("Usage:\n");
printf(" bocs [-C=<nsets>:<assoc>:<block_size>:<repl>] \n \
[-V=<nsets>:<assoc>:<block_size>:<repl>] <file name> \n");
printf(" where\n");
printf(" -C denotes a conventional cache\n");
printf(" -V denotes a victim cache\n");
printf(" and\n");
printf(" nsets: number of sets\n");
printf(" assoc: associativity\n");
printf(" block_size: the block (cache line) size\n");
printf(" repl: replacement algorithm\n");
printf(" l: LRU\n");
printf(" r: random\n");
printf(" f: FIFO\n");
printf("\n");
printf(" <file name> is the name of the file that contains the address trace\n");
}
int
main(int argc, char *argv[])
{
cache_t *primary_cache = NULL,*victim_cache = NULL;
//victim_cache_t *victim_cache = NULL;
int nsets, assoc, blk_size;
char policy;
int ninputs;
int opt;
char *name;
int primary_cache_defined;
void (*sim_main)() = NULL;
FILE *input = NULL;
if (argc==1)
{
print_usage();
exit(0);
}
primary_cache_defined = 0;
while (((opt = getopt(argc, argv, "C:V:")) != -1))
{
switch (opt)
{
case 'C': // Conventional cache
name = "Conventional";
ninputs=sscanf(optarg, "=%d:%d:%d:%c",
&nsets, &assoc, &blk_size, &policy);
if (ninputs==4)
{
primary_cache = cache_create(name, nsets, assoc, blk_size,
cache_char2policy(policy));
primary_cache_defined = 1;
}
else
{
print_usage();
exit(-1);
}
break;
case 'V': /*add a option for victim cache*/
if(primary_cache_defined == 0) /*check if the primary cache is defined*/
{
printf("Error: There must have a primary cache before a victim \
cache is defined. \n");
print_usage();
exit(-1);
}
else
{
name = "Victim";
ninputs=sscanf(optarg, "=%d:%d:%d:%c",
&nsets, &assoc, &blk_size, &policy);
if (ninputs==4)
{
victim_cache = victim_create(name, nsets, assoc, blk_size,
cache_char2policy(policy));
primary_cache->victim = victim_cache;
}
else
{
print_usage();
exit(-1);
}
}
break; /*add a option for victim cache*/
default:
print_usage();
exit(-1);
}
}
input = fopen(argv[argc-1], "rb");
if(primary_cache->victim == NULL) /*if there is no victim cache created*/
{
sim_main = cache_sim;
(*sim_main)(primary_cache, input);
}
else
{
sim_main = victim_sim;
(*sim_main)(primary_cache,victim_cache, input);
}
fclose(input);
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -