Profiling Java application with Systemtap

Source: Internet
Author: User
Tags systemtap

I ' m not a JVM internals geek but I am sure there was a a-do the job without restarting the JVM, and I found some COO L Stuff with Systemtap.

To does this, you has to install the packages on your Linux Distribution:systemtap and Systemtap-runtime-java (and Configu Re correctly your user environment):

[email protected] ~]# Yum install Systemtap Systemtap-runtime-java

Please note that I used a CentOS 7.4 distribution.

Then, and for the demo, I wrote a very small piece of Java. Do these steps:

    1. Prints the JVM PID
    2. Wait for a key to be pressed. During this time, you'll have to execute the Systemtap script I'll described later.
    3. Execute a loop ten times, each loop with the print a message and wait one second, and this last step was executed in a method n Ame "Loop_and_wait".

Here ' s the sample code:

123456789101112131415161718192021222324252627282930313233343536 package com.premiseo;import java.lang.*;import;import;import;class Example {   public static void loop_and_wait(int n) throws InterruptedException{           System.out.println("Waiting "+n+"ms... Tick");           Thread.sleep(n);        }   public static void main(String[] args) {      System.out.println("PID = ""@")[0]);      System.out.println("Press any key when ready ...");      try {        BufferedReader in = new BufferedReader(new InputStreamReader(;        String next = in.readLine();      }      catch (IOException ioe) {        ioe.printStackTrace();      }      try {        for (int i=0;i<10;i++) {           loop_and_wait(1000);        }      }      catch (InterruptedException ie) {        ie.printStackTrace();      }   }}

Then, compile and execute ... very basic I said

[Email protected] java]$ JAVAC-CP $CLASSPATH:. Com/premiseo/[[email protected] java]$ JAVA-CP $CLASSPATH:. Com.premiseo.ExamplePID = 9928Press any key when ready ... Waiting 1000ms ... Tickwaiting 1000ms ... Tickwaiting 1000ms ... Tickwaiting 1000ms ... Tickwaiting 1000ms ... Tickwaiting 1000ms ... Tickwaiting 1000ms ... Tickwaiting 1000ms ... Tickwaiting 1000ms ... Tickwaiting 1000ms ... Tick

Now, to answer to Tanel, I used a short Systemtap script that would profile the program and specially the loop_and_wait met Hod. I'll count the number of times the Loop_and_wait method has been called, and I'll account the time spent in this metho D execution.

To does, I had to write the probes related to:

    • The full name of the class, including the package name:com.premiseo.Example
    • The class name where the method is Defined:example
    • The method name I want to Profile:loop_and_wait

The first one would be executed when the program would start to execute the targeted method (Java ("Com.premiseo.Example"). Cl ("Example"). Method ("Loop_and_wait")), the second one would be a executed when the method would return (Java ("COM.PREMISEO.E Xample "). Class (" Example "). Method (" Loop_and_wait "). return)

The related Systemtap script is given below:

#!/usr/bin/env Stapglobal counter,timespent,tprobe begin {  printf ("Press CTRL + C to stop profiling\n")  counter= 0  timespent=0}probe java ("Com.premiseo.Example"). Class ("Example"). Method ("Loop_and_wait") {  counter++  T=gettimeofday_ms ()}probe java ("Com.premiseo.Example"). Class ("Example"). Method ("Loop_and_wait"). return{  Timespent+=gettimeofday_ms ()-t}probe End {   printf ("Number of calls for Loop_and_wait method:%ld \ n",    Counter)   printf ("Time Spent in method loop_and_wait:%ld msecs \ n", Timespent)}

Execution of this Systemtap script gave the following result (click the image for full size):

Is it dynamic? Yes, no need to restart the running JVM process from your want to target. If you want to target a specific JVM process ID, you can use the STAP's "-X" option, add the Modify your probe definition Like this:

Probe Java ("Com.premiseo.Example"). Class ("Example"). Method ("Loop_and_wait") {  if (pid () = = Target ())    counter++    T=gettimeofday_ms ()}

There ' s a limitation, you cannot use Wilcards in the Java probe definition (Java ("Com.premiseo.Example"). Class ("Example"). Method ("loop*") ... for example). That would has been useful to profiles a set of methods in the same class ... but not possible currently.

If you want to read more about this kind of stuff, please read the following websites:

    • Https://

And ... that's all for today!!

Profiling Java application with Systemtap

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.