[转帖]Java Flame Graphs

java,flame,graphs · 浏览次数 : 0

小编点评

**Java Flame Graphs** **Introduction:** Java flame graphs are a new way to visualize CPU usage. They provide a visual overview of the program's execution, showing the duration and frequency of different code paths. **Tools for Creating Flame Graphs:** * **Lightweight-Java-Profiler:** An open-source profiler that can generate flame graphs. * **Flame Graph Software:** A command-line tool that converts the output of the lightweight-java-profiler into a flame graph. **Steps for Creating Flame Graphs with Lightweight-Java-Profiler:** 1. Get the latest version of the lightweight-java-profiler from the GitHub repository. 2. Configure the Makefile to specify the desired sampling rate and other options. 3. Build the profiler. 4. Run the profiler and save the generated traces.txt file. 5. Convert the traces.txt file into a flame graph using the Flame Graph Software. **Customizing the Profiler:** * You can configure some options in the lightweight-java-profiler source file (src/globals.h). * These options include the sampling rate, maximum number of stack traces, and maximum number of frames to store. **Benefits of Flame Graphs:** * Provide a high-level overview of program execution. * Show the duration and frequency of code paths. * Allow you to identify bottlenecks and performance issues. **Additional Notes:** * Flame graphs require full stack traces to be generated. * The lightweight-java-profiler can also generate call tree graphs. * Flame graphs are increasingly becoming a standard feature in Java profilers. **Conclusion:** Java flame graphs are a valuable tool for understanding and optimizing program performance. By using the lightweight-java-profiler and the Flame Graph Software, you can create informative flame graphs that can help you identify and address performance issues in your Java applications.

正文

https://www.brendangregg.com/blog/2014-06-12/java-flame-graphs.html

 

Java flame graphs are a hot new way to visualize CPU usage. I'll show how to create them using free and open source tools: Google's lightweight-java-profiler (code.google.com) and my flame graph software (github). Hopefully, one day, flame graphs will be a standard feature on all Java profilers, alongside the call tree graphs that are standard today.

UPDATE (Aug 2015): See the Java in Flames Netflix Tech Blog post for the latest and best method for Java flame graphs, which uses Linux perf_events to show Java and system code paths together. I also describe the steps in Java CPU Flame Graphs. This blog post refers to a Java-only JVMTI-based method, that while works, has the caveats described in those links.

Flame Graphs show the big picture. Here is vert.x serving a simple JavaScript program:

 

Awesome! Mouse over elements to see details. Here is the direct SVG, and a non-interactive PNG version.

The y-axis is stack depth, and the x-axis spans the sample population. Each rectangle is a stack frame. Color is not important, it's randomized to differentiate frames. The ordering from left to right is also unimportant.

You look for the widest frames, from bottom up, and forks in the "flames", which indicate different code paths taken. See my CPU flame graphs page for a longer explanation, and the flame graphs presentation from USENIX/LISA`13. Once you get the hang of these, you can quickly identify and quantify CPU usage.

The large tower on the left is the Mozilla Rhino JavaScript engine, which is eating 42.70% CPU (look for the lowest mozilla frame; the percentage is inclusive of all child frames above it). vert.x with Java doesn't need to run this engine, freeing up those CPU resources, and roughly doubling maximum possible performance.

Other details are also interesting: the large plateau in write0(), at 31.99%, is desirable – that's vert.x responding to requests and doing work. To improve the performance further (aside from tuning write0()), examine and reduce time spent in other code paths. Eliminating Rhino provides the biggest win.

Collect flame graphs over different days or software versions, and you can also quickly quantify performance changes by comparing them.

Profile Collection

These flame graphs visualize sampled stack traces that were running on-CPU. You can use any profiler that gives you full stack traces, provided the profiler is accurate. I first tried CPU sampling using hprof, but found that it had issues.

For this example, I used the lightweight-java-profiler by Jeremy Manson. This is an open source demonstration of an accurate profiling technique, and not a point-and-click production-ready product, so some assembly (and caution) is required. My steps were:

1. Get software

svn checkout http://lightweight-java-profiler.googlecode.com/svn/trunk/ lightweight-java-profiler-read-only
cd lightweight-java-profiler-read-only

2. Customize Makefile

I edited the Makefile using vi, and set BITS to 64, and added an include path for my system. My changes looked like this (diff), yours may vary depending on include paths required:

4c4
< BITS?=32
---
> BITS?=64
49c49
< INCLUDES=-I$(JAVA_HOME)/$(HEADERS) -I$(JAVA_HOME)/$(HEADERS)/$(UNAME)
---
> INCLUDES=-I$(JAVA_HOME)/$(HEADERS) -I$(JAVA_HOME)/$(HEADERS)/$(UNAME) -I/usr/include/x86_64-linux-gnu

3. Build software:

make all

4. Set agent

I added the following option when running java (change path to match yours):

-agentpath:/usr/local/lightweight-java-profiler-read-only/build-64/liblagent.so

This samples when java starts until it exits, writing the report to a traces.txt file, which has a similar layout to hprof. The flame graph software will read this traces.txt file.

There are some overheads to have this running. For my program it was negligible (~1% loss in request rate, and a 7% increase in CPU consumption, for which headroom was available), but your mileage will vary. See the later Customizing the Profiler section for reducing the sampling rate if needed.

Flame Graph

Now to turn traces.txt into a flame graph:

git clone http://github.com/brendangregg/FlameGraph
cd FlameGraph
./stackcollapse-ljp.awk < ../traces.txt | ./flamegraph.pl > ../traces.svg

stackcollapse-ljp.awk is a simple awk program to convert the output of lightweight-java-profiler into the stack trace format that flame graph reads (one stack per line). The flamegraph.pl program has various options (list using -h) to customize the output, including changing the title.

Now open the traces.svg file in a browser.

Customizing the Profiler

It's worth mentioning that you can configure some options in lightweight-java-profiler by editing the source. See src/globals.h for:

// Number of times per second that we profile
static const int kNumInterrupts = 100;

// Maximum number of stack traces
static const int kMaxStackTraces = 3000;

// Maximum number of frames to store from the stack traces sampled.
static const int kMaxFramesToCapture = 128;

I may be inclined to reduce the sampling rate from 100 hertz to 50, or lower, if I'd like to collect data over a long run (minutes), to reduce the sampling overheads. I'd also increase the maximum stack frames, if my code exceeded 128. Flame graphs need full stacks to be drawn.

There's also Richard Warburton's honest-profiler, which builds upon the same accurate profiling technique. Like the lightweight-java-profiler, some assembly is required.

While these profilers see inside Java, my ideal profiler would include native user-level and kernel stacks. This would allow us to include JVM GC code paths, as well as other JVM internals, and kernel internals.

Flame graphs already have a history of proving their usefulness in other areas, including for kernel performance, Node.JS, Ruby, Perl, and others. See the updates section on my Flame Graphs page.

与[转帖]Java Flame Graphs相似的内容:

[转帖]Java Flame Graphs

https://www.brendangregg.com/blog/2014-06-12/java-flame-graphs.html Java flame graphs are a hot new way to visualize CPU usage. I'll show how to creat

[转帖]Java Warmup

https://www.brendangregg.com/blog/2016-09-28/java-warmup.html I gave a talk at JavaOne last week on flame graphs (slides here), and in Q&A was asked a

[转帖]CPU Flame Graphs

https://www.brendangregg.com/FlameGraphs/cpuflamegraphs.html#Java MySQL CPU Flame Graph Determining why CPUs are busy is a routine task for performanc

[转帖]C++:perf详解 + Flame Graph火焰图分析程序性能

因为项目需求,C++和java同时在搞,最近了解到Flame Graph火焰图这个工具,网上查了查资料,这里记录一下。 1 介绍 web site http://www.brendangregg.com/flamegraphs.html git: https://github.com/brendan

[转帖]Java FlameGraph 火焰图

http://www.wjhsh.net/xingzifei-p-7446264.html 上周一个偶然的机会听同事提到了Java FlameGraph,刚实验了一下,效果非常好。 一、什么是FlameGraph 直接看图说话。FlameGraph 是 SVG格式,矢量图,可以随意扩大缩小,看不清的

[转帖]Java 近期新闻:JEP 更新,GraalVM 贡献给 OpenJDK,JavaOne 重启

https://www.infoq.cn/article/kzzbQg5zgissaCcJlfey JEP 432记录模式(第二预览版)在上周从其 8294078 草案晋升为候选状态。相比 JEP 405 记录模式(预览版),该 JEP 更新了:对通用记录模式类型参数推断的支持、新增对记录模式出现在

[转帖]Java中线程的生命周期

https://blog.51cto.com/u_15773567/5832430 1 介绍 本篇文章我们讨论下Java中的一个非常核心的概念:线程的生命周期。在Java编程语言中,多线程编程非常重要。线程从创建到销毁是有生命周期的,在线程的生命周期中,线程会经历多种状态(state)。 2 线程状

[转帖]Java调优系列之工具篇之btrace、gperftools

https://github.com/landon30/Bulls/wiki/java-profiling-tools landon 网络游戏资深服务器架构师 2018-06-14 线上遇到了问题? 服务上线出问题,想增加打印日志怎么办? 线上怀疑某个接口慢,想打印接口耗时怎么办? 线上某个接口报错

[转帖]Java游戏服务器调优实践

https://www.jianshu.com/p/344f8141b63e Java Profiling Practice landon资深网络游戏服务器架构师 系统性能定义 Throughput 吞吐量,也就是每秒钟可以处理的请求数,任务数 Latency 系统延迟,也就是系统在处理一个请求或一

[转帖]java中方法不要写太长的真正原因

https://www.iteye.com/blog/enetor-976070 java中一般建议一个方法不要写的过长,不方便维护和阅读是其中的一个原因,但是其真正性能的原因大家知道吗? 我们知道,JVM一开始是以解释方式执行字节码的。当这段代码被执行的次数足够多以后,它会被动态优化并编译成机器码