-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
85 lines (73 loc) · 2.61 KB
/
main.cpp
File metadata and controls
85 lines (73 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// main.cpp
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include "Cache.h"
#include "ReplacementPolicyTypes.h"
int main(int argc, char* argv[]) {
if(argc < 5) {
std::cerr << "Usage: " << argv[0] << " <cache_size> <block_size> <associativity> <replacement_policy> <trace_file>\n";
std::cerr << "Replacement policies: LRU, FIFO, Random\n";
return 1;
}
// Parse command-line arguments
size_t cacheSize = std::stoul(argv[1]); // in bytes
size_t blockSize = std::stoul(argv[2]); // in bytes
int associativity = std::stoi(argv[3]);
std::string policyStr = argv[4];
std::string traceFile = argv[5];
ReplacementPolicyType policyType;
if(policyStr == "LRU") {
policyType = ReplacementPolicyType::LRU;
}
else if(policyStr == "FIFO") {
policyType = ReplacementPolicyType::FIFO;
}
else if(policyStr == "Random") {
policyType = ReplacementPolicyType::Random;
}
else {
std::cerr << "Unknown replacement policy: " << policyStr << "\n";
return 1;
}
// Configure cache
CacheConfig config;
config.cacheSize = cacheSize;
config.blockSize = blockSize;
config.associativity = associativity;
config.policyType = policyType;
Cache cache(config);
// Open trace file
std::ifstream infile(traceFile);
if(!infile.is_open()) {
std::cerr << "Failed to open trace file: " << traceFile << "\n";
return 1;
}
std::string line;
size_t totalAccesses = 0;
size_t hitCount = 0;
size_t missCount = 0;
while(std::getline(infile, line)) {
if(line.empty()) continue;
std::stringstream ss(line);
std::string addrStr;
ss >> addrStr;
// Assuming the address is in hexadecimal
uint64_t address = std::stoull(addrStr, nullptr, 16);
bool hit = cache.access(address);
totalAccesses++;
}
infile.close();
// Output results
std::cout << "Cache Configuration:\n";
std::cout << "Cache Size: " << cacheSize << " bytes\n";
std::cout << "Block Size: " << blockSize << " bytes\n";
std::cout << "Associativity: " << associativity << "\n";
std::cout << "Replacement Policy: " << policyStr << "\n\n";
std::cout << "Simulation Results:\n";
std::cout << "Total Accesses: " << (cache.getHits() + cache.getMisses()) << "\n";
std::cout << "Hits: " << cache.getHits() << " (" << cache.getHitRate() << "%)\n";
std::cout << "Misses: " << cache.getMisses() << " (" << cache.getMissRate() << "%)\n";
return 0;
}