Commit 5d6fef49 authored by  Rewaj  Shrestha's avatar Rewaj Shrestha

rs6017: Only evicts in a miss

parent d7040ee9
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
Simulation simulation = new Simulation(10, 1000, EvictionPolicy.FIFO);
simulation.run(10000000.0);
Simulation.run(50, 100, 1000000.0, 10, 1000000.0, EvictionPolicy.RAND);
}
}
......@@ -18,13 +18,37 @@ public class Simulation {
currTime += nextArrival();
int item = getItem();
processItemRequest(item);
System.out.println(((double) misses)/requests);
}
}
public static List<Double> run(int m, int n, double simulationTime, int numberOfSimulations, double warmupTime, EvictionPolicy evictionPolicy) {
List<Double> cacheMissRates = new ArrayList<>();
for (int i = 0; i < numberOfSimulations ; i ++) {
Simulation warmUpSimulation = new Simulation(m, n, evictionPolicy);
warmUpSimulation.run(warmupTime);
System.out.println(((double) warmUpSimulation.misses) / warmUpSimulation.requests);
Simulation recordSimulation = new Simulation(m, n, evictionPolicy, warmUpSimulation.cache);
recordSimulation.run(simulationTime);
cacheMissRates.add(((double) recordSimulation.misses) / recordSimulation.requests);
System.out.println(((double) recordSimulation.misses) / recordSimulation.requests);
}
double sum = cacheMissRates.stream().reduce(Double::sum).get();
return cacheMissRates;
}
public Simulation(int m, int n, EvictionPolicy evictionPolicy) {
this (m, n, evictionPolicy, new ArrayList<>());
}
public Simulation (int m, int n, EvictionPolicy evictionPolicy, List<Integer> cache) {
this.evictionPolicy = evictionPolicy;
this.cache = new ArrayList<>();
this.cache = cache;
this.cumulativeRateSum = new ArrayList<>();
this.misses = 0;
......@@ -34,13 +58,20 @@ public class Simulation {
// Populating cache
for (int i = 1; i <= m; i++) {
cache.add(i);
rateSum += 1.0/i;
}
for (int i = 1; i<= n; i++) {
this.rateSum += 1.0/i;
}
cumulativeRateSum.add(1/rateSum);
this.cumulativeRateSum.add(1/rateSum);
for (int i = 2; i <= n; i++) {
Double prev = cumulativeRateSum.get(cumulativeRateSum.size() - 1);
cumulativeRateSum.add(prev + (1/(rateSum * i)));
Double prev = this.cumulativeRateSum.get(this.cumulativeRateSum.size() - 1);
this.cumulativeRateSum.add(prev + (1.0/(rateSum * i)));
}
}
......@@ -55,23 +86,25 @@ public class Simulation {
public int getItem() {
double uniform = Math.random();
return -1 - Collections.binarySearch(cumulativeRateSum, uniform);
return -1 - Collections.binarySearch(cumulativeRateSum, uniform * cumulativeRateSum.get(cumulativeRateSum.size()-1));
}
public void processItemRequest(int item) {
requests ++;
if (!cache.contains(item)) {
misses++;
if (evictionPolicy == EvictionPolicy.FIFO) {
cache.remove(0);
cache.add(item);
} else if (evictionPolicy == EvictionPolicy.RAND) {
int i = (int) (Math.random() * cache.size());
cache.remove(i);
cache.add(i, item);
}
}
if (evictionPolicy == EvictionPolicy.FIFO) {
cache.remove(0);
cache.add(item);
} else if (evictionPolicy == EvictionPolicy.RAND) {
int i = (int) (Math.random() * cache.size());
cache.remove(i);
cache.add(i, item);
}
}
// public void evictItem(int item) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment