]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/graph/BarrierTracing.java
Multiple readers in db client
[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.BlockingAsyncProcedure;
9 import org.simantics.db.impl.query.CacheEntry;
10 import org.simantics.db.impl.query.QueryProcessor.SessionTask;
11
12 public class BarrierTracing {
13
14     public static final boolean BOOKKEEPING = false;
15     static final boolean RESTART_GUARD = BOOKKEEPING && false;
16
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<>();
24
25     synchronized public static void registerBAP(BlockingAsyncProcedure bap) {
26         baps.put(bap, new Exception());
27     }
28     
29     synchronized public static void unregisterBAP(BlockingAsyncProcedure bap) {
30         baps.remove(bap);
31     }
32
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);
44             e.printStackTrace();
45         }
46     }
47     
48     public static void trace(AsyncBarrierImpl barrier, CacheEntry<?> entry) {
49
50         if (RESTART_GUARD) {
51             synchronized (startMap) {
52                 startMap.put(barrier, new Exception());
53             }
54         }
55         synchronized (entryMap) {
56             entryMap.put(barrier, entry);
57         }
58         synchronized (debuggerMap) {
59             debuggerMap.put(barrier, new Debugger());
60         }
61         synchronized (reverseLookup) {
62             Collection<AsyncBarrierImpl> barriers = reverseLookup
63                     .get(barrier.caller);
64             if (barriers == null) {
65                 barriers = new ArrayList<AsyncBarrierImpl>();
66                 reverseLookup.put(barrier.caller, barriers);
67             }
68             barriers.add(barrier);
69         }
70
71     }
72
73     public static void inc(AsyncBarrierImpl barrier) {
74
75         barrier.inc(barrier, new Exception().getStackTrace()[2].toString());
76
77         if (RESTART_GUARD)
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");
83             }
84
85
86     }
87
88     public static void restart(AsyncBarrierImpl barrier) {
89         if (RESTART_GUARD)
90             BarrierTracing.restartMap.remove(barrier);
91         if (BOOKKEEPING)
92             BarrierTracing.debuggerMap.put(barrier, new Debugger());
93     }
94
95     public static void dec(AsyncBarrierImpl barrier, int count) {
96         if (count == 0) {
97             if (RESTART_GUARD) {
98                 restartMap.put(barrier, new Exception());
99             }
100             debuggerMap.remove(barrier);
101         }
102         else if (count < 0) {
103             BarrierTracing.startMap.get(barrier).printStackTrace();
104             BarrierTracing.restartMap.get(barrier).printStackTrace();
105             new Exception().printStackTrace();
106         }
107     }
108
109     public static class Debugger {
110
111         public Map<AsyncBarrierImpl, String> infos = new HashMap<>();
112
113         public synchronized void inc(AsyncBarrierImpl id, String info) {
114             if (id == null)
115                 return;
116             String exist = infos.get(id);
117             if (exist != null)
118                 throw new IllegalStateException("Already existing info " + id + " " + info);
119             infos.put(id, exist);
120         }
121
122         public synchronized void dec(AsyncBarrierImpl id) {
123             if (id == null)
124                 return;
125             String exist = infos.get(id);
126             if (exist == null) {
127                 System.err.println("No data for " + id);
128             } else {
129                 infos.remove(id);
130             }
131         }
132
133         @Override
134         public synchronized String toString() {
135             StringBuilder b = new StringBuilder();
136             for (String s : infos.values()) {
137                 b.append("info " + s + "\r\n");
138             }
139             return b.toString();
140         }
141
142         public boolean isEmpty() {
143             return infos.isEmpty();
144         }
145
146     }
147
148 }