我正在尝试使用控制器JavaAPI,基于
demos\api\samples\offline\
例子。
Controller.saveSnapshot()
-
-
我从中复制了config.xml文件
home
到工作目录。
-
server start
为我提供以下服务:
-agentpath:/home/alex/jprofiler10/bin/linux-x64/libjprofilerti.so=offline,id=116,config=/workdir/config_bak.xml
-
我创建了一个Java程序,该程序启动CPU评测,然后结束CPU评测,并尝试保存评测结果。
Controller.startCPURecording(真);
Controller.stopCPURecording();
System.out.println(“保存快照:+fName”);
Controller.saveSnapshot(新文件(fName));
System.out.println(“保存的快照:+fName”);
程序执行时不会出错,但不会创建快照文件。
编辑:
在其他\子进程中运行时,不会创建该文件。
例如,我希望创建文件的以下代码并没有这样做。
分析过程:
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
当我运行
PerfDemo
使用agentpath处理时,会创建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.