Skip to content

Commit

Permalink
Merge pull request #41823 from gabilang/fix-profiler-windows
Browse files Browse the repository at this point in the history
Fix profiler crashes in windows
  • Loading branch information
warunalakshitha authored Jan 3, 2024
2 parents bceb4d9 + 2062c7d commit b2c2c42
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.jar.JarFile;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
Expand All @@ -59,7 +60,6 @@ public class Profiler {
private final long profilerStartTime;
private String balJarArgs = null;
private String balJarName = null;
private String targetDir = null;
private String profilerDebugArg = null;
private final List<String> instrumentedPaths = new ArrayList<>();
private final List<String> instrumentedFiles = new ArrayList<>();
Expand All @@ -82,21 +82,19 @@ private void addShutdownHookAndCleanup() {
try {
long profilerTotalTime = TimeUnit.MILLISECONDS.convert(System.nanoTime(), TimeUnit.NANOSECONDS) -
profilerStartTime;
deleteFileIfExists(Constants.TEMP_JAR_FILE_NAME);
Files.deleteIfExists(Paths.get(Constants.TEMP_JAR_FILE_NAME));
OUT_STREAM.printf("%s[6/6] Generating output...%s%n", Constants.ANSI_CYAN, Constants.ANSI_RESET);
JsonParser jsonParser = new JsonParser();
HttpServer httpServer = new HttpServer();
String cpuFilePath = Paths.get(currentDir, CPU_PRE_JSON).toString();
jsonParser.initializeCPUParser(cpuFilePath);
deleteFileIfExists(cpuFilePath);
OUT_STREAM.printf(" Execution time: %d seconds %n", profilerTotalTime / 1000);
OUT_STREAM.printf(" Execution time: %d seconds %n", profilerTotalTime / 1000);
httpServer.initializeHTMLExport();
deleteFileIfExists(PERFORMANCE_JSON);
OUT_STREAM.println("--------------------------------------------------------------------------------");
} catch (IOException e) {
throw new ProfilerException("Error occurred while generating the output", e);
} finally {
deleteFileIfExists(targetDir);
}
}));
}
Expand Down Expand Up @@ -139,10 +137,6 @@ private void handleProfilerArguments(String[] args) {
this.balJarArgs = extractBalJarArgs(args[i + 1]);
addToUsedArgs(args, usedArgs, i);
}
case "--target" -> {
this.targetDir = args[i + 1];
addToUsedArgs(args, usedArgs, i);
}
case "--profiler-debug" -> {
this.profilerDebugArg = args[i + 1];
addToUsedArgs(args, usedArgs, i);
Expand Down Expand Up @@ -180,7 +174,8 @@ private void addToUsedArgs(String[] args, List<String> usedArgs, int i) {
private void extractProfiler() throws ProfilerException {
OUT_STREAM.printf("%s[1/6] Initializing...%s%n", Constants.ANSI_CYAN, Constants.ANSI_RESET);
try {
new ProcessBuilder("jar", "xvf", "Profiler.jar", "io/ballerina/runtime/profiler/runtime").start().waitFor();
Path profilerRuntimePath = Paths.get("io/ballerina/runtime/profiler/runtime");
new ProcessBuilder("jar", "xvf", "Profiler.jar", profilerRuntimePath.toString()).start().waitFor();
} catch (IOException | InterruptedException exception) {
throw new ProfilerException(exception);
}
Expand Down Expand Up @@ -228,8 +223,8 @@ private void initializeProfiling() throws ProfilerException {
moduleCount++;
}
}
OUT_STREAM.printf(" Instrumented module count: %d%n", moduleCount);
OUT_STREAM.printf(" Instrumented function count: %d%n", balFunctionCount);
OUT_STREAM.printf(" Instrumented module count: %d%n", moduleCount);
OUT_STREAM.printf(" Instrumented function count: %d%n", balFunctionCount);
modifyJar();
} catch (Throwable throwable) {
throw new ProfilerException(throwable);
Expand All @@ -247,7 +242,8 @@ private void modifyJar() throws InterruptedException, IOException {
for (String instrumentedFilePath : instrumentedPaths) {
FileUtils.deleteDirectory(new File(instrumentedFilePath));
}
FileUtils.deleteDirectory(new File("io/ballerina/runtime/profiler/runtime"));
Path filePath = Paths.get("io/ballerina/runtime/profiler/runtime");
FileUtils.deleteDirectory(new File(filePath.toString()));
profilerMethodWrapper.invokeMethods(profilerDebugArg);
}
}
Expand Down Expand Up @@ -281,12 +277,12 @@ private void analyseInstrumentedDirectories(File directory, String absolutePath)
}

private void updateInstrumentedFile(File fileEntry, String absolutePath) {
String fileEntryString = String.valueOf(fileEntry);
String fileEntryString = fileEntry.getPath();
if (fileEntryString.endsWith(Constants.CLASS_SUFFIX)) {
fileEntryString = fileEntryString.replaceAll(absolutePath, "");
int index = fileEntryString.lastIndexOf('/');
fileEntryString = fileEntryString.replaceAll(Pattern.quote(absolutePath), "");
int index = fileEntryString.lastIndexOf(File.separatorChar);
fileEntryString = index == -1 ? "" : fileEntryString.substring(0, index);
String[] fileEntryParts = fileEntryString.split("/");
String[] fileEntryParts = fileEntryString.split(Pattern.quote(File.separator));
instrumentedPaths.add(fileEntryParts[0]);
instrumentedFiles.add(fileEntryString);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ public void invokeMethods(String debugArg) throws IOException, InterruptedExcept
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(),
StandardCharsets.UTF_8))) {
try (InputStreamReader streamReader = new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8);
BufferedReader reader = new BufferedReader(streamReader)) {
reader.lines().forEach(OUT_STREAM::println);
}
process.waitFor();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import io.ballerina.runtime.profiler.util.Constants;
import io.ballerina.runtime.profiler.util.ProfilerException;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -49,8 +50,8 @@ public class HttpServer {

public void initializeHTMLExport() throws IOException {
String profilerOutputDir = System.getProperty(WORKING_DIRECTORY);
OUT_STREAM.printf(" Output: " + Constants.ANSI_YELLOW +
"%s/" + HTML_PROFILER_REPORT + Constants.ANSI_RESET + "%n", profilerOutputDir);
OUT_STREAM.printf(" Output: " + Constants.ANSI_YELLOW + "%s" + File.separator + HTML_PROFILER_REPORT +
Constants.ANSI_RESET + "%n", profilerOutputDir);
Path resourcePath = Paths.get(System.getenv(BALLERINA_HOME)).resolve("resources")
.resolve("profiler");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,6 @@ private void initiateProfiler(Project project) {
// Sets classpath with executable thin jar and all dependency jar paths.
commands.add("--file");
commands.add(getTargetFilePath(project));
commands.add("--target");
commands.add(targetPath.toString());
if (isInProfileDebugMode()) {
commands.add("--profiler-debug");
commands.add(getProfileDebugArg(err));
Expand All @@ -94,6 +92,12 @@ private void initiateProfiler(Project project) {
}
} catch (IOException | InterruptedException e) {
throw createLauncherException("error occurred while running the profiler ", e);
} finally {
try {
Files.deleteIfExists(targetPath);
} catch (IOException e) {
err.println("error occurred while deleting the profiler.jar file");
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,12 @@ private List<LogLeecher> getProfilerLogLeechers(String htmlFilePath) {
new LogLeecher("[2/6] Copying executable..."),
new LogLeecher("[3/6] Performing analysis..."),
new LogLeecher("[4/6] Instrumenting functions..."),
new LogLeecher(" Instrumented module count: "),
new LogLeecher(" Instrumented function count: "),
new LogLeecher(" Instrumented module count: "),
new LogLeecher(" Instrumented function count: "),
new LogLeecher("[5/6] Running executable..."),
new LogLeecher("[6/6] Generating output..."),
new LogLeecher(" Execution time: [1-5] seconds ", true, LogLeecher.LeecherType.INFO),
new LogLeecher(" Output: "),
new LogLeecher(" Execution time: [1-5] seconds ", true, LogLeecher.LeecherType.INFO),
new LogLeecher(" Output: "),
new LogLeecher(htmlFilePath));
}

Expand Down Expand Up @@ -125,15 +125,15 @@ public void testProfilerExecutionWithKillSignal() throws BallerinaTestException
new LogLeecher("[2/6] Copying executable..."),
new LogLeecher("[3/6] Performing analysis..."),
new LogLeecher("[4/6] Instrumenting functions..."),
new LogLeecher(" Instrumented module count: "),
new LogLeecher(" Instrumented function count: "),
new LogLeecher(" Instrumented module count: "),
new LogLeecher(" Instrumented function count: "),
new LogLeecher("[5/6] Running executable...")};
Process process = bMainInstance.runCommandAndGetProcess("profile", new String[]{packageName},
envProperties, sourceRoot);
LogLeecher[] afterExecleechers = new LogLeecher[]{
new LogLeecher("[6/6] Generating output..."),
new LogLeecher(" Execution time:"),
new LogLeecher(" Output: ")};
new LogLeecher(" Execution time:"),
new LogLeecher(" Output: ")};
ServerLogReader serverInfoLogReader = new ServerLogReader("inputStream", process.getInputStream());
addLogLeechers(beforeExecleechers, serverInfoLogReader);
addLogLeechers(afterExecleechers, serverInfoLogReader);
Expand Down

0 comments on commit b2c2c42

Please sign in to comment.