feat: Simulator 可選擇 Replace Policy
This commit is contained in:
parent
00f2460f49
commit
2c8d14323a
61
main.cpp
61
main.cpp
@ -1,5 +1,6 @@
|
||||
#include <iostream>
|
||||
#include <math.h>
|
||||
#include <iomanip>
|
||||
#include "simulator.h"
|
||||
|
||||
int main(){
|
||||
@ -14,38 +15,70 @@ int main(){
|
||||
std::cin >> temp >> temp >> blockSize;
|
||||
|
||||
setSize = cacheSize * 1024 / blockSize / waySize;
|
||||
std::cout << setSize << " sets" << std::endl << std::endl;
|
||||
|
||||
offsetSize = log2(blockSize);
|
||||
indexSize = log2(setSize);
|
||||
tagSize = machineBits - offsetSize - indexSize;
|
||||
|
||||
std::cout << "offset: " << offsetSize << std::endl;
|
||||
std::cout << "index : " << indexSize << std::endl;
|
||||
std::cout << "tag : " << tagSize << std::endl;
|
||||
std::cout << "num_set: " << setSize << std::endl;
|
||||
std::cout << "num_block_offset_bit: " << offsetSize << std::endl;
|
||||
std::cout << "num_index_bit: " << indexSize << std::endl;
|
||||
std::cout << "num_tag_bit: " << tagSize << std::endl << std::endl;
|
||||
|
||||
CacheSim::Simulator sim(machineBits, setSize, waySize, tagSize);
|
||||
CacheSim::Simulator FIFOSim(machineBits, setSize, waySize, tagSize, 0);
|
||||
CacheSim::Simulator LRUSim(machineBits, setSize, waySize, tagSize, 1);
|
||||
|
||||
std::string action, address;
|
||||
bool status;
|
||||
std::vector<std::pair<std::string, bool>> result;
|
||||
std::vector<std::pair<std::string, bool>> LRUResult, FIFOResult;
|
||||
while(std::cin >> action >> address){
|
||||
std::cout << "action: "<< action << " address: " << address << std::endl;
|
||||
status = sim.read(address);
|
||||
result.push_back(std::make_pair(address, status));
|
||||
std::cout << std::endl << std::endl;
|
||||
// std::cout << "action: "<< action << " address: " << address << std::endl;
|
||||
status = FIFOSim.read(address);
|
||||
FIFOResult.push_back(std::make_pair(address, status));
|
||||
|
||||
status = LRUSim.read(address);
|
||||
LRUResult.push_back(std::make_pair(address, status));
|
||||
// std::cout << std::endl << std::endl;
|
||||
}
|
||||
// FIFO
|
||||
|
||||
int allCount = 0, hitCount = 0;
|
||||
for(int i=0; i<result.size(); i++){
|
||||
std::cout << result[i].first << "... ";
|
||||
|
||||
std::cout << "FIFO Replace Policy:" << std::endl;
|
||||
for(int i=0; i<FIFOResult.size(); i++){
|
||||
std::cout << " " << std::setw(6) << std::setfill('0') << FIFOResult[i].first << "... ";
|
||||
allCount++;
|
||||
if(result[i].second){
|
||||
if(FIFOResult[i].second){
|
||||
hitCount++;
|
||||
std::cout << "HIT" << std::endl;
|
||||
}else{
|
||||
std::cout << "MISS" << std::endl;
|
||||
}
|
||||
}
|
||||
std::cout << "Hit rate: " << (double)hitCount/allCount*100 << "%" << std::endl;
|
||||
std::cout << std::endl;
|
||||
std::cout << " Hit rate: " << (double)hitCount/allCount*100 << "%" << std::endl;
|
||||
std::cout << " Miss rate: " << 100 - (double)hitCount/allCount*100 << "%" << std::endl << std::endl;
|
||||
|
||||
|
||||
|
||||
// LRU
|
||||
|
||||
allCount = 0, hitCount = 0;
|
||||
std::cout << "LRU Replace Policy:" << std::endl;
|
||||
for(int i=0; i<LRUResult.size(); i++){
|
||||
std::cout << " " << LRUResult[i].first << "... ";
|
||||
allCount++;
|
||||
if(LRUResult[i].second){
|
||||
hitCount++;
|
||||
std::cout << "HIT" << std::endl;
|
||||
}else{
|
||||
std::cout << "MISS" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
std::cout << "num_total_access: " << setSize << std::endl;
|
||||
|
||||
std::cout << std::endl;
|
||||
std::cout << " Hit rate: " << (double)hitCount/allCount*100 << "%" << std::endl;
|
||||
std::cout << " Miss rate: " << 100 - (double)hitCount/allCount*100 << "%" << std::endl;
|
||||
}
|
||||
|
||||
63
set.cpp
63
set.cpp
@ -8,9 +8,10 @@ using CacheSim::Set;
|
||||
using std::cout;
|
||||
using std::endl;
|
||||
|
||||
Set::Set(short waySize, short tagSize){
|
||||
Set::Set(short waySize, short tagSize, short replacePolicy){
|
||||
this->waySize = waySize;
|
||||
this->emptySize = waySize;
|
||||
this->replacePolicy = replacePolicy;
|
||||
|
||||
for(int i=0; i<waySize; i++){
|
||||
this->blocks.push_back(Block(tagSize));
|
||||
@ -22,10 +23,12 @@ bool Set::read(vector<bool> tag, short offset){
|
||||
short baseAccessTime; // 被替換掉的 block ,它的 access time
|
||||
for(int i=0; i<this->waySize; i++){
|
||||
if(this->blocks[i].getValid() && this->blocks[i].getTag() == tag){ // valid bit == true 且 tag 相同
|
||||
baseAccessTime= this->blocks[i].getAccessTime();
|
||||
this->blocks[i].access();
|
||||
this->countAccess(baseAccessTime); // 更新計算所有 way access 的時間點
|
||||
cout << "HIT: 在 " << i << " way 找到!" << endl;
|
||||
if(this->replacePolicy == 1){
|
||||
baseAccessTime= this->blocks[i].getAccessTime();
|
||||
this->blocks[i].access();
|
||||
this->countAccess(baseAccessTime); // 更新計算所有 way access 的時間點
|
||||
// cout << "HIT: 在 " << i << " way 找到!" << endl;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@ -34,7 +37,7 @@ bool Set::read(vector<bool> tag, short offset){
|
||||
if(this->emptySize > 0){
|
||||
for(int i=0; i<this->waySize; i++){
|
||||
if(this->blocks[i].getValid() == false){
|
||||
cout << "MISS: 在 " << i <<" way 寫入資料" << endl;
|
||||
// cout << "MISS: 在 " << i <<" way 寫入資料" << endl;
|
||||
this->blocks[i].update(tag); // 更新 tag
|
||||
this->blocks[i].access();
|
||||
baseAccessTime = this->waySize; // 每個都要更新
|
||||
@ -56,18 +59,18 @@ bool Set::read(vector<bool> tag, short offset){
|
||||
}
|
||||
|
||||
// update
|
||||
cout << "MISS: way 滿了,找到 " << maxIndex << " 在 " << maxTime << " 前更新" << endl;
|
||||
// cout << "MISS: way 滿了,找到 " << maxIndex << " 在 " << maxTime << " 前更新" << endl;
|
||||
baseAccessTime = this->blocks[maxIndex].getAccessTime();
|
||||
this->blocks[maxIndex].update(tag);
|
||||
this->blocks[maxIndex].access();
|
||||
|
||||
for(int i=0; i<this->waySize; i++){
|
||||
vector<bool> t = this->blocks[i].getTag();
|
||||
for(int j=0; j<t.size(); j++){
|
||||
cout << t[j];
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
// for(int i=0; i<this->waySize; i++){
|
||||
// vector<bool> t = this->blocks[i].getTag();
|
||||
// for(int j=0; j<t.size(); j++){
|
||||
// cout << t[j];
|
||||
// }
|
||||
// cout << endl;
|
||||
// }
|
||||
}
|
||||
|
||||
this->countAccess(baseAccessTime);
|
||||
@ -80,22 +83,22 @@ void Set::countAccess(short baseAccessTime){
|
||||
this->blocks[i].countAccess();
|
||||
}
|
||||
}
|
||||
for(int i=0; i<20; i++){
|
||||
cout << "=" ;
|
||||
}
|
||||
cout << "way 狀態";
|
||||
for(int i=0; i<20; i++){
|
||||
cout << "=";
|
||||
}
|
||||
cout<<endl;
|
||||
// for(int i=0; i<20; i++){
|
||||
// cout << "=" ;
|
||||
// }
|
||||
// cout << "way 狀態";
|
||||
// for(int i=0; i<20; i++){
|
||||
// cout << "=";
|
||||
// }
|
||||
// cout<<endl;
|
||||
|
||||
for(int i=0; i<this->waySize; i++){
|
||||
cout << this->blocks[i].getAccessTime() << " ";
|
||||
}
|
||||
cout<<endl;
|
||||
// for(int i=0; i<this->waySize; i++){
|
||||
// cout << this->blocks[i].getAccessTime() << " ";
|
||||
// }
|
||||
// cout<<endl;
|
||||
|
||||
for(int i=0; i<45; i++){
|
||||
cout << "=";
|
||||
}
|
||||
cout<<endl;
|
||||
// for(int i=0; i<45; i++){
|
||||
// cout << "=";
|
||||
// }
|
||||
// cout<<endl;
|
||||
}
|
||||
3
set.h
3
set.h
@ -10,10 +10,11 @@ namespace CacheSim{
|
||||
private:
|
||||
short waySize;
|
||||
short emptySize;
|
||||
short replacePolicy;
|
||||
vector<Block> blocks;
|
||||
|
||||
public:
|
||||
Set(short waySize, short tagSize);
|
||||
Set(short waySize, short tagSize, short replacePolicy);
|
||||
bool read(vector<bool> tag, short offset);
|
||||
void countAccess(short baseAccessTime);
|
||||
|
||||
|
||||
@ -5,25 +5,26 @@ using CacheSim::Simulator;
|
||||
using std::stringstream;
|
||||
using std::bitset;
|
||||
|
||||
Simulator::Simulator(int machineBits, short setSize, short waySize, short tagSize){
|
||||
Simulator::Simulator(int machineBits, short setSize, short waySize, short tagSize, short replacePolicy){
|
||||
this->setSize = setSize;
|
||||
this->tagSize = tagSize;
|
||||
this->machineBits = machineBits;
|
||||
this->replacePolicy = replacePolicy;
|
||||
for(int i=0; i<setSize; i++){
|
||||
this->sets.push_back(Set(waySize, tagSize));
|
||||
this->sets.push_back(Set(waySize, tagSize, replacePolicy));
|
||||
}
|
||||
}
|
||||
|
||||
bool Simulator::read(string address){
|
||||
vector<bool> addrBits = this->addressTranslate(address);
|
||||
for(int i=0; i<addrBits.size(); i++){
|
||||
std::cout << addrBits[i] << " ";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
// for(int i=0; i<addrBits.size(); i++){
|
||||
// std::cout << addrBits[i] << " ";
|
||||
// }
|
||||
// std::cout << std::endl;
|
||||
short index = this->getValue(addrBits, "index");
|
||||
short offset = this->getValue(addrBits, "offset");
|
||||
vector<bool> tag(addrBits.begin(), addrBits.begin()+this->tagSize);
|
||||
std::cout<<"更新 index: "<<index<<std::endl;
|
||||
// std::cout<<"更新 index: "<<index<<std::endl;
|
||||
return this->sets[index].read(tag, offset);
|
||||
}
|
||||
|
||||
|
||||
@ -10,12 +10,13 @@ namespace CacheSim{
|
||||
class Simulator
|
||||
{
|
||||
private:
|
||||
short replacePolicy;
|
||||
short setSize;
|
||||
short tagSize;
|
||||
int machineBits;
|
||||
vector<Set> sets;
|
||||
public:
|
||||
Simulator(int machineBits, short setSize, short waySize, short tagSize);
|
||||
Simulator(int machineBits, short setSize, short waySize, short tagSize, short replacePolicy);
|
||||
bool read(string address);
|
||||
bool write(string address);
|
||||
vector<bool> addressTranslate(string address);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user