Skip to content

Commit

Permalink
Refactor strand dump logic and fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
ravinperera00 committed Dec 17, 2024
1 parent a1e03c5 commit 10289a1
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;

Expand All @@ -45,10 +47,10 @@ public final class StrandDump {
private static final String WORKING_DIR = System.getProperty("user.dir") + "/";
private static final String FILENAME = "threadDump" + LocalDateTime.now();
private static final String VIRTUAL_THREAD_IDENTIFIER = "virtual";
private static final String ISOLATED_WORKER_IDENTIFIER = "io.ballerina.runtime.internal.scheduling." +
"Scheduler.lambda$startIsolatedWorker";
private static final String NON_ISOLATED_WORKER_IDENTIFIER = "io.ballerina.runtime.internal.scheduling." +
"Scheduler.lambda$startNonIsolatedWorker";
private static final String ISOLATED_IDENTIFIER = "io.ballerina.runtime.internal.scheduling." +
"Scheduler.lambda$startIsolated";
private static final String NON_ISOLATED_IDENTIFIER = "io.ballerina.runtime.internal.scheduling." +
"Scheduler.lambda$startNonIsolated";
private static final String JAVA_TRACE_PATTERN = "java\\.|\\.java(?::\\d+)?"; // .java, java., .java:(any number)
private static final String BAL_TRACE_PATTERN = "\\.bal:\\d+"; // .bal:(any number)
private static volatile HotSpotDiagnosticMXBean hotSpotDiagnosticMXBean;
Expand All @@ -69,16 +71,16 @@ public static String getStrandDump() {
private static String generateOutput(String dump) {
String[] dumpItems = dump.split("\\n\\n");
int id = 0;
Set<Integer> isolatedWorkerList = new HashSet<>();
Set<Integer> nonIsolatedWorkerList = new HashSet<>();
ArrayList<ArrayList<String>> balTraces = new ArrayList<>();
Set<Integer> isolatedStrandList = new HashSet<>();
Set<Integer> nonIsolatedStrandList = new HashSet<>();
Map<Integer, ArrayList<String>> balTraces = new HashMap<>();
Pattern javaPattern = Pattern.compile(JAVA_TRACE_PATTERN);
Pattern balPattern = Pattern.compile(BAL_TRACE_PATTERN);
for (String item : dumpItems) {
String[] lines = item.split("\\n");
String[] subitems = lines[0].split("\" ");
ArrayList<String> balTraceItems = new ArrayList<>();
boolean balStrand = false;
boolean isBalStrand = false;
if (subitems.length > 1 && subitems[1].equals(VIRTUAL_THREAD_IDENTIFIER)) {
balTraceItems.add("\tStrand " + lines[0].replace(VIRTUAL_THREAD_IDENTIFIER, ":") + "\n\t\tat");
String prefix = " ";
Expand All @@ -87,21 +89,21 @@ private static String generateOutput(String dump) {
balTraceItems.add(prefix + line + "\n");
prefix = "\t\t ";
if (balPattern.matcher(line).find()) {
balStrand = true;
isBalStrand = true;
}
} else {
if (line.contains(ISOLATED_WORKER_IDENTIFIER)) {
isolatedWorkerList.add(id);
} else if (line.contains(NON_ISOLATED_WORKER_IDENTIFIER)) {
nonIsolatedWorkerList.add(id);
if (line.contains(ISOLATED_IDENTIFIER)) {
isolatedStrandList.add(id);
} else if (line.contains(NON_ISOLATED_IDENTIFIER)) {
nonIsolatedStrandList.add(id);
}
}
}
if (balStrand) {
balTraces.add(balTraceItems);
if (isBalStrand) {
balTraces.put(id, balTraceItems);
} else {
isolatedWorkerList.remove(id);
nonIsolatedWorkerList.remove(id);
isolatedStrandList.remove(id);
nonIsolatedStrandList.remove(id);
}
id++;
}
Expand All @@ -112,19 +114,23 @@ private static String generateOutput(String dump) {
outputStr.append(dateTimeFormatter.format(localDateTime));
outputStr.append("]\n===============================================================\n\n");
outputStr.append("Total Strand count \t\t\t:\t").append(balTraces.size()).append("\n\n");
outputStr.append("Total Isolated Worker count \t\t:\t").append(isolatedWorkerList.size()).append("\n\n");
outputStr.append("Total Non Isolated Worker count \t\t:\t").append(nonIsolatedWorkerList.size()).
outputStr.append("Total Isolated Strand count \t\t:\t").append(isolatedStrandList.size()).append("\n\n");
outputStr.append("Total Non Isolated Strand count \t\t:\t").append(nonIsolatedStrandList.size()).
append("\n\n");
outputStr.append("================================================================\n");
outputStr.append("\nIsolated Workers:\n\n");
for (int strandId: isolatedWorkerList) {
balTraces.get(strandId).forEach(outputStr::append);
outputStr.append("\n");
outputStr.append("\nIsolated Strands:\n\n");
for (int strandId: isolatedStrandList) {
if (balTraces.containsKey(strandId)) {
balTraces.get(strandId).forEach(outputStr::append);
outputStr.append("\n");
}
}
outputStr.append("Non Isolated Workers:\n\n");
for (int strandId: nonIsolatedWorkerList) {
balTraces.get(strandId).forEach(outputStr::append);
outputStr.append("\n");
outputStr.append("Non Isolated Strands:\n\n");
for (int strandId: nonIsolatedStrandList) {
if (balTraces.containsKey(strandId)) {
balTraces.get(strandId).forEach(outputStr::append);
outputStr.append("\n");
}
}
return outputStr.toString();
}
Expand All @@ -140,4 +146,4 @@ private static HotSpotDiagnosticMXBean getHotSpotDiagnosticMXBean() throws IOExc
MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
return ManagementFactory.newPlatformMXBeanProxy(mBeanServer, HOT_SPOT_BEAN_NAME, HotSpotDiagnosticMXBean.class);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ Ballerina Strand Dump \[\d*/\d*/\d* \d*:\d*:\d*\]

Total Strand count \t\t\t:\t9

Total Isolated Worker count \t\t:\t2
Total Isolated Strand count \t\t:\t2

Total Non Isolated Worker count \t\t:\t7
Total Non Isolated Strand count \t\t:\t7

================================================================

Isolated Workers:
Isolated Strands:

\tStrand #\d* \"\w*\" :
\t\tat balProgram1.\$lambda\$_0\(balProgram1.bal:\d*\)
Expand All @@ -19,7 +19,7 @@ Isolated Workers:
\t\tat balProgram1.\$lambda\$_1\(balProgram1.bal:\d*\)
\t\t lambdas.\$_generated1balProgram1.\$lambda\$_1\$lambda1\$\(balProgram1.bal:\d*\)

Non Isolated Workers:
Non Isolated Strands:

\tStrand #\d* \"\w*\" :
\t\tat balProgram1.\$lambda\$_2\(balProgram1.bal:\d*\)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ Ballerina Strand Dump \[\d*/\d*/\d* \d*:\d*:\d*\]

Total Strand count \t\t\t:\t7

Total Isolated Worker count \t\t:\t0
Total Isolated Strand count \t\t:\t0

Total Non Isolated Worker count \t\t:\t7
Total Non Isolated Strand count \t\t:\t7

================================================================

Isolated Workers:
Isolated Strands:

Non Isolated Workers:
Non Isolated Strands:

\tStrand #\d* \"\w*\" :
\t\tat testOrg.testPackageWithModules.0.main.bar\(main.bal:\d*\)
Expand Down Expand Up @@ -63,4 +63,3 @@ Non Isolated Workers:
\t\tat ballerina.lang&0046runtime.0.runtime.sleep\(runtime.bal:\d*\)
\t\t testOrg.testPackageWithModules\$test.0.tests.main_test.\$lambda\$_11\(tests/main_test.bal:\d*\)
\t\t testOrg.testPackageWithModules\$test.0.lambdas.\$_generated2tests.main_test.\$lambda\$_11\$lambda1\$\(tests/main_test.bal:\d*\)

Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ Ballerina Strand Dump \[\d*/\d*/\d* \d*:\d*:\d*\]

Total Strand count \t\t\t:\t17

Total Isolated Worker count \t\t:\t4
Total Isolated Strand count \t\t:\t4

Total Non Isolated Worker count \t\t:\t13
Total Non Isolated Strand count \t\t:\t13

================================================================

Isolated Workers:
Isolated Strands:

\tStrand #\d* \"\w*\" :
\t\tat ballerina.lang&0046runtime.0.runtime.sleep\(runtime.bal:\d*\)
Expand All @@ -32,7 +32,7 @@ Isolated Workers:
\t\t testOrg.testPackageWithModules&0046anotherutils.0.creators.\$_function_calls.call\(Unknown Source\)
\t\t testOrg.testPackageWithModules.0.lambdas.\$_generated1main.entryfunc\$lambda\$2\$\(main.bal:\d*\)

Non Isolated Workers:
Non Isolated Strands:

\tStrand #\d* \"\w*\" :
\t\tat testOrg.testPackageWithModules.0.main.\$lambda\$_4\(main.bal:\d*\)
Expand Down

0 comments on commit 10289a1

Please sign in to comment.