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 <iostream>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
#include <iomanip>
|
||||||
#include "simulator.h"
|
#include "simulator.h"
|
||||||
|
|
||||||
int main(){
|
int main(){
|
||||||
@ -14,38 +15,70 @@ int main(){
|
|||||||
std::cin >> temp >> temp >> blockSize;
|
std::cin >> temp >> temp >> blockSize;
|
||||||
|
|
||||||
setSize = cacheSize * 1024 / blockSize / waySize;
|
setSize = cacheSize * 1024 / blockSize / waySize;
|
||||||
std::cout << setSize << " sets" << std::endl << std::endl;
|
|
||||||
|
|
||||||
offsetSize = log2(blockSize);
|
offsetSize = log2(blockSize);
|
||||||
indexSize = log2(setSize);
|
indexSize = log2(setSize);
|
||||||
tagSize = machineBits - offsetSize - indexSize;
|
tagSize = machineBits - offsetSize - indexSize;
|
||||||
|
|
||||||
std::cout << "offset: " << offsetSize << std::endl;
|
std::cout << "num_set: " << setSize << std::endl;
|
||||||
std::cout << "index : " << indexSize << std::endl;
|
std::cout << "num_block_offset_bit: " << offsetSize << std::endl;
|
||||||
std::cout << "tag : " << tagSize << 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;
|
std::string action, address;
|
||||||
bool status;
|
bool status;
|
||||||
std::vector<std::pair<std::string, bool>> result;
|
std::vector<std::pair<std::string, bool>> LRUResult, FIFOResult;
|
||||||
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;
|
||||||
status = sim.read(address);
|
status = FIFOSim.read(address);
|
||||||
result.push_back(std::make_pair(address, status));
|
FIFOResult.push_back(std::make_pair(address, status));
|
||||||
std::cout << std::endl << std::endl;
|
|
||||||
|
status = LRUSim.read(address);
|
||||||
|
LRUResult.push_back(std::make_pair(address, status));
|
||||||
|
// std::cout << std::endl << std::endl;
|
||||||
}
|
}
|
||||||
|
// FIFO
|
||||||
|
|
||||||
int allCount = 0, hitCount = 0;
|
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++;
|
allCount++;
|
||||||
if(result[i].second){
|
if(FIFOResult[i].second){
|
||||||
hitCount++;
|
hitCount++;
|
||||||
std::cout << "HIT" << std::endl;
|
std::cout << "HIT" << std::endl;
|
||||||
}else{
|
}else{
|
||||||
std::cout << "MISS" << std::endl;
|
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::cout;
|
||||||
using std::endl;
|
using std::endl;
|
||||||
|
|
||||||
Set::Set(short waySize, short tagSize){
|
Set::Set(short waySize, short tagSize, short replacePolicy){
|
||||||
this->waySize = waySize;
|
this->waySize = waySize;
|
||||||
this->emptySize = waySize;
|
this->emptySize = waySize;
|
||||||
|
this->replacePolicy = replacePolicy;
|
||||||
|
|
||||||
for(int i=0; i<waySize; i++){
|
for(int i=0; i<waySize; i++){
|
||||||
this->blocks.push_back(Block(tagSize));
|
this->blocks.push_back(Block(tagSize));
|
||||||
@ -22,10 +23,12 @@ bool Set::read(vector<bool> tag, short offset){
|
|||||||
short baseAccessTime; // 被替換掉的 block ,它的 access time
|
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 相同
|
||||||
baseAccessTime= this->blocks[i].getAccessTime();
|
if(this->replacePolicy == 1){
|
||||||
this->blocks[i].access();
|
baseAccessTime= this->blocks[i].getAccessTime();
|
||||||
this->countAccess(baseAccessTime); // 更新計算所有 way access 的時間點
|
this->blocks[i].access();
|
||||||
cout << "HIT: 在 " << i << " way 找到!" << endl;
|
this->countAccess(baseAccessTime); // 更新計算所有 way access 的時間點
|
||||||
|
// cout << "HIT: 在 " << i << " way 找到!" << endl;
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -34,7 +37,7 @@ bool Set::read(vector<bool> tag, short offset){
|
|||||||
if(this->emptySize > 0){
|
if(this->emptySize > 0){
|
||||||
for(int i=0; i<this->waySize; i++){
|
for(int i=0; i<this->waySize; i++){
|
||||||
if(this->blocks[i].getValid() == false){
|
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].update(tag); // 更新 tag
|
||||||
this->blocks[i].access();
|
this->blocks[i].access();
|
||||||
baseAccessTime = this->waySize; // 每個都要更新
|
baseAccessTime = this->waySize; // 每個都要更新
|
||||||
@ -56,18 +59,18 @@ 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();
|
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++){
|
// for(int i=0; i<this->waySize; i++){
|
||||||
vector<bool> t = this->blocks[i].getTag();
|
// vector<bool> t = this->blocks[i].getTag();
|
||||||
for(int j=0; j<t.size(); j++){
|
// for(int j=0; j<t.size(); j++){
|
||||||
cout << t[j];
|
// cout << t[j];
|
||||||
}
|
// }
|
||||||
cout << endl;
|
// cout << endl;
|
||||||
}
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
this->countAccess(baseAccessTime);
|
this->countAccess(baseAccessTime);
|
||||||
@ -80,22 +83,22 @@ void Set::countAccess(short baseAccessTime){
|
|||||||
this->blocks[i].countAccess();
|
this->blocks[i].countAccess();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for(int i=0; i<20; i++){
|
// for(int i=0; i<20; i++){
|
||||||
cout << "=" ;
|
// cout << "=" ;
|
||||||
}
|
// }
|
||||||
cout << "way 狀態";
|
// cout << "way 狀態";
|
||||||
for(int i=0; i<20; i++){
|
// for(int i=0; i<20; i++){
|
||||||
cout << "=";
|
// cout << "=";
|
||||||
}
|
// }
|
||||||
cout<<endl;
|
// cout<<endl;
|
||||||
|
|
||||||
for(int i=0; i<this->waySize; i++){
|
// for(int i=0; i<this->waySize; i++){
|
||||||
cout << this->blocks[i].getAccessTime() << " ";
|
// cout << this->blocks[i].getAccessTime() << " ";
|
||||||
}
|
// }
|
||||||
cout<<endl;
|
// cout<<endl;
|
||||||
|
|
||||||
for(int i=0; i<45; i++){
|
// for(int i=0; i<45; i++){
|
||||||
cout << "=";
|
// cout << "=";
|
||||||
}
|
// }
|
||||||
cout<<endl;
|
// cout<<endl;
|
||||||
}
|
}
|
||||||
3
set.h
3
set.h
@ -10,10 +10,11 @@ namespace CacheSim{
|
|||||||
private:
|
private:
|
||||||
short waySize;
|
short waySize;
|
||||||
short emptySize;
|
short emptySize;
|
||||||
|
short replacePolicy;
|
||||||
vector<Block> blocks;
|
vector<Block> blocks;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Set(short waySize, short tagSize);
|
Set(short waySize, short tagSize, short replacePolicy);
|
||||||
bool read(vector<bool> tag, short offset);
|
bool read(vector<bool> tag, short offset);
|
||||||
void countAccess(short baseAccessTime);
|
void countAccess(short baseAccessTime);
|
||||||
|
|
||||||
|
|||||||
@ -5,25 +5,26 @@ using CacheSim::Simulator;
|
|||||||
using std::stringstream;
|
using std::stringstream;
|
||||||
using std::bitset;
|
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->setSize = setSize;
|
||||||
this->tagSize = tagSize;
|
this->tagSize = tagSize;
|
||||||
this->machineBits = machineBits;
|
this->machineBits = machineBits;
|
||||||
|
this->replacePolicy = replacePolicy;
|
||||||
for(int i=0; i<setSize; i++){
|
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){
|
bool Simulator::read(string address){
|
||||||
vector<bool> addrBits = this->addressTranslate(address);
|
vector<bool> addrBits = this->addressTranslate(address);
|
||||||
for(int i=0; i<addrBits.size(); i++){
|
// for(int i=0; i<addrBits.size(); i++){
|
||||||
std::cout << addrBits[i] << " ";
|
// std::cout << addrBits[i] << " ";
|
||||||
}
|
// }
|
||||||
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(addrBits.begin(), addrBits.begin()+this->tagSize);
|
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);
|
return this->sets[index].read(tag, offset);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -10,12 +10,13 @@ namespace CacheSim{
|
|||||||
class Simulator
|
class Simulator
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
|
short replacePolicy;
|
||||||
short setSize;
|
short setSize;
|
||||||
short tagSize;
|
short tagSize;
|
||||||
int machineBits;
|
int machineBits;
|
||||||
vector<Set> sets;
|
vector<Set> sets;
|
||||||
public:
|
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 read(string address);
|
||||||
bool write(string address);
|
bool write(string address);
|
||||||
vector<bool> addressTranslate(string address);
|
vector<bool> addressTranslate(string address);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user