for demo
This commit is contained in:
parent
2c8d14323a
commit
8da8d0e845
2
Makefile
2
Makefile
@ -3,7 +3,7 @@ CC = g++
|
||||
all: sim
|
||||
|
||||
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:
|
||||
rm sim
|
||||
|
||||
86
main.cpp
86
main.cpp
@ -1,9 +1,10 @@
|
||||
#include <iostream>
|
||||
#include <math.h>
|
||||
#include <string.h>
|
||||
#include <iomanip>
|
||||
#include "simulator.h"
|
||||
|
||||
int main(){
|
||||
int main(int argc, char* argv[]){
|
||||
std::string temp;
|
||||
int machineBits, cacheSize, waySize, blockSize;
|
||||
short setSize;
|
||||
@ -23,62 +24,71 @@ int main(){
|
||||
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;
|
||||
std::cout << "num_tag_bit: " << tagSize << std::endl;
|
||||
|
||||
CacheSim::Simulator FIFOSim(machineBits, setSize, waySize, tagSize, 0);
|
||||
CacheSim::Simulator LRUSim(machineBits, setSize, waySize, tagSize, 1);
|
||||
CacheSim::Simulator* Sim;
|
||||
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;
|
||||
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){
|
||||
// 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));
|
||||
tuple<bool, short, short> status = Sim->read(address);
|
||||
Result.push_back(
|
||||
std::make_tuple(
|
||||
action,
|
||||
address,
|
||||
std::get<0>(status),
|
||||
std::get<1>(status),
|
||||
std::get<2>(status)
|
||||
)
|
||||
);
|
||||
// std::cout << std::endl << std::endl;
|
||||
}
|
||||
// FIFO
|
||||
|
||||
int allCount = 0, hitCount = 0;
|
||||
|
||||
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 << "... ";
|
||||
// std::cout << argv[1] << " Replace Policy:" << std::endl;
|
||||
for(int i=0; i<Result.size(); i++){
|
||||
// std::cout << " " << std::setw(6) << std::setfill('0') << Result[i].first << "... ";
|
||||
allCount++;
|
||||
if(FIFOResult[i].second){
|
||||
if(std::get<2>(Result[i])){
|
||||
hitCount++;
|
||||
std::cout << "HIT" << std::endl;
|
||||
}else{
|
||||
std::cout << "MISS" << 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;
|
||||
std::cout << "num_total_access: " << allCount << std::endl;
|
||||
std::cout << "num_hit: " << hitCount << 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)
|
||||
std::cout << "0x" << std::hex << std::get<4>(Result[i]);
|
||||
else
|
||||
std::cout << "0";
|
||||
|
||||
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;
|
||||
if(i != Result.size()-1)
|
||||
std::cout << 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 的情況
|
||||
short baseAccessTime; // 被替換掉的 block ,它的 access time
|
||||
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 的時間點
|
||||
// cout << "HIT: 在 " << i << " way 找到!" << endl;
|
||||
}
|
||||
return true;
|
||||
return std::make_tuple(true, i);
|
||||
}
|
||||
}
|
||||
|
||||
// MISS 的情況,需要寫入 $
|
||||
short writeWay;
|
||||
if(this->emptySize > 0){
|
||||
for(int i=0; i<this->waySize; i++){
|
||||
if(this->blocks[i].getValid() == false){
|
||||
// cout << "MISS: 在 " << i <<" way 寫入資料" << endl;
|
||||
this->blocks[i].update(tag); // 更新 tag
|
||||
this->blocks[i].access();
|
||||
writeWay = i;
|
||||
baseAccessTime = this->waySize; // 每個都要更新
|
||||
break;
|
||||
}
|
||||
@ -63,6 +65,7 @@ bool Set::read(vector<bool> tag, short offset){
|
||||
baseAccessTime = this->blocks[maxIndex].getAccessTime();
|
||||
this->blocks[maxIndex].update(tag);
|
||||
this->blocks[maxIndex].access();
|
||||
writeWay = maxIndex;
|
||||
|
||||
// for(int i=0; i<this->waySize; i++){
|
||||
// vector<bool> t = this->blocks[i].getTag();
|
||||
@ -74,7 +77,7 @@ bool Set::read(vector<bool> tag, short offset){
|
||||
}
|
||||
|
||||
this->countAccess(baseAccessTime);
|
||||
return false;
|
||||
return std::make_tuple(false, writeWay);
|
||||
}
|
||||
|
||||
void Set::countAccess(short baseAccessTime){
|
||||
|
||||
4
set.h
4
set.h
@ -1,7 +1,9 @@
|
||||
#include "block.h"
|
||||
#include <vector>
|
||||
#include <tuple>
|
||||
|
||||
using std::vector;
|
||||
using std::tuple;
|
||||
using CacheSim::Block;
|
||||
|
||||
namespace CacheSim{
|
||||
@ -15,7 +17,7 @@ namespace CacheSim{
|
||||
|
||||
public:
|
||||
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);
|
||||
|
||||
};
|
||||
|
||||
@ -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);
|
||||
// for(int i=0; i<addrBits.size(); i++){
|
||||
// std::cout << addrBits[i] << " ";
|
||||
@ -25,11 +25,13 @@ bool Simulator::read(string address){
|
||||
short offset = this->getValue(addrBits, "offset");
|
||||
vector<bool> tag(addrBits.begin(), addrBits.begin()+this->tagSize);
|
||||
// std::cout<<"更新 index: "<<index<<std::endl;
|
||||
return this->sets[index].read(tag, offset);
|
||||
}
|
||||
|
||||
bool Simulator::write(string address){
|
||||
return true;
|
||||
tuple<bool, short> setResult = this->sets[index].read(tag, offset);
|
||||
return std::make_tuple(
|
||||
std::get<0>(setResult),
|
||||
index,
|
||||
std::get<1>(setResult)
|
||||
);
|
||||
}
|
||||
|
||||
vector<bool> Simulator::addressTranslate(string address){
|
||||
|
||||
@ -1,9 +1,11 @@
|
||||
#include "set.h"
|
||||
#include <vector>
|
||||
#include <tuple>
|
||||
#include <iostream>
|
||||
#include <math.h>
|
||||
using CacheSim::Set;
|
||||
using std::vector;
|
||||
using std::tuple;
|
||||
using std::string;
|
||||
|
||||
namespace CacheSim{
|
||||
@ -17,8 +19,7 @@ namespace CacheSim{
|
||||
vector<Set> sets;
|
||||
public:
|
||||
Simulator(int machineBits, short setSize, short waySize, short tagSize, short replacePolicy);
|
||||
bool read(string address);
|
||||
bool write(string address);
|
||||
tuple<bool, short, short> read(string address);
|
||||
vector<bool> addressTranslate(string address);
|
||||
short getValue(vector<bool> addrBits, string type);
|
||||
};
|
||||
|
||||
Loading…
Reference in New Issue
Block a user