Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use input arguments to parse lib home #106

Merged
merged 2 commits into from
Oct 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ jobs:
- name: Execute spring-boot-demo.jar
run: |
dir

Start-Process java `
"-javaagent:C:\runner\spring-startup-analyzer\lib\spring-profiler-agent.jar", `
"-Dproject.name=generic-windows-x64-demo", `
Expand Down Expand Up @@ -272,7 +273,7 @@ jobs:
-Dspring-startup-analyzer.admin.http.server.port=8066 \
-jar /home/runner/spring-startup-analyzer/spring-boot-demo.jar &

sleep 600
sleep 800

file_count=$(ls -al /home/runner/spring-startup-analyzer/output/ | grep -c "alpine-linux-arm64-demo")
if [ "$file_count" -eq 2 ]; then
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@
import java.util.logging.Level;
import java.util.logging.Logger;

import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;

/**
* @author linyimin
**/
public class ProfilerAgentBoostrap {

private static final String BRIDGE_JAR = "spring-profiler-bridge.jar";
private static final String AGENT_JAR = "spring-profiler-agent.jar";

private static final Logger logger = Logger.getLogger(ProfilerAgentBoostrap.class.getSimpleName());
private static final String LIB_HOME = getLibHome();
Expand All @@ -29,6 +33,8 @@ public static void premain(String args, Instrumentation instrumentation) {

logger.info("command args: " + args);

System.out.println("premain LibHome: " + LIB_HOME);

// bridge.jar
File spyJarFile = new File(LIB_HOME + BRIDGE_JAR);
if (!spyJarFile.exists()) {
Expand Down Expand Up @@ -85,7 +91,7 @@ private static List<URL> getJars(String path) throws MalformedURLException {
throw new IllegalStateException(path + " is not exit.");
}

File[] files = folder.listFiles(file -> !file.getName().contains(BRIDGE_JAR) && file.isFile() && file.getName().endsWith("jar"));
File[] files = folder.listFiles(file -> file.isFile() && file.getName().endsWith("jar") && !file.getName().contains(BRIDGE_JAR) && !file.getName().contains(AGENT_JAR));

if (files == null) {
throw new IllegalStateException(path + " does not contain any jar files.");
Expand All @@ -101,18 +107,24 @@ private static List<URL> getJars(String path) throws MalformedURLException {

private static String getLibHome() {

String currentFilePath = ProfilerAgentBoostrap.class.getProtectionDomain()
.getCodeSource()
.getLocation()
.getPath();

File file = new File(currentFilePath);

if (!file.exists()) {
return System.getProperty("user.home") + File.separator + "spring-startup-analyzer" + File.separator;
RuntimeMXBean bean = ManagementFactory.getRuntimeMXBean();
List<String> jvmArgs = bean.getInputArguments();

for (String jvmArg : jvmArgs) {

int index= jvmArg.indexOf(":");
if (index + 1 >= jvmArg.length()) {
continue;
}

String value = jvmArg.substring(index + 1);

if (value.endsWith(AGENT_JAR)) {
return value.substring(0, value.lastIndexOf(File.separator) + 1);
}
}

return file.getParent() + File.separator;
return System.getProperty("user.home") + File.separator + "spring-startup-analyzer" + File.separator + "lib" + File.separator;

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,5 @@ private String buildMethodQualifier(AtEnterEvent event) {

return className + "." + event.methodName;
}

}