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 13 years ago
parent 3f606407bc
commit 7598a9e26b

@ -36,6 +36,7 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Pattern;
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 String text;
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.text = text;
}
@Override
public boolean equals(Object a) {
public boolean equals(final Object a) {
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));
}
@Override
@ -80,10 +81,10 @@ public class ThreadDump extends HashMap<ThreadDump.StackTrace, List<String>> imp
this.id = name;
}
@Override
public boolean equals(Object a) {
public boolean equals(final Object a) {
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));
}
@Override
@ -100,7 +101,7 @@ public class ThreadDump extends HashMap<ThreadDump.StackTrace, List<String>> imp
return Thread.getAllStackTraces();
}
public static boolean canProduceLockedBy(File logFile) {
public static boolean canProduceLockedBy(final File logFile) {
// check os version
if (!OS.canExecUnix) return false;
@ -114,7 +115,7 @@ public class ThreadDump extends HashMap<ThreadDump.StackTrace, List<String>> imp
return true;
}
public ThreadDump(File logFile) throws IOException {
public ThreadDump(final File logFile) throws IOException {
super();
// 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();
// get the current process PID
int pid = OS.getPID();
final int pid = OS.getPID();
// 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
long sizeAfter = logFile.length();
final long sizeAfter = logFile.length();
if (sizeAfter <= sizeBefore) return;
RandomAccessFile raf = new RandomAccessFile(logFile, "r");
final RandomAccessFile raf = new RandomAccessFile(logFile, "r");
raf.seek(sizeBefore);
byte[] b = new byte[(int) (sizeAfter - sizeBefore)];
final byte[] b = new byte[(int) (sizeAfter - sizeBefore)];
raf.readFully(b);
raf.close();
@ -153,13 +154,13 @@ public class ThreadDump extends HashMap<ThreadDump.StackTrace, List<String>> imp
public static Thread.State threadState(String line) {
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(' ');
if (p >= 0) line = line.substring(0, p).trim();
try {
return Thread.State.valueOf(line);
} catch (IllegalArgumentException e) {
System.out.println("wong state: " + line);
} catch (final IllegalArgumentException e) {
return null;
}
}
@ -171,16 +172,13 @@ public class ThreadDump extends HashMap<ThreadDump.StackTrace, List<String>> imp
int p;
List<String> list = new ArrayList<String>();
Thread.State state = null;
Thread.State state0;
while ((line = br.readLine()) != null) {
p = line.indexOf(statestatement);
if (p >= 0) try {
state = Thread.State.valueOf(line.substring(p + statestatement.length()).trim());
} catch (IllegalArgumentException e) {
System.out.println("wong state: " + line);
}
state0 = threadState(line);
if (state0 != null) state = state0;
if (line.isEmpty()) {
if (thread != null) {
this.put(new ThreadDump.StackTrace(thread, state), list);
put(new ThreadDump.StackTrace(thread, state), list);
}
list = new ArrayList<String>();
thread = null;
@ -198,7 +196,7 @@ public class ThreadDump extends HashMap<ThreadDump.StackTrace, List<String>> imp
}
// recognize last thread
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 = "";
File classFile;
if ((stateIn == null || stateIn.equals(thread.getState())) && stackTraceElements.length > 0) {
StringBuilder sb = new StringBuilder(3000);
final StringBuilder sb = new StringBuilder(3000);
if (plain) {
classFile = getClassFile(classPath, stackTraceElements[stackTraceElements.length - 1].getClassName());
tracename = classFile.getName();
@ -229,7 +227,7 @@ public class ThreadDump extends HashMap<ThreadDump.StackTrace, List<String>> imp
while (tracename.length() < 20) 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;
boolean cutcore = true;
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();
List<String> threads = this.get(threaddump);
List<String> threads = get(threaddump);
if (threads == null) threads = new ArrayList<String>();
Thread.State state = null;
for (String t: threads) {
int p = t.indexOf(statestatement);
for (final String t: threads) {
final int p = t.indexOf(statestatement);
if (p >= 0) {
state = Thread.State.valueOf(t.substring(p + statestatement.length()).trim());
}
}
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, "");
// write dumps
for (final Entry<StackTrace, List<String>> entry: this.entrySet()) {
List<String> threads = entry.getValue();
for (final Entry<StackTrace, List<String>> entry: entrySet()) {
final List<String> threads = entry.getValue();
for (final String t: threads) bufferappend(buffer, plain, t);
bufferappend(buffer, plain, entry.getKey().text);
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, "");
Map<StackTrace, Integer> locks = this.countLocks();
for (int i = this.size() + 10; i > 0; i--) {
for (Map.Entry<StackTrace, Integer> entry: locks.entrySet()) {
final Map<StackTrace, Integer> locks = countLocks();
for (int i = size() + 10; i > 0; i--) {
for (final Map.Entry<StackTrace, 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());
final List<String> list = get(entry.getKey());
if (list == null) continue;
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, "");
}
}
@ -376,7 +374,7 @@ public class ThreadDump extends HashMap<ThreadDump.StackTrace, List<String>> imp
public List<Map.Entry<StackTrace, List<String>>> freerun() {
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
if (entry.getKey().state != Thread.State.RUNNABLE) continue runf;
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() {
int p;
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()) {
if ((p = s.indexOf("locked <")) > 0) {
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) {
int p;
final List<String> list = this.get(threadName);
final List<String> list = get(threadName);
if (list == null) return null;
for (final String s: list) {
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()) {
// look where the lock has an effect
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);
}
return count;
}
public void print() {
for (final StackTrace thread: this.keySet()) print(thread);
for (final StackTrace thread: keySet()) print(thread);
}
public void print(StackTrace thread) {
final List<String> list = this.get(thread);
public void print(final StackTrace thread) {
final List<String> list = get(thread);
if (list == null) return;
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("");
}
public static void main(String[] args) {
public static void main(final String[] args) {
ThreadDump dump = null;
if (args.length == 0) {
//dump = new ThreadDump();
}
if (args.length == 2 && args[0].equals("-f")) {
File dumpfile = new File(args[1]);
final File dumpfile = new File(args[1]);
try {
dump = new ThreadDump(dumpfile);
} catch (IOException e) {
} catch (final IOException e) {
e.printStackTrace();
}
}
//dump.print();
assert dump != null;
Map<StackTrace, Integer> locks = dump.countLocks();
List<Map.Entry<StackTrace, List<String>>> freerun = dump.freerun();
final Map<StackTrace, Integer> locks = dump.countLocks();
final List<Map.Entry<StackTrace, List<String>>> freerun = dump.freerun();
assert locks != null;
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 (Map.Entry<StackTrace, Integer> entry: locks.entrySet()) {
for (final Map.Entry<StackTrace, Integer> entry: locks.entrySet()) {
if (entry.getValue().intValue() == i) {
System.out.println("holds lock for " + i + " threads:");
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());
for (Map.Entry<StackTrace, List<String>> entry: freerun) {
for (final Map.Entry<StackTrace, List<String>> entry: freerun) {
System.out.println("freerunner:");
dump.print(entry.getKey());
}

Loading…
Cancel
Save