Properly close resources (even on error) on OS and ThreadDump classes.

Also updated some JavaDoc and main() function usage message on the same
ones.
pull/135/head
luccioman 7 years ago
parent fe75f326d8
commit 6e497241f7

@ -114,11 +114,15 @@ public class ThreadDump extends HashMap<ThreadDump.StackTrace, List<String>> imp
return true;
}
/**
* Try to get the thread dump from a yacy.log file which is available when YaCy is started with
* startYACY.sh -l
* @param logFile the log file to read
* @throws IOException when a read/write error occurred
*/
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
// startYACY.sh -l
long sizeBefore = 0;
if (canProduceLockedBy(logFile)) {
sizeBefore = logFile.length();
@ -126,7 +130,7 @@ public class ThreadDump extends HashMap<ThreadDump.StackTrace, List<String>> imp
// get the current process PID
final int pid = OS.getPID();
// call kill -3 on the pid
// call kill -3 (SIGQUIT) on the pid to request a core dump from the current process
if (pid >= 0) try {OS.execSynchronous("kill -3 " + pid);} catch (final IOException e) {}
}
@ -134,14 +138,14 @@ public class ThreadDump extends HashMap<ThreadDump.StackTrace, List<String>> imp
final long sizeAfter = logFile.length();
if (sizeAfter <= sizeBefore) return;
final RandomAccessFile raf = new RandomAccessFile(logFile, "r");
raf.seek(sizeBefore);
final byte[] b = new byte[(int) (sizeAfter - sizeBefore)];
raf.readFully(b);
raf.close();
// import the thread dump;
importText(new ByteArrayInputStream(b));
try(final RandomAccessFile raf = new RandomAccessFile(logFile, "r");) {
raf.seek(sizeBefore);
final byte[] b = new byte[(int) (sizeAfter - sizeBefore)];
raf.readFully(b);
// import the thread dump;
importText(new ByteArrayInputStream(b));
}
}
public ThreadDump(final InputStream is) throws IOException {
@ -446,9 +450,6 @@ public class ThreadDump extends HashMap<ThreadDump.StackTrace, List<String>> imp
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")) {
final File dumpfile = new File(args[1]);
try {
@ -456,8 +457,10 @@ public class ThreadDump extends HashMap<ThreadDump.StackTrace, List<String>> imp
} catch (final IOException e) {
e.printStackTrace();
}
} else {
System.out.println("Usage : java " + ThreadDump.class.getName() + " -f /path/to/yacy.log");
return;
}
//dump.print();
assert dump != null;
final Map<StackTrace, Integer> locks = dump.countLocks();
final List<Map.Entry<StackTrace, List<String>>> freerun = dump.freerun();

@ -178,13 +178,21 @@ public final class OS {
private static List<String> execSynchronousProcess(Process p) throws IOException {
String line;
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
final List<String> output = new ArrayList<String>();
while ((line = in.readLine()) != null) output.add(line);
in.close();
in = new BufferedReader(new InputStreamReader(p.getErrorStream()));
while ((line = in.readLine()) != null) output.add(line);
in.close();
final List<String> output = new ArrayList<String>();
try (final InputStreamReader streamReader = new InputStreamReader(p.getInputStream());
final BufferedReader in = new BufferedReader(streamReader);) {
while ((line = in.readLine()) != null) {
output.add(line);
}
}
try (final InputStreamReader streamReader = new InputStreamReader(p.getErrorStream());
final BufferedReader in = new BufferedReader(streamReader);) {
while ((line = in.readLine()) != null) {
output.add(line);
}
}
return output;
}

Loading…
Cancel
Save