1 package org.simantics.db.impl.graph;
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.HashMap;
8 import org.simantics.db.impl.BlockingAsyncProcedure;
9 import org.simantics.db.impl.query.CacheEntry;
10 import org.simantics.db.impl.query.QueryProcessor.SessionTask;
12 public class BarrierTracing {
14 public static final boolean BOOKKEEPING = false;
15 static final boolean RESTART_GUARD = BOOKKEEPING && true;
17 public static Map<SessionTask,Exception> tasks = new HashMap<>();
18 public static final HashMap<AsyncBarrierImpl, Collection<AsyncBarrierImpl>> reverseLookup = new HashMap<>();
19 public static final HashMap<AsyncBarrierImpl, Debugger> debuggerMap = new HashMap<>();
20 public static final HashMap<AsyncBarrierImpl, CacheEntry<?>> entryMap = new HashMap<>();
21 public static final HashMap<AsyncBarrierImpl, Throwable> restartMap = new HashMap<>();
22 public static final HashMap<AsyncBarrierImpl, Throwable> startMap = new HashMap<>();
23 public static final HashMap<BlockingAsyncProcedure, Throwable> baps = new HashMap<>();
25 synchronized public static void registerBAP(BlockingAsyncProcedure bap) {
26 baps.put(bap, new Exception());
29 synchronized public static void unregisterBAP(BlockingAsyncProcedure bap) {
33 synchronized public static void printBAPS() {
34 for(BlockingAsyncProcedure bap : baps.keySet()) {
35 Throwable e = baps.get(bap);
36 System.err.println("BlockingAsyncProcedure");
37 System.err.println("-key: " + bap.key);
38 System.err.println("-queryGraph: " + bap.queryGraph);
39 System.err.println("-callerGraph: " + bap.callerGraph);
40 System.err.println("-procedure: " + bap.procedure);
41 System.err.println("-pendingTaskSupport: " + bap.pendingTaskSupport);
42 System.err.println("-result: " + bap.result);
43 System.err.println("-exception: " + bap.exception);
48 public static void trace(AsyncBarrierImpl barrier, CacheEntry<?> entry) {
51 synchronized (startMap) {
52 startMap.put(barrier, new Exception());
55 synchronized (entryMap) {
56 entryMap.put(barrier, entry);
58 synchronized (debuggerMap) {
59 debuggerMap.put(barrier, new Debugger());
61 synchronized (reverseLookup) {
62 Collection<AsyncBarrierImpl> barriers = reverseLookup
64 if (barriers == null) {
65 barriers = new ArrayList<AsyncBarrierImpl>();
66 reverseLookup.put(barrier.caller, barriers);
68 barriers.add(barrier);
73 public static void inc(AsyncBarrierImpl barrier) {
75 barrier.inc(barrier, new Exception().getStackTrace()[2].toString());
78 if(restartMap.containsKey(barrier)) {
79 startMap.get(barrier).printStackTrace();
80 restartMap.get(barrier).printStackTrace();
81 new Exception().printStackTrace();
82 throw new IllegalStateException("Unplanned restart");
88 public static void restart(AsyncBarrierImpl barrier) {
90 BarrierTracing.restartMap.remove(barrier);
92 BarrierTracing.debuggerMap.put(barrier, new Debugger());
95 public static void dec(AsyncBarrierImpl barrier, int count) {
98 restartMap.put(barrier, new Exception());
100 debuggerMap.remove(barrier);
102 else if (count < 0) {
103 BarrierTracing.startMap.get(barrier).printStackTrace();
104 BarrierTracing.restartMap.get(barrier).printStackTrace();
105 new Exception().printStackTrace();
109 public static class Debugger {
111 public Map<AsyncBarrierImpl, String> infos = new HashMap<>();
113 public synchronized void inc(AsyncBarrierImpl id, String info) {
116 String exist = infos.get(id);
118 throw new IllegalStateException("Already existing info " + id + " " + info);
119 infos.put(id, exist);
122 public synchronized void dec(AsyncBarrierImpl id) {
125 String exist = infos.get(id);
127 System.err.println("No data for " + id);
134 public synchronized String toString() {
135 StringBuilder b = new StringBuilder();
136 for (String s : infos.values()) {
137 b.append("info " + s + "\r\n");
142 public boolean isEmpty() {
143 return infos.isEmpty();