JFR-Java应用的黑匣子

原创
2020/03/27 10:50
阅读数 634

JFR是什么

JSR,全称Java Flight Recorder,翻译过来就是java 飞行记录器。和飞机上的黑匣子类似,用于记录Java应用运行过程中的各种事件及数据。而后,使用相应的工具来分析产生的数据,一般使用JMC, java Mission Control。

JFR是oracle jdk的商业特性,因此需要有licence才用于商业用途。个人自己玩玩的话,无所谓了。

启动

因为JFR是商业特性,所以,在应用启动时,需加上相关的参数,才可以使用。

java -XX:+UnlockCommercialFeatures -XX:+FlightRecorder MyApp

运行JFR有两种方式,每种方式都需要应用启动时加入上述两个参数。

plan A

在应用启动时,加入JFR监控

java -XX:+UnlockCommercialFeatures -XX:+FlightRecorder -XX:StartFlightRecording=duration=60s,filename=myrecording.jfr MyApp
planB

更加灵活的诊断应用,启动时无需加上JFR诊断参数,需要诊断时,使用JFR相关命令对特定的jvm进程监控

首先,必不可少的,需要在应用启动时加入如下参数

java -XX:+UnlockCommercialFeatures -XX:+FlightRecorder MyApp

而后,使用JFR命令,启动监控

jcmd <jvm pid> JFR.start duration=60s filename=myrecording.jfr

让子弹飞一会··········

jcmd <jvm pid> JFR.stop

简要介绍下两个参数, filename: jfr产生的数据记录到的文件 duration:持续监控诊断时间

诊断分析

有了数据文件,我们可以使用jdk自带的jmc工具分析数据文件。

初始打开文件后,首页会显示概要分析,也就是一些分析建议。 比如上述

  1. 发现内存占用已达到97.9%,建议增大内存
  2. 发现物理机上有其他进程正在运行,如果是服务器环境,建议在该计算机上只运行关键进程会更好

继续往下看;

从上可以看到很多信息,大致分为4类

  1. Java应用程序,此部分主要为应用程序相关的数据,包括线程、内存对象、文件IO、方法概要分析等
  2. JVM内部,主要为垃圾回收、类加载等等
  3. 环境,jvm配置参数、系统属性等

JFR原理

有人在怀疑,在生产环境开启JFR是否会有影响,影响是肯定有的,但不会大,官方给出的是3%,实际运行过程中发现<1%.为何会如此高效呢,下图是从官网摘出来的。

JFR是基于事件驱动,异步收集数据,所有的事件数据首先写入当前线程的buffer中,也就是ThreadLocal,无竞争!threadlocal buffer满了之后,再写到Global buffer中,注意,此处是有竞争的;当前两处均为内存中。当global buffer满了之后会再写到本地磁盘文件中。

原文如下:

Understanding Data Flow

JFR collects data from the JVM (through internal APIs) and from the Java application (through the JFR APIs). This data is stored in small thread-local buffers that are flushed to a global in-memory buffer. Data in the global in-memory buffer is then written to disk. Disk write operations are expensive, so you should try to minimize them by carefully selecting the event data you enable for recording. The format of the binary recording files is very compact and efficient for applications to read and write.

There is no information overlap between the various buffers. A particular chunk of data is available either in memory or on disk, but never in both places. This has the following implications:

Data not yet flushed to a disk buffer will not be available in the event of a power failure.

A JVM crash can result in some data being available in the core file (that is, the in-memory buffer) and some in the disk buffer. JFR does not provide the capability to merge such buffers.

There may be a small delay before data collected by JFR is available to you (for example, when it has to be moved to a different buffer before it can be made visible).

The data in the recording file may not be in time sequential order as the data is collected in chunks from several thread buffers.

In some cases, the JVM drops the event order to ensure that it does not crash. Any data that cannot be written fast enough to disk is discarded. When this happens, the recording file will include information on which time period was affected. This information will also be logged to the logging facility of the JVM.

You can configure JFR to not write any data to disk. In this mode, the global buffer acts as a circular buffer and the oldest data is dropped when the buffer is full. This very low-overhead operating mode still collects all the vital data necessary for root-cause problem analysis. Because the most recent data is always available in the global buffer, it can be written to disk on demand whenever operations or surveillance systems detect a problem. However, in this mode, only the last few minutes of data is available, so it only contains the most recent events. If you need to get the full history of operation for a long period of time, use the default mode when events are written to disk regularly.

Java Flight Recorder Architecture

JFR is comprised of the following components:

JFR runtime is the recording engine inside the JVM that produces the recordings. The runtime engine itself is comprised of the following components:

The agent controls buffers, disk I/O, MBeans, and so on. This component provides a dynamic library written in C and Java code, and also provides a JVM-independent pure Java implementation.

The producers insert data into the buffers. They can collect events from the JVM and the Java application, and (through a Java API) from third-party applications.

Flight Recorder plugin for Java Mission Control (JMC) enables you to work with JFR from the JMC client, using a graphical user interface (GUI) to start, stop, and configure recordings, as well as view recording files.

至此,就简单的介绍完了!

备注:

  1. 在Mac OS 上,java 1.8x版的jmc启动后不可用,此问题是oracle的问题,可以使用jdk7或者jdk9及更高版本jdk来分析数据

Java Mission Control 5.5 Release Notes

Java Mission Control from JDK 1.8.0_161 frozen upon startup on Mac OS X

  1. 或者你也可以单独安装JMC JDK Mission Control (JMC) 7 Downloads
  2. 官方介绍文档
展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部