基于demos\api\samples\offline\
示例,我试图使用Controller Java API来分析脱机模式下在Weblogic上运行的应用程序。
我的问题是,尽管Controller.saveSnapshot()
正常退出,但没有创建.jps文件。
我创建了一个会话配置,当我将其附加到GUI中的进程时,它运行良好。
我将config.xml文件从home
复制到了工作目录。
我通过以下行为我的服务更新了server start
中的JVM参数:
-agentpath:/home/alex/jprofiler10/bin/linux-x64/libjprofilerti.so=offline,id=116,config=/workdir/config_bak.xml
我创建了一个Java程序,该程序开始CPU分析,然后结束CPU分析,并尝试保存分析结果。
Controller.startCPURecording(true);
performProfiledTask();
Controller.stopCPURecording();
String fName =“ test.jps”;
System.out.println(“保存快照:” + fName);
Controller.saveSnapshot(new File(fName));
System.out.println(“保存的快照:” + fName);
程序执行无错误,但未创建快照文件。
我尝试以sudo特权运行它,结果相同。
编辑:
也许我听不懂,但在我看来,如果Controller仅在与配置文件代码相同的进程中运行,则JProfiler脱机版可以工作。
在其他\ child进程中运行时,不会创建文件。
例如,以下代码(我希望创建文件)不会执行该操作。
分析过程:
import java.io.File;
import java.io.IOException;
import com.jprofiler.api.controller.Controller;
//turn on profiling and run a child process with offline profiling enabled in agentpath
public class PerfDemo {
public static void main(String[] args) throws IOException {
System.out.println("Started profiling");
// On startup, JProfiler does not record any data. The various recording subsystems have to be
// switched on programatically.
Controller.startCPURecording(true);
try {
exec(CpuIntensiveProcess.class);
} catch (Exception ex) {
ex.printStackTrace();
System.out.println("Exception: "+ex.getStackTrace());
}
// You can switch off recording at any point. Recording can be switched on again.
Controller.stopCPURecording();
saveSnapshot("snapshot.jps");
}
private static void saveSnapshot(String fileName){
System.out.println("saving snapshot: "+fileName);
Controller.saveSnapshot(new File(fileName));
}
//execute a new process with offline profiling enabled in agentpath
public static int exec(Class klass) throws IOException, InterruptedException{
String javaHome = System.getProperty("java.home");
String javaBin = javaHome +
File.separator + "bin" +
File.separator + "java";
String classpath = System.getProperty("java.class.path");
String className = klass.getName();
ProcessBuilder builder = new ProcessBuilder(
javaBin, "-cp", classpath, "-agentpath:\"c:\\\\Progra~1\\\\jprofiler10\\\\bin\\\\windows-x64\\\\jprofilerti.dll\"=offline,id=110,config=config.xml", className );
System.out.println("starting process");
Process process = builder.inheritIO().start();
process.waitFor();
System.out.println("process finished");
return process.exitValue();
}
}
配置文件过程:
import static java.lang.Math.*;
import java.io.IOException;
public class CpuIntensiveProcess {
public static void main(String[] args) throws IOException {
for (int i=0;i<50000000;i++) {
double d = tan(atan(tan(atan(tan(atan(tan(atan(tan(atan(123456789.123456789))))))))));
double h =d+1;
}
}
}
运行时批处理:
@echo off
SET CLASSPATH=.;jprofiler-controller-10.1.4.jar;agent.jar;
"c:\Program Files\Java\jdk-10.0.1\bin\javac.exe" -cp %CLASSPATH% *.java
java -cp %CLASSPATH% PerfDemo
REM If I run in this configuration, the profiling is of the PerfDemo process and not of the CpuIntensiveProcess process
REM SET AGENTPATH=-agentpath:"c:\\Progra~1\\jprofiler10\\bin\\windows-x64\\jprofilerti.dll"=offline,id=110,config=config.xml
REM java -cp %CLASSPATH% %AGENTPATH% PerfDemo
set "CURR_DIR=%cd%"
pushd "c:\Program Files\jprofiler10\bin"
jpexport %CURR_DIR%/snapshot.jps -outputdir=%CURR_DIR% HotSpots out.csv
popd
type out.csv
运行批处理时,没有创建.jps文件。
当我使用agentpath运行PerfDemo
进程时,会创建jps文件,但是分析是同一进程,而不是其他进程。
输出:
C:\dev\docs\bak\sample_other_process_profiling>profile.bat
Started profiling
Starting child process...
JProfiler> Protocol version 59
JProfiler> Java 9+ detected.
JProfiler> JVMTI version 1.1 detected.
JProfiler> Offline profiling mode.
JProfiler> 64-bit library
JProfiler> Using config file config.xml (id: 110)
JProfiler> Listening on port: 8849.
JProfiler> Enabling native methods instrumentation.
JProfiler> Can retransform classes.
JProfiler> Can retransform any class.
JProfiler> Native library initialized
JProfiler> VM initialized
JProfiler> Retransforming 7 base class files.
JProfiler> Base classes instrumented.
JProfiler> Using instrumentation
JProfiler> Time measurement: elapsed time
JProfiler> CPU profiling enabled
Child process finished
Saving snapshot: snapshot.jps
Controller.saveSnapshot finished, snapshot should exist on the disk: snapshot.jps
The snapshot file C:\dev\docs\bak\sample_other_process_profiling\snapshot.jps does not exist.
The system cannot find the file specified.
答案 0 :(得分:1)
通常仅通过使用-agentpath:
VM参数启动JVM,控制器类才在加载了性能分析代理的JVM中起作用。
在您的情况下,您在未加载性能分析代理的进程中调用控制器类,因此该调用无效。同样,这些调用对子进程没有任何影响。
您必须将对控制器类的调用移至子进程中,以使其正常工作。