{"id":3913,"date":"2025-07-13T12:05:13","date_gmt":"2025-07-13T04:05:13","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=3913"},"modified":"2025-07-13T12:05:13","modified_gmt":"2025-07-13T04:05:13","slug":"%e4%bd%bf%e7%94%a8-perf-intel-vtune-%e5%88%86%e6%9e%90-cpu-cache-%e5%91%bd%e4%b8%ad%e7%8e%87%e5%af%b9%e6%af%94","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/07\/13\/%e4%bd%bf%e7%94%a8-perf-intel-vtune-%e5%88%86%e6%9e%90-cpu-cache-%e5%91%bd%e4%b8%ad%e7%8e%87%e5%af%b9%e6%af%94\/","title":{"rendered":"\u4f7f\u7528\u00a0perf\/Intel VTune\u00a0\u5206\u6790 CPU cache \u547d\u4e2d\u7387\u5bf9\u6bd4"},"content":{"rendered":"\n<p>\u4e0b\u6587\u8bb2\u89e3\u5982\u4f55\u4f7f\u7528 Linux \u6027\u80fd\u5206\u6790\u5de5\u5177 <strong>perf<\/strong> \u548c Intel \u7684 <strong>VTune Profiler<\/strong> \u6765\u5206\u6790 Java \u7a0b\u5e8f\u4e2d CPU \u7f13\u5b58\u547d\u4e2d\u7387\uff0c\u4ece\u800c\u8bc4\u4f30\u4f2a\u5171\u4eab\u5bf9\u6027\u80fd\u7684\u5f71\u54cd\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e00\u3001\u51c6\u5907\u5de5\u4f5c<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>perf<\/strong> \u662f Linux \u81ea\u5e26\u7684\u6027\u80fd\u5206\u6790\u5de5\u5177\uff0c\u9002\u5408\u5feb\u901f\u91c7\u6837\u5206\u6790\u3002<\/li>\n\n\n\n<li><strong>Intel VTune Profiler<\/strong> \u662f Intel \u5b98\u65b9\u9ad8\u7ea7\u6027\u80fd\u5206\u6790\u5de5\u5177\uff0c\u652f\u6301\u66f4\u7ec6\u7c92\u5ea6\u7684\u786c\u4ef6\u4e8b\u4ef6\u76d1\u63a7\u548c\u53ef\u89c6\u5316\u3002<\/li>\n\n\n\n<li>\u8981\u5206\u6790\u7684\u7a0b\u5e8f\uff1a<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f2a\u5171\u4eab\u7248\u672c\uff08\u65e0 padding \u6216\u65e0 <code>@Contended<\/code>\uff09<\/li>\n\n\n\n<li>\u975e\u4f2a\u5171\u4eab\u7248\u672c\uff08\u52a0 padding \u6216\u52a0 <code>@Contended<\/code>\uff09<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u786e\u4fdd\u673a\u5668 CPU \u652f\u6301\u786c\u4ef6\u6027\u80fd\u8ba1\u6570\u5668\uff08\u51e0\u4e4e\u6240\u6709\u73b0\u4ee3 Intel\/AMD CPU \u90fd\u652f\u6301\uff09\u3002<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e8c\u3001\u7528 perf \u91c7\u96c6 Cache \u547d\u4e2d\u7387\u6570\u636e<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u542f\u52a8\u6d4b\u8bd5\u7a0b\u5e8f<\/h3>\n\n\n\n<p>\u5047\u8bbe\u4f60\u7528 JMH \u5199\u4e86\u4e24\u4e2a\u57fa\u51c6\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>FalseSharingBenchmark<\/code> (\u4f2a\u5171\u4eab)<\/li>\n\n\n\n<li><code>NoFalseSharingBenchmark<\/code> (\u975e\u4f2a\u5171\u4eab)<\/li>\n<\/ul>\n\n\n\n<p>\u5148\u5206\u522b\u7528\u547d\u4ee4\u542f\u52a8\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>java -jar benchmark.jar FalseSharingBenchmark\njava -jar benchmark.jar NoFalseSharingBenchmark<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2. \u4f7f\u7528 perf \u91c7\u96c6\u786c\u4ef6\u4e8b\u4ef6<\/h3>\n\n\n\n<p>\u53ef\u4ee5\u7528 <code>perf stat<\/code> \u6765\u91c7\u96c6 cache \u76f8\u5173\u4e8b\u4ef6\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>perf stat -e cache-references,cache-misses -p &lt;java_pid&gt; sleep 10<\/code><\/pre>\n\n\n\n<p>\u6216\u8005\u76f4\u63a5\u8fd0\u884c\u7a0b\u5e8f\u5e76\u91c7\u96c6\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>perf stat -e cache-references,cache-misses java -jar benchmark.jar FalseSharingBenchmark\nperf stat -e cache-references,cache-misses java -jar benchmark.jar NoFalseSharingBenchmark<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3. \u89e3\u91ca\u8f93\u51fa\u793a\u4f8b<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code> Performance counter stats for 'java -jar benchmark.jar':\n\n    1,234,567 cache-references\n      56,789 cache-misses          # 4.60% of all cache refs\n\n       0.123456789 seconds time elapsed<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>cache-references<\/strong>\uff1aCPU \u8bbf\u95ee\u7f13\u5b58\u7684\u603b\u6b21\u6570<\/li>\n\n\n\n<li><strong>cache-misses<\/strong>\uff1a\u8bbf\u95ee\u7f13\u5b58\u5931\u8d25\uff0c\u5fc5\u987b\u8bbf\u95ee\u66f4\u6162\u7684\u5185\u5b58\u5c42\u6b21<\/li>\n\n\n\n<li><strong>miss rate<\/strong> = cache-misses \/ cache-references<\/li>\n<\/ul>\n\n\n\n<p><strong>\u4f2a\u5171\u4eab\u901a\u5e38\u5bfc\u81f4\u7f13\u5b58\u884c\u9891\u7e41\u5931\u6548\uff0ccache-misses \u4f1a\u663e\u8457\u4e0a\u5347\u3002<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e09\u3001\u4f7f\u7528 Intel VTune Profiler \u8be6\u7ec6\u5206\u6790<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u5b89\u88c5 VTune<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.intel.com\/content\/www\/us\/en\/develop\/tools\/vtune-profiler.html\">Intel VTune Profiler \u4e0b\u8f7d<\/a><\/li>\n\n\n\n<li>\u652f\u6301 Linux\/Windows\/macOS<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">2. \u542f\u52a8\u91c7\u96c6<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>vtune -collect memory-access -- java -jar benchmark.jar FalseSharingBenchmark\nvtune -collect memory-access -- java -jar benchmark.jar NoFalseSharingBenchmark<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3. \u4e3b\u8981\u5173\u6ce8\u6307\u6807<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>L1 Cache Misses<\/strong><\/li>\n\n\n\n<li><strong>L2 Cache Misses<\/strong><\/li>\n\n\n\n<li><strong>LLC (Last Level Cache) Misses<\/strong><\/li>\n\n\n\n<li><strong>Memory Bound stalls<\/strong><\/li>\n\n\n\n<li><strong>Data Sharing \/ False Sharing events<\/strong>\uff08\u90e8\u5206\u7248\u672c\u652f\u6301\uff09<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">4. \u5206\u6790\u89c6\u56fe<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u67e5\u770b\u70ed\u70b9\u51fd\u6570<\/li>\n\n\n\n<li>\u8bc6\u522b\u9ad8\u5ef6\u8fdf\u7684\u5185\u5b58\u8bbf\u95ee<\/li>\n\n\n\n<li>\u6bd4\u8f83\u4e24\u4e2a\u7248\u672c\u7f13\u5b58\u672a\u547d\u4e2d\u7387\u548c stalls \u5dee\u5f02<\/li>\n\n\n\n<li>\u53ef\u89c6\u5316\u7f13\u5b58\u4e00\u81f4\u6027\u4e8b\u4ef6\uff0c\u5224\u65ad\u662f\u5426\u5b58\u5728\u4f2a\u5171\u4eab\u5bfc\u81f4\u7684\u7f13\u5b58\u9891\u7e41\u5931\u6548<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u56db\u3001\u5177\u4f53\u5b9e\u8df5\u793a\u4f8b<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u4f2a\u5171\u4eab\u7248\u672c perf \u91c7\u6837\u793a\u4f8b<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>perf stat -e cache-references,cache-misses java -jar benchmark.jar FalseSharingBenchmark<\/code><\/pre>\n\n\n\n<p>\u8f93\u51fa\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cache-references: 1,500,000\ncache-misses: 150,000 (10%)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2. \u975e\u4f2a\u5171\u4eab\u7248\u672c perf \u91c7\u6837\u793a\u4f8b<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>perf stat -e cache-references,cache-misses java -jar benchmark.jar NoFalseSharingBenchmark<\/code><\/pre>\n\n\n\n<p>\u8f93\u51fa\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cache-references: 1,600,000\ncache-misses: 20,000 (1.25%)<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong>\u547d\u4e2d\u7387\u63d0\u5347\u660e\u663e\uff0c\u8bf4\u660e\u907f\u514d\u4f2a\u5171\u4eab\u6709\u6548\u51cf\u5c11\u7f13\u5b58\u5931\u6548\u3002<\/strong><\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e94\u3001\u5176\u4ed6\u5206\u6790\u5efa\u8bae<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u7ed3\u5408 <code>perf record<\/code> + <code>perf report<\/code> \u67e5\u770b\u5177\u4f53\u8c03\u7528\u6808<\/strong>\uff0c\u5b9a\u4f4d\u70ed\u70b9\u548c\u5931\u6548\u53d1\u751f\u51fd\u6570\u3002<\/li>\n\n\n\n<li>VTune \u7684 <strong>\u201cMemory Access\u201d<\/strong> \u5206\u6790\u53ef\u4ee5\u7ed3\u5408\u65f6\u5e8f\u56fe\u67e5\u770b\u7f13\u5b58\u5931\u6548\u4e0e\u7ebf\u7a0b\u64cd\u4f5c\u5173\u8054\u3002<\/li>\n\n\n\n<li>\u5173\u6ce8 CPU <strong>C-state<\/strong> \u548c <strong>\u7ebf\u7a0b\u8fc1\u79fb<\/strong>\uff0c\u907f\u514d\u6df7\u6dc6\u6027\u80fd\u74f6\u9888\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u516d\u3001\u603b\u7ed3<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u5de5\u5177<\/th><th>\u4f18\u70b9<\/th><th>\u9002\u7528\u573a\u666f<\/th><\/tr><\/thead><tbody><tr><td>perf<\/td><td>\u8f7b\u91cf\u7ea7\uff0cLinux \u901a\u7528\uff0c\u5feb\u901f<\/td><td>\u5feb\u901f\u7edf\u8ba1\u7f13\u5b58\u547d\u4e2d\u7387\uff0c\u521d\u6b65\u6027\u80fd\u8bca\u65ad<\/td><\/tr><tr><td>Intel VTune<\/td><td>\u53ef\u89c6\u5316\u754c\u9762\uff0c\u6df1\u5165\u5206\u6790<\/td><td>\u8be6\u7ec6\u7f13\u5b58\u884c\u4e3a\u5206\u6790\uff0c\u4f2a\u5171\u4eab\u5b9a\u4f4d<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e03\u3001\u53c2\u8003\u8d44\u6599\u548c\u5b98\u65b9\u94fe\u63a5<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/perf.wiki.kernel.org\/index.php\/Main_Page\">perf \u5b98\u65b9\u6587\u6863<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.intel.com\/content\/www\/us\/en\/develop\/documentation\/vtune-help\/top.html\">Intel VTune Profiler \u7528\u6237\u6307\u5357<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/mechanical-sympathy.blogspot.com\/2011\/07\/false-sharing.html\">Java \u6027\u80fd\u4f18\u5316 &#8211; \u4f2a\u5171\u4eab\u4e0e\u7f13\u5b58\u884c<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/www.brendangregg.com\/perf.html\">Linux Perf Tutorial by Brendan Gregg<\/a><\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e0b\u6587\u8bb2\u89e3\u5982\u4f55\u4f7f\u7528 Linux \u6027\u80fd\u5206\u6790\u5de5\u5177 perf \u548c Intel \u7684 VTune Profiler \u6765\u5206\u6790 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3914,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[86],"tags":[],"class_list":["post-3913","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java-"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3913","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=3913"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3913\/revisions"}],"predecessor-version":[{"id":3915,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3913\/revisions\/3915"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/3914"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=3913"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=3913"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=3913"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}