feat: LRU Replace Policy
This commit is contained in:
parent
a1b1d292d4
commit
00f2460f49
9
Makefile
Normal file
9
Makefile
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
CC = g++
|
||||||
|
|
||||||
|
all: sim
|
||||||
|
|
||||||
|
sim: main.cpp block.cpp set.cpp simulator.cpp
|
||||||
|
$(CC) main.cpp block.cpp set.cpp simulator.cpp -o sim
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm sim
|
||||||
19
main.cpp
19
main.cpp
@ -27,10 +27,25 @@ int main(){
|
|||||||
CacheSim::Simulator sim(machineBits, setSize, waySize, tagSize);
|
CacheSim::Simulator sim(machineBits, setSize, waySize, tagSize);
|
||||||
|
|
||||||
std::string action, address;
|
std::string action, address;
|
||||||
|
bool status;
|
||||||
|
std::vector<std::pair<std::string, bool>> result;
|
||||||
while(std::cin >> action >> address){
|
while(std::cin >> action >> address){
|
||||||
std::cout << "action: "<< action << " address: " << address << std::endl;
|
std::cout << "action: "<< action << " address: " << address << std::endl;
|
||||||
sim.read(address);
|
status = sim.read(address);
|
||||||
|
result.push_back(std::make_pair(address, status));
|
||||||
std::cout << std::endl << std::endl;
|
std::cout << std::endl << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int allCount = 0, hitCount = 0;
|
||||||
|
for(int i=0; i<result.size(); i++){
|
||||||
|
std::cout << result[i].first << "... ";
|
||||||
|
allCount++;
|
||||||
|
if(result[i].second){
|
||||||
|
hitCount++;
|
||||||
|
std::cout << "HIT" << std::endl;
|
||||||
|
}else{
|
||||||
|
std::cout << "MISS" << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
std::cout << "Hit rate: " << (double)hitCount/allCount*100 << "%" << std::endl;
|
||||||
}
|
}
|
||||||
|
|||||||
26
set.cpp
26
set.cpp
@ -19,10 +19,12 @@ Set::Set(short waySize, short tagSize){
|
|||||||
|
|
||||||
bool Set::read(vector<bool> tag, short offset){
|
bool Set::read(vector<bool> tag, short offset){
|
||||||
// HIT 的情況
|
// HIT 的情況
|
||||||
|
short baseAccessTime; // 被替換掉的 block ,它的 access time
|
||||||
for(int i=0; i<this->waySize; i++){
|
for(int i=0; i<this->waySize; i++){
|
||||||
if(this->blocks[i].getValid() && this->blocks[i].getTag() == tag){ // valid bit == true 且 tag 相同
|
if(this->blocks[i].getValid() && this->blocks[i].getTag() == tag){ // valid bit == true 且 tag 相同
|
||||||
this->blocks[i].access(); // 重設 access 的時間點
|
baseAccessTime= this->blocks[i].getAccessTime();
|
||||||
this->countAccess(); // 更新計算所有 way access 的時間點
|
this->blocks[i].access();
|
||||||
|
this->countAccess(baseAccessTime); // 更新計算所有 way access 的時間點
|
||||||
cout << "HIT: 在 " << i << " way 找到!" << endl;
|
cout << "HIT: 在 " << i << " way 找到!" << endl;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -35,6 +37,7 @@ bool Set::read(vector<bool> tag, short offset){
|
|||||||
cout << "MISS: 在 " << i <<" way 寫入資料" << endl;
|
cout << "MISS: 在 " << i <<" way 寫入資料" << endl;
|
||||||
this->blocks[i].update(tag); // 更新 tag
|
this->blocks[i].update(tag); // 更新 tag
|
||||||
this->blocks[i].access();
|
this->blocks[i].access();
|
||||||
|
baseAccessTime = this->waySize; // 每個都要更新
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -43,7 +46,7 @@ bool Set::read(vector<bool> tag, short offset){
|
|||||||
short maxIndex = -1, maxTime = -1;
|
short maxIndex = -1, maxTime = -1;
|
||||||
short tempTime;
|
short tempTime;
|
||||||
|
|
||||||
// 找最久沒被 access 的 index
|
// 找最久沒被 access 的 block index
|
||||||
for(int i=0; i<this->waySize; i++){
|
for(int i=0; i<this->waySize; i++){
|
||||||
tempTime = this->blocks[i].getAccessTime();
|
tempTime = this->blocks[i].getAccessTime();
|
||||||
if(tempTime > maxTime){
|
if(tempTime > maxTime){
|
||||||
@ -54,17 +57,28 @@ bool Set::read(vector<bool> tag, short offset){
|
|||||||
|
|
||||||
// update
|
// 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].update(tag);
|
||||||
this->blocks[maxIndex].access();
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
this->countAccess();
|
this->countAccess(baseAccessTime);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Set::countAccess(){
|
void Set::countAccess(short baseAccessTime){
|
||||||
for(int i=0; i<this->waySize; i++){
|
for(int i=0; i<this->waySize; i++){
|
||||||
this->blocks[i].countAccess();
|
if(this->blocks[i].getAccessTime() < baseAccessTime){ // 小於 basetime 就 ++,補足 basetime 缺的 hole
|
||||||
|
this->blocks[i].countAccess();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
for(int i=0; i<20; i++){
|
for(int i=0; i<20; i++){
|
||||||
cout << "=" ;
|
cout << "=" ;
|
||||||
|
|||||||
2
set.h
2
set.h
@ -15,7 +15,7 @@ namespace CacheSim{
|
|||||||
public:
|
public:
|
||||||
Set(short waySize, short tagSize);
|
Set(short waySize, short tagSize);
|
||||||
bool read(vector<bool> tag, short offset);
|
bool read(vector<bool> tag, short offset);
|
||||||
void countAccess();
|
void countAccess(short baseAccessTime);
|
||||||
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -22,11 +22,9 @@ bool Simulator::read(string address){
|
|||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
short index = this->getValue(addrBits, "index");
|
short index = this->getValue(addrBits, "index");
|
||||||
short offset = this->getValue(addrBits, "offset");
|
short offset = this->getValue(addrBits, "offset");
|
||||||
vector<bool> tag;
|
vector<bool> tag(addrBits.begin(), addrBits.begin()+this->tagSize);
|
||||||
tag.assign(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);
|
return this->sets[index].read(tag, offset);
|
||||||
// return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Simulator::write(string address){
|
bool Simulator::write(string address){
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user