https://laurent-leturgez.com/2017/12/22/profiling-java-application-with-systemtap/https://myaut.github.io/dtrace-stap-book/app/java.html
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:
- Prints the JVM PID
- Wait for a key to be pressed. During this time, you'll have to execute the Systemtap script I'll described later.
- 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 java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
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 = "
+java.lang.management.ManagementFactory.getRuntimeMXBean().getName().split(
"@"
)[
0
]);
System.out.println(
"Press any key when ready ..."
);
try {
BufferedReader in =
new BufferedReader(
new InputStreamReader(System.in));
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/example.java[[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://developers.redhat.com/blog/2014/01/10/probing-java-w-systemtap/
- https://sourceware.org/systemtap/langref/Probe_points.html#SECTION00056000000000000000
- Https://myaut.github.io/dtrace-stap-book/app/java.html
And ... that's all for today!!
Profiling Java application with Systemtap