Skip to content

Commit

Permalink
Properly close resources (even on error) on OS and ThreadDump classes.
Browse files Browse the repository at this point in the history
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 6e49724
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 22 deletions.
33 changes: 18 additions & 15 deletions source/net/yacy/kelondro/logging/ThreadDump.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
Expand Down
22 changes: 15 additions & 7 deletions source/net/yacy/kelondro/util/OS.java
Original file line number Diff line number Diff line change
Expand Up @@ -178,13 +178,21 @@ public static List<String> execSynchronous(final String[] command) throws IOExce

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;
}

Expand Down

0 comments on commit 6e49724

Please sign in to comment.