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 && false;
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()) {
36 Throwable t = baps.get(bap);
42 public static void trace(AsyncBarrierImpl barrier, CacheEntry<?> entry) {
45 synchronized (startMap) {
46 startMap.put(barrier, new Exception());
49 synchronized (entryMap) {
50 entryMap.put(barrier, entry);
52 synchronized (debuggerMap) {
53 debuggerMap.put(barrier, new Debugger());
55 synchronized (reverseLookup) {
56 Collection<AsyncBarrierImpl> barriers = reverseLookup
58 if (barriers == null) {
59 barriers = new ArrayList<AsyncBarrierImpl>();
60 reverseLookup.put(barrier.caller, barriers);
62 barriers.add(barrier);
67 public static void inc(AsyncBarrierImpl barrier) {
69 barrier.inc(barrier, new Exception().getStackTrace()[2].toString());
72 if(restartMap.containsKey(barrier)) {
73 startMap.get(barrier).printStackTrace();
74 restartMap.get(barrier).printStackTrace();
75 new Exception().printStackTrace();
76 throw new IllegalStateException("Unplanned restart");
82 public static void restart(AsyncBarrierImpl barrier) {
84 BarrierTracing.restartMap.remove(barrier);
86 BarrierTracing.debuggerMap.put(barrier, new Debugger());
89 public static void dec(AsyncBarrierImpl barrier, int count) {
92 restartMap.put(barrier, new Exception());
94 debuggerMap.remove(barrier);
97 BarrierTracing.startMap.get(barrier).printStackTrace();
98 BarrierTracing.restartMap.get(barrier).printStackTrace();
99 new Exception().printStackTrace();
103 public static class Debugger {
105 public Map<AsyncBarrierImpl, String> infos = new HashMap<>();
107 public synchronized void inc(AsyncBarrierImpl id, String info) {
110 String exist = infos.get(id);
112 throw new IllegalStateException("Already existing info " + id + " " + info);
113 infos.put(id, exist);
116 public synchronized void dec(AsyncBarrierImpl id) {
119 String exist = infos.get(id);
121 System.err.println("No data for " + id);
128 public synchronized String toString() {
129 StringBuilder b = new StringBuilder();
130 for (String s : infos.values()) {
131 b.append("info " + s + "\r\n");
136 public boolean isEmpty() {
137 return infos.isEmpty();