{"id":1188,"date":"2015-04-06T16:43:55","date_gmt":"2015-04-06T08:43:55","guid":{"rendered":"http:\/\/coderbee.net\/?p=1188"},"modified":"2019-01-06T21:22:43","modified_gmt":"2019-01-06T13:22:43","slug":"java-flight-recordings-jfr-java-%e9%a3%9e%e8%a1%8c%e8%ae%b0%e5%bd%95%e5%99%a8-part-1","status":"publish","type":"post","link":"https:\/\/coderbee.net\/index.php\/jvm\/20150406\/1188","title":{"rendered":"Java Flight Recordings (JFR) &#8212; Java \u98de\u884c\u8bb0\u5f55\u5668 &#8211; part 1"},"content":{"rendered":"<h2>\u4e00\u3001JFR \u98de\u884c\u8bb0\u5f55\u5668<\/h2>\n<p>Java Flight Recorder\uff08JFR\uff09\u662f\u4e00\u4e2a\u5546\u4e1a\u7279\u6027\uff0c\u7528\u5728\u751f\u4ea7\u670d\u52a1\u5668\u4e0a\u662f\u9700\u8981\u5546\u4e1a\u8bb8\u53ef\u7684\u3002<\/p>\n<p>JFR \u8bb0\u5f55\u4e86\u5173\u4e8e Java \u8fd0\u884c\u65f6\u53ca\u8fd0\u884c\u5728\u5176\u5185\u7684 Java \u5e94\u7528\u7a0b\u5e8f\u7684\u8be6\u7ec6\u4fe1\u606f\uff0c\u8bb0\u5f55\u7528\u5c11\u91cf\u7684\u5f00\u9500\u5b8c\u6210\u3002\u6570\u636e\u662f\u4f5c\u4e3a\u65f6\u95f4\u4e0a\u7684\u6570\u636e\u70b9\uff08\u79f0\u4e3a\u4e8b\u4ef6\uff09\u8bb0\u5f55\u7684\u3002\u5178\u578b\u7684\u4e8b\u4ef6\u53ef\u4ee5\u662f\u7ebf\u7a0b\u7b49\u5f85\u9501\u3001GC\u3001CPU \u5468\u671f\u4f7f\u7528\u6570\u636e\u7b49\u3002<\/p>\n<p>\u5728\u521b\u5efa\u98de\u884c\u8bb0\u5f55\u65f6\uff0c\u4f60\u53ef\u4ee5\u9009\u62e9\u54ea\u4e9b\u4e8b\u4ef6\u5e94\u5f53\u4fdd\u5b58\uff0c\u8fd9\u53eb\u505a\u8bb0\u5f55\u6a21\u677f\u3002\u6709\u4e9b\u6a21\u677f\u53ea\u4fdd\u5b58\u57fa\u672c\u4e8b\u4ef6\uff0c\u5bf9\u6027\u80fd\u51e0\u4e4e\u6ca1\u6709\u5f71\u54cd\u3002\u5176\u4ed6\u6a21\u677f\u53ef\u80fd\u6709\u8f7b\u5fae\u7684\u6027\u80fd\u5f00\u9500\uff0c\u8fd8\u53ef\u80fd\u89e6\u53d1 GC \u6765\u6536\u96c6\u66f4\u591a\u4fe1\u606f\u3002\u901a\u5e38\uff0c\u8d85\u8fc7\u767e\u5206\u4e4b\u51e0\u7684\u5f00\u9500\u662f\u5f88\u7f55\u89c1\u3002<\/p>\n<p>\u98de\u884c\u8bb0\u5f55\u53ef\u7528\u4e8e\u8c03\u8bd5\u5f88\u5927\u8303\u56f4\u7684\u95ee\u9898\uff0c\u4ece\u6027\u80fd\u95ee\u9898\u5230\u5185\u5b58\u6cc4\u6f0f\u6216\u4e25\u91cd\u7684\u9501\u7ade\u4e89\u3002<\/p>\n<h3>1\u3001\u8bb0\u5f55\u7c7b\u578b<\/h3>\n<h4>1.1\u3001\u8fde\u7eed\u5f55\u5236<\/h4>\n<p>\u4e00\u4e2a\u8fde\u7eed\u8bb0\u5f55\u662f\u6307\u8bb0\u5f55\u603b\u662f\u5f00\u7740\u5e76\u4fdd\u5b58\uff0c\u4f8b\u5982\uff0c\u8fc7\u53bb\u516d\u5c0f\u65f6\u7684\u6570\u636e\u3002\u5982\u679c\u5e94\u7528\u7a0b\u5e8f\u9677\u5165\u95ee\u9898\uff0c\u4f60\u53ef\u4ee5\u8f6c\u50a8\uff08dump\uff09\u8fd9\u4e9b\u6570\u636e\uff0c\u4f8b\u5982\uff0c\u4ece\u8fc7\u53bb\u4e00\u5c0f\u65f6\u7684\uff0c\u770b\u770b\u51fa\u751f\u95ee\u9898\u65f6\u53d1\u751f\u4e86\u4ec0\u4e48\u3002<\/p>\n<p>\u8fde\u7eed\u8bb0\u5f55\u7684\u9ed8\u8ba4\u8bbe\u7f6e\u662f\u4f7f\u7528\u8bb0\u5f55 profile\uff0c\u5f00\u9500\u6781\u4f4e\u3002\u8fd9\u4e2a profile \u4e0d\u6536\u96c6\u5806\u7edf\u8ba1\u4fe1\u606f\u6216\uff08\u5185\u5b58\uff09\u5206\u914d\u6027\u80fd\u5206\u6790\uff0c\u4f46\u4ecd\u7136\u6536\u96c6\u4e86\u5f88\u591a\u6709\u7528\u6570\u636e\u3002<\/p>\n<p>\u4fdd\u6301\u6301\u7eed\u5f55\u5236\u4e00\u76f4\u5141\u8bb8\u662f\u5f88\u597d\u7684\uff0c\u5bf9\u4e8e\u8c03\u8bd5\u975e\u5e38\u7f55\u89c1\u7684\u95ee\u9898\u65f6\u975e\u5e38\u6709\u7528\u3002\u8bb0\u5f55\u53ef\u4ee5\u7528 <code>jcmd<\/code> \u6216 <code>JMC<\/code> \u624b\u5de5\u8f6c\u50a8\u3002\u4f60\u4e5f\u53ef\u4ee5\u5728 JMC \u91cc\u8bbe\u7f6e\u89e6\u53d1\u5668\u5728\u4e00\u4e9b\u7279\u5b9a\u7684\u6761\u4ef6\u88ab\u6ee1\u8db3\u540e\u8f6c\u50a8\u98de\u884c\u8bb0\u5f55\u3002<\/p>\n<h4>1.2\u3001\u6027\u80fd\u5206\u6790\u5f55\u5236<\/h4>\n<p>\u6027\u80fd\u5206\u6790\u5f55\u5236\u662f\u6307\u8bb0\u5f55\u5f00\u7740\uff0c\u8fd0\u884c\u4e00\u5b9a\u65f6\u95f4\uff0c\u7136\u540e\u505c\u6b62\u3002\u901a\u5e38\uff0c \u6027\u80fd\u5206\u6790\u5f55\u5236\u5141\u8bb8\u66f4\u591a\u7684\u65f6\u95f4\uff0c\u6709\u53ef\u80fd\u5bf9\u6027\u80fd\u6709\u8f83\u5927\u7684\u51b2\u51fb\u3002\u88ab\u5f00\u542f\u7684\u65f6\u95f4\u53ef\u4ee5\u88ab\u4fee\u6539\uff0c\u53d6\u51b3\u4e8e\u4f60\u7684 \u6027\u80fd\u5206\u6790\u5f55\u5236\u3002<\/p>\n<p>\u4f7f\u7528 \u6027\u80fd\u5206\u6790\u5f55\u5236 \u7684\u5178\u578b\u573a\u666f\u5982\u4e0b\uff1a<\/p>\n<ul>\n<li>\u5256\u6790\u8fd0\u884c\u6700\u9891\u7e41\u7684\u65b9\u6cd5\u548c\u521b\u5efa\u5bf9\u8c61\u6700\u591a\u7684\u5730\u65b9\u3002<\/li>\n<li>\u67e5\u627e\u4f7f\u7528\u4e86\u8d8a\u6765\u8d8a\u591a\u5185\u5b58\u7684\u7c7b\uff0c\u6697\u793a\u7740\u5185\u5b58\u6cc4\u6f0f\u3002<\/li>\n<li>\u67e5\u627e\u56e0\u4e3a\u540c\u6b65\u800c\u5bfc\u81f4\u7684\u74f6\u9888\uff0c\u66f4\u591a\u7c7b\u578b\u7684\u60c5\u666f\u3002<\/li>\n<\/ul>\n<p>\u6027\u80fd\u5206\u6790\u5f55\u5236 \u5c06\u7ed9\u51fa\u5f88\u591a\u4fe1\u606f\uff0c\u5373\u4f7f\u4f60\u4e0d\u662f\u5728\u5b9a\u4f4d\u7279\u5b9a\u7684\u95ee\u9898\u3002 \u6027\u80fd\u5206\u6790\u5f55\u5236 \u5c06\u7ed9\u4f60\u5173\u4e8e\u5e94\u7528\u7a0b\u5e8f\u7684\u5f88\u597d\u7684\u89c6\u56fe\uff0c\u53ef\u4ee5\u5e2e\u52a9\u4f60\u627e\u51fa\u74f6\u9888\u6216\u9700\u8981\u63d0\u9ad8\u7684\u5730\u65b9\u3002<\/p>\n<p><!--more--><\/p>\n<h2>\u4e8c\u3001\u5982\u4f55\u751f\u6210\u98de\u884c\u8bb0\u5f55<\/h2>\n<p>\u6709\u4e09\u79cd\u65b9\u6cd5\u53ef\u7528\u4e8e\u751f\u6210\u98de\u884c\u8bb0\u5f55\uff1a\u7528 JMC \u6765\u751f\u6210\u98de\u884c\u8bb0\u5f55\u3001\u7528\u547d\u4ee4\u884c\u542f\u52a8\u6807\u8bb0\u6765\u751f\u6210\u98de\u884c\u8bb0\u5f55\u3001\u7528\u89e6\u53d1\u5668\u6765\u81ea\u52a8\u5f55\u5236\u3002<\/p>\n<h3>2.1\u3001\u7528 JMC \u6765\u751f\u6210\u98de\u884c\u8bb0\u5f55<\/h3>\n<p>\u5728  JDK 8u40 \u4e4b\u524d\uff0cJVM \u5fc5\u987b\u5e26\u542f\u52a8\u6807\u8bb0\uff1a <code>-XX:+UnlockCommercialFeatures -XX:FlightRecorder<\/code>\uff0c\u4ece  JDK 8u40 \u5f00\u59cb\uff0cJFR \u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u542f\u7528\u3002<\/p>\n<ol>\n<li>\n<p>\u68c0\u67e5\u8fd0\u884c\u4e2d\u7684\u5f55\u5236<br \/>\n\u5982\u56fe\uff1a<img decoding=\"async\" src=\"http:\/\/coderbee.net\/wp-content\/uploads\/2015\/04\/jmc-jfr.png\" alt=\"jmc-jfr\"  \/><br \/>\n\u5bf9\u73b0\u6709\u5f55\u5236\u7684\u64cd\u4f5c\uff1a<br \/>\n<img decoding=\"async\" src=\"http:\/\/coderbee.net\/wp-content\/uploads\/2015\/04\/jmc-jfr-op.png\" alt=\"jmc-jfr-op\" \/><\/p>\n<\/li>\n<li>\n<p>\u8f6c\u50a8\u8fde\u7eed\u5f55\u5236<br \/>\n\u5982\u4e0a\u56fe\uff0c\u9009\u62e9\u76f8\u5e94\u7684\u8f6c\u50a8\u9009\u9879\u5373\u53ef\uff1a<br \/>\n<img decoding=\"async\" src=\"http:\/\/coderbee.net\/wp-content\/uploads\/2015\/04\/jfr-dump.png\" alt=\"jfr-dump\" \/><\/p>\n<\/li>\n<li>\n<p>\u5f00\u542f\u65b0\u7684\u5f55\u5236<br \/>\n<img decoding=\"async\" src=\"http:\/\/coderbee.net\/wp-content\/uploads\/2015\/04\/jfr-start-recording.png\" alt=\"jfr-start-recording\"  \/><br \/>\n\u56fe\u4e2d\u7684 \u56fa\u5b9a\u65f6\u95f4\u8bb0\u5f55 \u5c31\u662f\u6027\u80fd\u5256\u6790\u5f55\u5236\u3002<\/p>\n<\/li>\n<\/ol>\n<p><em>\u6ce8\u610f\uff1a<\/em>\u5178\u578b\u7684\u6027\u80fd\u5256\u6790\u5f55\u5236\u7684\u5f00\u9500\u662f <code>2%<\/code>\u3002<\/p>\n<p>\u9ed8\u8ba4\u8bbe\u7f6e\u5728\u6570\u636e\u548c\u6027\u80fd\u4e4b\u95f4\u6709\u597d\u7684\u5e73\u8861\u3002\u5728\u6709\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u80fd\u60f3\u6dfb\u52a0\u989d\u5916\u7684\u4e8b\u4ef6\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u5728\u8c03\u67e5\u5185\u5b58\u6cc4\u6f0f\u6216\u60f3\u770b\u770b\u5360\u636e\u6700\u591a Java \u5806\u7684\u5bf9\u8c61\uff0c\u5f00\u542f <strong>\u5806\u7edf\u8ba1<\/strong>\u3002\u8fd9\u5c06\u89e6\u53d1\u4e24\u4e2a\u8001\u5e74\u4ee3\u6536\u96c6\uff0c\u5728\u5f55\u5236\u5f00\u542f\u548c\u7ed3\u675f\u65f6\uff0c\u6240\u4ee5\u5c06\u5f15\u5165\u4e00\u4e9b\u989d\u5916\u7684\u5ef6\u8fdf\u3002\u4f60\u4e5f\u53ef\u4ee5\u9009\u62e9\u5c55\u793a\u629b\u51fa\u7684\u6240\u6709\u5f02\u5e38\uff0c\u5373\u4f7f\u662f\u88ab\u6355\u83b7\u7684\u3002\u5bf9\u4e8e\u6709\u7684\u5e94\u7528\u7a0b\u5e8f\uff0c\u8fd9\u5c06\u751f\u6210\u5f88\u591a\u4e8b\u4ef6\u3002<\/p>\n<p><strong>Threshold<\/strong> \u7684\u503c\u662f\u5f55\u5236\u4e8b\u4ef6\u7684\u957f\u5ea6\u3002\u4f8b\u5982\uff0c\u9ed8\u8ba4\u5730\uff0c\u8d85\u8fc7 10ms \u7684\u540c\u6b65\u5c06\u751f\u6210\u4e8b\u4ef6\u3002\u8fd9\u610f\u5473\u7740\uff0c\u5982\u679c\u4e00\u4e2a\u7ebf\u7a0b\u7b49\u5f85\u4e00\u4e2a\u9501\u8d85\u8fc7 10 ms\uff0c\u4e00\u4e2a\u4e8b\u4ef6\u88ab\u4fdd\u5b58\u3002\u4f60\u53ef\u4ee5\u964d\u4f4e\u8fd9\u4e2a\u503c\u6765\u5f97\u5230\u77ed\u7ade\u4e89\u7684\u66f4\u8be6\u7ec6\u6570\u636e\u3002<\/p>\n<p><strong>\u7ebf\u7a0b\u8f6c\u50a8<\/strong> \u8bbe\u7f6e\u7ed9\u4f60\u4e00\u4e2a\u5468\u671f\u5730\u8f6c\u50a8\u7ebf\u7a0b\u7684\u9009\u9879\u3002\u8fd9\u4e9b\u662f\u666e\u901a\u7684\u6587\u672c\u7684\u7ebf\u7a0b\u8f6c\u50a8\u3002\u7ebf\u7a0b\u8f6c\u50a8\u8865\u8db3\u4e86\u4e8b\u4ef6\u3002<\/p>\n<h3>2.2\u3001\u7528\u547d\u4ee4\u884c\u542f\u52a8\u6807\u8bb0\u6765\u751f\u6210\u98de\u884c\u8bb0\u5f55<\/h3>\n<ol>\n<li>\n<p>\u5f00\u59cb\u6027\u80fd\u5256\u6790\u5f55\u5236<br \/>\n<code>java -XX:+UnlockCommercialFeatures -XX:+FlightRecorder -XX:StartFlightRecording=delay=20s,duration=60s,name=myrecording,filename=C:\\TEMP\\myrecording.jfr,settings=profile MyApp<\/code><br \/>\n\u8868\u793a JVM \u542f\u52a8 20 \u79d2\u540e\u5f00\u59cb\u5f55\u5236 60 \u79d2\uff0c\u6570\u636e\u4fdd\u5b58\u5230 <code>C:\\TEMP\\myrecording.jfr<\/code>  \u7684\u6027\u80fd\u5256\u6790\u5f55\u5236\u3002<br \/>\n<code>settings<\/code> \u53c2\u6570\u8981\u4e48\u662f\u6307\u5411\u6a21\u677f\u7684\u8def\u5f84\u6216\u8005\u540d\u5b57\uff0c\u9ed8\u8ba4\u7684\u6a21\u677f\u4f4d\u4e8e <code>jre\/lib\/jfr<\/code> \u6587\u4ef6\u5939\u3002\u4e24\u4e2a\u6807\u51c6\u7684 profiles \u662f\uff1adefault\uff0c\u4e00\u4e2a\u4f4e\u5f00\u9500\u7684\u8bbe\u7f6e\uff0c\u7528\u4e8e\u8fde\u7eed\u5f55\u5236\uff1b profile\uff0c\u6536\u96c6\u66f4\u591a\u6570\u636e\uff0c\u4e3b\u8981\u7528\u4e8e\u6027\u80fd\u5256\u6790\u5f55\u5236\u3002<\/p>\n<\/li>\n<li>\n<p>\u5f00\u59cb\u8fde\u7eed\u5f55\u5236<br \/>\n<code>java -XX:+UnlockCommercialFeatures -XX:+FlightRecorder -XX:FlightRecorderOptions=defaultrecording=true,disk=true,repository=\/tmp,maxage=6h,settings=default MyApp<\/code><br \/>\n\u6570\u636e\u88ab\u4e34\u65f6\u4fdd\u5b58\u5230 \/tmp` \u76ee\u5f55\uff0c\u4fdd\u5b58\u6700\u8fd1 6 \u5c0f\u65f6\u7684\u6570\u636e\uff0c\u7a0d\u540e\u53ef\u4ee5\u8f6c\u50a8\u51fa\u6765\u3002<\/p>\n<\/li>\n<li>\n<p>\u4f7f\u7528\u8bca\u65ad\u5de5\u5177<br \/>\n\u8fd8\u53ef\u4ee5\u901a\u8fc7 Java \u547d\u4ee4\u884c\u8bca\u65ad\u547d\u4ee4\u6765\u63a7\u5236\u5f55\u5236\u3002\u6700\u7b80\u5355\u7684\u662f\u7528\u8bca\u65ad\u5de5\u5177 <code>jcmd<\/code>\u3002<\/p>\n<\/li>\n<\/ol>\n<h3>2.3\u3001\u7528\u89e6\u53d1\u5668\u6765\u81ea\u52a8\u5f55\u5236<\/h3>\n<p>\u4f60\u53ef\u4ee5\u8bbe\u7f6e JMC \u6765\u81ea\u52a8\u5f00\u59cb\u6216\u8f6c\u50a8\u98de\u884c\u8bb0\u5f55\uff0c\u5982\u679c\u6709\u6761\u4ef6\u5339\u914d\u4e86\uff0c\u8fd9\u53ef\u4ee5\u901a\u8fc7 JMX \u63a7\u5236\u53f0\u5b8c\u6210\u3002<br \/>\n<img decoding=\"async\" src=\"http:\/\/coderbee.net\/wp-content\/uploads\/2015\/04\/jfr-automatic-trigger.png\" alt=\"jfr-automatic-trigger\"  \/><\/p>\n<p>\u4f60\u53ef\u4ee5\u5728\u5e94\u7528\u7a0b\u5e8f\u7684\u4efb\u610f MBean \u4e0a\u521b\u5efa\u89e6\u53d1\u5668\u3002\u6709\u4e00\u4e9b\u9ed8\u8ba4\u7684\u89e6\u53d1\u5668\u8bbe\u7f6e\u7528\u4e8e\u666e\u901a\u6761\u4ef6\uff0c\u4f8b\u5982 CPU \u4f7f\u7528\u60c5\u51b5\uff0c\u7ebf\u7a0b\u6b7b\u9501\uff0c\u6216\u5927\u7684\u5b58\u6d3b\u96c6\u5408\u3002<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/coderbee.net\/wp-content\/uploads\/2015\/04\/jfr-automatic-trigger-setup.png\" alt=\"jfr-automatic-trigger-setup\" \/><br \/>\n\u5728\u4e0a\u56fe\u4e2d\uff0c <code>Action<\/code> \u9009\u9879\u5361\u8868\u793a\u6761\u4ef6\u5339\u914d\u540e\u8981\u6267\u884c\u7684\u52a8\u4f5c\u3002<\/p>\n<hr\/>\n<p>\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u5fae\u4fe1\u516c\u4f17\u53f7: <strong>coderbee\u7b14\u8bb0<\/strong>\uff0c\u53ef\u4ee5\u66f4\u53ca\u65f6\u56de\u590d\u4f60\u7684\u8ba8\u8bba\u3002<br \/>\n<img loading=\"lazy\" decoding=\"async\" width=\"258\" height=\"258\" src=\"https:\/\/coderbee.net\/wp-content\/uploads\/2019\/01\/coderbee-note.jpg\" class=\"alignnone size-full wp-image-1707\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001JFR \u98de\u884c\u8bb0\u5f55\u5668 Java Flight Recorder\uff08JFR\uff09\u662f\u4e00\u4e2a &hellip; <a href=\"https:\/\/coderbee.net\/index.php\/jvm\/20150406\/1188\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[96],"tags":[213,233,214],"_links":{"self":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/1188"}],"collection":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/comments?post=1188"}],"version-history":[{"count":7,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/1188\/revisions"}],"predecessor-version":[{"id":1751,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/1188\/revisions\/1751"}],"wp:attachment":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/media?parent=1188"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/categories?post=1188"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/tags?post=1188"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}