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 private static final String KEY = "SIMANTICS_DB_IMPL_BARRIER_TRACING";
16 public static final boolean BOOKKEEPING = Boolean.parseBoolean(
17 System.getProperty("org.simantics.db.impl.barrierTracing",
18 System.getenv(KEY) != null ? System.getenv(KEY) : "false")
20 static final boolean RESTART_GUARD = BOOKKEEPING && false;
22 public static Map<SessionTask,Exception> tasks = new HashMap<>();
23 public static final HashMap<AsyncBarrierImpl, Collection<AsyncBarrierImpl>> reverseLookup = new HashMap<>();
24 public static final HashMap<AsyncBarrierImpl, Debugger> debuggerMap = new HashMap<>();
25 public static final HashMap<AsyncBarrierImpl, CacheEntry<?>> entryMap = new HashMap<>();
26 public static final HashMap<AsyncBarrierImpl, Throwable> restartMap = new HashMap<>();
27 public static final HashMap<AsyncBarrierImpl, Throwable> startMap = new HashMap<>();
28 public static final HashMap<BlockingAsyncProcedure, Throwable> baps = new HashMap<>();
30 synchronized public static void registerBAP(BlockingAsyncProcedure bap) {
31 baps.put(bap, new Exception());
34 synchronized public static void unregisterBAP(BlockingAsyncProcedure bap) {
38 synchronized public static void printBAPS() {
39 for(BlockingAsyncProcedure bap : baps.keySet()) {
41 Throwable t = baps.get(bap);
47 public static void trace(AsyncBarrierImpl barrier, CacheEntry<?> entry) {
50 synchronized (startMap) {
51 startMap.put(barrier, new Exception());
54 synchronized (entryMap) {
55 entryMap.put(barrier, entry);
57 synchronized (debuggerMap) {
58 debuggerMap.put(barrier, new Debugger());
60 synchronized (reverseLookup) {
61 Collection<AsyncBarrierImpl> barriers = reverseLookup
63 if (barriers == null) {
64 barriers = new ArrayList<AsyncBarrierImpl>();
65 reverseLookup.put(barrier.caller, barriers);
67 barriers.add(barrier);
72 public static void inc(AsyncBarrierImpl barrier) {
74 barrier.inc(barrier, new Exception().getStackTrace()[2].toString());
77 if(restartMap.containsKey(barrier)) {
78 startMap.get(barrier).printStackTrace();
79 restartMap.get(barrier).printStackTrace();
80 new Exception().printStackTrace();
81 throw new IllegalStateException("Unplanned restart");
87 public static void restart(AsyncBarrierImpl barrier) {
89 BarrierTracing.restartMap.remove(barrier);
91 BarrierTracing.debuggerMap.put(barrier, new Debugger());
94 public static void dec(AsyncBarrierImpl barrier, int count) {
97 restartMap.put(barrier, new Exception());
99 debuggerMap.remove(barrier);
101 else if (count < 0) {
102 BarrierTracing.startMap.get(barrier).printStackTrace();
103 BarrierTracing.restartMap.get(barrier).printStackTrace();
104 new Exception().printStackTrace();
108 public static class Debugger {
110 public Map<AsyncBarrierImpl, String> infos = new HashMap<>();
112 public synchronized void inc(AsyncBarrierImpl id, String info) {
115 String exist = infos.get(id);
117 throw new IllegalStateException("Already existing info " + id + " " + info);
118 infos.put(id, exist);
121 public synchronized void dec(AsyncBarrierImpl id) {
124 String exist = infos.get(id);
126 System.err.println("No data for " + id);
133 public synchronized String toString() {
134 StringBuilder b = new StringBuilder();
135 for (String s : infos.values()) {
136 b.append("info " + s + "\r\n");
141 public boolean isEmpty() {
142 return infos.isEmpty();