Skip to content

Commit

Permalink
Use current directory to run temp.jar
Browse files Browse the repository at this point in the history
  • Loading branch information
HindujaB committed Oct 20, 2023
1 parent e0e53c5 commit 4e80200
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@
import java.util.zip.ZipInputStream;

import static io.ballerina.runtime.profiler.util.Constants.CPU_PRE_JSON;
import static io.ballerina.runtime.profiler.util.Constants.CURRENT_DIR_KEY;
import static io.ballerina.runtime.profiler.util.Constants.OUT_STREAM;
import static io.ballerina.runtime.profiler.util.Constants.PERFORMANCE_JSON;

/**
* This class is used to as the driver class of the Ballerina profiler.
Expand All @@ -67,10 +69,12 @@ public class Profiler {
private int balFunctionCount = 0;
private int moduleCount = 0;
private final ProfilerMethodWrapper profilerMethodWrapper;
private final String currentDir;

public Profiler(long profilerStartTime) {
this.profilerStartTime = profilerStartTime;
this.profilerMethodWrapper = new ProfilerMethodWrapper();
this.currentDir = System.getenv(CURRENT_DIR_KEY);
}

private void addShutdownHookAndCleanup() {
Expand All @@ -83,11 +87,12 @@ private void addShutdownHookAndCleanup() {
OUT_STREAM.printf("%s[6/6] Generating output...%s%n", Constants.ANSI_CYAN, Constants.ANSI_RESET);
JsonParser jsonParser = new JsonParser();
HttpServer httpServer = new HttpServer();
jsonParser.initializeCPUParser();
deleteFileIfExists(CPU_PRE_JSON);
String cpuFilePath = Paths.get(currentDir, CPU_PRE_JSON).toString();
jsonParser.initializeCPUParser(cpuFilePath);
deleteFileIfExists(cpuFilePath);
OUT_STREAM.printf(" ○ Execution time: %d seconds %n", profilerTotalTime / 1000);
httpServer.initializeHTMLExport(this.sourceRoot);
deleteFileIfExists("performance_report.json");
deleteFileIfExists(PERFORMANCE_JSON);
OUT_STREAM.println("--------------------------------------------------------------------------------");
} catch (IOException e) {
throw new ProfilerException("Error occurred while generating the output", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,16 @@
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.jar.Attributes;
import java.util.jar.Manifest;

import static io.ballerina.runtime.profiler.util.Constants.CURRENT_DIR_KEY;
import static io.ballerina.runtime.profiler.util.Constants.ERROR_STREAM;
import static io.ballerina.runtime.profiler.util.Constants.OUT_STREAM;
import static io.ballerina.runtime.profiler.util.Constants.USER_DIR;

/**
* This class is used as the method wrapper for the Ballerina profiler.
Expand All @@ -57,12 +60,13 @@ public void invokeMethods(String debugArg) throws IOException, InterruptedExcept
if (debugArg != null) {
commands.add(debugArg);
}
commands.add(Constants.TEMP_JAR_FILE_NAME);
commands.add(Paths.get(System.getProperty(USER_DIR), Constants.TEMP_JAR_FILE_NAME).toString());
if (balJarArgs != null) {
commands.add(balJarArgs);
}
ProcessBuilder processBuilder = new ProcessBuilder(commands);
processBuilder.inheritIO();
processBuilder.directory(new File(System.getenv(CURRENT_DIR_KEY)));
Process process = processBuilder.start();
OUT_STREAM.printf(Constants.ANSI_CYAN + "[5/6] Running executable..." + Constants.ANSI_RESET + "%n");
try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.nio.charset.StandardCharsets;

import static io.ballerina.runtime.profiler.util.Constants.OUT_STREAM;
import static io.ballerina.runtime.profiler.util.Constants.PERFORMANCE_JSON;

/**
* This class contains the HTTP server of the Ballerina profiler.
Expand All @@ -36,7 +37,7 @@ public class HttpServer {
public void initializeHTMLExport(String sourceRoot) throws IOException {
OUT_STREAM.printf(" ○ Output: " + Constants.ANSI_YELLOW +
"%s/ProfilerOutput.html" + Constants.ANSI_RESET + "%n", sourceRoot);
String content = FileUtils.readFileAsString("performance_report.json");
String content = FileUtils.readFileAsString(PERFORMANCE_JSON);
FrontEnd frontEnd = new FrontEnd();
String htmlData = frontEnd.getSiteData(content);
String fileName = "ProfilerOutput.html";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import java.util.ArrayList;
import java.util.List;

import static io.ballerina.runtime.profiler.util.Constants.CPU_PRE_JSON;
import static io.ballerina.runtime.profiler.util.Constants.OUT_STREAM;

/**
Expand All @@ -41,9 +40,9 @@ public class JsonParser {

private static final String VALUE_KEY = "value";

public void initializeCPUParser() {
public void initializeCPUParser(String cpuFilePath) {
try {
String jsonInput = FileUtils.readFileAsString(CPU_PRE_JSON);
String jsonInput = FileUtils.readFileAsString(cpuFilePath);
List<StackTrace> input = populateStackTraceItems(jsonInput);
// Create a Data object to store the output
Data output = new Data("Root", input.get(0).time, new ArrayList<>());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class Constants {

public static final String CLASS_SUFFIX = ".class";
public static final String CPU_PRE_JSON = "cpu_pre.json";
public static final String PERFORMANCE_JSON = "performance_report.json";
public static final String TEMP_JAR_FILE_NAME = "temp.jar";
public static final String STRAND_PROFILER_STACK_PROPERTY = "b7a.profile.stack";

Expand All @@ -31,6 +32,8 @@ public class Constants {
"(L" + STRAND_CLASS + ";L" + DATA_CLASS + ";)V";
public static final String PROFILE_ANALYZER = "io/ballerina/runtime/profiler/runtime/ProfileAnalyzer";
public static final String GET_INSTANCE_DESCRIPTOR = "()L" + PROFILE_ANALYZER + ";";
public static final String CURRENT_DIR_KEY = "current.dir";
public static final String USER_DIR = "user.dir";

private Constants() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
public class RunProfilerTask implements Task {
private final PrintStream err;
private static final String JAVA_OPTS = "JAVA_OPTS";
private static final String CURRENT_DIR_KEY = "current.dir";

public RunProfilerTask(PrintStream errStream) {
this.err = errStream;
Expand Down Expand Up @@ -84,6 +85,7 @@ private void initiateProfiler(Project project) {
}
ProcessBuilder pb = new ProcessBuilder(commands).inheritIO();
pb.environment().put(JAVA_OPTS, getAgentArgs());
pb.environment().put(CURRENT_DIR_KEY, System.getProperty(USER_DIR));
setWorkingDirectory(project, projectKind, pb);
Process process = pb.start();
process.waitFor();
Expand Down

0 comments on commit 4e80200

Please sign in to comment.