forked from seal-hub/ERCatcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFastOnDemandSHB.java
128 lines (115 loc) · 4.83 KB
/
FastOnDemandSHB.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package com.ercatcher.svc;
import com.ercatcher.LOG;
import com.ercatcher.ConcurrencyAnalysis.C3G.C3GManager;
import com.ercatcher.ConcurrencyAnalysis.ThreadLattice;
import java.util.*;
public class FastOnDemandSHB extends AbstractOnDemandSHB{
private int[][] happensBeforeMat;
private static final int UNCHECKED = -1;
private static final int CHECKED_BUT_UNKOWN = 1; // Cache?
private static final int HBEFORE = 2;
private static final int HAFTER = 3;
private static final int SELF = 4;
private int DURING_PASS_MINIMIZE_THRESHOLD = 20;
private int BEFORE_ITER_MINIMIZE_THRESHOLD = 40;
public FastOnDemandSHB(C3GManager c3GManager) {
super(c3GManager);
LOG.logln("Using Fast SHB", LOG.VERBOSE);
happensBeforeMat = new int[svcMap.size()][svcMap.size()];
for (int i = 0; i < svcMap.size(); i++)
for (int j = 0; j < svcMap.size(); j++)
happensBeforeMat[i][j] = UNCHECKED;
}
@Override
protected boolean quickHB(StaticVectorClock first, StaticVectorClock second) {
if(first == second)
return false;
if(first == null || second == null)
return false;
if(isComputationFinished()){
if(first.temporaryOrder > second.temporaryOrder)
return false;
}
if(happensBeforeMat[first.id][second.id] == HAFTER)
return false;
if(happensBeforeMat[first.id][second.id] == HBEFORE)
return true;
return false;
}
@Override
protected void updateSVCs(StaticVectorClock currentSVC, Map<ThreadLattice, Set<StaticVectorClock>> myHAInvokers) {
for(Set<StaticVectorClock> svcSet : myHAInvokers.values())
for(StaticVectorClock haSVC : svcSet) {
if(currentSVC instanceof InvokerSVC && haSVC.getThreadLattice().equals(currentSVC.getThreadLattice()))
currentSVC.addHappensAfterRelation(haSVC);
else
currentSVC.addAllHappensAfterRelation(haSVC);
happensBeforeMat[currentSVC.id][haSVC.id] = HAFTER;
happensBeforeMat[haSVC.id][currentSVC.id] = HBEFORE;
}
}
@Override
protected void addHBRelation(StaticVectorClock first, StaticVectorClock second) {
second.addHappensAfterRelation(first);
happensBeforeMat[second.id][first.id] = HAFTER;
happensBeforeMat[first.id][second.id] = HBEFORE;
}
@Override
protected void minimizeDuringPass(Map<StaticVectorClock, Map<ThreadLattice, Set<StaticVectorClock>>> haInvokers, StaticVectorClock child, ThreadLattice threadLattice) {
if (haInvokers.get(child).get(threadLattice).size() > DURING_PASS_MINIMIZE_THRESHOLD)
haInvokers.get(child).put(threadLattice, minimize(new ArrayList<>(haInvokers.get(child).get(threadLattice))));
}
@Override
protected void minimizeBeforeIter() {
for (StaticVectorClock svc : svcMap.values()) {
if(svc.getLatestHappensAfterSize() > BEFORE_ITER_MINIMIZE_THRESHOLD)
svc.minimize(this::minimize);
}
}
@Override
public boolean happensBefore(StaticVectorClock first, StaticVectorClock second) {
if(quickHB(first, second))
return true;
List<StaticVectorClock> queue = new ArrayList<>();
Set<StaticVectorClock> hasBeenAddedToQueue = new HashSet<>();
queue.add(second);
hasBeenAddedToQueue.add(second);
boolean result = false;
boolean flag = false;
List<StaticVectorClock> newlyAdded = new ArrayList<>();
for(int i=0; i< queue.size(); i++){
StaticVectorClock svc = queue.get(i);
for(Iterator<StaticVectorClock> it = svc.iterAll(); it.hasNext(); ) {
StaticVectorClock haNode = it.next();
if (hasBeenAddedToQueue.contains(haNode))
continue;
if(svc != second)
newlyAdded.add(haNode);
if (haNode == first) {
result = true;
flag = true;
break;
}
queue.add(haNode);
hasBeenAddedToQueue.add(haNode);
}
if(flag)
break;
}
for (StaticVectorClock svc : newlyAdded) {
second.addAllHappensAfterRelation(svc);
// Matrix
happensBeforeMat[second.id][svc.id] = HAFTER;
happensBeforeMat[svc.id][second.id] = HBEFORE;
}
if (result) {
happensBeforeMat[second.id][first.id] = HAFTER;
happensBeforeMat[first.id][second.id] = HBEFORE;
}
return result;
}
@Override
public boolean reaches(StaticVectorClock first, StaticVectorClock second) {
return false;
}
}