Skip to content

Commit

Permalink
Add signal handler that prints thread dump
Browse files Browse the repository at this point in the history
Adds a signal handler that will print thread dump to standard output on
the following signals: SIGABRT, SIGTERM, SIGINT.
  • Loading branch information
Bouncheck committed Oct 21, 2024
1 parent dec6aac commit 67f8796
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions tools/stress/src/org/apache/cassandra/stress/Stress.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
package org.apache.cassandra.stress;

import java.io.*;
import java.lang.management.ManagementFactory;
import java.lang.management.ThreadInfo;
import java.lang.management.ThreadMXBean;
import java.net.Socket;
import java.net.SocketException;

Expand All @@ -27,6 +30,9 @@
import org.apache.cassandra.utils.FBUtilities;
import org.apache.cassandra.utils.WindowsTimer;

import sun.misc.Signal;
import sun.misc.SignalHandler;

public final class Stress
{

Expand Down Expand Up @@ -56,6 +62,8 @@ public final class Stress

public static void main(String[] arguments) throws Exception
{
registerSignalHandler();

if (FBUtilities.isWindows)
WindowsTimer.startTimerPeriod(1);

Expand Down Expand Up @@ -196,4 +204,32 @@ public void run()
}
}

private static String threadDump(boolean lockedMonitors, boolean lockedSynchronizers) {
StringBuffer threadDump = new StringBuffer(System.lineSeparator());
ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
for(ThreadInfo threadInfo : threadMXBean.dumpAllThreads(lockedMonitors, lockedSynchronizers)) {
threadDump.append(threadInfo.toString());
}
return threadDump.toString();
}

private static void registerSignalHandler() {
SignalHandler handler = signal -> {
System.out.println("Caught signal: " + signal.getName());
System.out.println(threadDump(true, true));
switch (signal.getName()) {
case "ABRT":
System.exit(128 + 6);
case "TERM":
System.exit(128 + 15);
case "INT":
System.exit(128 + 2);
default:
System.exit(1);
}
};
Signal.handle(new Signal("ABRT"), handler);
Signal.handle(new Signal("TERM"), handler);
Signal.handle(new Signal("INT"), handler);
}
}

0 comments on commit 67f8796

Please sign in to comment.