X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.db.impl%2Fsrc%2Forg%2Fsimantics%2Fdb%2Fimpl%2Fgraph%2FAsyncBarrierImpl.java;h=b94ec6f6857cb0e0ca913ef920a5231c9ecc3577;hb=199e71d04a503618e59a1667ea3abee36be08732;hp=229c3d6a9e965b5b14167896334c2e83b1b2508a;hpb=9f0fd59be54719b1fe9322d8fd37e4950857308c;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/graph/AsyncBarrierImpl.java b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/graph/AsyncBarrierImpl.java index 229c3d6a9..b94ec6f68 100644 --- a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/graph/AsyncBarrierImpl.java +++ b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/graph/AsyncBarrierImpl.java @@ -11,172 +11,58 @@ *******************************************************************************/ package org.simantics.db.impl.graph; -import java.util.ArrayList; -import java.util.Arrays; import java.util.Collection; -import java.util.HashMap; import java.util.concurrent.atomic.AtomicInteger; import org.simantics.db.common.utils.Logger; import org.simantics.db.exception.RuntimeDatabaseException; +import org.simantics.db.impl.query.CacheEntry; import org.simantics.db.impl.query.QueryProcessor.AsyncBarrier; final public class AsyncBarrierImpl extends AtomicInteger implements AsyncBarrier { private static final long serialVersionUID = 4724463372850048672L; - static final HashMap> reverseLookup = new HashMap>(); - static final HashMap debuggerMap = new HashMap(); - static final HashMap restartMap = new HashMap(); - static final int WAIT_TIME = 600; - public static final boolean BOOKKEEPING = false; public static final boolean PRINT = false; - static final boolean RESTART_GUARD = false; - - final private AsyncBarrierImpl caller; - //private final Semaphore sema = new Semaphore(0); + final public AsyncBarrierImpl caller; - public AsyncBarrierImpl(AsyncBarrierImpl caller) { + public AsyncBarrierImpl(AsyncBarrierImpl caller, CacheEntry entry) { super(0); - if (BOOKKEEPING) { - synchronized (debuggerMap) { - debuggerMap.put(this, new Debugger()); - } - synchronized (reverseLookup) { - Collection barriers = reverseLookup - .get(caller); - if (barriers == null) { - barriers = new ArrayList(); - reverseLookup.put(caller, barriers); - } - barriers.add(this); - } - } this.caller = caller; + if (BarrierTracing.BOOKKEEPING) { + BarrierTracing.trace(this, entry); + } } - public class Debugger { - public HashMap> infos = new HashMap>(); - - public synchronized void inc(Object id, String info) { - if (id == null) - return; - ArrayList exist = infos.get(id); - if (exist == null) { - exist = new ArrayList(); - infos.put(id, exist); - } else { - // System.err.println("Appending " + id + " += " + info); - } - exist.add(info); - // String exist = infos.put(id, info); - // if(exist != null) System.err.println("replacing " + exist + - // " => " + info + " for " + id); - } - - public synchronized void dec(Object id) { - if (id == null) - return; - ArrayList exist = infos.get(id); - if (exist == null) { - System.err.println("No data for " + id); - } else { - exist.remove(0); - if (exist.isEmpty()) - infos.remove(id); - } - } - - @Override - public synchronized String toString() { - StringBuilder b = new StringBuilder(); - for (ArrayList ss : infos.values()) { - for (String s : ss) - b.append("info " + s + "\r\n"); - } - return b.toString(); - } - - public synchronized void toErr(int indent) { - char[] spaces = new char[indent]; - Arrays.fill(spaces, ' '); - for (ArrayList ss : infos.values()) { - for (String s : ss) { - if (!s.startsWith("#")) - continue; - StringBuilder b = new StringBuilder(); - b.append(spaces); - b.append(s); - System.err.println(b.toString()); - } - } - } - } public void inc() { - if (BOOKKEEPING) - inc(new Object(), new Exception().getStackTrace()[1].toString()); - else - inc(null, null); - + if(BarrierTracing.BOOKKEEPING) { + BarrierTracing.inc(this); + } else { + inc(null, null); + } + } - public void inc(String debug) { - - if (BOOKKEEPING) - inc(new Object(), new Exception().getStackTrace()[1].toString()); - else - inc(null, null); - - } - - public void inc(Object id, String info) { - - // if (PRINT) { - // if (get() < 5) - // new Exception("inc " + get() + " " + this).printStackTrace(); - // } - - if (BOOKKEEPING) { - Debugger debugger = debuggerMap.get(this); - if (debugger != null) - debugger.inc(id, info); - // StackTraceElement[] tr = new Exception().getStackTrace(); - // if(tr.length == 4) - // debugger.inc(new String[] { debug, tr[2].toString(), - // tr[3].toString() }); - // else if(tr.length == 5) - // debugger.inc(new String[] { debug, tr[2].toString(), - // tr[3].toString(), tr[4].toString() }); - // else if(tr.length == 6) - // debugger.inc(new String[] { debug, tr[2].toString(), - // tr[3].toString(), tr[4].toString(), tr[5].toString() }); - // else - // debugger.inc(new String[] { debug, tr[2].toString(), - // tr[3].toString(), tr[4].toString(), tr[5].toString(), - // tr[6].toString() }); - } - - // new Exception().printStackTrace(); + void inc(Object id, String info) { if(PRINT) { - System.err.println("inc barrier[" + get() + "] " + this); StackTraceElement[] elems = new Exception().getStackTrace(); for(int i=0;i<4;i++) System.err.println(elems[i]); - } if (incrementAndGet() == 1) { if (caller != null) { - if (BOOKKEEPING) - caller.inc(this, "Child"); - else - caller.inc(null, null); + if(BarrierTracing.BOOKKEEPING) { + caller.inc(this, "Child"); + } else { + caller.inc(null, null); + } } } @@ -192,10 +78,13 @@ final public class AsyncBarrierImpl extends AtomicInteger implements AsyncBarrie int count = decrementAndGet(); if (count < 1) { + if(BarrierTracing.BOOKKEEPING) { + BarrierTracing.dec(this, count); + } if (count == 0) { - if (caller != null) - caller.dec(this); - // sema.release(); + if (caller != null) { + caller.dec(); + } } if (count < 0) { Logger.defaultLogError( @@ -207,78 +96,25 @@ final public class AsyncBarrierImpl extends AtomicInteger implements AsyncBarrie } - public void dec(Object id) { - - if (PRINT) { - if (get() < 5) - new Exception("dec" + get() + " " + this).printStackTrace(); - } - - if (BOOKKEEPING) { - Debugger debugger = debuggerMap.get(this); - if (debugger != null) - debugger.dec(id); - // StackTraceElement[] tr = new Exception().getStackTrace(); - // if(tr.length == 3) - // debugger.dec(new String[] { debug, tr[2].toString() }); - // else if(tr.length == 4) - // debugger.dec(new String[] { debug, tr[2].toString(), - // tr[3].toString() }); - // else - // debugger.dec(new String[] { debug, tr[2].toString(), - // tr[3].toString(), tr[4].toString() }); - } - - // System.err.println("barrier " + this); - // StackTraceElement[] elems = new Exception().getStackTrace(); - // for(int i=0;i<3;i++) System.err.println(elems[i]); - // new Exception().printStackTrace(); - - int count = decrementAndGet(); - if (count < 1) { - if (count == 0) { - if (caller != null) - caller.dec(this); - if (RESTART_GUARD) - restartMap.put(this, true); - // sema.release(); - // if(DEBUGGER) { - // debuggerMap.remove(this); - // } - // if(REVERSE_LOOKUP) { - // reverseLookup.remove(this); - // } - } - if (count < 0) { - Logger.defaultLogError( - "Database request processing error. The application code has performed illegal actions (probably called multiple times the execute or exception method of a single result request.", - new Exception()); - // String message = ; - // System.out.println(message); - // if (DEBUGGER) { - // JOptionPane.showMessageDialog(null, message); - // System.out.println(debugger); - // } - // sema.release(); - System.exit(-1); - } - assert (count >= 0); - } + public static String report(AsyncBarrierImpl barrier) { + CacheEntry e = BarrierTracing.entryMap.get(barrier); + if(e != null) return e.toString(); + else return "Barrier@" + System.identityHashCode(barrier); } - - private static void printReverse(AsyncBarrierImpl barrier, int indent) { + + public static void printReverse(AsyncBarrierImpl barrier, int indent) { if (barrier.get() == 0) return; for (int i = 0; i < indent; i++) System.err.print(" "); - System.err.println("[" + barrier.get() + " requests]: " + barrier); - if (BOOKKEEPING) { - Debugger debugger = debuggerMap.get(barrier); - debugger.toErr(indent + 2); - } + System.err.println("[" + barrier.get() + " requests]: " + report(barrier)); +// if (BOOKKEEPING) { +// Debugger debugger = debuggerMap.get(barrier); +// debugger.toErr(indent + 2); +// } - Collection children = reverseLookup.get(barrier); + Collection children = BarrierTracing.reverseLookup.get(barrier); if (children != null) { for (AsyncBarrierImpl child : children) printReverse(child, indent + 2); @@ -313,12 +149,10 @@ final public class AsyncBarrierImpl extends AtomicInteger implements AsyncBarrie + ") is taking long to execute, so far " + (waitCount / 1000) + " s."); - if (BOOKKEEPING) { - - synchronized (reverseLookup) { + if (BarrierTracing.BOOKKEEPING) { + synchronized (BarrierTracing.reverseLookup) { printReverse(this, 0); } - } // if(Development.DEVELOPMENT) { @@ -345,12 +179,9 @@ final public class AsyncBarrierImpl extends AtomicInteger implements AsyncBarrie public void restart() { assertReady(); - // log.clear(); - // sema.drainPermits(); - if (RESTART_GUARD) - restartMap.remove(this); - if (BOOKKEEPING) - debuggerMap.put(this, new Debugger()); + if(BarrierTracing.BOOKKEEPING) { + BarrierTracing.restart(this); + } } public void assertReady() { @@ -374,8 +205,9 @@ final public class AsyncBarrierImpl extends AtomicInteger implements AsyncBarrie @Override public String toString() { - return "AsyncBarrierImpl@" + System.identityHashCode(this) - + " - counter = " + get() + " - caller = " + caller; + return report(this); +// return "AsyncBarrierImpl@" + System.identityHashCode(this) +// + " - counter = " + get() + " - caller = " + caller; } }