Skip to content

Commit

Permalink
Thread dump to file (#604)
Browse files Browse the repository at this point in the history
  • Loading branch information
belaban committed Feb 28, 2022
1 parent d293230 commit 2f7cb09
Showing 1 changed file with 22 additions and 18 deletions.
40 changes: 22 additions & 18 deletions src/org/jgroups/util/ThreadPool.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.jgroups.annotations.ManagedOperation;
import org.jgroups.annotations.Property;
import org.jgroups.conf.AttributeType;
import org.jgroups.logging.Log;
import org.jgroups.protocols.TP;

import java.io.BufferedWriter;
Expand All @@ -22,7 +23,6 @@
* @since 5.2
*/
public class ThreadPool implements Lifecycle {
private static final String THREAD_DUMP_PATH = System.getProperty("jgroups.threaddump.path");
protected Executor thread_pool;
protected final TP tp;

Expand All @@ -45,6 +45,10 @@ public class ThreadPool implements Lifecycle {
@Property(description="The number of times a thread pool needs to be full before a thread dump is logged")
protected int thread_dumps_threshold=1;

@Property(description="Path to which the thread dump will be written. Ignored if null",
systemProperty="jgroups.threaddump.path")
protected String thread_dump_path;




Expand Down Expand Up @@ -177,25 +181,25 @@ public boolean execute(Runnable task) {
tp.getMessageStats().incrNumRejectedMsgs(1);
// https://issues.redhat.com/browse/JGRP-2403
if(thread_dumps.incrementAndGet() == thread_dumps_threshold) {
String threadDump = Util.dumpThreads();
if (THREAD_DUMP_PATH != null) {
File file = new File(THREAD_DUMP_PATH, "jgroups_threaddump_" + System.currentTimeMillis() + ".txt");
try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
writer.write(threadDump);
tp.getLog().fatal("%s: thread pool is full (max=%d, active=%d); " +
"thread dump (dumped once, until thread_dump is reset): %s",
tp.getAddress(), max_threads, getThreadPoolSize(), file.getAbsolutePath());
} catch (IOException e) {
tp.getLog().warn(String.format("Cannot generate the thread dump file %s", file.getAbsolutePath()), e);
tp.getLog().fatal("%s: thread pool is full (max=%d, active=%d); " +
"thread dump (dumped once, until thread_dump is reset):\n%s",
tp.getAddress(), max_threads, getThreadPoolSize(), threadDump);
String thread_dump=Util.dumpThreads();
Log l=tp.getLog();
if(thread_dump_path != null) {
File file=new File(thread_dump_path, "jgroups_threaddump_" + System.currentTimeMillis() + ".txt");
try(BufferedWriter writer=new BufferedWriter(new FileWriter(file))) {
writer.write(thread_dump);
l.fatal("%s: thread pool is full (max=%d, active=%d); thread dump (dumped once, until thread_dump is reset): %s",
tp.getAddress(), max_threads, getThreadPoolSize(), file.getAbsolutePath());
}
catch(IOException e) {
l.warn("%s: cannot generate the thread dump to %s: %s", tp.getAddress(), file.getAbsolutePath(), e);
l.fatal("%s: thread pool is full (max=%d, active=%d); " +
"thread dump (dumped once, until thread_dump is reset):\n%s",
tp.getAddress(), max_threads, getThreadPoolSize(), thread_dump);
}
} else {
tp.getLog().fatal("%s: thread pool is full (max=%d, active=%d); " +
"thread dump (dumped once, until thread_dump is reset):\n%s",
tp.getAddress(), max_threads, getThreadPoolSize(), threadDump);
}
else
l.fatal("%s: thread pool is full (max=%d, active=%d); thread dump (dumped once, until thread_dump is reset):\n%s",
tp.getAddress(), max_threads, getThreadPoolSize(), thread_dump);
}
return false;
}
Expand Down

0 comments on commit 2f7cb09

Please sign in to comment.