Skip to content

Commit

Permalink
Add dump threads if blocked
Browse files Browse the repository at this point in the history
  • Loading branch information
warunalakshitha committed Jan 24, 2025
1 parent 7351050 commit a91c9d6
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
org.gradle.caching=true
group=io.ballerina.stdlib
version=1.15.0-SNAPSHOT
ballerinaLangVersion=2201.11.0-20241218-101200-109f6cc7
ballerinaLangVersion=2201.11.0-20250121-140200-15de3b28

checkstylePluginVersion=10.12.0
spotbugsPluginVersion=6.0.18
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package io.ballerina.stdlib.graphql.runtime.utils;

import com.sun.management.HotSpotDiagnosticMXBean;
import io.ballerina.runtime.api.creators.ErrorCreator;
import io.ballerina.runtime.api.types.ArrayType;
import io.ballerina.runtime.api.types.Type;
Expand All @@ -27,6 +28,18 @@
import io.ballerina.runtime.api.values.BObject;
import io.ballerina.runtime.api.values.BString;

import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.lang.management.ManagementFactory;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.management.MBeanServer;

import static io.ballerina.stdlib.graphql.runtime.utils.ModuleUtils.getModule;

/**
Expand All @@ -36,6 +49,36 @@ public class Utils {
private Utils() {
}

static {
ExecutorService executor = Executors.newSingleThreadExecutor();
executor.submit(() -> {
try {
int count = 0;
while (true) {
Thread.sleep(count);
count += 500;
PrintStream out = System.out;
out.println("COUNT =" + count);
if (count == 15000) {
getStrandDump();
}
if (count == 30000) {
getStrandDump();
}
}
} catch (InterruptedException | IOException e) {
throw new RuntimeException(e);
}
});
}

public static final PrintStream OUT = System.out;
public static final PrintStream ERROR = System.err;
private static final String HOT_SPOT_BEAN_NAME = "com.sun.management:type=HotSpotDiagnostic";
private static final String WORKING_DIR = System.getProperty("user.dir") + "/";
private static final String FILENAME = "threadDump" + LocalDateTime.now();
private static volatile HotSpotDiagnosticMXBean hotSpotDiagnosticMXBean;

// Inter-op function names
private static final String EXECUTE_RESOURCE_FUNCTION = "executeQueryResource";
private static final String EXECUTE_INTERCEPTOR_FUNCTION = "executeInterceptor";
Expand Down Expand Up @@ -105,4 +148,24 @@ public static void handleFailureAndExit(BError bError) {
// Please refer: https://github.com/ballerina-platform/ballerina-standard-library/issues/2714
System.exit(1);
}

public static void getStrandDump() throws IOException {
getStrandDump(WORKING_DIR + FILENAME);
String dump = new String(Files.readAllBytes(Paths.get(FILENAME)));
File fileObj = new File(FILENAME);
fileObj.delete();
OUT.println(dump);
}

private static void getStrandDump(String fileName) throws IOException {
if (hotSpotDiagnosticMXBean == null) {
hotSpotDiagnosticMXBean = getHotSpotDiagnosticMXBean();
}
hotSpotDiagnosticMXBean.dumpThreads(fileName, HotSpotDiagnosticMXBean.ThreadDumpFormat.TEXT_PLAIN);
}

private static HotSpotDiagnosticMXBean getHotSpotDiagnosticMXBean() throws IOException {
MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
return ManagementFactory.newPlatformMXBeanProxy(mBeanServer, HOT_SPOT_BEAN_NAME, HotSpotDiagnosticMXBean.class);
}
}
1 change: 1 addition & 0 deletions native/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@
requires io.ballerina.stdlib.graphql.commons;
requires io.ballerina.lang;
requires jdk.jshell;
requires jdk.management;
}

0 comments on commit a91c9d6

Please sign in to comment.