for demo
This commit is contained in:
parent
2c8d14323a
commit
8da8d0e845
2
Makefile
2
Makefile
@ -3,7 +3,7 @@ CC = g++
|
|||||||
all: sim
|
all: sim
|
||||||
|
|
||||||
sim: main.cpp block.cpp set.cpp simulator.cpp
|
sim: main.cpp block.cpp set.cpp simulator.cpp
|
||||||
$(CC) main.cpp block.cpp set.cpp simulator.cpp -o sim
|
$(CC) -std=c++11 main.cpp block.cpp set.cpp simulator.cpp -o sim
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm sim
|
rm sim
|
||||||
|
|||||||
88
main.cpp
88
main.cpp
@ -1,9 +1,10 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
#include <string.h>
|
||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
#include "simulator.h"
|
#include "simulator.h"
|
||||||
|
|
||||||
int main(){
|
int main(int argc, char* argv[]){
|
||||||
std::string temp;
|
std::string temp;
|
||||||
int machineBits, cacheSize, waySize, blockSize;
|
int machineBits, cacheSize, waySize, blockSize;
|
||||||
short setSize;
|
short setSize;
|
||||||
@ -23,62 +24,71 @@ int main(){
|
|||||||
std::cout << "num_set: " << setSize << std::endl;
|
std::cout << "num_set: " << setSize << std::endl;
|
||||||
std::cout << "num_block_offset_bit: " << offsetSize << std::endl;
|
std::cout << "num_block_offset_bit: " << offsetSize << std::endl;
|
||||||
std::cout << "num_index_bit: " << indexSize << std::endl;
|
std::cout << "num_index_bit: " << indexSize << std::endl;
|
||||||
std::cout << "num_tag_bit: " << tagSize << std::endl << std::endl;
|
std::cout << "num_tag_bit: " << tagSize << std::endl;
|
||||||
|
|
||||||
CacheSim::Simulator FIFOSim(machineBits, setSize, waySize, tagSize, 0);
|
CacheSim::Simulator* Sim;
|
||||||
CacheSim::Simulator LRUSim(machineBits, setSize, waySize, tagSize, 1);
|
if(strcmp(argv[1], "FIFO") == 0){
|
||||||
|
Sim = new CacheSim::Simulator(machineBits, setSize, waySize, tagSize, 0);
|
||||||
|
}else if(strcmp(argv[1], "LRU") == 0){
|
||||||
|
Sim = new CacheSim::Simulator(machineBits, setSize, waySize, tagSize, 1);
|
||||||
|
}else{
|
||||||
|
std::cout << "Usage: ./sim [ LRU | FIFO]" << std::endl;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
std::string action, address;
|
std::string action, address;
|
||||||
bool status;
|
bool status;
|
||||||
std::vector<std::pair<std::string, bool>> LRUResult, FIFOResult;
|
std::vector<std::tuple<std::string, std::string, bool, short, short>> 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;
|
||||||
status = FIFOSim.read(address);
|
tuple<bool, short, short> status = Sim->read(address);
|
||||||
FIFOResult.push_back(std::make_pair(address, status));
|
Result.push_back(
|
||||||
|
std::make_tuple(
|
||||||
status = LRUSim.read(address);
|
action,
|
||||||
LRUResult.push_back(std::make_pair(address, status));
|
address,
|
||||||
|
std::get<0>(status),
|
||||||
|
std::get<1>(status),
|
||||||
|
std::get<2>(status)
|
||||||
|
)
|
||||||
|
);
|
||||||
// std::cout << std::endl << std::endl;
|
// std::cout << std::endl << std::endl;
|
||||||
}
|
}
|
||||||
// FIFO
|
// FIFO
|
||||||
|
|
||||||
int allCount = 0, hitCount = 0;
|
int allCount = 0, hitCount = 0;
|
||||||
|
// std::cout << argv[1] << " Replace Policy:" << std::endl;
|
||||||
std::cout << "FIFO Replace Policy:" << std::endl;
|
for(int i=0; i<Result.size(); i++){
|
||||||
for(int i=0; i<FIFOResult.size(); i++){
|
// std::cout << " " << std::setw(6) << std::setfill('0') << Result[i].first << "... ";
|
||||||
std::cout << " " << std::setw(6) << std::setfill('0') << FIFOResult[i].first << "... ";
|
|
||||||
allCount++;
|
allCount++;
|
||||||
if(FIFOResult[i].second){
|
if(std::get<2>(Result[i])){
|
||||||
hitCount++;
|
hitCount++;
|
||||||
std::cout << "HIT" << std::endl;
|
|
||||||
}else{
|
|
||||||
std::cout << "MISS" << std::endl;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
std::cout << std::endl;
|
std::cout << "num_total_access: " << allCount << std::endl;
|
||||||
std::cout << " Hit rate: " << (double)hitCount/allCount*100 << "%" << std::endl;
|
std::cout << "num_hit: " << hitCount << std::endl;
|
||||||
std::cout << " Miss rate: " << 100 - (double)hitCount/allCount*100 << "%" << std::endl << std::endl;
|
std::cout << "num_miss: " << allCount - hitCount << std::endl;
|
||||||
|
std::cout << "hit_rate: " << std::fixed << std::setprecision(2) << (double)hitCount/allCount*100 << "%" << std::endl;
|
||||||
|
std::cout << std::endl << "hit trace:" << std::endl << std::endl;
|
||||||
|
|
||||||
|
for(int i=0; i<Result.size(); i++){
|
||||||
|
if(std::get<2>(Result[i])){
|
||||||
|
std::cout << std::get<0>(Result[i]) << " " << std::get<1>(Result[i]);
|
||||||
|
std::cout << " hit ";
|
||||||
|
|
||||||
|
std::cout << "set: ";
|
||||||
|
if(std::get<3>(Result[i]) != 0)
|
||||||
|
std::cout << "0x" << std::hex << std::get<3>(Result[i]) << " ";
|
||||||
|
else
|
||||||
|
std::cout << "0 ";
|
||||||
|
|
||||||
// LRU
|
std::cout << "way: ";
|
||||||
|
if(std::get<4>(Result[i]) != 0)
|
||||||
allCount = 0, hitCount = 0;
|
std::cout << "0x" << std::hex << std::get<4>(Result[i]);
|
||||||
std::cout << "LRU Replace Policy:" << std::endl;
|
else
|
||||||
for(int i=0; i<LRUResult.size(); i++){
|
std::cout << "0";
|
||||||
std::cout << " " << LRUResult[i].first << "... ";
|
|
||||||
allCount++;
|
if(i != Result.size()-1)
|
||||||
if(LRUResult[i].second){
|
std::cout << std::endl;
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|||||||
9
set.cpp
9
set.cpp
@ -18,7 +18,7 @@ Set::Set(short waySize, short tagSize, short replacePolicy){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Set::read(vector<bool> tag, short offset){
|
tuple<bool, short> Set::read(vector<bool> tag, short offset){
|
||||||
// HIT 的情況
|
// HIT 的情況
|
||||||
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++){
|
||||||
@ -29,17 +29,19 @@ bool Set::read(vector<bool> tag, short offset){
|
|||||||
this->countAccess(baseAccessTime); // 更新計算所有 way access 的時間點
|
this->countAccess(baseAccessTime); // 更新計算所有 way access 的時間點
|
||||||
// cout << "HIT: 在 " << i << " way 找到!" << endl;
|
// cout << "HIT: 在 " << i << " way 找到!" << endl;
|
||||||
}
|
}
|
||||||
return true;
|
return std::make_tuple(true, i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// MISS 的情況,需要寫入 $
|
// MISS 的情況,需要寫入 $
|
||||||
|
short writeWay;
|
||||||
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();
|
||||||
|
writeWay = i;
|
||||||
baseAccessTime = this->waySize; // 每個都要更新
|
baseAccessTime = this->waySize; // 每個都要更新
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -63,6 +65,7 @@ bool Set::read(vector<bool> tag, short offset){
|
|||||||
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();
|
||||||
|
writeWay = maxIndex;
|
||||||
|
|
||||||
// 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();
|
||||||
@ -74,7 +77,7 @@ bool Set::read(vector<bool> tag, short offset){
|
|||||||
}
|
}
|
||||||
|
|
||||||
this->countAccess(baseAccessTime);
|
this->countAccess(baseAccessTime);
|
||||||
return false;
|
return std::make_tuple(false, writeWay);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Set::countAccess(short baseAccessTime){
|
void Set::countAccess(short baseAccessTime){
|
||||||
|
|||||||
4
set.h
4
set.h
@ -1,7 +1,9 @@
|
|||||||
#include "block.h"
|
#include "block.h"
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <tuple>
|
||||||
|
|
||||||
using std::vector;
|
using std::vector;
|
||||||
|
using std::tuple;
|
||||||
using CacheSim::Block;
|
using CacheSim::Block;
|
||||||
|
|
||||||
namespace CacheSim{
|
namespace CacheSim{
|
||||||
@ -15,7 +17,7 @@ namespace CacheSim{
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
Set(short waySize, short tagSize, short replacePolicy);
|
Set(short waySize, short tagSize, short replacePolicy);
|
||||||
bool read(vector<bool> tag, short offset);
|
tuple<bool, short> read(vector<bool> tag, short offset);
|
||||||
void countAccess(short baseAccessTime);
|
void countAccess(short baseAccessTime);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|||||||
@ -15,7 +15,7 @@ Simulator::Simulator(int machineBits, short setSize, short waySize, short tagSiz
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Simulator::read(string address){
|
tuple<bool, short, short> 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] << " ";
|
||||||
@ -25,11 +25,13 @@ bool Simulator::read(string address){
|
|||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Simulator::write(string address){
|
tuple<bool, short> setResult = this->sets[index].read(tag, offset);
|
||||||
return true;
|
return std::make_tuple(
|
||||||
|
std::get<0>(setResult),
|
||||||
|
index,
|
||||||
|
std::get<1>(setResult)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
vector<bool> Simulator::addressTranslate(string address){
|
vector<bool> Simulator::addressTranslate(string address){
|
||||||
|
|||||||
@ -1,9 +1,11 @@
|
|||||||
#include "set.h"
|
#include "set.h"
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <tuple>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
using CacheSim::Set;
|
using CacheSim::Set;
|
||||||
using std::vector;
|
using std::vector;
|
||||||
|
using std::tuple;
|
||||||
using std::string;
|
using std::string;
|
||||||
|
|
||||||
namespace CacheSim{
|
namespace CacheSim{
|
||||||
@ -17,8 +19,7 @@ namespace CacheSim{
|
|||||||
vector<Set> sets;
|
vector<Set> sets;
|
||||||
public:
|
public:
|
||||||
Simulator(int machineBits, short setSize, short waySize, short tagSize, short replacePolicy);
|
Simulator(int machineBits, short setSize, short waySize, short tagSize, short replacePolicy);
|
||||||
bool read(string address);
|
tuple<bool, short, short> read(string address);
|
||||||
bool write(string address);
|
|
||||||
vector<bool> addressTranslate(string address);
|
vector<bool> addressTranslate(string address);
|
||||||
short getValue(vector<bool> addrBits, string type);
|
short getValue(vector<bool> addrBits, string type);
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user