Profiling Java Application with Systemtap
https://laurent-leturgez.com/2017/12/22/profiling-java-application-with-systemtap/
I’m not a JVM internals geek but I was sure there was a way to do the job without restarting the JVM, and I found some cool stuff with Systemtap.
To do this, you have to install two packages on your linux distribution: systemtap and systemtap-runtime-java (and configure correctly your user environment):
[root@spark ~]# 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 that do these steps:
- Prints the JVM PID
- Wait for a key to be pressed. During this time, you will have to execute the systemtap script I will described later.
- Execute a loop ten times, each loop with print a message and wait one second, and this last step is executed in a method name “loop_and_wait”.
Here’s the sample code:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
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
[spark@spark java]$ javac -cp $CLASSPATH:. com/premiseo/Example.java
[spark@spark java]$ java -cp $CLASSPATH:. com.premiseo.Example
PID = 9928
Press any key when ready ... Waiting 1000ms... Tick
Waiting 1000ms... Tick
Waiting 1000ms... Tick
Waiting 1000ms... Tick
Waiting 1000ms... Tick
Waiting 1000ms... Tick
Waiting 1000ms... Tick
Waiting 1000ms... Tick
Waiting 1000ms... Tick
Waiting 1000ms... Tick
Now, to answer to Tanel, I used a short systemtap script that will profile the program and specially the loop_and_wait method. I will count the number of times the loop_and_wait method has been called, and I will account the time spent in this method execution.
To do that, I had to write two 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 will be executed when the program will start to execute the targeted method (java(“com.premiseo.Example”).class(“Example”).method(“loop_and_wait”)), the second one will be executed when the method will return (java(“com.premiseo.Example”).class(“Example”).method(“loop_and_wait”).return)
The related systemtap script is given below:
#!/usr/bin/env stap
global counter,timespent,t
probe 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 you 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 have been useful to profile 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的更多相关文章
- velocity模板引擎学习(4)-在standalone的java application中使用velocity及velocity-tools
通常velocity是配合spring mvc之类的框架在web中使用,但velocity本身其实对运行环境没有过多的限制,在单独的java application中也可以独立使用,下面演示了利用ve ...
- maven: 打包可运行的jar包(java application)及依赖项处理
IDE环境中,可以直接用exec-maven-plugin插件来运行java application,类似下面这样: <plugin> <groupId>org.codehau ...
- How to run a (Tomcat)Java application server on a Azure virtual machine
http://www.windowsazure.com/en-us/documentation/articles/virtual-machines-java-run-tomcat-applicatio ...
- The differences between Java application and Java applet
在Java语言中,能够独立运行的程序称为Java应用程序(Application).Java语言还有另外一种程序--Applet程序.Applet程序(也称Java小程序)是运行于各种网页文件中,用于 ...
- Java Applet与Java Application的区别
转自:http://www.educity.cn/java/500609.html 在Java语言中,能够独立运行的程序称为Java应用程序(Application).Java语言还有另外一种程序-- ...
- How to deploy JAVA Application on Azure Service Fabric
At this moment, Azure Service Fabric does not support JAVA application natively (but it's on the sup ...
- Java Applet与Java Application的特点
java application是应用程序,用于桌面开发,java applet是小应用程序,一般嵌入到网页里运行.applet一般用于B/S页面上作为插件式的开发,而application主要是桌面 ...
- Debugging java application with netbean
Debugging Java Applications with NetBeans from:https://manikandanmv.wordpress.com/2009/09/24/debu ...
- 非web环境的注解配置的spring项目应用(non-web, Spring-data-jpa, JavaConfig, Java Application, Maven, AnnotationConfigApplicationContext)
非web环境的spring应用 springframework提供的spring容器,非常适合应用于javaweb环境中. 同时,spring组件的低耦合性为普通java应用也提供了足够的支持. 以下 ...
随机推荐
- 转载:Linux操作系统(1.3.1)《深入理解Nginx》(陶辉)
原文:https://book.2cto.com/201304/19611.html 1.3 准备工作 由于Linux具有免费.使用广泛.商业支持越来越完善等特点,本书将主要针对Linux上运行的Ng ...
- Python-JS基础(基础结构~函数)
程序本质上分为三大结构: 顺序结构.分支结构.循环结构JavaScript中的程序结构也是这样,下面我们来分别介绍JS中的三种基本程序结构:我们上篇博客中介绍到的使用逻辑运算符&&实现 ...
- OneNET麒麟座应用开发之五:获取加速度传感器ADXL345数据
由于数据采集站基本都安装在野外或者楼顶,安装位置以及震动对检测数据的准确性有一定影响.所以想要有一个位置状态数据,正好发现麒麟作上有ADXL345,这样一个数字输出的加速度传感器.如图中红框所示: 1 ...
- Oracle 11g安装步骤以及Oracle11g创建表空间和用户,并授权
Oracle 11g安装步骤详解 一.Oracle 下载 注意Oracle分成两个文件,下载完后,将两个文件解压到同一目录下即可. 路径名称中,最好不要出现中文,也不要出现空格等不规则字符. 官方下地 ...
- Redhat5_linux 系统环境下 oracl11g的安装教程图解
linux_oracl11g 安装步骤 操作系统的安装敬请参考此文:VM 安装 linux Enterprise_R5_U4_Server_I386_DVD教程图解 设置linux服务器的静态地址请参 ...
- vue+element之多表单验证
方法一:利用promise var p1=new Promise(function(resolve, reject) { this.$refs[form1].validate((valid) => ...
- hdu 1542 线段树+扫描线 学习
学习扫描线ing... 玄学的东西... 扫描线其实就是用一条假想的线去扫描一堆矩形,借以求出他们的面积或周长(这一篇是面积,下一篇是周长) 扫描线求面积的主要思想就是对一个二维的矩形的某一维上建立一 ...
- python3 + selenium 之窗口切换
窗口切换 此代码来源学习后对淘宝操作实践记录: 以下代码在Chrome61和IE11上正常运行,Firefox5.7上运行存在一些问题须改进,应该是火狐不兼容差link_text部分和循环经常报错,在 ...
- 使用Struts,实现简单的登录
一.新建项目Struts 1.右键 new————Web Project 2.点击项目——右键——myeclipse——add Struts Capabilities.....——选择struts2. ...
- python 全栈开发,Day65(MySQL练习题,参考答案)
一.MySQL练习题 一.表关系 请创建如下表,并创建相关约束 二.操作表 1.自行创建测试数据 2.查询“生物”课程比“物理”课程成绩高的所有学生的学号.ps:针对的是自己的生物成绩比物理成绩高,再 ...