# JFR Recording

JFR (Java Flight Recorder) is a common JVM monitoring tool to help analyze the JVM usage status. For more details about JFR, you can search the internet.

Our JFR recording menu supports collecting and viewing diagnostic and profiling data of a running Java application, which usually aims to facilitate performance tuning. 

It shares almost 95% of the features of the famous JProfiler. Moreover, developers can directly use our JFR recording menu online without the need to seek TS to switch on and off for concerns about system stability.

After choosing a node of the service, you can click **Start JFR Recording** to input the duration. Then the system will start recording JFR. When the recording is over, you will get a record file for further analysis. 
   
![JFR Recording](./image/monitor/jfr.png)


## Background

Previously, analyzing performance-related issues in the targeted environment was a tedious task. Tools like JProfiler need to be installed in the environment or service to scrape the data. Not only is the installation configuration cumbersome, but some environments (such as production) also do not allow the installation for stability. Starting with JDK 11, a free profiling tool is introduced: Java Flight Record (JFR). JFR can be utilized directly in the runtime environment without the necessity to modify the startup configuration in advance, and JFR's impact on performance is very small (overheads of profile mode is less than 2%). The default functions of JFR and several InsureMO default extensions (JDBC and Rest API) can basically meet 99% of the requirements of performance analysis. ICS has built-in functions and related management interfaces.

## UI Operation

We provide a web UI to simplify JFR-related operations.

Click **Monitor** > **JFR Management** in the menu.
Then select the specified **Microservice Name** and the corresponding **Microservice Node** (IP and Port) to list the existing JFR records.

![JFR Recording](./image/jfr/jfr_query.png)

Click **Start JFR Recording**, and then define the duration of recording in a pop-up window. Click **Confirm** to start recording. JFR will stop recording automatically after the specified duration. You can also stop the operation manually.

![JFR Recording](./image/jfr/jfr_record.png)

The Query Results page lists all JFR records in the instance. If the recording status is **Running**, you can click **Stop** to terminate the recording in advance. If the record status is **Closed**, you can download the JFR file, or delete the record (and delete the server-side JFR file).

![JFR Recording](./image/jfr/jfr_running.png)


## JFR Analysis 

### Tools

To view the exported JFR files, you need to use two tools at the same time: 
* JProfiler 11 or higher, mainly for viewing CPU-related performance information
* Java Mission Control 7.1 or above, mainly for viewing SQL access and Rest API calls

<div class="docs-note"><span class="docs-admonitions-text">note</span>

If the recording time is too short and the CPU data is not captured, JProfiler will report an error when opening the corresponding file.

</div>

### CPU Performance Data Analysis (JProfiler)

In JProfiler, click **Hot Spots** and **Call Tree** in **CPU views**.

![JFR Recording](./image/jfr/jfr_jprofiler.png)


### JDBC Data Analysis (JMC)

1. Click **Outline** > **Event Browser**. Then find **Databases** > **JDBC** in **Event Types Tree**, and click **Create a new page using the selected event types** in the right-click menu.

![JFR Recording](./image/jfr/jfr_jmc.png)

2. In the **List** of the newly created JDBC page, click **Group By** > **SQL** in the right-click menu.

![JFR Recording](./image/jfr/jfr_jmc2.png)

3. On the SQL Group Statistics Results page, click **Visible Columns** in the right-click menu, and then select **Total Duration** and **Average Duration**.

![JFR Recording](./image/jfr/jfr_jmc3.png)

4. You can sort operations according to **Count**, **Total Duration**, **Avg Duration** and other dimensions, and you can refer to specific call points after selecting one of the SQL lists.

![JFR Recording](./image/jfr/jfr_jmc4.png)


### Rest API Analysis(JMC)

 Click **Outline** > **Event Browser**. Then find **RPC** > **Rest API** in **Event Types Tree**. The subsequent operation is the same as [JDBC Data Analysis (JMC)](#jdbc-data-analysis-jmc).


### View the Overall Situation of the Resource (JProfiler)

![JFR Recording](./image/jfr/jfr_jprofiler_resource.png)


### View Thread Status (JProfiler)

![JFR Recording](./image/jfr/jfr_jprofiler_thread1.png)

![JFR Recording](./image/jfr/jfr_jprofiler_thread2.png)


### View Network Usage (JProfiler)

![JFR Recording](./image/jfr/jfr_jprofiler_network.png)


### View Network Usage (JMC)

Click **Outline** > **Java Application** > **Socket I/O** to see the network usage. After selecting **Remote Address**, you can view the detailed data.

![JFR Recording](./image/jfr/jfr_jmc_socket.png)


### View CPU Usage (JProfiler)

![JFR Recording](./image/jfr/jfr_jprofiler_cpu.png)


### View CPU Usage (JMC)

Click **Outline** > **Environment** > **Processes**. Then you can see the CPU usage.

![JFR Recording](./image/jfr/jfr_jmc_cpu.png)


### View Memory Usage (JProfiler)

![JFR Recording](./image/jfr/jfr_jprofiler_memory.png)

![JFR Recording](./image/jfr/jfr_jprofiler_cpu2.png)

Click **Outline** > **JVM Internals** > **Garbage Collections**. Then you can see memory and GC usage.

![JFR Recording](./image/jfr/jfr_jmc_cpu2.png)


## Apply Settings

ICS foundation has done the following extension work. If you need to analyze custom functions, you can refer to the foundation's code implementation.


### Rest API Calls  

In foundation auto configuration, add interceptors to all created RestTemplates, and log Rest API calls to JFR. No additional configuration is required.

### SQL Access  

In log4jdbc, StatementSpy or PreparedStatementSpy records the corresponding JDBC events to JFR each time SQL is executed. You need to set the driver of the connection pool to the log4jdbc driver customized by InsureMO in the configuration center.

### JDK8 Compatibility Issues  

JDK8 does not support JFR-related operations to avoid compilation errors. Instead, a set of mock JFR interfaces and implementations is provided.







  