JFR

JFR(Java Flight Recorder)是Java虚拟机(JVM)提供的一个性能监控和故障诊断工具。它的主要作用是收集、记录并分析Java应用程序在运行时的性能数据。JFR在Java 7 Update 40及更高版本中被引入,并在Java 11及以后的版本中成为开源项目。

1
2
3
4
5
6
7
8
9
# 程序启动的时候启动 JFR 功能
java -XX:StartFlightRecording xxx.jar

java -XX:StartFlightRecording:filename=/path/foo.jfr,dumponexit=true

# 已经启动的程序使用 JFR
jcmd pid JFR.start

jcmd pid JFR.dump filename=foo.jfr maxsize=50MB

JFR View

1
2
jfr view [view] [recording file]
jcmd [pid] JFR.view [view]

JFR 视图命令的初始版本中提供了 70 多种不同的视图选项,将来可能会增加。每个视图都提供了从应用程序级别到 JVM 再到环境的整个堆栈的不同视角。

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

Java virtual machine views:
class-modifications gc-concurrent-phases longest-compilations
compiler-configuration gc-configuration native-memory-committed
compiler-phases gc-cpu-time native-memory-reserved
compiler-statistics gc-pause-phases safepoints
deoptimizations-by-reason gc-pauses tlabs
deoptimizations-by-site gc-references vm-operations
gc heap-configuration

Environment views:
active-recordings cpu-information jvm-flags
active-settings cpu-load native-libraries
container-configuration cpu-load-samples network-utilization
container-cpu-throttling cpu-tsc recording
container-cpu-usage environment-variables system-information
container-io-usage events-by-count system-processes
container-memory-usage events-by-name system-properties

Application views:
allocation-by-class exception-count native-methods
allocation-by-site file-reads-by-path object-statistics
allocation-by-thread file-writes-by-path pinned-threads
class-loaders finalizers socket-reads-by-host
contention-by-address hot-methods socket-writes-by-host
contention-by-class latencies-by-type thread-allocation
contention-by-site longest-class-loading thread-count
contention-by-thread memory-leaks-by-class thread-cpu-load
exception-by-message memory-leaks-by-site thread-start
exception-by-site modules

如下示例:

1
$ jfr view thread-count [recording-file]

有几个选项可以修改从 JFR 视图返回的数据的格式:

1
2
3
4
--width [number-of-columns]
--cell-height [number of rows]
--verbose
--truncate beginning

需要在视图之前提供这些选项,如以下示例所示:

1
2
3
4
5
6
7
8
9
$ jfr view --width 40 thread-count recording.jfr

Output:

Java Thread Statistics

Time Acti... Daem... Accu... Peak...
------- ------- ------- ------- -------
21:4... 26 22 145 34

本站由 江湖浪子 使用 Stellar 1.28.1 主题创建。
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议,转载请注明出处。