Hallo,
ich soll eine Cache Simulation schreiben. Ein Teil des Quellcodes ist vom Prof gestellt und sollte nicht verändert werden. Nur die Passagen mit //Your Code ... sollen fertig gestellt werden. (Der Code folgt am Ende des Beitrages.)
Die Datensätze kommen vom Linux Tool Valgrind. (Instruction Cache Zugriffe sollen ignoriert werden.)
Mein aktuelles Problem ist das ich, die Hex Adressen nicht in long konvertiert bekomme. Fehlermeldung:
Exception in thread "main" java.lang.NumberFormatException: For input string: "ffeffffb8"
Dazu sollen wir laut dem vorgegebenen Code einen LRU implementieren. Jedoch ohne die Blöcke zu speichern in einem Array. (Kommentar: //We don't need that now)
Ich muss zugeben, dass ich den Code vom Prof noch nicht ganz durchgestiegen bin. Deshalb wäre ich über jede Hilfe dankbar.
ich soll eine Cache Simulation schreiben. Ein Teil des Quellcodes ist vom Prof gestellt und sollte nicht verändert werden. Nur die Passagen mit //Your Code ... sollen fertig gestellt werden. (Der Code folgt am Ende des Beitrages.)
Die Datensätze kommen vom Linux Tool Valgrind. (Instruction Cache Zugriffe sollen ignoriert werden.)
Mein aktuelles Problem ist das ich, die Hex Adressen nicht in long konvertiert bekomme. Fehlermeldung:
Exception in thread "main" java.lang.NumberFormatException: For input string: "ffeffffb8"
Java:
adress = Integer.parseInt(split[2].subSequence(0, split[2].length() - 2).toString(), 16);
Dazu sollen wir laut dem vorgegebenen Code einen LRU implementieren. Jedoch ohne die Blöcke zu speichern in einem Array. (Kommentar: //We don't need that now)
Ich muss zugeben, dass ich den Code vom Prof noch nicht ganz durchgestiegen bin. Deshalb wäre ich über jede Hilfe dankbar.
Java:
public enum BlockState {
hit, miss, eviction;
}
public class CacheConfig {
public int numIndexBits;
public int associativity;
public int numCacheBlocks;
public int blockSize; // = in Byte
public CacheConfig(int s, int e, int b) {
numIndexBits = s;
associativity = e;
numCacheBlocks = (int) Math.pow(2, numIndexBits);
blockSize = b;
}
}
public class CacheBlock {
public boolean[] valid;
private long[] tag;
private int lru;
// public long[] data; //We don't need that now
public static CacheConfig cacheConfig;
public CacheBlock(int assoc) {
tag = new long[assoc];
valid = new boolean[assoc];
lru = 0;
}
public static int getBlockAddr(long addr) {
// Your code...
return (int) addr/cacheConfig.blockSize;
}
private int getLRU() { //Größe der Warteschlange?
// Your code...
return this.lru;
}
private int getEntry(long addr) {
for(int i = 0; i < tag.length; i++) {
if (tag[i] == addr) return i;
}
return -1;
}
public static BlockState read(long addr, CacheBlock[] theCache) {
int blockAddr = getBlockAddr(addr);
CacheBlock cb = theCache[blockAddr];
int entryIdx = cb.getEntry(addr);
if (entryIdx >= 0) {
//Entry existed -> hit
return BlockState.hit;
}
else {
//Entry didn't exist. Get next LRU
entryIdx = cb.getLRU();
//Decide if read is hit or inviction or miss
// Your code...
if(entryIdx >= 0) {
//Entry existed -> hit
return BlockState.hit;
}
}
//Entry didn't exist
return BlockState.miss;
}
public static BlockState write(long addr, CacheBlock[] theCache) {
// Your code...
int blockAddr = getBlockAddr(addr);
CacheBlock cb = theCache[blockAddr];
int entryIdx = cb.getEntry(addr);
if (entryIdx >= 0) {
//Entry existed -> eviction
return BlockState.eviction;
}
return BlockState.miss;
}
}
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
public class CacheSim {
private static long hitCount = 0;
private static long missCount = 0;
private static long evictionCount = 0;
private static long lineCount = 0;
public static void main(String[] args) throws Exception {
int blockCount = 0;
int associativity = 1;
int blockBitsSize = 0;
File traceFile = null;
boolean verbose = false;
boolean help = false;
for (int i = 0; i < args.length; i++) {
switch (args[i]) {
case "-s":
i++;
blockCount = Integer.parseInt(args[i]);
break;
case "-E":
i++;
associativity = Integer.parseInt(args[i]);
break;
case "-b":
i++;
int blockAddressBits = Integer.parseInt(args[i]);
if (blockAddressBits < 3)
throw new IllegalArgumentException(
"block size has to be at least 8 bits/1 byte because of byte-aligned memory");
blockBitsSize = (int) Math.pow(2, blockAddressBits) >>> 3;
break;
case "-t":
i++;
String traceFileName = args[i];
traceFile = new File(traceFileName);
break;
case "-v":
verbose = true;
break;
case "-h":
help = true;
break;
}
}
if (help) {
System.out.println("-s <s> Anzahl der Indexbits s, S = 2^s ist die Anzahl der Cache Blöcke\n"
+ "-E <E> Assoziativität des Caches, E = Anzahl der Blöcke pro Satz\n"
+ "-b <b> Exponent der Block Bits, B = 2^b ist die Blockgröße (Bsp.: b=4 -> B=16 -> 2 Byte)\n"
+ "-t <tracefile> der Name der valgrind Trace Datei, die Ihr Simulator simulieren soll\n"
+ "-v aktiviert den 'verbose' Mode, bei dem Ihr Cachesimulator für jede eingelesene Trace Zeile das aktuelle Cache Verhalten ausgibt (siehe unten).\n"
+ "-h gibt die verfügbaren Optionen Ihres Simulators aus, d.h. die Beschreibung dieser Flags.");
}
System.out.println("Block size in byte: " + blockBitsSize);
// Building up the cache
final CacheConfig cc = new CacheConfig(blockCount, associativity, blockBitsSize);
CacheBlock[] theCache = new CacheBlock[cc.numCacheBlocks];
CacheBlock.cacheConfig = cc; // This is not very nice!!!
for (int i = 0; i < theCache.length; i++) {
theCache[i] = new CacheBlock(cc.associativity);
}
System.out.println("Cache size: " + theCache.length);
// Reading file and simulation
FileReader fr = new FileReader(traceFile);
BufferedReader br = new BufferedReader(fr);
String currentLine = br.readLine();
// Your code...
long adress = 0;
int byteSize = 0;
while(currentLine != null) {
if(currentLine.startsWith(" ")) {
String[] split = currentLine.split(" ");
adress = Integer.parseInt(split[2].subSequence(0, split[2].length() - 2).toString(), 16);
byteSize = (int) currentLine.charAt(currentLine.length() - 1) - '0';
CacheBlock.read(adress, theCache);
}
currentLine = br.readLine();
}
String str = "";
for(int k = 0; k < args.length; k++) {
str+= args[k] + " ";
}
System.out.println(str);
System.out.println("hits: " + hitCount + ", misses: " + missCount + ", evictions: " + evictionCount);
}
}
------------- Kleiner Auszug aus tracefile ------------
S ffeffffb8,8
I 04004a70,1
S ffeffffb0,8
I 04004a71,3
I 04004a74,2
S ffeffffa8,8
I 04004a76,2
S ffeffffa0,8
I 04004a78,2
S ffeffff98,8
I 04004a7a,2
S ffeffff90,8
I 04004a7c,1
S ffeffff88,8
I 04004a7d,3
I 04004a80,4
I 04004a84,2
I 04004a86,4
I 04004a8a,2
I 04004a8c,3
I 04004a8f,7
L 04222e70,8
I 04004a96,7
S 04222c98,8
I 04004a9d,7
I 04004aa4,3
I 04004aa7,7
L 04223000,8
I 04004aae,3
I 04004ab1,7
S 04223a08,8