Skip to content

Commit

Permalink
Add logic to dump strands
Browse files Browse the repository at this point in the history
  • Loading branch information
warunalakshitha committed Dec 16, 2024
1 parent 8b46143 commit fedce86
Showing 1 changed file with 54 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package io.ballerina.runtime.internal;

import com.sun.management.HotSpotDiagnosticMXBean;
import io.ballerina.identifier.Utils;
import io.ballerina.runtime.api.Module;
import io.ballerina.runtime.api.Runtime;
Expand All @@ -35,12 +36,24 @@
import io.ballerina.runtime.internal.scheduling.Scheduler;
import io.ballerina.runtime.internal.values.FutureValue;

import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.lang.management.ManagementFactory;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import javax.management.MBeanServer;

import static io.ballerina.identifier.Utils.encodeNonFunctionIdentifier;
import static io.ballerina.runtime.api.constants.RuntimeConstants.ANON_ORG;
Expand All @@ -63,6 +76,47 @@ public class BalRuntime extends Runtime {
public boolean moduleStarted = false;
public boolean moduleStopped = false;

public static final PrintStream OUT = System.out;
private static volatile HotSpotDiagnosticMXBean hotSpotDiagnosticMXBean;

static {
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);

// Schedule getStrandDump to run every 15 seconds
scheduler.scheduleAtFixedRate(() -> {
try {
getStrandDump();
} catch (IOException e) {
throw new RuntimeException(e);
}
}, 0, 15, TimeUnit.MINUTES);
}

public static void getStrandDump() throws IOException {

String fileName = "threadDump" + LocalDateTime.now();
getStrandDump(System.getProperty("user.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, "com.sun.management:type=HotSpotDiagnostic",
HotSpotDiagnosticMXBean.class);
}

public BalRuntime(Module rootModule) {
this.scheduler = new Scheduler(this);
this.rootModule = rootModule;
Expand Down

0 comments on commit fedce86

Please sign in to comment.