18
18
19
19
package io .ballerina .stdlib .graphql .runtime .utils ;
20
20
21
+ import com .sun .management .HotSpotDiagnosticMXBean ;
21
22
import io .ballerina .runtime .api .creators .ErrorCreator ;
22
23
import io .ballerina .runtime .api .types .ArrayType ;
23
24
import io .ballerina .runtime .api .types .Type ;
27
28
import io .ballerina .runtime .api .values .BObject ;
28
29
import io .ballerina .runtime .api .values .BString ;
29
30
30
- import java .io .BufferedReader ;
31
- import java .io .InputStreamReader ;
31
+ import java .io .File ;
32
+ import java .io .IOException ;
32
33
import java .io .PrintStream ;
33
34
import java .lang .management .ManagementFactory ;
35
+ import java .nio .file .Files ;
36
+ import java .nio .file .Paths ;
37
+ import java .time .LocalDateTime ;
38
+
39
+ import javax .management .MBeanServer ;
34
40
35
41
import static io .ballerina .stdlib .graphql .runtime .utils .ModuleUtils .getModule ;
36
42
@@ -45,15 +51,19 @@ private Utils() {
45
51
Thread .startVirtualThread (() -> {
46
52
try {
47
53
Thread .sleep (5 * 60 * 1000 );
48
- dumpThreads ();
49
- } catch (InterruptedException e ) {
54
+ getStrandDump ();
55
+ } catch (InterruptedException | IOException e ) {
50
56
throw new RuntimeException (e );
51
57
}
52
58
});
53
59
}
54
60
55
61
public static final PrintStream OUT = System .out ;
56
62
public static final PrintStream ERROR = System .err ;
63
+ private static final String HOT_SPOT_BEAN_NAME = "com.sun.management:type=HotSpotDiagnostic" ;
64
+ private static final String WORKING_DIR = System .getProperty ("user.dir" ) + "/" ;
65
+ private static final String FILENAME = "threadDump" + LocalDateTime .now ();
66
+ private static volatile HotSpotDiagnosticMXBean hotSpotDiagnosticMXBean ;
57
67
58
68
// Inter-op function names
59
69
private static final String EXECUTE_RESOURCE_FUNCTION = "executeQueryResource" ;
@@ -125,32 +135,23 @@ public static void handleBErrorAndExit(BError bError) {
125
135
System .exit (1 );
126
136
}
127
137
128
- public static void dumpThreads () {
129
- try {
130
- // Get the current process ID (PID)
131
- String pid = ManagementFactory .getRuntimeMXBean ().getName ().split ("@" )[0 ];
132
-
133
- // Build the jcmd command
134
- String command = "jcmd " + pid ;
135
-
136
- // Execute the jcmd command
137
- Process process = Runtime .getRuntime ().exec (command );
138
-
139
- // Read the output of the command
140
- try (BufferedReader reader = new BufferedReader (new InputStreamReader (process .getInputStream ()))) {
141
- String line ;
142
- while ((line = reader .readLine ()) != null ) {
143
- OUT .println (line );
144
- }
145
- }
138
+ public static void getStrandDump () throws IOException {
139
+ getStrandDump (WORKING_DIR + FILENAME );
140
+ String dump = new String (Files .readAllBytes (Paths .get (FILENAME )));
141
+ File fileObj = new File (FILENAME );
142
+ fileObj .delete ();
143
+ OUT .println (dump );
144
+ }
146
145
147
- // Wait for the process to complete
148
- int exitCode = process .waitFor ();
149
- if (exitCode != 0 ) {
150
- ERROR .println ("jcmd command failed with exit code: " + exitCode );
151
- }
152
- } catch (Exception error ) {
153
- error .printStackTrace ();
146
+ private static void getStrandDump (String fileName ) throws IOException {
147
+ if (hotSpotDiagnosticMXBean == null ) {
148
+ hotSpotDiagnosticMXBean = getHotSpotDiagnosticMXBean ();
154
149
}
150
+ hotSpotDiagnosticMXBean .dumpThreads (fileName , HotSpotDiagnosticMXBean .ThreadDumpFormat .TEXT_PLAIN );
151
+ }
152
+
153
+ private static HotSpotDiagnosticMXBean getHotSpotDiagnosticMXBean () throws IOException {
154
+ MBeanServer mBeanServer = ManagementFactory .getPlatformMBeanServer ();
155
+ return ManagementFactory .newPlatformMXBeanProxy (mBeanServer , HOT_SPOT_BEAN_NAME , HotSpotDiagnosticMXBean .class );
155
156
}
156
157
}
0 commit comments