]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/graph/BarrierTracing.java
Merge "Trying to wait for procedures"
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / graph / BarrierTracing.java
1 package org.simantics.db.impl.graph;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.HashMap;
6 import java.util.Map;
7
8 import org.simantics.db.impl.query.CacheEntry;
9 import org.simantics.db.impl.query.QueryProcessor.SessionTask;
10
11 public class BarrierTracing {
12
13     public static final boolean BOOKKEEPING = false;
14     static final boolean RESTART_GUARD = BOOKKEEPING && false;
15
16     public static Map<SessionTask,Exception> tasks = new HashMap<>();
17     public static final HashMap<AsyncBarrierImpl, Collection<AsyncBarrierImpl>> reverseLookup = new HashMap<>();
18     public static final HashMap<AsyncBarrierImpl, Debugger> debuggerMap = new HashMap<>();
19     public static final HashMap<AsyncBarrierImpl, CacheEntry<?>> entryMap = new HashMap<>();
20     public static final HashMap<AsyncBarrierImpl, Throwable> restartMap = new HashMap<>();
21     public static final HashMap<AsyncBarrierImpl, Throwable> startMap = new HashMap<>();
22
23     public static void trace(AsyncBarrierImpl barrier, CacheEntry<?> entry) {
24
25         if (RESTART_GUARD) {
26             synchronized (startMap) {
27                 startMap.put(barrier, new Exception());
28             }
29         }
30         synchronized (entryMap) {
31             entryMap.put(barrier, entry);
32         }
33         synchronized (debuggerMap) {
34             debuggerMap.put(barrier, new Debugger());
35         }
36         synchronized (reverseLookup) {
37             Collection<AsyncBarrierImpl> barriers = reverseLookup
38                     .get(barrier.caller);
39             if (barriers == null) {
40                 barriers = new ArrayList<AsyncBarrierImpl>();
41                 reverseLookup.put(barrier.caller, barriers);
42             }
43             barriers.add(barrier);
44         }
45
46     }
47
48     public static void inc(AsyncBarrierImpl barrier) {
49
50         barrier.inc(barrier, new Exception().getStackTrace()[2].toString());
51
52         if (RESTART_GUARD)
53             if(restartMap.containsKey(barrier)) {
54                 startMap.get(barrier).printStackTrace();
55                 restartMap.get(barrier).printStackTrace();
56                 new Exception().printStackTrace();
57                 throw new IllegalStateException("Unplanned restart");
58             }
59
60
61     }
62
63     public static void restart(AsyncBarrierImpl barrier) {
64         if (RESTART_GUARD)
65             BarrierTracing.restartMap.remove(barrier);
66         if (BOOKKEEPING)
67             BarrierTracing.debuggerMap.put(barrier, new Debugger());
68     }
69
70     public static void dec(AsyncBarrierImpl barrier, int count) {
71         if (count == 0) {
72             if (RESTART_GUARD) {
73                 restartMap.put(barrier, new Exception());
74             }
75             debuggerMap.remove(barrier);
76         }
77         else if (count < 0) {
78             BarrierTracing.startMap.get(barrier).printStackTrace();
79             BarrierTracing.restartMap.get(barrier).printStackTrace();
80             new Exception().printStackTrace();
81         }
82     }
83
84     public static class Debugger {
85
86         public Map<AsyncBarrierImpl, String> infos = new HashMap<>();
87
88         public synchronized void inc(AsyncBarrierImpl id, String info) {
89             if (id == null)
90                 return;
91             String exist = infos.get(id);
92             if (exist != null)
93                 throw new IllegalStateException("Already existing info " + id + " " + info);
94             infos.put(id, exist);
95         }
96
97         public synchronized void dec(AsyncBarrierImpl id) {
98             if (id == null)
99                 return;
100             String exist = infos.get(id);
101             if (exist == null) {
102                 System.err.println("No data for " + id);
103             } else {
104                 infos.remove(id);
105             }
106         }
107
108         @Override
109         public synchronized String toString() {
110             StringBuilder b = new StringBuilder();
111             for (String s : infos.values()) {
112                 b.append("info " + s + "\r\n");
113             }
114             return b.toString();
115         }
116
117         public boolean isEmpty() {
118             return infos.isEmpty();
119         }
120
121     }
122
123 }