Skip to content

Distributed Peer-to-Peer Web Search Engine and Intranet Search Appliance

added new feature for Thread dump: "THREADS WITH STATES: LOCK FOR OTHERS" will show only such threads that lock other threads. This is the 'opposite part' of the blocked threads. Because that this uses a thread dump that is produced with a kill -3 on the PID of the process and such thread dumps are written by the Java core outside of System.out and Sytem.err it is necessary to read the dump from a log in the file system. Such a log is only written if YaCy is started with startYACY.sh on a linux system. That means: this feature is only available on linux and Mac OS X if YaCy is started with ./startYACY.sh -l

orbiter0a117273745f
diff --git a/htroot/Threaddump_p.java b/htroot/Threaddump_p.java
index 47e5e282b..c6ca134c7 100644
--- a/htroot/Threaddump_p.java
+++ b/htroot/Threaddump_p.java
@@ -34,7 +34,6 @@ import java.util.ArrayList;
import net.yacy.cora.protocol.RequestHeader;
import net.yacy.kelondro.logging.ThreadDump;
-import net.yacy.kelondro.util.OS;
import de.anomic.search.Switchboard;
import de.anomic.server.serverObjects;
@@ -83,10 +82,17 @@ public class Threaddump_p {
ThreadDump.appendStackTraceStats(appPath, buffer, traces, plain, null);
} else {
// write a thread dump to standard error output
+ /*
if (OS.canExecUnix) {
int pid = OS.getPID();
if (pid >= 0) try {OS.execSynchronous("kill -3 " + pid);} catch (IOException e) {}
}
+ */
+ try {
+ new ThreadDump().appendBlockTraces(buffer, plain);
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
// generate a single thread dump
final Map<Thread,StackTraceElement[]> stackTraces = ThreadDump.getAllStackTraces();
diff --git a/source/net/yacy/kelondro/logging/ThreadDump.java b/source/net/yacy/kelondro/logging/ThreadDump.java
index 4ab129c88..4bbe9541d 100644
--- a/source/net/yacy/kelondro/logging/ThreadDump.java
+++ b/source/net/yacy/kelondro/logging/ThreadDump.java
@@ -26,11 +26,13 @@
package net.yacy.kelondro.logging;
import java.io.BufferedReader;
+import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
+import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@@ -92,45 +94,38 @@ public class ThreadDump extends HashMap<ThreadDump.Thread, List<String>> impleme
public static Map<java.lang.Thread, StackTraceElement[]> getAllStackTraces() {
return java.lang.Thread.getAllStackTraces();
}
- /*
- public ThreadDump() {
+
+ public ThreadDump() throws IOException {
super();
- VirtualMachineManager manager = Bootstrap.virtualMachineManager();
- AttachingConnector ac = manager.attachingConnectors().get(0);
- Map<String, Connector.Argument> env = ac.defaultArguments();
- env.get("port").setValue("8066");
- env.get("hostname").setValue("localhost");
- VirtualMachine vm = null;
- try {
- vm = ac.attach(env);
- } catch (IOException e1) {
- e1.printStackTrace();
- } catch (IllegalConnectorArgumentsException e1) {
- e1.printStackTrace();
- }
-
- Process proc = vm.process();
- InputStream is = proc.getInputStream();
-
+ // try to get the thread dump from yacy.log which is available when YaCy is started with
+ // startYACY.sh -l
+
+ File logFile = new File("yacy.log");
+ if (!logFile.exists()) return;
+ long sizeBefore = logFile.length();
+
// get the current process PID
int pid = OS.getPID();
// call kill -3 on the pid
if (pid >= 0) try {OS.execSynchronous("kill -3 " + pid);} catch (IOException e) {}
-
- //byte[] b = os.toByteArray();
- //System.err.println("dump size: " + b.length + ", log out lines = " + o.size());
+
+ // read the log from the dump
+ long sizeAfter = logFile.length();
+ if (sizeAfter <= sizeBefore) return;
+
+ RandomAccessFile raf = new RandomAccessFile(logFile, "r");
+ raf.seek(sizeBefore);
+ byte[] b = new byte[(int) (sizeAfter - sizeBefore)];
+ raf.readFully(b);
+ raf.close();
+ System.out.println("dump size: " + b.length);
// import the thread dump;
- try {
- importText(is);
- } catch (IOException e) {
- e.printStackTrace();
- }
-
+ importText(new ByteArrayInputStream(b));
}
- */
+
public ThreadDump(final File f) throws IOException {
this(new FileInputStream(f));
}
@@ -245,6 +240,29 @@ public class ThreadDump extends HashMap<ThreadDump.Thread, List<String>> impleme
}
bufferappend(buffer, plain, "");
}
+
+ public void appendBlockTraces(
+ final StringBuilder buffer,
+ final boolean plain) {
+ bufferappend(buffer, plain, "THREADS WITH STATES: LOCK FOR OTHERS");
+ bufferappend(buffer, plain, "");
+
+ Map<Thread, Integer> locks = this.countLocks();
+ for (int i = 0; i < this.size() + 10; i++) {
+ for (Map.Entry<Thread, Integer> entry: locks.entrySet()) {
+ if (entry.getValue().intValue() == i) {
+ bufferappend(buffer, plain, "holds lock for " + i + " threads:");
+ final List<String> list = this.get(entry.getKey());
+ if (list == null) continue;
+ bufferappend(buffer, plain, "Thread: " + entry.getKey());
+ for (String s: list) bufferappend(buffer, plain, " " + s);
+ bufferappend(buffer, plain, "");
+ }
+ }
+ }
+ bufferappend(buffer, plain, "");
+ }
+
public static void appendStackTraceStats(
final File rootPath,