Skip to content

Commit

Permalink
Use a serial to element map in CycleBuilder
Browse files Browse the repository at this point in the history
This avoids a quadratic behavior in CycleBuilder (O(number_of_cycles^2)), and
instead makes it linear. Improves the situation described in #132 but does not
fully fix it.
  • Loading branch information
lupino3 committed Aug 11, 2016
1 parent e69978b commit b0f654c
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions src/main/java/org/edumips64/ui/common/CycleBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.HashMap;
import java.util.Map;

public class CycleBuilder {
Expand All @@ -42,6 +43,9 @@ public class CycleBuilder {
// Data structure that contains the actual time diagram of the pipeline.
List<CycleElement> elementsList;

// Lookup map from serial number to index into elementsList.
Map<Long, Integer> serialToElementIndexMap;

// Stalls counters.
int RAWStalls, WAWStalls, structStallsEX, structStallsDivider, structStallsFuncUnit;
// Used to understand if the EX instruction is in structural stall (memory).
Expand All @@ -55,6 +59,7 @@ public CycleBuilder(CPU cpu) {
this.cpu = cpu;
instr = new Instruction[5];
elementsList = Collections.synchronizedList(new LinkedList<CycleElement>());
serialToElementIndexMap = Collections.synchronizedMap(new HashMap<>());
updateStalls();
}
public List<CycleElement> getElementsList() {
Expand All @@ -68,15 +73,7 @@ public int getInstructionsCount() {
// next CycleElement belonging to the given instruction serial number that
// is not finalized (i.e., past WB) and has not been updated yet.
public int getInstructionToUpdate(long serialNumber) {
for (int i = 0; i < elementsList.size(); ++i) {
CycleElement tmp = elementsList.get(i);

if (tmp.getSerialNumber() == serialNumber && tmp.getUpdateTime() == curTime - 1 && !tmp.isFinalized()) {
return i;
}
}

return -1;
return serialToElementIndexMap.get(serialNumber);
}


Expand Down Expand Up @@ -185,7 +182,11 @@ public void step() {
if (instr[0] != null) {
if (!inputStallOccurred) {
// We must instantiate a new CycleElement only if the CPU is running or there was a JumpException and the the IF instruction was changed.
elementsList.add(new CycleElement(instr[0], curTime));
synchronized(elementsList) {
int newIndex = elementsList.size();
elementsList.add(newIndex, new CycleElement(instr[0], curTime));
serialToElementIndexMap.put(instr[0].getSerialNumber(), newIndex);
}
instructionsCount++;
} else {
index = getInstructionToUpdate(instr[0].getSerialNumber());
Expand Down

0 comments on commit b0f654c

Please sign in to comment.