Permalink
Browse files

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

Also updated some JavaDoc and main() function usage message on the same
ones.
  • Loading branch information...
luccioman committed Oct 16, 2017
1 parent fe75f32 commit 6e497241f78e48cb7784283a2808dd5b194a8b06
Showing with 33 additions and 22 deletions.
  1. +18 −15 source/net/yacy/kelondro/logging/ThreadDump.java
  2. +15 −7 source/net/yacy/kelondro/util/OS.java
@@ -114,34 +114,38 @@ public static boolean canProduceLockedBy(final File logFile) {
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();
// 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) {}
}
// read the log from the dump
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,18 +450,17 @@ public void print(final StackTrace thread) {
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 {
dump = new ThreadDump(dumpfile);
} 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 static void execAsynchronous(final File scriptFile) throws IOException {
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;
}

0 comments on commit 6e49724

Please sign in to comment.