-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
840c302
commit 5d39615
Showing
6 changed files
with
131 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
cmake_minimum_required(VERSION 3.23) | ||
project(eviction) | ||
|
||
set(CMAKE_CXX_STANDARD 17) | ||
|
||
add_executable(eviction main.cpp SwapManager.cpp SwapManager.h) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// | ||
// Created by Fangyuan Huang on 10/30/22. | ||
// | ||
|
||
#include "SwapManager.h" | ||
|
||
#include <iostream> | ||
|
||
using namespace std; | ||
|
||
void SwapManager::push(int address) { | ||
if (space.size() >= capacity) { | ||
if (strategy == "FILO") { | ||
space.pop_back(); | ||
} else if (strategy == "FIFO") { | ||
space.erase(space.begin()); | ||
} else { | ||
cout << "unrecognized strategy:" << strategy << endl; | ||
} | ||
} | ||
space.push_back(address); | ||
} | ||
|
||
bool SwapManager::get(int address) { | ||
bool exist = find(space.begin(), space.end(), address) != space.end(); | ||
// this->total += 1; | ||
total += 1; | ||
if (exist) { | ||
hit += 1; | ||
printf("Job %d found! HIT = %d, MISS = %d, TOTAL = %d, HITRATE = %.2f\n", | ||
address, hit, miss, total, 100. * hit / total); | ||
} else { | ||
miss += 1; | ||
push(address); | ||
printf("Job %d miss! HIT = %d, MISS = %d, TOTAL = %d, HITRATE = %.2f\n", | ||
address, hit, miss, total, 100. * hit / total); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
void SwapManager::setCapacity(int capacity) { | ||
if (capacity < 0) | ||
throw invalid_argument("capacity"); | ||
this->capacity = capacity; | ||
} | ||
|
||
void SwapManager::setStrategy(string strategy) { | ||
if (strategy != "FIFO" && strategy != "FILO") | ||
throw invalid_argument("strategy"); | ||
this->strategy = strategy; | ||
} | ||
|
||
SwapManager::SwapManager(int capacity, string strategy) { | ||
cout << "Constructing a swap manager..." << endl; | ||
this->space = vector<int>(); | ||
hit = 0; | ||
miss = 0; | ||
total = 0; | ||
setCapacity(capacity); | ||
setStrategy(strategy); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// | ||
// Created by Fangyuan Huang on 10/30/22. | ||
// | ||
|
||
#ifndef EVICTION_SWAPMANAGER_H | ||
#define EVICTION_SWAPMANAGER_H | ||
|
||
#include <vector> | ||
#include <string> | ||
|
||
using namespace std; | ||
|
||
class SwapManager { | ||
public: | ||
SwapManager(int capacity, string strategy); | ||
vector<int> space; | ||
// add an address into memory, if full, kick one out according to strategy | ||
void push(int address); | ||
// tells whether the space contains the address in its memory | ||
bool get(int address); | ||
void setCapacity(int capacity); | ||
void setStrategy(string strategy); | ||
|
||
private: | ||
int capacity; | ||
string strategy; | ||
int hit; | ||
int miss; | ||
int total; | ||
|
||
}; | ||
|
||
|
||
#endif //EVICTION_SWAPMANAGER_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#include <iostream> | ||
|
||
#include "SwapManager.h" | ||
|
||
int main() { | ||
vector<int> jobList = {1,2,3,4,5,6,7,1,2,3,4,5,6,7}; | ||
// test 1, FIFO | ||
SwapManager FIFO = SwapManager(5, "FIFO"); | ||
for( int job: jobList) { | ||
FIFO.get(job); | ||
} | ||
|
||
SwapManager FILO = SwapManager(5, "FILO"); | ||
for( int job: jobList) { | ||
FILO.get(job); | ||
} | ||
|
||
std::cout << "Hello, World!" << std::endl; | ||
return 0; | ||
} |