fix for thread dump

git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@7992 6c8d7289-2bf4-0310-a012-ef5d649a1542
pull/1/head
orbiter 14 years ago
parent 3f606407bc
commit 7598a9e26b

@ -36,6 +36,7 @@ import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import net.yacy.document.parser.html.CharacterCoding; import net.yacy.document.parser.html.CharacterCoding;
@ -53,15 +54,15 @@ public class ThreadDump extends HashMap<ThreadDump.StackTrace, List<String>> imp
public static class StackTrace { public static class StackTrace {
public String text; public String text;
public Thread.State state; public Thread.State state;
public StackTrace(final String text, Thread.State state) { public StackTrace(final String text, final Thread.State state) {
this.state = state; this.state = state;
this.text = text; this.text = text;
} }
@Override @Override
public boolean equals(Object a) { public boolean equals(final Object a) {
return (a != null && a instanceof StackTrace && this.text.equals(((StackTrace) a).text)); return (a != null && a instanceof StackTrace && this.text.equals(((StackTrace) a).text));
} }
public boolean equals(StackTrace a) { public boolean equals(final StackTrace a) {
return (a != null && this.text.equals(a.text)); return (a != null && this.text.equals(a.text));
} }
@Override @Override
@ -80,10 +81,10 @@ public class ThreadDump extends HashMap<ThreadDump.StackTrace, List<String>> imp
this.id = name; this.id = name;
} }
@Override @Override
public boolean equals(Object a) { public boolean equals(final Object a) {
return (a != null && a instanceof Lock && this.id.equals(((Lock) a).id)); return (a != null && a instanceof Lock && this.id.equals(((Lock) a).id));
} }
public boolean equals(Lock a) { public boolean equals(final Lock a) {
return (a != null && this.id.equals(a.id)); return (a != null && this.id.equals(a.id));
} }
@Override @Override
@ -100,7 +101,7 @@ public class ThreadDump extends HashMap<ThreadDump.StackTrace, List<String>> imp
return Thread.getAllStackTraces(); return Thread.getAllStackTraces();
} }
public static boolean canProduceLockedBy(File logFile) { public static boolean canProduceLockedBy(final File logFile) {
// check os version // check os version
if (!OS.canExecUnix) return false; if (!OS.canExecUnix) return false;
@ -114,7 +115,7 @@ public class ThreadDump extends HashMap<ThreadDump.StackTrace, List<String>> imp
return true; return true;
} }
public ThreadDump(File logFile) throws IOException { public ThreadDump(final File logFile) throws IOException {
super(); super();
// try to get the thread dump from yacy.log which is available when YaCy is started with // try to get the thread dump from yacy.log which is available when YaCy is started with
@ -124,19 +125,19 @@ public class ThreadDump extends HashMap<ThreadDump.StackTrace, List<String>> imp
sizeBefore = logFile.length(); sizeBefore = logFile.length();
// get the current process PID // get the current process PID
int pid = OS.getPID(); final int pid = OS.getPID();
// call kill -3 on the pid // call kill -3 on the pid
if (pid >= 0) try {OS.execSynchronous("kill -3 " + pid);} catch (IOException e) {} if (pid >= 0) try {OS.execSynchronous("kill -3 " + pid);} catch (final IOException e) {}
} }
// read the log from the dump // read the log from the dump
long sizeAfter = logFile.length(); final long sizeAfter = logFile.length();
if (sizeAfter <= sizeBefore) return; if (sizeAfter <= sizeBefore) return;
RandomAccessFile raf = new RandomAccessFile(logFile, "r"); final RandomAccessFile raf = new RandomAccessFile(logFile, "r");
raf.seek(sizeBefore); raf.seek(sizeBefore);
byte[] b = new byte[(int) (sizeAfter - sizeBefore)]; final byte[] b = new byte[(int) (sizeAfter - sizeBefore)];
raf.readFully(b); raf.readFully(b);
raf.close(); raf.close();
@ -153,13 +154,13 @@ public class ThreadDump extends HashMap<ThreadDump.StackTrace, List<String>> imp
public static Thread.State threadState(String line) { public static Thread.State threadState(String line) {
int p = line.indexOf(statestatement); int p = line.indexOf(statestatement);
if (p >= 0) line = line.substring(p + statestatement.length()).trim(); if (p < 0) return null;
line = line.substring(p + statestatement.length()).trim();
p = line.indexOf(' '); p = line.indexOf(' ');
if (p >= 0) line = line.substring(0, p).trim(); if (p >= 0) line = line.substring(0, p).trim();
try { try {
return Thread.State.valueOf(line); return Thread.State.valueOf(line);
} catch (IllegalArgumentException e) { } catch (final IllegalArgumentException e) {
System.out.println("wong state: " + line);
return null; return null;
} }
} }
@ -171,16 +172,13 @@ public class ThreadDump extends HashMap<ThreadDump.StackTrace, List<String>> imp
int p; int p;
List<String> list = new ArrayList<String>(); List<String> list = new ArrayList<String>();
Thread.State state = null; Thread.State state = null;
Thread.State state0;
while ((line = br.readLine()) != null) { while ((line = br.readLine()) != null) {
p = line.indexOf(statestatement); state0 = threadState(line);
if (p >= 0) try { if (state0 != null) state = state0;
state = Thread.State.valueOf(line.substring(p + statestatement.length()).trim());
} catch (IllegalArgumentException e) {
System.out.println("wong state: " + line);
}
if (line.isEmpty()) { if (line.isEmpty()) {
if (thread != null) { if (thread != null) {
this.put(new ThreadDump.StackTrace(thread, state), list); put(new ThreadDump.StackTrace(thread, state), list);
} }
list = new ArrayList<String>(); list = new ArrayList<String>();
thread = null; thread = null;
@ -198,7 +196,7 @@ public class ThreadDump extends HashMap<ThreadDump.StackTrace, List<String>> imp
} }
// recognize last thread // recognize last thread
if (thread != null) { if (thread != null) {
this.put(new ThreadDump.StackTrace(thread, state), list); put(new ThreadDump.StackTrace(thread, state), list);
} }
} }
@ -220,7 +218,7 @@ public class ThreadDump extends HashMap<ThreadDump.StackTrace, List<String>> imp
String tracename = ""; String tracename = "";
File classFile; File classFile;
if ((stateIn == null || stateIn.equals(thread.getState())) && stackTraceElements.length > 0) { if ((stateIn == null || stateIn.equals(thread.getState())) && stackTraceElements.length > 0) {
StringBuilder sb = new StringBuilder(3000); final StringBuilder sb = new StringBuilder(3000);
if (plain) { if (plain) {
classFile = getClassFile(classPath, stackTraceElements[stackTraceElements.length - 1].getClassName()); classFile = getClassFile(classPath, stackTraceElements[stackTraceElements.length - 1].getClassName());
tracename = classFile.getName(); tracename = classFile.getName();
@ -229,7 +227,7 @@ public class ThreadDump extends HashMap<ThreadDump.StackTrace, List<String>> imp
while (tracename.length() < 20) tracename = tracename + "_"; while (tracename.length() < 20) tracename = tracename + "_";
tracename = "[" + tracename + "] "; tracename = "[" + tracename + "] ";
} }
String threadtitle = tracename + "Thread= " + thread.getName() + " " + (thread.isDaemon()?"daemon":"") + " id=" + thread.getId() + " " + thread.getState().toString(); final String threadtitle = tracename + "Thread= " + thread.getName() + " " + (thread.isDaemon()?"daemon":"") + " id=" + thread.getId() + " " + thread.getState().toString();
String className; String className;
boolean cutcore = true; boolean cutcore = true;
for (int i = 0; i < stackTraceElements.length; i++) { for (int i = 0; i < stackTraceElements.length; i++) {
@ -253,17 +251,17 @@ public class ThreadDump extends HashMap<ThreadDump.StackTrace, List<String>> imp
} }
} }
final String threaddump = sb.toString(); final String threaddump = sb.toString();
List<String> threads = this.get(threaddump); List<String> threads = get(threaddump);
if (threads == null) threads = new ArrayList<String>(); if (threads == null) threads = new ArrayList<String>();
Thread.State state = null; Thread.State state = null;
for (String t: threads) { for (final String t: threads) {
int p = t.indexOf(statestatement); final int p = t.indexOf(statestatement);
if (p >= 0) { if (p >= 0) {
state = Thread.State.valueOf(t.substring(p + statestatement.length()).trim()); state = Thread.State.valueOf(t.substring(p + statestatement.length()).trim());
} }
} }
threads.add(threadtitle); threads.add(threadtitle);
this.put(new StackTrace(threaddump, state), threads); put(new StackTrace(threaddump, state), threads);
} }
} }
} }
@ -276,8 +274,8 @@ public class ThreadDump extends HashMap<ThreadDump.StackTrace, List<String>> imp
bufferappend(buffer, plain, ""); bufferappend(buffer, plain, "");
// write dumps // write dumps
for (final Entry<StackTrace, List<String>> entry: this.entrySet()) { for (final Entry<StackTrace, List<String>> entry: entrySet()) {
List<String> threads = entry.getValue(); final List<String> threads = entry.getValue();
for (final String t: threads) bufferappend(buffer, plain, t); for (final String t: threads) bufferappend(buffer, plain, t);
bufferappend(buffer, plain, entry.getKey().text); bufferappend(buffer, plain, entry.getKey().text);
bufferappend(buffer, plain, ""); bufferappend(buffer, plain, "");
@ -291,15 +289,15 @@ public class ThreadDump extends HashMap<ThreadDump.StackTrace, List<String>> imp
bufferappend(buffer, plain, "THREADS WITH STATES: LOCK FOR OTHERS"); bufferappend(buffer, plain, "THREADS WITH STATES: LOCK FOR OTHERS");
bufferappend(buffer, plain, ""); bufferappend(buffer, plain, "");
Map<StackTrace, Integer> locks = this.countLocks(); final Map<StackTrace, Integer> locks = countLocks();
for (int i = this.size() + 10; i > 0; i--) { for (int i = size() + 10; i > 0; i--) {
for (Map.Entry<StackTrace, Integer> entry: locks.entrySet()) { for (final Map.Entry<StackTrace, Integer> entry: locks.entrySet()) {
if (entry.getValue().intValue() == i) { if (entry.getValue().intValue() == i) {
bufferappend(buffer, plain, "holds lock for " + i + " threads:"); bufferappend(buffer, plain, "holds lock for " + i + " threads:");
final List<String> list = this.get(entry.getKey()); final List<String> list = get(entry.getKey());
if (list == null) continue; if (list == null) continue;
bufferappend(buffer, plain, "Thread= " + entry.getKey()); bufferappend(buffer, plain, "Thread= " + entry.getKey());
for (String s: list) bufferappend(buffer, plain, " " + (plain ? s : s.replaceAll("<", "&lt;").replaceAll(">", "&gt;"))); for (final String s: list) bufferappend(buffer, plain, " " + (plain ? s : s.replaceAll("<", "&lt;").replaceAll(">", "&gt;")));
bufferappend(buffer, plain, ""); bufferappend(buffer, plain, "");
} }
} }
@ -376,7 +374,7 @@ public class ThreadDump extends HashMap<ThreadDump.StackTrace, List<String>> imp
public List<Map.Entry<StackTrace, List<String>>> freerun() { public List<Map.Entry<StackTrace, List<String>>> freerun() {
final List<Map.Entry<StackTrace, List<String>>> runner = new ArrayList<Map.Entry<StackTrace, List<String>>>(); final List<Map.Entry<StackTrace, List<String>>> runner = new ArrayList<Map.Entry<StackTrace, List<String>>>();
runf: for (final Map.Entry<StackTrace, List<String>> entry: this.entrySet()) { runf: for (final Map.Entry<StackTrace, List<String>> entry: entrySet()) {
// check if the thread is locked or holds a lock // check if the thread is locked or holds a lock
if (entry.getKey().state != Thread.State.RUNNABLE) continue runf; if (entry.getKey().state != Thread.State.RUNNABLE) continue runf;
for (final String s: entry.getValue()) { for (final String s: entry.getValue()) {
@ -394,7 +392,7 @@ public class ThreadDump extends HashMap<ThreadDump.StackTrace, List<String>> imp
public Map<Lock, StackTrace> locks() { public Map<Lock, StackTrace> locks() {
int p; int p;
final Map<Lock, StackTrace> locks = new HashMap<Lock, StackTrace>(); final Map<Lock, StackTrace> locks = new HashMap<Lock, StackTrace>();
for (final Map.Entry<StackTrace, List<String>> entry: this.entrySet()) { for (final Map.Entry<StackTrace, List<String>> entry: entrySet()) {
for (final String s: entry.getValue()) { for (final String s: entry.getValue()) {
if ((p = s.indexOf("locked <")) > 0) { if ((p = s.indexOf("locked <")) > 0) {
locks.put(new Lock(s.substring(p + 8, s.indexOf('>'))), entry.getKey()); locks.put(new Lock(s.substring(p + 8, s.indexOf('>'))), entry.getKey());
@ -412,7 +410,7 @@ public class ThreadDump extends HashMap<ThreadDump.StackTrace, List<String>> imp
*/ */
public Lock lockedBy(final StackTrace threadName) { public Lock lockedBy(final StackTrace threadName) {
int p; int p;
final List<String> list = this.get(threadName); final List<String> list = get(threadName);
if (list == null) return null; if (list == null) return null;
for (final String s: list) { for (final String s: list) {
if ((p = s.indexOf("<")) > 0 && s.indexOf("locked <") < 0) { if ((p = s.indexOf("<")) > 0 && s.indexOf("locked <") < 0) {
@ -428,45 +426,45 @@ public class ThreadDump extends HashMap<ThreadDump.StackTrace, List<String>> imp
for (final Map.Entry<Lock, StackTrace> entry: locks.entrySet()) { for (final Map.Entry<Lock, StackTrace> entry: locks.entrySet()) {
// look where the lock has an effect // look where the lock has an effect
int c = 0; int c = 0;
for (final StackTrace thread: this.keySet()) if (entry.getKey().equals(lockedBy(thread))) c++; for (final StackTrace thread: keySet()) if (entry.getKey().equals(lockedBy(thread))) c++;
if (c > 0) count.put(entry.getValue(), c); if (c > 0) count.put(entry.getValue(), c);
} }
return count; return count;
} }
public void print() { public void print() {
for (final StackTrace thread: this.keySet()) print(thread); for (final StackTrace thread: keySet()) print(thread);
} }
public void print(StackTrace thread) { public void print(final StackTrace thread) {
final List<String> list = this.get(thread); final List<String> list = get(thread);
if (list == null) return; if (list == null) return;
System.out.println("Thread: " + thread); System.out.println("Thread: " + thread);
for (String s: list) System.out.println(" " + s); for (final String s: list) System.out.println(" " + s);
System.out.println(""); System.out.println("");
} }
public static void main(String[] args) { public static void main(final String[] args) {
ThreadDump dump = null; ThreadDump dump = null;
if (args.length == 0) { if (args.length == 0) {
//dump = new ThreadDump(); //dump = new ThreadDump();
} }
if (args.length == 2 && args[0].equals("-f")) { if (args.length == 2 && args[0].equals("-f")) {
File dumpfile = new File(args[1]); final File dumpfile = new File(args[1]);
try { try {
dump = new ThreadDump(dumpfile); dump = new ThreadDump(dumpfile);
} catch (IOException e) { } catch (final IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
//dump.print(); //dump.print();
assert dump != null; assert dump != null;
Map<StackTrace, Integer> locks = dump.countLocks(); final Map<StackTrace, Integer> locks = dump.countLocks();
List<Map.Entry<StackTrace, List<String>>> freerun = dump.freerun(); final List<Map.Entry<StackTrace, List<String>>> freerun = dump.freerun();
assert locks != null; assert locks != null;
System.out.println("*** Thread Dump Lock report; dump size = " + dump.size() + ", locks = " + locks.size() + ", freerunner = " + freerun.size()); System.out.println("*** Thread Dump Lock report; dump size = " + dump.size() + ", locks = " + locks.size() + ", freerunner = " + freerun.size());
for (int i = 0; i < dump.size() + 10; i++) { for (int i = 0; i < dump.size() + 10; i++) {
for (Map.Entry<StackTrace, Integer> entry: locks.entrySet()) { for (final Map.Entry<StackTrace, Integer> entry: locks.entrySet()) {
if (entry.getValue().intValue() == i) { if (entry.getValue().intValue() == i) {
System.out.println("holds lock for " + i + " threads:"); System.out.println("holds lock for " + i + " threads:");
dump.print(entry.getKey()); dump.print(entry.getKey());
@ -475,7 +473,7 @@ public class ThreadDump extends HashMap<ThreadDump.StackTrace, List<String>> imp
} }
System.out.println("*** Thread freerunner report; dump size = " + dump.size() + ", locks = " + locks.size() + ", freerunner = " + freerun.size()); System.out.println("*** Thread freerunner report; dump size = " + dump.size() + ", locks = " + locks.size() + ", freerunner = " + freerun.size());
for (Map.Entry<StackTrace, List<String>> entry: freerun) { for (final Map.Entry<StackTrace, List<String>> entry: freerun) {
System.out.println("freerunner:"); System.out.println("freerunner:");
dump.print(entry.getKey()); dump.print(entry.getKey());
} }

Loading…
Cancel
Save