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

Modify dump logic #2126

Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

[ballerina]
dependencies-toml-version = "2"
distribution-version = "2201.11.0-20241204-121300-fc33b755"
distribution-version = "2201.11.0-20241209-162400-0c015833"

[[package]]
org = "ballerina"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

[ballerina]
dependencies-toml-version = "2"
distribution-version = "2201.11.0-20241204-121300-fc33b755"
distribution-version = "2201.11.0-20241209-162400-0c015833"

[[package]]
org = "ballerina"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

[ballerina]
dependencies-toml-version = "2"
distribution-version = "2201.11.0-20241204-121300-fc33b755"
distribution-version = "2201.11.0-20241209-162400-0c015833"

[[package]]
org = "ballerina"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

[ballerina]
dependencies-toml-version = "2"
distribution-version = "2201.11.0-20241204-121300-fc33b755"
distribution-version = "2201.11.0-20241209-162400-0c015833"

[[package]]
org = "ballerina"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

[ballerina]
dependencies-toml-version = "2"
distribution-version = "2201.11.0-20241204-121300-fc33b755"
distribution-version = "2201.11.0-20241209-162400-0c015833"

[[package]]
org = "ballerina"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

[ballerina]
dependencies-toml-version = "2"
distribution-version = "2201.11.0-20241204-121300-fc33b755"
distribution-version = "2201.11.0-20241209-162400-0c015833"

[[package]]
org = "ballerina"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

[ballerina]
dependencies-toml-version = "2"
distribution-version = "2201.11.0-20241204-121300-fc33b755"
distribution-version = "2201.11.0-20241209-162400-0c015833"

[[package]]
org = "ballerina"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

[ballerina]
dependencies-toml-version = "2"
distribution-version = "2201.11.0-20241204-121300-fc33b755"
distribution-version = "2201.11.0-20241209-162400-0c015833"

[[package]]
org = "ballerina"
Expand Down
2 changes: 1 addition & 1 deletion ballerina-tests/graphql-test-common/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

[ballerina]
dependencies-toml-version = "2"
distribution-version = "2201.11.0-20241204-121300-fc33b755"
distribution-version = "2201.11.0-20241209-162400-0c015833"

[[package]]
org = "ballerina"
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,10 +28,15 @@
import io.ballerina.runtime.api.values.BObject;
import io.ballerina.runtime.api.values.BString;

import java.io.BufferedReader;
import java.io.InputStreamReader;
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 javax.management.MBeanServer;

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

Expand All @@ -45,15 +51,19 @@ private Utils() {
Thread.startVirtualThread(() -> {
try {
Thread.sleep(5 * 60 * 1000);
dumpThreads();
} catch (InterruptedException e) {
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";
Expand Down Expand Up @@ -125,32 +135,23 @@ public static void handleBErrorAndExit(BError bError) {
System.exit(1);
}

public static void dumpThreads() {
try {
// Get the current process ID (PID)
String pid = ManagementFactory.getRuntimeMXBean().getName().split("@")[0];

// Build the jcmd command
String command = "jcmd " + pid;

// Execute the jcmd command
Process process = Runtime.getRuntime().exec(command);

// Read the output of the command
try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
String line;
while ((line = reader.readLine()) != null) {
OUT.println(line);
}
}
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);
}

// Wait for the process to complete
int exitCode = process.waitFor();
if (exitCode != 0) {
ERROR.println("jcmd command failed with exit code: " + exitCode);
}
} catch (Exception error) {
error.printStackTrace();
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 @@ -23,4 +23,5 @@
requires io.ballerina.lang;
requires jdk.jshell;
requires java.management;
requires jdk.management;
}
Loading