alex440
alex440

Reputation: 1657

Using Jprofiler Controller with weblogic process in offline mode

I am trying to profile an application running on Weblogic in offline mode, using the Controller Java API, based on the demos\api\samples\offline\ example.

My problem is that the .jps file is not being created, though Controller.saveSnapshot() exits with no error.

  1. I created a session configuration that is working good when I am attaching to the process in GUI.

  2. I copied the config.xml file from home to the working directory.

  3. I updated the JVM params in server start for my service with the following line:

    -agentpath:/home/alex/jprofiler10/bin/linux-x64/libjprofilerti.so=offline,id=116,config=/workdir/config_bak.xml

  4. I created a Java program which starts CPU profiling and then ends CPU profiling and tries to save the profiling result.

    Controller.startCPURecording(true);
    performProfiledTask();
    Controller.stopCPURecording();

    String fName = "test.jps"; System.out.println("saving snapshot: "+fName); Controller.saveSnapshot(new File(fName));
    System.out.println("saved snapshot: "+fName);

The program executes without error but the snapshot file is not created.

I tried to run it with sudo privileges with the same result.


EDIT:

Maybe I don't understand something, but it seems to me that the JProfiler offline works if the Controller is run only in the same process as the profiled code.

When it is run in a different\child process, the file is not being created.

For example, the following code, which I expect to create file, does not do it.

Profiling process:

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(); 
    }
}

Profiled process:

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;
        }        
    }   
}

Runtime batch:

@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

When I run the batch, there is no .jps file created.

When I run the PerfDemo process with agentpath, the jps file is created, but the profiling is of the same process, not of the other process.

Output:

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.

Upvotes: 1

Views: 362

Answers (1)

Ingo Kegel
Ingo Kegel

Reputation: 48045

The controller class is only functional in a JVM where the profiling agent is loaded, usually by starting the JVM with the -agentpath: VM parameter.

In your case, you call the controller class in a process where the profiling agent is not loaded, so the calls have no effect. Also, these calls have no effect whatsoever on child processes.

You would have to move the calls to the controller class into the child process to make it work.

Upvotes: 1

Related Questions