Java性能调优作为大型分布式系统提供高性能服务的必修课,其重要性不言而喻。

好的分析工具能起到事半功倍的效果,利用分析利器JMC、JFR,可以实现性能问题的准确定位。

本文主要阐述如何利用JFR生成性能日志

JMC:Java Mission Control

JFR:Java Flight Recorder

1. 打开JFR

JVM_OPT中添加:-XX:+UnlockCommercialFeatures -XX:+FlightRecorder -Djavax.xml.parsers.SAXParserFactory=com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl"

2. 启动JFR

登陆服务器,找到应用PID,执行命令:jcmd $PID JFR.start name=abc,duration=120s

3. Dump JFR

等待至少duration(本文设定120s)后,执行命令:jcmd $PID JFR.dump name=abc,duration=120s filename=abc.jfr(注意,文件名必须为.jfr后缀)

4. 检查JFR状态

执行命令:jcmd $PID JFR.check name=abc,duration=120s

5. 停止JFR

执行命令:jcmd $PID JFR.stop name=abc,duration=120s

6. JMC分析

切回开发机器,下载步骤3中生成的abc.jfr,打开jmc,导入abc.jfr即可进行可视化分析

==================================================================================

另一种配置方法, 已经成功:

java 参数  -XX:+UnlockCommercialFeatures -XX:+FlightRecorder -XX:NativeMemoryTracking=summary -XX:FlightRecorderOptions=defaultrecording=true

sudo -utomcat jcmd 8588 JFR.start settings=/tmp/my-profiling.jfc duration=6m filename=/tmp/hsperfdata_tomcat/new2.jfr

my-profiling.jfc 这个是模板文件, 内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<configuration version="1.0" name="my-profiling" description="Low overhead configuration for profiling, typically around 2 % overhead." provider="Oracle">

<producer uri="http://www.oracle.com/hotspot/jvm/" label="Oracle JDK">

<control>

<selection name="gc-level" default="detailed" label="Garbage Collector">
        <option label="Off" name="off">off</option>
        <option label="Normal" name="detailed">normal</option>
        <option label="All" name="all">all</option>
      </selection>

<condition name="gc-enabled-normal" true="true" false="false">
        <or>
          <test name="gc-level" operator="equal" value="normal"/>
          <test name="gc-level" operator="equal" value="all"/>
        </or>
      </condition>

<condition name="gc-enabled-all" true="true" false="false">
        <test name="gc-level" operator="equal" value="all"/>
      </condition>

<selection name="compiler-level" default="detailed" label="Compiler">
        <option label="Off" name="off">off</option>
        <option label="Normal" name="normal">normal</option>
        <option label="Detailed" name="detailed">detailed</option>
        <option label="All" name="all">all</option>
      </selection>

<condition name="compiler-enabled" true="false" false="true">
        <test name="compiler-level" operator="equal" value="off"/>
      </condition>

<condition name="compiler-enabled-failure" true="true" false="false">
        <or>
          <test name="compiler-level" operator="equal" value="detailed"/>
          <test name="compiler-level" operator="equal" value="all"/>
        </or>
      </condition>

<condition name="compiler-sweeper-threshold" true="0 ms" false="100 ms">
        <test name="compiler-level" operator="equal" value="all"/>
      </condition>

<condition name="compiler-compilation-threshold" true="1000 ms">
        <test name="compiler-level" operator="equal" value="normal"/>
      </condition>

<condition name="compiler-compilation-threshold" true="100 ms">
        <test name="compiler-level" operator="equal" value="detailed"/>
      </condition>

<condition name="compiler-compilation-threshold" true="0 ms">
        <test name="compiler-level" operator="equal" value="all"/>
      </condition>

<condition name="compiler-phase-threshold" true="60 s">
        <test name="compiler-level" operator="equal" value="normal"/>
      </condition>

<condition name="compiler-phase-threshold" true="10 s">
        <test name="compiler-level" operator="equal" value="detailed"/>
      </condition>

<condition name="compiler-phase-threshold" true="0 s">
        <test name="compiler-level" operator="equal" value="all"/>
      </condition>

<selection name="method-sampling-interval" default="maximum" label="Method Sampling">
        <option label="Off" name="off">999 d</option>
        <option label="Normal" name="normal">20 ms</option>
        <option label="Maximum" name="maximum">10 ms</option>
      </selection>

<condition name="method-sampling-enabled" true="false" false="true">
        <test name="method-sampling-interval" operator="equal" value="999 d"/>
      </condition>

<selection name="thread-dump-interval" default="everyMinute" label="Thread Dump">
        <option label="Off" name="off">999 d</option>
        <option label="At least Once" name="normal">everyChunk</option>
        <option label="Every 60 s" name="everyMinute">60 s</option>
        <option label="Every 10 s" name="everyTenSecond">10 s</option>
        <option label="Every 1 s" name="everySecond">1 s</option>
      </selection>

<condition name="thread-dump-enabled" true="false" false="true">
        <test name="thread-dump-interval" operator="equal" value="999 d"/>
      </condition>

<selection name="exception-level" default="errors" label="Exceptions">
        <option label="Off" name="off">off</option>
        <option label="Errors Only" name="errors">errors</option>
        <option label="All Exceptions, including Errors" name="all">all</option>
      </selection>

<condition name="enable-errors" true="true" false="false">
        <or>
          <test name="exception-level" operator="equal" value="errors"/>
          <test name="exception-level" operator="equal" value="all"/>
        </or>
      </condition>

<condition name="enable-exceptions" true="true" false="false">
        <test name="exception-level" operator="equal" value="all"/>
      </condition>

<text name="synchronization-threshold" label="Synchronization Threshold" contentType="timespan" minimum="0 s">10 ms</text>

<text name="file-io-threshold" label="File I/O Threshold" contentType="timespan" minimum="0 s">10 ms</text>

<text name="socket-io-threshold" label="Socket I/O Threshold" contentType="timespan" minimum="0 s">10 ms</text>

<flag name="heap-statistics-enabled" label="Heap Statistics">false</flag>

<flag name="class-loading-enabled" label="Class Loading">true</flag>

<flag name="allocation-profiling-enabled" label="Allocation Profiling">true</flag>

</control>

<event path="java/statistics/thread_allocation">
      <setting name="enabled">true</setting>
      <setting name="period">everyChunk</setting>
    </event>

<event path="java/statistics/class_loading">
      <setting name="enabled">true</setting>
      <setting name="period">1000 ms</setting>
    </event>

<event path="java/statistics/threads">
      <setting name="enabled">true</setting>
      <setting name="period">1000 ms</setting>
    </event>

<event path="java/thread_start">
      <setting name="enabled">true</setting>
    </event>

<event path="java/thread_end">
      <setting name="enabled">true</setting>
    </event>

<event path="java/thread_sleep">
      <setting name="enabled">true</setting>
      <setting name="stackTrace">true</setting>
      <setting name="threshold" control="synchronization-threshold">10 ms</setting>
    </event>

<event path="java/thread_park">
      <setting name="enabled">true</setting>
      <setting name="stackTrace">true</setting>
      <setting name="threshold" control="synchronization-threshold">10 ms</setting>
    </event>

<event path="java/monitor_enter">
      <setting name="enabled">true</setting>
      <setting name="stackTrace">true</setting>
      <setting name="threshold" control="synchronization-threshold">10 ms</setting>
    </event>

<event path="java/monitor_wait">
      <setting name="enabled">true</setting>
      <setting name="stackTrace">true</setting>
      <setting name="threshold" control="synchronization-threshold">10 ms</setting>
    </event>

<event path="vm/class/load">
      <setting name="enabled" control="class-loading-enabled">true</setting>
      <setting name="stackTrace">true</setting>
      <setting name="threshold">0 ms</setting>
    </event>

<event path="vm/class/unload">
      <setting name="enabled" control="class-loading-enabled">true</setting>
    </event>

<event path="vm/info">
      <setting name="enabled">true</setting>
      <setting name="period">everyChunk</setting>
    </event>

<event path="vm/initial_system_property">
      <setting name="enabled">true</setting>
      <setting name="period">everyChunk</setting>
    </event>

<event path="vm/prof/execution_sample">
      <setting name="enabled" control="method-sampling-enabled">true</setting>
      <setting name="period" control="method-sampling-interval">10 ms</setting>
    </event>

<event path="vm/prof/execution_sampling_info">
      <setting name="enabled">false</setting>
      <setting name="threshold">1 ms</setting>
    </event>

<event path="vm/runtime/execute_vm_operation">
      <setting name="enabled">true</setting>
      <setting name="threshold">10 ms</setting>
    </event>

<event path="vm/runtime/thread_dump">
      <setting name="enabled" control="thread-dump-enabled">true</setting>
      <setting name="period" control="thread-dump-interval">60 s</setting>
    </event>

<event path="vm/flag/long">
      <setting name="enabled">true</setting>
      <setting name="period">everyChunk</setting>
    </event>

<event path="vm/flag/ulong">
      <setting name="enabled">true</setting>
      <setting name="period">everyChunk</setting>
    </event>

<event path="vm/flag/double">
      <setting name="enabled">true</setting>
      <setting name="period">everyChunk</setting>
    </event>

<event path="vm/flag/boolean">
      <setting name="enabled">true</setting>
      <setting name="period">everyChunk</setting>
    </event>

<event path="vm/flag/string">
      <setting name="enabled">true</setting>
      <setting name="period">everyChunk</setting>
    </event>

<event path="vm/flag/long_changed">
      <setting name="enabled">true</setting>
    </event>

<event path="vm/flag/ulong_changed">
      <setting name="enabled">true</setting>
    </event>

<event path="vm/flag/double_changed">
      <setting name="enabled">true</setting>
    </event>

<event path="vm/flag/boolean_changed">
      <setting name="enabled">true</setting>
    </event>

<event path="vm/flag/string_changed">
      <setting name="enabled">true</setting>
    </event>

<event path="vm/gc/detailed/object_count">
      <setting name="enabled" control="heap-statistics-enabled">false</setting>
      <setting name="period">everyChunk</setting>
    </event>

<event path="vm/gc/configuration/gc">
      <setting name="enabled" control="gc-enabled-normal">true</setting>
      <setting name="period">everyChunk</setting>
    </event>

<event path="vm/gc/configuration/heap">
      <setting name="enabled" control="gc-enabled-normal">true</setting>
      <setting name="period">everyChunk</setting>
    </event>

<event path="vm/gc/configuration/young_generation">
      <setting name="enabled" control="gc-enabled-normal">true</setting>
      <setting name="period">everyChunk</setting>
    </event>

<event path="vm/gc/configuration/tlab">
      <setting name="enabled" control="gc-enabled-normal">true</setting>
      <setting name="period">everyChunk</setting>
    </event>

<event path="vm/gc/configuration/survivor">
      <setting name="enabled" control="gc-enabled-normal">true</setting>
      <setting name="period">everyChunk</setting>
    </event>

<event path="vm/gc/detailed/object_count_after_gc">
      <setting name="enabled">false</setting>
    </event>

<event path="vm/gc/heap/summary">
      <setting name="enabled" control="gc-enabled-normal">true</setting>
    </event>

<event path="vm/gc/heap/ps_summary">
      <setting name="enabled" control="gc-enabled-normal">true</setting>
    </event>

<event path="vm/gc/heap/metaspace_summary">
      <setting name="enabled" control="gc-enabled-normal">true</setting>
    </event>

<event path="vm/gc/metaspace/gc_threshold">
      <setting name="enabled" control="gc-enabled-normal">true</setting>
    </event>

<event path="vm/gc/metaspace/allocation_failure">
      <setting name="enabled" control="gc-enabled-normal">true</setting>
      <setting name="stackTrace">true</setting>
    </event>

<event path="vm/gc/metaspace/out_of_memory">
      <setting name="enabled" control="gc-enabled-normal">true</setting>
      <setting name="stackTrace">true</setting>
    </event>

<event path="vm/gc/metaspace/chunk_free_list_summary">
      <setting name="enabled" control="gc-enabled-normal">true</setting>
    </event>

<event path="vm/gc/collector/garbage_collection">
      <setting name="enabled" control="gc-enabled-normal">true</setting>
      <setting name="threshold">0 ms</setting>
    </event>

<event path="vm/gc/collector/parold_garbage_collection">
      <setting name="enabled" control="gc-enabled-normal">true</setting>
      <setting name="threshold">0 ms</setting>
    </event>

<event path="vm/gc/collector/young_garbage_collection">
      <setting name="enabled" control="gc-enabled-normal">true</setting>
      <setting name="threshold">0 ms</setting>
    </event>

<event path="vm/gc/collector/old_garbage_collection">
      <setting name="enabled" control="gc-enabled-normal">true</setting>
      <setting name="threshold">0 ms</setting>
    </event>

<event path="vm/gc/collector/g1_garbage_collection">
      <setting name="enabled" control="gc-enabled-normal">true</setting>
      <setting name="threshold">0 ms</setting>
    </event>

<event path="vm/gc/phases/pause">
      <setting name="enabled" control="gc-enabled-normal">true</setting>
      <setting name="threshold">0 ms</setting>
    </event>

<event path="vm/gc/phases/pause_level_1">
      <setting name="enabled" control="gc-enabled-normal">true</setting>
      <setting name="threshold">0 ms</setting>
    </event>

<event path="vm/gc/phases/pause_level_2">
      <setting name="enabled" control="gc-enabled-normal">true</setting>
      <setting name="threshold">0 ms</setting>
    </event>

<event path="vm/gc/phases/pause_level_3">
      <setting name="enabled" control="gc-enabled-all">false</setting>
      <setting name="threshold">0 ms</setting>
    </event>

<event path="vm/gc/reference/statistics">
      <setting name="enabled" control="gc-enabled-normal">true</setting>
    </event>

<event path="vm/gc/detailed/promotion_failed">
      <setting name="enabled" control="gc-enabled-normal">true</setting>
    </event>

<event path="vm/gc/detailed/evacuation_failed">
      <setting name="enabled" control="gc-enabled-normal">true</setting>
    </event>

<event path="vm/gc/detailed/evacuation_info">
      <setting name="enabled" control="gc-enabled-normal">true</setting>
    </event>

<event path="vm/gc/detailed/concurrent_mode_failure">
      <setting name="enabled" control="gc-enabled-normal">true</setting>
    </event>

<event path="vm/gc/detailed/allocation_requiring_gc">
      <setting name="enabled" control="gc-enabled-all">false</setting>
      <setting name="stackTrace">true</setting>
    </event>

<event path="vm/compiler/config">
      <setting name="enabled" control="compiler-enabled">true</setting>
      <setting name="period">everyChunk</setting>
    </event>

<event path="vm/compiler/stats">
      <setting name="enabled" control="compiler-enabled">true</setting>
      <setting name="period">1000 ms</setting>
    </event>

<event path="vm/compiler/compilation">
      <setting name="enabled" control="compiler-enabled">true</setting>
      <setting name="threshold" control="compiler-compilation-threshold">100 ms</setting>
    </event>

<event path="vm/compiler/phase">
      <setting name="enabled" control="compiler-enabled">true</setting>
      <setting name="threshold" control="compiler-phase-threshold">10 s</setting>
    </event>

<event path="vm/compiler/failure">
      <setting name="enabled" control="compiler-enabled-failure">true</setting>
    </event>

<event path="vm/code_sweeper/config">
      <setting name="enabled" control="compiler-enabled">true</setting>
      <setting name="period">everyChunk</setting>
    </event>

<event path="vm/code_sweeper/stats">
      <setting name="enabled" control="compiler-enabled">true</setting>
      <setting name="period">everyChunk</setting>
    </event>

<event path="vm/code_sweeper/sweep">
      <setting name="enabled" control="compiler-enabled">true</setting>
      <setting name="threshold" control="compiler-sweeper-threshold">100 ms</setting>
    </event>

<event path="vm/code_cache/config">
      <setting name="enabled" control="compiler-enabled">true</setting>
      <setting name="period">everyChunk</setting>
    </event>

<event path="vm/code_cache/stats">
      <setting name="enabled" control="compiler-enabled">true</setting>
      <setting name="period">everyChunk</setting>
    </event>

<event path="vm/code_cache/full">
      <setting name="enabled" control="compiler-enabled">true</setting>
    </event>

<event path="os/information">
      <setting name="enabled">true</setting>
      <setting name="period">everyChunk</setting>
    </event>

<event path="os/processor/cpu_information">
      <setting name="enabled">true</setting>
      <setting name="period">everyChunk</setting>
    </event>

<event path="os/processor/context_switch_rate">
      <setting name="enabled" control="compiler-enabled">true</setting>
      <setting name="period">10 s</setting>
    </event>

<event path="os/processor/cpu_load">
      <setting name="enabled">true</setting>
      <setting name="period">1000 ms</setting>
    </event>

<event path="os/processor/cpu_tsc">
      <setting name="enabled">true</setting>
      <setting name="period">everyChunk</setting>
    </event>

<event path="os/system_process">
      <setting name="enabled">true</setting>
      <setting name="period">everyChunk</setting>
    </event>

<event path="os/initial_environment_variable">
      <setting name="enabled">true</setting>
      <setting name="period">everyChunk</setting>
    </event>

<event path="os/memory/physical_memory">
      <setting name="enabled">true</setting>
      <setting name="period">everyChunk</setting>
    </event>

<event path="java/object_alloc_in_new_TLAB">
      <setting name="enabled" control="allocation-profiling-enabled">true</setting>
      <setting name="stackTrace">true</setting>
    </event>

<event path="java/object_alloc_outside_TLAB">
      <setting name="enabled" control="allocation-profiling-enabled">true</setting>
      <setting name="stackTrace">true</setting>
    </event>

</producer>

<producer uri="http://www.oracle.com/hotspot/jdk/" label="Oracle JDK">

<event path="java/file_read">
      <setting name="enabled">true</setting>
      <setting name="stackTrace">true</setting>
      <setting name="threshold" control="http://www.oracle.com/hotspot/jvm/file-io-threshold">10 ms</setting>
    </event>

<event path="java/file_write">
      <setting name="enabled">true</setting>
      <setting name="stackTrace">true</setting>
      <setting name="threshold" control="http://www.oracle.com/hotspot/jvm/file-io-threshold">10 ms</setting>
    </event>

<event path="java/socket_read">
      <setting name="enabled">true</setting>
      <setting name="stackTrace">true</setting>
      <setting name="threshold" control="http://www.oracle.com/hotspot/jvm/socket-io-threshold">10 ms</setting>
    </event>

<event path="java/socket_write">
      <setting name="enabled">true</setting>
      <setting name="stackTrace">true</setting>
      <setting name="threshold" control="http://www.oracle.com/hotspot/jvm/socket-io-threshold">10 ms</setting>
    </event>

<event path="java/exception_throw">
      <setting name="enabled" control="http://www.oracle.com/hotspot/jvm/enable-exceptions">false</setting>
      <setting name="stackTrace">true</setting>
    </event>

<event path="java/error_throw">
      <setting name="enabled" control="http://www.oracle.com/hotspot/jvm/enable-errors">true</setting>
      <setting name="stackTrace">true</setting>
    </event>

<event path="java/statistics/throwables">
      <setting name="enabled">true</setting>
      <setting name="period">1000 ms</setting>
    </event>

</producer>

<producer uri="http://www.oracle.com/hotspot/jfr-info/" label="Oracle JDK">

<event path="recordings/recording">
      <setting name="enabled">true</setting>
    </event>

<event path="recordings/recording_setting">
      <setting name="enabled">true</setting>
    </event>

</producer>

</configuration>

Java性能调优:利用JFR生成性能日志的更多相关文章

  1. [Spark性能调优] 第一章:性能调优的本质、Spark资源使用原理和调优要点分析

    本課主題 大数据性能调优的本质 Spark 性能调优要点分析 Spark 资源使用原理流程 Spark 资源调优最佳实战 Spark 更高性能的算子 引言 我们谈大数据性能调优,到底在谈什么,它的本质 ...

  2. JVM性能调优2:JVM性能调优参数整理

    序号 参数名 说明 JDK 默认值 使用过 1 JVM执行模式 2 -client-server 设置该JVM运行与Client 或者Server Hotspot模式,这两种模式从本质上来说是在JVM ...

  3. JVM性能调优(4) —— 性能调优工具

    前序文章: JVM性能调优(1) -- JVM内存模型和类加载运行机制 JVM性能调优(2) -- 垃圾回收器和回收策略 JVM性能调优(3) -- 内存分配和垃圾回收调优 一.JDK工具 先来看看有 ...

  4. spark 性能调优(一) 性能调优的本质、spark资源使用原理、调优要点分析

    转载:http://www.cnblogs.com/jcchoiling/p/6440709.html 一.大数据性能调优的本质 编程的时候发现一个惊人的规律,软件是不存在的!所有编程高手级别的人无论 ...

  5. MySql(九):MySQL性能调优——Schema设计的性能优化

    一.高效的模型设计 先了解下数据库设计的三大范式 第一范式:要求有主键,并且要求每一个字段原子性不可再分 第二范式:要求所有非主键字段完全依赖主键,不能产生部分依赖 第三范式:所有非主键字段和主键字段 ...

  6. PHP性能调优---php-fpm中启用慢日志配置(用于检测执行较慢的PHP脚本)

    虽然通过nginx accesslog可以记录用户访问某个接口或者网页所消耗的时间,但是不能清晰地追踪到具体哪个位置或者说函数慢,所以通过php-fpm慢日志,slowlog设置可以让我们很好的看见哪 ...

  7. 【JAVA进阶架构师指南】之五:JVM性能调优

    前言   首先给大家说声对不起,最近属实太忙了,白天上班,晚上加班,回家还要收拾家里,基本每天做完所有事儿都是凌晨一两点了,没有精力再搞其他的了.   好了,进入正题,让我们来聊聊JVM篇最后一个章节 ...

  8. 成为Java GC专家(5)—Java性能调优原则

    并不是每个程序都需要调优.如果一个程序性能表现和预期一样,你不必付出额外的精力去提高它的性能.然而,在程序调试完成之后,很难马上就满足它的性能需求,于是就有了调优这项工作.无论哪种编程语言,对应用程序 ...

  9. JVM性能调优监控工具专题一:JVM自带性能调优工具(jps,jstack,jmap,jhat,jstat,hprof)

    性能分析工具jstatjmapjhatjstack 前提概要:         JDK本身提供了很多方便的JVM性能调优监控工具,除了集成式的VisualVM和jConsole外,还有jps.jsta ...

随机推荐

  1. How to Pronounce the Word THEM

    How to Pronounce the Word THEM Share Tweet Share Tagged With: THEM Reduction Study the THEM reductio ...

  2. dp-最长公共子序列

    最长公共子序列(NYOJ36) 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 咱们就不拐弯抹角了,如题,需要你做的就是写一个程序,得出最长公共子序列.tip:最长公 ...

  3. do{}while() ;异常语句

    //while (true) //只要括号里面是true(正确的如:(1==1)),就会无限循环 //{ //} //do{}while() //不管while满足与否,首先先做一遍 //然后去看wh ...

  4. ubuntu搭建ftp服务器

    (1).首先用命令检查是否安装了vsftpd vsftpd -version  如果未安装用一下命令安装 sudo apt-get install vsftpd 安装完成后,再次输入vsftpd -v ...

  5. form 表单排序

    <html>  <head></head>  <body>   <form class="form-inline">  ...

  6. blat

    1)  产生背景---------------------------------------------------2002年的时候,随着人类基因组项目不断推进,需要将大量ESTs(300万) 及m ...

  7. Spring Boot logback

    前言 今天来介绍下spring Boot如何配置日志logback,我刚学习的时候,是带着下面几个问题来查资料的,你呢 如何引入日志? 日志输出格式以及输出方式如何配置? 代码中如何使用? 正文 Sp ...

  8. python简单基础代码

    1.从键盘输入两个数,并计算A的B次幂:number1=raw_input('input number1:')number2=raw_input('input number2:')print 'num ...

  9. 源码包的解压 .tar.gz /.tar.bz2的解压

    解压方式如下: .tar.gz     格式解压为          tar   -zxvf   xx.tar.gz .tar.bz2   格式解压为          tar   -jxvf    ...

  10. asp.net回发页面被刷新后悔重新执行回发事件的解决方法

    做项目,进行数据修改操作后,重新加载数据,本来是没问题的.但是在这个时候刷新下页面,发现修改操作又重新执行了一次,并弹出“修改成功”的提示框. 百度了下,找到以下解决方法,解决了问题: Page.Cl ...