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.query.CacheEntry;
9 import org.simantics.db.impl.query.QueryProcessor.SessionTask;
11 public class BarrierTracing {
13 public static final boolean BOOKKEEPING = false;
14 static final boolean RESTART_GUARD = BOOKKEEPING && false;
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<>();
23 public static void trace(AsyncBarrierImpl barrier, CacheEntry<?> entry) {
26 synchronized (startMap) {
27 startMap.put(barrier, new Exception());
30 synchronized (entryMap) {
31 entryMap.put(barrier, entry);
33 synchronized (debuggerMap) {
34 debuggerMap.put(barrier, new Debugger());
36 synchronized (reverseLookup) {
37 Collection<AsyncBarrierImpl> barriers = reverseLookup
39 if (barriers == null) {
40 barriers = new ArrayList<AsyncBarrierImpl>();
41 reverseLookup.put(barrier.caller, barriers);
43 barriers.add(barrier);
48 public static void inc(AsyncBarrierImpl barrier) {
50 barrier.inc(barrier, new Exception().getStackTrace()[2].toString());
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");
63 public static void restart(AsyncBarrierImpl barrier) {
65 BarrierTracing.restartMap.remove(barrier);
67 BarrierTracing.debuggerMap.put(barrier, new Debugger());
70 public static void dec(AsyncBarrierImpl barrier, int count) {
73 restartMap.put(barrier, new Exception());
75 debuggerMap.remove(barrier);
78 BarrierTracing.startMap.get(barrier).printStackTrace();
79 BarrierTracing.restartMap.get(barrier).printStackTrace();
80 new Exception().printStackTrace();
84 public static class Debugger {
86 public Map<AsyncBarrierImpl, String> infos = new HashMap<>();
88 public synchronized void inc(AsyncBarrierImpl id, String info) {
91 String exist = infos.get(id);
93 throw new IllegalStateException("Already existing info " + id + " " + info);
97 public synchronized void dec(AsyncBarrierImpl id) {
100 String exist = infos.get(id);
102 System.err.println("No data for " + id);
109 public synchronized String toString() {
110 StringBuilder b = new StringBuilder();
111 for (String s : infos.values()) {
112 b.append("info " + s + "\r\n");
117 public boolean isEmpty() {
118 return infos.isEmpty();