[Java] How to record JFR (Java Flight Recorder) and output dump file

2 minute read

This section describes how to record JFR (Java Flight Recorder) and output a dump file. Here, record the status of Eclipse GlassFish 5.1.0.

Environment

  • OS: CentOS Linux release 7.7.1908
  • jdk1.8
  • GlassFish 5.1.0
[[email protected] ~]$ cat /etc/redhat-release
CentOS Linux release 7.7.1908 (Core)
[[email protected] ~]$
[[email protected] ~]$ java -version
java version "1.8.0_241"
Java(TM) SE Runtime Environment (build 1.8.0_241-b07)
Java HotSpot(TM) 64-Bit Server VM (build 25.241-b07, mixed mode)
[[email protected] ~]$
[[email protected] ~]$ /opt/glassfish5/glassfish/bin/asadmin version
Version string could not be obtained from Server [localhost:4848].
(Turn debugging on e.g. by setting AS_DEBUG=true in your environment, to see the details.)
Using locally retrieved version string from version class.
Version = GlassFish Server Open Source Edition 5.1.0 (build default-private)
Command version executed successfully.
[[email protected] ~]$

For the installation of GlassFish 5.1.0, please refer to the Posted how to install Eclipse GlassFish 5.1.0 on CentOS 7 posted earlier. Please give me.

1. Setting JVM options when starting Glassfish

Add the following to the JVM options when starting Glassfish.

  • -XX:+UnlockCommercialFeatures
  • -XX:+FlightRecorder
  • -XX:FlightRecorderOptions=<..>

Specifically, “domain/configs/java-config” of domain.xml under “/opt/glassfish5/glassfish/domains/domain1/config” (java-config tag in configs tag in domain tag) Add the following 3 lines to (inside).

`Add to xml:/opt/glassfish5/glassfish/domains/domain1/config/domain.xml

-XX:+UnlockCommercialFeatures -XX:+FlightRecorder -XX:FlightRecorderOptions=defaultrecording=true,disk=true,repository=/var/log/jvm/repositories,maxsize=2048m,maxage=25h,dumponexit=true,dumponexitpath=/var/log/jvm/ dump.jfr

*Please refer to [Java page](https://docs.oracle.com/javase/jp/8/docs/technotes/tools/windows/java.html"java") for details of FlightRecorderOptions settings.

Here, since `/var/log/jvm` is used as a temporary directory, a `jvm` directory is created under `/var/log`, and the glassfish user creates files and files under `/var/log/jvm`. Be prepared to create a directory.


#### **` execution result`**
```console

[[email protected] ~]$ su
password:
[[email protected] glassfish]# cd /var/log
[[email protected] log]#
[[email protected] log]# mkdir jvm
[[email protected] log]# chown glassfish:glassfish jvm
[[email protected] log]# exit
exit
[[email protected] ~]$

2. Start Glassfish

Start Glassfish with the following command.

/opt/glassfish5/glassfish/bin/asadmin start-domain domain1

execution result


[[email protected] config]$ /opt/glassfish5/glassfish/bin/asadmin start-domain domain1
Waiting for domain1 to start ...
Successfully started the domain: domain1
domain Location: /opt/glassfish5/glassfish/domains/domain1
Log File: /opt/glassfish5/glassfish/domains/domain1/logs/server.log
Admin Port: 4848
Command start-domain executed successfully.
[[email protected] config]$

3. Output of JFR dump file

First, check the process ID of glassfish with the following command.

ps -ef | grep glassfish | grep -v grep

execution result


[[email protected] config]$ ps -ef | grep glassfish | grep -v grepglassfi+ 26302     1  5 19:54 pts/0    00:00:16 /usr/java/jdk1.8.0_241-amd64/bin/java -cp /opt/glassfish5/glassfish/modules/glassfish.jar -XX:+UnlockCommercialFeatures -XX:+UnlockDiagnosticVMOptions -XX:NewRatio=2 -XX:FlightRecorderOptions=defaultrecording=true,disk=true,repository=/var/log/jvm/repositories,maxsize=2048m,maxage=25h,dumponexit=true,dumponexitpath=/var/log/jvm/dump.jfr -XX:+FlightRecorder -XX:MaxPermSize=192m -Xmx512m -javaagent:/opt/glassfish5/glassfish/lib/monitor/flashlight-agent.jar -client -Djavax.xml.accessExternalSchema=all -Djavax.net.ssl.trustStore=/opt/glassfish5/glassfish/domains/domain1/config/cacerts.jks -Djdk.tls.rejectClientInitiatedRenegotiation=true -Djdk.corba.allowOutputStreamSubclass=true -Dfelix.fileinstall.dir=/opt/glassfish5/glassfish/modules/autostart/ -Dorg.glassfish.additionalOSGiBundlesToStart=org.apache.felix.shell,org.apache.felix.gogo.runtime,org.apache.felix.gogo.shell,org.apache.felix.gogo.command,org.apache.felix.shell.remote,org.apache.felix.fileinstall -Dcom.sun.aas.installRoot=/opt/glassfish5/glassfish -Dfelix.fileinstall.poll=5000 -Djava.endorsed.dirs=/opt/glassfish5/glassfish/modules/endorsed:/opt/glassfish5/glassfish/lib/endorsed -Djava.security.policy=/opt/glassfish5/glassfish/domains/domain1/config/server.policy -Dosgi.shell.telnet.maxconn=1 -Dfelix.fileinstall.bundles.startTransient=true -Dcom.sun.enterprise.config.config_environment_factory_class=com.sun.enterprise.config.serverbeans.AppserverConfigEnvironmentFactory -Dfelix.fileinstall.log.level=2 -Djavax.net.ssl.keyStore=/opt/glassfish5/glassfish/domains/domain1/config/keystore.jks -Djava.security.auth.login.config=/opt/glassfish5/glassfish/domains/domain1/config/login.conf -Dfelix.fileinstall.disableConfigSave=false -Dfelix.fileinstall.bundles.new.start=true -Dcom.sun.aas.instanceRoot=/opt/glassfish5/glassfish/domains/domain1 -Dosgi.shell.telnet.port=6666 -Dgosh.args=--nointeractive -Dcom.sun.enterprise.security.httpsOutboundKeyAlias=s1as -Dosgi.shell.telnet.ip=127.0.0.1 -DANTLR_USE_DIRECT_CLASS_LOADING=true -Djava.awt.headless=true -Dcom.ctc.wstx.returnNullForDefaultNamespace=true -Djava.ext.dirs=/usr/java/jdk1.8.0_241-amd64/lib/ext:/usr/java/jdk1.8.0_241-amd64/jre/lib/ext:/opt/glassfish5/glassfish/domains/domain1/lib/ext -Djdbc.drivers=org.apache.derby.jdbc.ClientDriver -Djava.library.path=/opt/glassfish5/glassfish/lib:/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib com.sun.enterprise.glassfish.bootstrap.ASMain -upgrade false -domaindir /opt/glassfish5/glassfish/domains/domain1 -read-stdin true -asadmin-args --host,,,localhost,,,--port,,,4848,,,--secure=false,,,--terse=false,,,--echo=false,,,--interactive=true,,,start-domain,,,--verbose=false,,,--watchdog=false,,,--debug=false,,,--domaindir,,,/opt/glassfish5/glassfish/domains,,,domain1 -domainname domain1 -instancename server -type DAS -verbose false -asadmin-classpath /opt/glassfish5/glassfish/lib/client/appserver-cli.jar -debug false -asadmin-classname com.sun.enterprise.admin.cli.AdminMain
[[email protected] config]$

プロセスIDが26302であることがわかります。

以下のコマンドでJFR(Java Flight Recorder)の記録をダンプファイルに出力します。

jcmd <プロセスID> JFR.dump recording=0 filename=<出力ダンプファイルのパス>

実行結果


[[email protected] ~]$ jcmd 26302 JFR.dump recording=0 filename=/home/glassfish/perf/test_$(date "+%Y%m%d_%H%M%S").jfr
26302:
Dumped recording 0, 2.5 MB written to:

/home/glassfish/perf/test_20200726_200546.jfr
[[email protected] ~]$

出力されたダンプファイルを確認します。

実行結果


[[email protected] ~]$ cd /home/glassfish/perf
[[email protected] perf]$ ls -l
合計 2600
- rw-rw-r--. 1 glassfish glassfish 2660207  7月 26 20:05 test_20200726_200546.jfr
[[email protected] perf]$

3. 「jmc.exe」で出力されたダンプファイル(jfrファイル)を参照

出力したjfrダンプファイル(test_20200726_200546.jfr)をjdk1.8がインストールされているWindowsPCに転送します。

C:\Program Files\Java\jdk1.8.0_231\bin」配下の「jmc.exe」(Java Mission Contorol)を起動します。

01.png

メニューから「ファイル」-「ファイルを開く」で出力したjfrダンプファイル(test_20200726_200546.jfr)を開きます。

02.png

03.png

「jmc.exe」(Java Mission Contorol)で出力したjfrダンプファイルを参照することができます。

04.png

参考

Java Platform, Standard Editionツール・リファレンス