import java.util.*;
import java.util.zip.*;
import java.util.List;
import java.util.regex.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.*;
import java.util.concurrent.locks.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import javax.swing.table.*;
import java.io.*;
import java.net.*;
import java.lang.reflect.*;
import java.lang.ref.*;
import java.lang.management.*;
import java.security.*;
import java.security.spec.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.imageio.*;
import java.math.*;
import java.text.NumberFormat;
import static x30_pkg.x30_util.DynamicObject;
class main {
static void profileToConsole(Runnable r) {
poorMansProfileToConsole(r);
}
static A profileToConsole(IF0 r) {
if (r == null) return null;
Var var = new Var();
poorMansProfileToConsole(new Runnable() { public void run() { try { var.set(r.get()) ;
} catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "var.set(r!)"; }});
return var.get();
}
static void poorMansProfileToConsole(Runnable r) {
poorMansProfiling();
try {
r.run();
} finally {
poorMansProfiling_stopAndPrintResults();
}
}
static RuntimeException rethrow(Throwable t) {
if (t instanceof Error)
_handleError((Error) t);
throw t instanceof RuntimeException ? (RuntimeException) t : new RuntimeException(t);
}
static RuntimeException rethrow(String msg, Throwable t) {
throw new RuntimeException(msg, t);
}
static MultiSet poorMansProfiling_results = new MultiSet();
static int poorMansProfiling_samples;
static java.util.Timer poorMansProfiling_timer;
static Lock poorMansProfiling_lock = lock();
static Thread poorMansProfiling_threadToSample; // otherwise sample all threads
static void poorMansProfiling() {
poorMansProfiling(poorMansProfiling_defaultInterval());
}
static void poorMansProfiling(Thread thread) {
poorMansProfiling(poorMansProfiling_defaultInterval(), thread);
}
static void poorMansProfiling(int interval) {
poorMansProfiling(interval, null);
}
static void poorMansProfiling(int interval, Thread thread) {
Lock __0 = poorMansProfiling_lock; lock(__0); try {
poorMansProfiling_threadToSample = thread;
poorMansProfiling_stop();
poorMansProfiling_clear();
poorMansProfiling_timer = doEvery_daemon(interval, new Runnable() { public void run() { try {
Map map =
poorMansProfiling_threadToSample != null
? litmap(poorMansProfiling_threadToSample, poorMansProfiling_threadToSample.getStackTrace())
: runnableThreadsWithStackTraces();
Lock __1 = poorMansProfiling_lock; lock(__1); try {
poorMansProfiling_samples++;
for (Thread t : keys(map)) {
{ if (isSystemThread(t)) continue; }
StringBuilder buf = new StringBuilder();
for (StackTraceElement e : map.get(t))
buf.append(e).append("\n");
poorMansProfiling_results.add(str(buf));
}
} finally { unlock(__1); }
} catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "Map map =\r\n poorMansProfiling_threadToSampl..."; }});
} finally { unlock(__0); } }
static void poorMansProfiling_stop() {
Lock __2 = poorMansProfiling_lock; lock(__2); try {
if (poorMansProfiling_timer != null) {
stopTimer(poorMansProfiling_timer);
poorMansProfiling_timer = null;
}
} finally { unlock(__2); } }
static void poorMansProfiling_clear() {
Lock __3 = poorMansProfiling_lock; lock(__3); try {
poorMansProfiling_results.clear();
poorMansProfiling_samples = 0;
} finally { unlock(__3); } }
static MultiSet poorMansProfiling_results() {
return new MultiSet(poorMansProfiling_results);
}
static MultiSet poorMansProfiling_stopAndGetResults() {
Lock __4 = poorMansProfiling_lock; lock(__4); try {
poorMansProfiling_stop();
return poorMansProfiling_results();
} finally { unlock(__4); } }
static void poorMansProfiling_stopAndPrintResults() {
poorMansProfiling_renderFullResults_backwards.set(true);
print(poorMansProfiling_stopAndRenderResults());
}
static void _handleError(Error e) {
call(javax(), "_handleError", e);
}
static void lock(Lock lock) { try {
ping();
if (lock == null) return;
try {
vmBus_send("locking", lock, "thread" , currentThread());
lock.lockInterruptibly();
vmBus_send("locked", lock, "thread" , currentThread());
} catch (InterruptedException e) {
Object reason = vm_threadInterruptionReasonsMap().get(currentThread());
print("Locking interrupted! Reason: " + strOr(reason, "Unknown"));
printStackTrace(e);
rethrow(e);
}
// NO call to ping here! Make sure lock is always released.
} catch (Exception __e) { throw rethrow(__e); } }
static void lock(Lock lock, String msg) {
print("Locking: " + msg);
lock(lock);
}
static void lock(Lock lock, String msg, long timeout) {
print("Locking: " + msg);
lockOrFail(lock, timeout);
}
static ReentrantLock lock() {
return fairLock();
}
// in ms
static int poorMansProfiling_defaultInterval() {
return 25;
}
// firstDelay = delay
static FixedRateTimer doEvery_daemon(long delay, final Object r) { return doEvery_daemon(defaultTimerName(), delay, r); }
static FixedRateTimer doEvery_daemon(String timerName, long delay, final Object r) {
return doEvery_daemon(timerName, delay, delay, r);
}
static FixedRateTimer doEvery_daemon(long delay, long firstDelay, final Object r) { return doEvery_daemon(defaultTimerName(), delay, firstDelay, r); }
static FixedRateTimer doEvery_daemon(String timerName, long delay, long firstDelay, final Object r) {
FixedRateTimer timer = new FixedRateTimer(true);
timer.scheduleAtFixedRate(smartTimerTask(r, timer, delay), firstDelay, delay);
return timer;
}
static FixedRateTimer doEvery_daemon(double delaySeconds, final Object r) {
return doEvery_daemon(toMS(delaySeconds), r);
}
static HashMap litmap(Object... x) {
HashMap map = new HashMap();
litmap_impl(map, x);
return map;
}
static void litmap_impl(Map map, Object... x) {
if (x != null) for (int i = 0; i < x.length-1; i += 2)
if (x[i+1] != null)
map.put(x[i], x[i+1]);
}
static Map runnableThreadsWithStackTraces() {
Map map = filterMap((__22, __23) -> isThreadRunnable_x(__22, __23), Thread.getAllStackTraces());
map.remove(currentThread());
return map;
}
static Set keys(Map map) {
return map == null ? new HashSet() : map.keySet();
}
// convenience shortcut for keys_gen
static Set keys(Object map) {
return keys((Map) map);
}
static Set keys(MultiSet ms) {
return ms.keySet();
}
static Set keys(MultiMap mm) {
return mm.keySet();
}
static boolean isSystemThread(Thread t) {
ThreadGroup g = t.getThreadGroup();
return g != null && g.getName().equals("system");
}
static String str(Object o) {
return o == null ? "null" : o.toString();
}
static String str(char[] c) {
return new String(c);
}
static void unlock(Lock lock, String msg) {
if (lock == null) return;
lock.unlock();
vmBus_send("unlocked", lock, "thread" , currentThread());
print("Unlocked: " + msg); // print afterwards to make sure the lock is always unlocked
}
static void unlock(Lock lock) {
if (lock == null) return;
lock.unlock();
vmBus_send("unlocked", lock, "thread" , currentThread());
}
static void stopTimer(java.util.Timer timer) {
if (timer != null) timer.cancel();
}
static volatile StringBuffer local_log = new StringBuffer(); // not redirected
static volatile Appendable print_log = local_log; // might be redirected, e.g. to main bot
// in bytes - will cut to half that
static volatile int print_log_max = 1024*1024;
static volatile int local_log_max = 100*1024;
static boolean print_silent = false; // total mute if set
static Object print_byThread_lock = new Object();
static volatile ThreadLocal