![]() |
![]() |
![]() |
![]() |
3 Profiling a Java program
3.1 Starting a Java Application
OptimizeIt can profile a Java application that is either packaged in a JAR file, or is given the location of the class file containing the Main method for the application.
Note: By default OptimizeIt profiles your application with a Java 2 runtime. You can select another virtual machine by using the Virtual Machine tab, see "Virtual machine options" in chapter 3.6.
To start profiling an application
1. Select New from the File menu.
2. In the Program type section of the dialog box, choose Application.
3. Enter the main class of the application.
- If the application is in a JAR file, click Browse to select the JAR file location.
- If the application is in a ZIP file, enter the fully qualified name of the class containing the Main method. For example:
com.foo.bar.Main
- If the application is not in a JAR or ZIP file, click Browse to select the class file that contains the main method.
4. (Optional) Make sure the working directory is correct. If the application does not require a working directory, ignore this option.
5. (Optional) Add classes to the class path. If the application requires special classes not indicated in the default class path, click Change under the Class path list to select directories, jar file or zip files containing the extra classes.
6. (Optional) Add source code directories to the source path. This allows OptimizeIt to show relevant source code. Specific files can be added later during the profiling session.
7. (Optional) Click the Virtual Machines tab. Select the virtual machine you want to use in the list. For more information on the different options available in this tab, see "Virtual machine options" in chapter 3.6.
8. Click the Start Now button. OptimizeIt starts the application and opens the memory profiler for the application.
Once you have configured OptimizeIt to start your application, you can save this configuration and reopen it later. Save and Open commands are available from the file menu.
3.2 Starting a Java Applet
OptimizeIt can profile a Java applet given an HTML file or a URL.
Note: Profiling an applet requires a Java Development Kit (JDK). Make sure to select a virtual machine included in a JDK from the Virtual Machine tab, see "Virtual machine options" in chapter 3.6.
To start profiling an applet
1. Select New from the File menu.
2. In the Program type section of the dialog box, choose Applet.
3. Enter the file name or URL of the applet.
4. If the applet is on your local disk, click the Browse button and select the HTML file. If the applet is a web page, enter the URL of that page.
5. (Optional) Make sure the working directory is correct. If the applet does not require a specific working directory, ignore this option.
6. (Optional) Add classes to the class path. If the applet requires special classes not indicated in the default class path, click Change under the Class path list to select directories, jar files or zip files containing the extra classes.
7. (Optional) Add source code directories to the source path. This allows OptimizeIt to show relevant source code. Specific files can be added later during the profiling session.
8. (Optional) Click the Virtual Machines tab. Select the virtual machine you want to use in the list. For more information on the different options available in this tab, see "Virtual machine options" in chapter 3.6.
9. Click the Start Now button. OptimizeIt starts the applet and opens the memory profiler for the applet.
Once you have configured OptimizeIt to start your applet, you can save this configuration and reopen it later. Save and Open commands are available from the file menu.
Note: When started from OptimizeIt, an applet is run with AppletViewer. It is also possible to profile an applet that run inside a web browser when using the Java plug-in. This operation is described in the tutorial "Profiling an applet running inside a web browser".
3.3 Starting a Java Servlet
OptimizeIt can profile a Java servlet given its class.
Note: By default OptimizeIt profiles your application with a Java 2 runtime. You can select another virtual machine by using the Virtual Machine tab, see "Virtual machine options" in chapter 3.6.
To start profiling a servlet
1. Select New from the File menu.
2. In the Program type section of the dialog box, choose Servlet.
3. If it is the first time you have profiled a servlet with OptimizeIt, the servlet wizard will start. The wizard will guide you through the servlet configuration, see "Configuring servlet support" in chapter 2.4 for more information.
4. Click Browse to select the class file that contains your servlet.
5. (Optional) Make sure the working directory is correct. If the servlet does not require a specific working directory, ignore this option.
6. (Optional) Add any parameters required by the servlet.
7. (Optional) Add classes to the class path. If the servlet requires special classes not indicated in the default class path, click Change under the Class path list to select directories, jar files and zip files containing the extra classes.
8. (Optional) Add source code directories to the source path. This allows OptimizeIt to show relevant source code. Specific files can be added later during the profiling session.
9. Click the Virtual Machines tab. Select the virtual machine you want to use in the list. For more information on the different options available in this tab, see "Virtual machine options" in chapter 3.6.
10. Click the Start Now button. OptimizeIt starts the servlet and opens the memory profiler for the servlet. The browser is automatically launched to execute the servlet. If any servlet parameters have been specified, they are added to the servlet URL.
3.4 Profiling EJBs or JSPs
Profiling EJBs or JSPs can be done by profiling the application server that runs your Java code. OptimizeIt can be integrated with most application servers. For more information, see "Integration with application servers" in chapter 8.2.
3.5 Start options
OptimizeIt allows you to control profiling through several options. These options affect the Java application selected in the Edit settings panel.
Option Description Pause after launch Pauses the test program just before executing the Main method. Use this option to give yourself some time to configure OptimizeIt or to start some profilers before the tested application starts.
Disables the method System.exit() in the virtual machine. Use this option to test a command line program such as a compiler that performs a task and then exits the running virtual machine. Use the Stop button to exit the program when your profiling is complete.
Disables the OptimizeIt memory profiler. The OptimizeIt memory profiler adds overhead that can change the CPU profiler results. Use this option when you are focusing on CPU-related issues only.
Opens a console for the program. Use this option if your program expects some input from System.in. When this option is off, anything printed using System.out and System.err is printed in the OptimizeIt console.
Enables the OptimizeIt audit system API. When the API is enabled, OptimizeIt memory and CPU profilers are disabled by default. This allows the tested program to use OptimizeIt API to precisely enable both profilers when needed.
Starts the CPU profiler just before executing the main method. The CPU profiler is started with the current option selected in the CPU profiler inspector.
Extra Java parameters Specifies a string passed directly to the virtual machine running the test program. Use this field to add Java virtual machine arguments such as -mx or -verbosegc. Specifies a string passed directly to the tested application when launched. Lists the class path defined in the default class path. If the test program requires extra classes, JAR files, or ZIP files, click the Change button to select additions to the class path. These additions apply to this test program only.
Lists the directories in which OptimizeIt searches for source code. If the test program source code is not included in the default source path, click the Change button to add a location to the source path. These additions apply to this test program only.
3.6 Virtual machine options
The OptimizeIt Virtual Machine Tab allows you to select and configure the virtual machine you want to use to profile your application.
Adding a virtual machine
In order to add a virtual machine click on the Add virtual machines button. This starts the virtual machine wizard:
1. Click on Next.
2. Select a directory from which OptimizeIt will look for available virtual machines.
3. Click on search.
4. Once OptimizeIt has finished its search, it displays the virtual machines found.
5. Click on finish to add those virtual machines to the list of available virtual machines.
Setting the virtual machine properties
When you select a virtual machine from the list, OptimizeIt only enables the available options for that virtual machine.
Java runtime
This property sets the virtual machine runtime used. The following table shows which flag is added to the virtual machine invocation:
Audit System
This option sets which library OptimizeIt should use. OptimizeIt automatically selects the appropriate library when the selected virtual machine is recognized. See "OptimizeIt libraries" on page 24 for more information on the different libraries.
Enabling the JIT
You can enable the JIT (Just In Time compiler) when profiling by selecting the corresponding option.
Note: The JIT can only be enabled when the Universal JVMPI/JNI audit system is selected.
3.7 Profiling a program started from the command line
In addition to running your test program from OptimizeIt, you can run it from a command prompt.
Invoking your application from outside OptimizeIt allows you to do the following:
· Set custom variables
· Run the application as part of a script
· Run the program on a different machine
To profile your test program invoked from a command prompt:
1. Set up the OptimizeIt audit system
2. Launch the OptimizeIt audit system, specifying your test program
3. Run OptimizeIt
4. Connect the audit system to the OptimizeIt user interface
Setting up the OptimizeIt audit system
To use the OptimizeIt audit system, you need to modify your CLASSPATH and PATH environment variables to include the following directories:
CLASSPATH <InstallDir>\OptimizeIt\lib\optit.jar
PATH <InstallDir>\OptimizeIt\lib
Launching your program
The OptimizeIt audit system is a set of Java classes and native code.
To show the audit system options
C:\> java intuitive.audit.Audit
To run the audit system with JDK 1.1
Invoke the audit system with the following options:
C:\> java -noclassgc -Djava.compiler=NONE intuitive.audit.Audit [-port portNumber] [-pause] [-wait] [-dmp] [-noexit] [-startCPUprofiler[:<CPUProfilerOptions>]] [-offlineprofiling[:<offlineprofilingOptions>]] [-enableAPI] ClassName arg1, arg2, ...
Note: With early Java versions the OptimizeIt audit system requires that the class garbage collection is disabled. Always use the -noclassgc Java virtual machine argument when starting an application from the command line. You also need to disable the JIT. This can be done by adding -Djava.compiler=NONE
To run the audit system with JDK 1.2
Invoke the audit system with the following options:
C:\> java -classic -Xrunoii -Xnoclassgc -Djava.compiler=NONE intuitive.audit.Audit [-port portNumber] [-pause] [-wait] [-dmp] [-noexit] [-startCPUprofiler[:<CPUProfilerOptions>]] [-offlineprofiling[:<offlineprofilingOptions>]] [-enableAPI] ClassName arg1, arg2, ...
Note: With some environments, the -classic option is necessary to force the classic runtime. -Xrunoii is necessary to load OptimizeIt's JVMPI agent. The OptimizeIt audit system requires that the class garbage collection is disabled. Always use the -Xnoclassgc Java virtual machine argument when starting an application from the command line. You also need to disable the JIT. This can be done by using -Djava.compiler=NONE.
Note: Running an applet with JDK 1.2 requires that you use oldjava.exe instead of java.exe
To run the audit system with JDK 1.3
Invoke the audit system with the following options:
C:\> java -classic -Xrunoii intuitive.audit.Audit [-port portNumber] [-pause] [-wait] [-dmp] [-noexit] [-startCPUprofiler[:<CPUProfilerOptions>]] [-offlineprofiling[:<offlineprofilingOptions>]] [-enableAPI] ClassName arg1, arg2, ...
Audit system options
The following table describes these parameters. Note that many of these options provide the same functionality as the Start options.
Description Specifies the port you want to use for the communication link between the audit utility and the OptimizeIt application.
Causes the launched program to be paused immediately after launch.
Enables the OptimizeIt audit system API. When the API is enabled, OptimizeIt memory and CPU profiler are disabled. The audit system waits for the test program to enable profilers from Java.
Starts the CPU profiler just before executing the main method, See "Starting the CPU profiler from the command line" on page 30 for more information on the available options.
Starts the profiling in offline mode. Snapshots are generated automatically at a given time, see "Offline profiling" in chapter 3.9 for more information on the available options.
Specifies the location of OptimizeIt DLLs if different from your PATH environment variable. Note that this option has been deprecated since OptimizeIt 3.1. You sould not need to use this option.
The main class the for the test program. If the test program is an applet, use sun.applet.AppletViewer and then add the path of the applet HTML file or URL.
OptimizeIt libraries
OptimizeIt comes with several libraries to support the different virtual machines:
The audit system automatically detects which virtual machine is used and then selects the matching library. If you want to select a specific library to load, use the -DAUDIT=<libraryType> property, where libraryType is one of the following: 11, 12 or jni.
For example, the following line starts the profiling of the SwingSet program and specifies OptimizeIt to use the Java 2 Universal library:
c:\> java -classic -Xrunoii -DAUDIT=jni intuitive.audit.Audit SwingSet
Examples with JDK 1.1
Use the following command to launch the applet contained in the file example.html:
C:\> java -noclassgc -Djava.compiler=NONE intuitive.audit.Audit sun.applet.AppletViewer example.html
Use the following command to pause the application com.busy.BusyApp immediately after launch:
C:\> java -noclassgc -Djava.compiler=NONE intuitive.audit.Audit -pause com.busy.BusyApp
Examples with JDK 1.2
Use the following command to launch the applet contained in the file example.html:
C:\> oldjava -classic -Xrunoii -Xnoclassgc -Djava.compiler=NONE intuitive.audit.Audit sun.applet.AppletViewer example.html
Use the following command to pause the application com.busy.BusyApp immediately after launch:
C:\> java -classic -Xrunoii -Xnoclassgc -Djava.compiler=NONE intuitive.audit.Audit -pause com.busy.BusyApp
Example with JDK 1.3
Use the following command to start the com.busy.BusyApp application with the CPU profiler automatically started:
C:\> java -classic -Xrunoii intuitive.audit.Audit -startCPUprofiler com.busy.BusyApp
Connecting the audit system to the OptimizeIt user interface
After the test program is running, you need to direct the results generated by the audit system into the OptimizeIt user interface. This procedure is called "attaching" OptimizeIt to the test program.
To show the audit system results
1. In OptimizeIt, select New from the File menu.
2. In the Edit Settings panel select Remote Application in the Program Type section.
3. Type localhost in the Host Name field.
4. Enter the port number you want to use for the communication link between OptimizeIt and the audit system in the Port number field. Change this value only if you used the -port option when launching the test program.
5. (Optional) Add source code directories to the source path. This allows OptimizeIt to show the relevant source code. Specific files can be added later during the profiling session.
6. Click the Attach Now button.
3.8 Profiling a Java program running on a different machine
Profiling a Java program running on a different machine is similar to testing a Java program started from the command line. Follow the instructions above to start the test program on another machine. Start OptimizeIt, and create some new settings with Remote Application as Program type. In the Host name field, type the name of the machine running the test program.
3.9 Offline profiling
OptimizeIt allows you to start the profiling session from the command line and automatically generate snapshots at fixed intervals. Snapshots can be reloaded in OptimizeIt for later analysis. This feature can be used to profile an application server over a long period or in a production environment (for example). Offline profiling also limits overhead since you do not attach to the application.
Note: You cannot attach OptimizeIt to an audit system configured for offline profiling.
Starting offline profiling
In order to start online profiling, you must start your application from the command line with the option:
-offlineprofiling[:<offlineprofilingOptions>]
Syntax
Each offline profiling option is associated with a value. The option and the value must be separated with an equal character `='. The different options are separated by comas `,'. No space should be used.
Note: The value of the directory and comments options may contain spaces. in that case, make sure to include the values between quotes `"'.
For the initial delay and delay values, the unit used is specified just after the value, with the following syntax:
Symbol used Corresponding unit h hours m minutes s seconds ms milliseconds If no unit is specified, seconds are assumed.
· delay=1m sets the delay to 1 minute
· initialDelay=2h sets the initial delay to 2 hours
· delay=10 sets the delay to 10 seconds
For values that are boolean the syntax is: true, false.
Options
The following table describes the different options available:
Option Description initialDelay The delay before the generation of the first snapshot. Must be positive or zero. If not specified, the value of the option delay is used delay The delay between the generation of 2 snapshots. Must be positive and not zero. counter The number of snapshots to be generated. Must be positive. If not specified, the number of snapshots generated is not limited. directory The directory where the snapshots are generated. Must be a valid directory. If not specified, snapshots are generated in the current directory. filename The name used for the snapshots. If no filename is specified, the filename is constructed from the main class name. updateFile Boolean that sets if the same snapshot should be reused. If set to yes, only one file is used for all the snapshots and the preceding snapshot is overwritten each time a new snapshot is generated. appendTime Boolean that sets if the date and time of the snapshot generation should be appended at the end of the filename. True by default. If set to false, a counter is used to diferenciate the different snapshot files. includeCPU Boolean that sets if the CPU profiling information should be stored in the snapshot. If true and the CPU profiler is not automatically started, it starts the CPU profiler with the default options. See "Starting the CPU profiler from the command line" on page 30 for more information. includeMemory Boolean that sets if the memory profiler information (heap mode and backtrace mode) should be stored in the snapshot. True by default. includeReferences Boolean that sets if the memory profiler reference graph (and reference from roots with JDK 1.2 and later) should be stored in the snapshot. False by default. Note: The amount of data to be stored for the reference mode is important. The snapshots generated with this option are larger files and take more time to be generated. Only select this option when you really need the reference mode information.
comment String used to add comments to the snapshot. Use quotes " if the string contains spaces. Examples
The following command line starts the offline profiling of the application AppServer with the JDK 1.2. A snapshot will be generated every minute. The filenames of the snapshot will be AppServer_snapshot_<dateAndTime>.snp. The snapshots will be generated in the directory c:\TEMP.
c:\>java -classic -Xrunoii -Xnoclassgc -Djava.compiler=NONE intuive.audit.Audit -offlineprofiling:delay=1m,directory=c:\TEMP com.busy.AppServer
The following command line starts the profiling of the SwingSet application with the JDK 1.2. The first snapshot will be generated after 5 minutes, and then a snapshot will be generated every hour to reach a total of 10 snapshots. The reference graph will be included. The snapshots will be generated in the current directory, and will be named profiling<dateAndTime>.snp.
c:\>java -classic -Xrunoii -Xnoclassgc -Djava.compiler=NONE intuive.audit.Audit -offlineprofiling:initialDelay=5m,delay=1h,counter=10,filename=profiling,includeReferences=true SwingSet
The following command line starts the profiling of the BusyApp application with the JDK 1.3. A snapshot will be regenerated every 5 minutes in the current directory, using the filename test.snp. The CPU information wil be included. (See "Starting the CPU profiler from the command line" on page 30 for more information on the -startCPUprofiler command).
c:\>java -classic -Xrunoii intuive.audit.Audit -startCPUprofiler:type=sampler,samplingPeriod=10 -generatesnapshot:delay=5m,filename=test,includeCPU=true,updateFile=true BusyApp
The following command line starts the profiling of the AppServer application with the JDK 1.1. Just one snapshot will be generated, and will be updated each hour (each new snapshot will overwrite the previous snapshot).
c:\>java -noclassgc -Djava.compiler=NONE intuitive.audit.Audit -generatesnapshot:delay=1h,updateFile=true,comment="Snapshot of the application server in a stressfull environment." AppServer
Starting the CPU profiler from the command line
By default, the OptimizeIt CPU profiler is only started when triggered from the OptimizeIt user interface. Use the following option to start the CPU profiler automatically before the tested program is started.
-startCPUprofiler[:<CPUoptions>]
Syntax
Each option is associated with a value, the different options are separated by comas `,'.
Options
The following table describes the different options available:
Note: These options are similar to the options of the CPU profiler accessible from OptimizeIt, see "Using the CPU profiler" in chapter 5 for more information on these options.
Examples
The following command line starts the application BusyApp with JDK 1.2 with the sampler. The sampling rate is 5 ms, the display precision is line and only the real CPU time is recorded.
c:\> java -classic -Xrunoii -Xnoclassgc -Djava.compiler=NONE intuitive.audit.Audit -startCPUprofiler:type=sampler,samplingPeriod=5,onlyCPU=true BusyApp
The following command launches the applet contained in the file example.html with JDK 1.2, and starts the instrumentation. The precision is milliseconds, and methods that consumes less than 50 ms are excluded.
C:\> java -classic -Xrunoii -Xnoclassgc -Djava.compiler=NONE intuitive.audit.Audit -startCPUprofiler:type=instrumentation,precision=milli,filterEnabled=true,filterDelay=50 sun.applet.AppletViewer example.html
Using filters from the command line
OptimizeIt provides filters to filter the profiling information, See "What are filters" on page 34 for more information on filters.
In order to use filters for offline profiling, OptimizeIt provides an option to specify filters from the command line. The syntax of this option is:
-filter[:<filterOptions>]
Syntax
Each option is associated with a value, the different options are separated by comas `,'.
The value of the option patternList is a list of patterns (one or more) separated by comas `,'. The list is in parenthesis `(` and `)'.
A pattern is a string that describe a package, a class or a method. The patterns can use the well know special characters * and !. For example, java.lang.* means all the packages and classes included under java.lang, and !java.swing.* means everything that is not included in the java.swing package,
Options
The following table describes the different options available:
Examples
The following command line starts the application BusyApp with JDK 1.3 and filter for memory profiler only the resources used by the String class and the swing package.
c:\> java -classic -Xrunoii intuitive.audit.Audit -filter:CPU=false,memory=true,operation=or,patternList=(java.lang.String,javax.swing.*) BusyApp
3.10 Profiling with filters
OptimizeIt provides filters that can be used to automatically remove information about resources used by particular packages, classes or methods when profiling.
What are filters
Filters are sets of patterns that define packages, classes or methods that should be ignored by OptimizeIt when profiling an application, an applet or a remote application. Filters can be applied to the CPU profiler, memory profiler or both.
When a filter is applied to the CPU profiler, OptimizeIt ignores CPU or time usage consumed by Java code matching the filter pattern. When a filter is applied to memory profiler, OptimizeIt ignores object allocations performed by Java code matching the filter pattern.
OptimizeIt provides several filters ready to use, including filters dedicated to many application servers. You can also create your own filters, see "Creating filters" in chapter 7.6.
Filters are very convenient for profiling servlets, EJBs and JSPs. In that context, they can be used to remove resources used by the application server, allowing you to focus on your Java code.
Note: Filters only remove information about resources used by code matching the filter pattern. Filters don't remove information about methods invoked from filtered methods. For example, if a filtered method calls an unfiltered method that allocates an instance, this instance won't be filtered. Identically, if a filtered method invokes a method that consumes CPU time, this CPU time won't be filtered.
Using filters
1. Select New from the File menu.
2. Fill the Startup section as described in the previous sections.
3. Switch to the Filters section by clicking the Filters tab.
4. If you want to see the patterns associated with a filter, double click on the filter in the table.
5. To enable a filter with the memory profiler, click the Ignore memory usage column.
6. To enable a filter for CPU profiler, click the Ignore CPU usage column.
7. If you want to disable all filters, click the Disable all filters option.
8. When you are done with your filter selection, click the Start now button (Attach now if you profile a remote application).
Note: Filters are specified at launch time. You can't change the filters for a profiling already started.
3.11 Starting OptimizeIt from the test program
In some environments, the Java virtual machine is started automatically. The OptimizeIt audit system can be invoked from the test program using the OptimizeIt application programming interface (API). Invoking OptimizeIt from your application allows you to invoke profiling at specific places in your source code.
To invoke OptimizeIt from inside your test program
1. Set up the OptimizeIt audit system
2. Add OptimizeIt API calls in your program
3. Start your test program
4. Run OptimizeIt
5. Connect the audit system to the OptimizeIt user interface
Setting up the OptimizeIt audit system
To use the OptimizeIt audit system, you need to modify your CLASSPATH and PATH environment variables to include the following directories:
CLASSPATH: <InstallDir>\OptimizeIt\lib\optit.jar
PATH: <InstallDir>\OptimizeIt\lib
Adding OptimizeIt API calls in your program
The following example shows how to start the OptimizeIt audit system from Java code. For more information about OptimizeIt audit API, read the API reference documentation.
import intuitive.audit.Audit;
void startAuditSystem() {
/** Start the OptimizeIt audit system with some default options **/
Audit.start(1470, Audit.DEFAULT_OPTIONS);
}
/** Note: after starting the audit system. you need to start the profilers by using Audit.enableProfiler(). If you want the profilers to be enabled all the time, change the option in Audit.start() from Audit.DEFAULT_OPTIONS to Audit.PROFILERS_ALWAYS_ENABLED **/
Starting your test program
Although the test program starts the audit system, some extra parameters need to be added to the code or the shell script that starts the virtual machine.
With JDK 1.1, it is necessary to disable class garbage collection by using the -noclassgc option. It is also necessary to disable the JIT.
Example: java -noclassgc -Djava.compiler=NONE TestProgram
· disable class garbage collection
With JDK 1.2, it is necessary to use the following options:
· start the oii profiling interface
· disable the JIT
· disable Hotspot
Example: java -classic -Xnoclassgc -Xrunoii -Djava.compiler=NONE TestProgram
With JDK 1.3, it is necessary to use the following options:
· start the oii profiling interface
· select the classic runtime
Intuitive Systems http://www.optimizeit.com optimizeit@intuisys.com |
![]() |
![]() |
![]() |
![]() |