From e343ee140bea729b938b0f9033403ef5990f7c0b Mon Sep 17 00:00:00 2001 From: michaeloffner Date: Mon, 16 Dec 2024 16:33:32 +0100 Subject: [PATCH] improve loading bundles --- .../java/lucee/loader/util/log/Logging.java | 88 +++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 loader/src/main/java/lucee/loader/util/log/Logging.java diff --git a/loader/src/main/java/lucee/loader/util/log/Logging.java b/loader/src/main/java/lucee/loader/util/log/Logging.java new file mode 100644 index 0000000000..bcb95337cd --- /dev/null +++ b/loader/src/main/java/lucee/loader/util/log/Logging.java @@ -0,0 +1,88 @@ +package lucee.loader.util.log; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.Map.Entry; + +import lucee.loader.util.Util; + +public class Logging { + + public static void dumpThreadPositions(File target) throws IOException { + StackTraceElement[] stes; + String line; + List elements; + for (Entry e: Thread.getAllStackTraces().entrySet()) { + stes = e.getValue(); + if (stes == null || stes.length == 0) continue; + elements = new ArrayList<>(); + for (int i = 0; i < stes.length; i++) { + // if (stes[i].getLineNumber() > 0) { + elements.add(stes[i]); + // } + } + if (elements.size() == 0) continue; + // print.e(stes); + line = "{\"stack\":["; + String del = ""; + for (StackTraceElement ste: elements) { + line += (del + "\"" + ste.getClassName() + "." + (Util.isEmpty(ste.getMethodName(), true) ? "" : ste.getMethodName()) + "():" + ste.getLineNumber() + "\""); + del = ","; + } + + line += "],\"thread\":\"" + e.getKey().getName() + "\",\"id\":" + e.getKey().getId() + ",\"time\":" + System.currentTimeMillis() + "}\n"; + Util.write(target, line, Util.UTF8, true); + } + } + + public static void startupLog() { + String dumpPath = Util.getSystemPropOrEnvVar("lucee.dump.threads", null); + if (!Util.isEmpty(dumpPath, true)) { + + long start = System.currentTimeMillis(); + + int tmp = 100; + try { + tmp = Integer.parseInt(Util.getSystemPropOrEnvVar("lucee.dump.threads.interval", null)); + } + catch (Throwable e) { + } + final int interval = tmp; + + tmp = 10000; + try { + tmp = Integer.parseInt(Util.getSystemPropOrEnvVar("lucee.dump.threads.max", null)); + } + catch (Throwable e) { + } + int max = tmp; + + // Create a new thread to run the task + Thread thread = new Thread(() -> { + while (true) { + try { + if ((start + max) < System.currentTimeMillis()) { + break; + } + // Call the dumpThreadPositions method + File target = new File(dumpPath); + dumpThreadPositions(target); + + // Pause for the specified interval + if (interval > 0) Util.sleep(interval); + } + catch (IOException e) { + Util.sleep(1000); + } + } + }); + + // Start the thread + thread.start(); + } + + } + +}