This discussion is archived
1 Reply Latest reply: Oct 17, 2012 1:11 PM by Bill S RSS

Need help creating a recording from JVM startup to JVM shutdown (28.0.1)

Bill S Newbie
Currently Being Moderated
I'm trying to record performance of a stand-alone java app from start to finish (memory allocated, CPU level, and other interesting things). I want to start a flight recording when my app starts up and finish the recording when the app exists.

Part 1
Start up is no problem because I can use command line args. Stopping is a bit more tricky because it seems that letting the program terminate naturally causes the recording to error. Since I don't know how long the program will run (somewhere between 20-30 hours), I can't just plug in a duration during startup. If there is a way to do the recording just using command line args, I'm interested in what args to use for the stopping part.

Here is what I tried:
-XX:StartFlightRecording=duration=5d,filename=my_recording.jfr
Part 2
The app is mine, so I'm happy to start/stop the recording programmatically too, if that will work. I tried this:
JVMFactory.getJVM().getDiagnosticCommand().execute("start_flightrecording", "name=myname,duration=0,filename=myfilename.jfr");
but it fails with this error:
Exception in thread "main" java.lang.UnsatisfiedLinkError: no jmapi in java.library.path
     at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1738)
     at java.lang.Runtime.loadLibrary0(Runtime.java:823)
     at java.lang.System.loadLibrary(System.java:1028)
     at bea.jmapi.JVMImpl.<clinit>(JVMImpl.java:26)
     at com.bea.jvm.JVMFactory$1.run(JVMFactory.java:39)
     at com.bea.jvm.JVMFactory$1.run(JVMFactory.java:37)
     at java.security.AccessController.doPrivileged(Native Method)
     at com.bea.jvm.JVMFactory.createJVM(JVMFactory.java:36)
     at com.bea.jvm.JVMFactory.getJVM(JVMFactory.java:29)
     at RunTests.main(RunTests.java:69)
The JRockit book warns about security manager errors (I don't know if this is a security manager error or just a different type of security error). My program is kind of like "HelloWorld.java", it doesn't run in a container or use any fancy frameworks. I tried adding a policy file, so now my batch file looks like this:
set PATH=D:\jrockit64-R28.0.1\bin;%PATH%
set JAVA_HOME=D:\jrockit64-R28.0.1
set JVM_ARGS=-Djava.security.manager -Djava.security.policy=TestDW.policy
java %JVM_ARGS% -jar TestDW.jar 4 21 "2010-05-01 10:00:00 GMT"
pause
and my TestDW.policy file looks like this:
grant { 
     permission com.bea.jvm.ManagementPermission "createInstance";
};
but that isn't working either and I get this error:
Exception in thread "Main Thread" java.security.AccessControlException: access denied (java.lang.RuntimePermission setFactory)
     at java.security.AccessControlContext.checkPermission(AccessControlContext.java:323)
     at java.security.AccessController.checkPermission(AccessController.java:546)
     at java.lang.SecurityManager.checkPermission(SecurityManager.java:532)
     at java.lang.SecurityManager.checkSetFactory(SecurityManager.java:1612)
     at java.net.URL.setURLStreamHandlerFactory(URL.java:1081)
     at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:46)
(I created my 'Runnable JAR' file in eclipse)

Any tips on how to start the recording through code would be helpful (or links on the web or page numbers in the JRockit book).

Thank you!
Bill
  • 1. Re: Need help creating a recording from JVM startup to JVM shutdown (28.0.1)
    Bill S Newbie
    Currently Being Moderated
    I think the problem was with my environment picking up the wrong JVM (not JRockit). Here is the full listing of my working test class. No special security files are needed whatsoever.
    import java.util.ArrayList;
    
    import com.bea.jvm.DiagnosticCommand.Parameter;
    import com.bea.jvm.JVMFactory;
    
    // In eclipse, go to the 'Window' menu and select 'Preferences'
    // On the dialog, type 'Installed JREs' in the box that says, "Type Filter Text"
    // Click on 'Installed JREs' on the tree
    // If the default JRE is not JRockit v28 or later, click 'add' to add it
    // eg. C:\Program Files (x86)\Java\jrockit-jdk1.6.0_17
    // Once added, check the box in front to select it (sometimes it doesn't stick, so double-check it)
    
    // After creating your project, go to the 'Project' menu and select 'Properties'
    // On the dialog, select 'Java Build Path' then the 'Libraries' tab
    // Click 'Add External Jar' and find the file 'rt.jar' in your JRockit JVM folder
    
    public class flightRecordingExample
    {
         public static void main(String[] args) throws InterruptedException
         {
              // Start the flight recorder
              ArrayList<Parameter> startParameters = new ArrayList<Parameter>();
              startParameters.add(new Parameter("name", "recording_1"));
              startParameters.add(new Parameter("duration", 0)); // 0 means record forever
              startParameters.add(new Parameter("filename", "C:/myRecording.jfr"));
              JVMFactory.getJVM().getDiagnosticCommand().execute("start_flightrecording", startParameters);
    
              // Do something for a while
              for (int i = 0; i < 10; i++)
              {
                   System.out.println(String.format("CPU load is %3.2f%%", JVMFactory.getJVM().getMachine().getCPULoad() * 100.0));
                   Thread.sleep(1000);
              }
    
              // Stop the flight recorder
              ArrayList<Parameter> stopParameters = new ArrayList<Parameter>();
              stopParameters.add(new Parameter("name", "recording_1"));
              JVMFactory.getJVM().getDiagnosticCommand().execute("stop_flightrecording", stopParameters);
    
              // Now open C:/myRecording.jrf using jrmc.exe in your JRockit JVM /bin directory
    
         }
    }

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points