Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print thread dump on specific signals #27

Merged
merged 3 commits into from
Oct 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Comment on lines +33 to +34
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are the sun.misc.* inside the internal parts of JDK and shouldn't be accessed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but outside of shutdown hooks I don't know of a way to do this. If it ever gets removed I assume there will be something else for signal handling and in that case this change should be easy to port or ditch altogether since it does not go too deep.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, there is no other way to get to the Unix signals, and handle specific signal through Java Public APIs


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);
CodeLieutenant marked this conversation as resolved.
Show resolved Hide resolved
Signal.handle(new Signal("TERM"), handler);
Signal.handle(new Signal("INT"), handler);
}
}