aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorov-4 <hausemasterstream@gmail.com>2026-05-25 00:17:02 +0000
committerov-4 <hausemasterstream@gmail.com>2026-05-25 00:17:02 +0000
commitc4b8e7e6d732ff8eebf7106babc78d73632d54ee (patch)
treea3b2fb390aeef1459912a8228f3d144766e30079
parente4540230ef3f6466b40cb44d679ba3c640c88c54 (diff)
implemented -h and -v arg
-rw-r--r--cachelab-handout/csim.cc40
1 files changed, 34 insertions, 6 deletions
diff --git a/cachelab-handout/csim.cc b/cachelab-handout/csim.cc
index 3d65edd..ebce7e6 100644
--- a/cachelab-handout/csim.cc
+++ b/cachelab-handout/csim.cc
@@ -18,9 +18,10 @@ extern "C" {
using namespace std;
long long s, E, b,
- hitCnt, missCnt, evaCnt;
+ hitCnt, missCnt, eviCnt;
size_t timeCnt;
string traceFile;
+bool DEBUG = false;
class ov4Cache
{
@@ -32,7 +33,7 @@ public:
vector<vector<ov4Cache>> cache;
-void findFreeSlot(const size_t Tag, const size_t Set) // update evaCnt
+void findFreeSlot(const size_t Tag, const size_t Set) // update eviCnt
{
// if we can find free slot!
auto findFree = find_if(cache[Set].begin(), cache[Set].end(),
@@ -49,7 +50,7 @@ void findFreeSlot(const size_t Tag, const size_t Set) // update evaCnt
}
// no slot is free, and we have to find the oldest
- evaCnt++;
+ eviCnt++;
auto findOld = min_element(cache[Set].begin(), cache[Set].end(),
[](const ov4Cache &x, const ov4Cache &y)
{
@@ -58,6 +59,7 @@ void findFreeSlot(const size_t Tag, const size_t Set) // update evaCnt
findOld->valid = true;
findOld->time = timeCnt;
findOld->tag = Tag;
+ if (DEBUG) cout << "eviction ";
}
void solve(const size_t addr) // update hitCnt, missCnt
@@ -77,10 +79,14 @@ void solve(const size_t addr) // update hitCnt, missCnt
{
hitCnt++;
res->time = timeCnt; // update the time, because we use it again
+
+ if (DEBUG) cout << "hit ";
+
return;
}
missCnt++;
+ if (DEBUG) cout << "miss ";
findFreeSlot(Tag, Set);
}
@@ -91,7 +97,7 @@ int main(int argc, char *argv[])
{
// get arguments
int opt = '?';
- while ((opt = getopt(argc, argv, "s:E:b:t:")) != -1)
+ while ((opt = getopt(argc, argv, "s:E:b:t:vh")) != -1)
{
switch (opt)
{
@@ -107,6 +113,25 @@ int main(int argc, char *argv[])
case 't':
traceFile = optarg;
break;
+ case 'v':
+ DEBUG = true;
+ break;
+ case 'h':
+ cout << "\
+Usage: ./csim [-hv] -s <num> -E <num> -b <num> -t <file>\n\
+Options:\n\
+ -h Print this help message.\n\
+ -v Optional verbose flag.\n\
+ -s <num> Number of set index bits.\n\
+ -E <num> Number of lines per set.\n\
+ -b <num> Number of block offset bits.\n\
+ -t <file> Trace file.\n\
+\n\
+Examples:\n\
+ linux> ./csim -s 4 -E 1 -b 4 -t traces/yi.trace\n\
+ linux> ./csim -v -s 8 -E 2 -b 4 -t traces/yi.trace" << endl;
+ return 0;
+ break;
case '?':
cerr << "Unknow arg" << endl;
break;
@@ -126,7 +151,7 @@ int main(int argc, char *argv[])
// handle every line
// NB. `L` `S` are acutally same operations in terms of counting
- // for `M`, it can be treated as `L` followed by `S`, and subsequently twice `solve()`
+ // for `M`, it can be treated as `L` followed by `S`, and consequently twice `solve()`
string line;
while (getline(file, line))
{
@@ -136,12 +161,15 @@ int main(int argc, char *argv[])
int useless;
sscanf(line.c_str(), " %c %zx,%d", &op, &addr, &useless);
+ if (DEBUG) cout << op << " " << hex << addr << "," << useless << " ";
+
if (op == 'S' || op == 'L') solve(addr);
else {solve(addr); solve(addr); }
+ cout << endl;
}
- printSummary(hitCnt, missCnt, evaCnt);
+ printSummary(hitCnt, missCnt, eviCnt);
file.close();
return 0;