{"id":566,"date":"2013-11-10T19:39:47","date_gmt":"2013-11-10T11:39:47","guid":{"rendered":"http:\/\/coderbee.net\/?p=566"},"modified":"2018-03-26T10:14:54","modified_gmt":"2018-03-26T02:14:54","slug":"false-sharing-%e4%bc%aa%e5%85%b1%e4%ba%ab","status":"publish","type":"post","link":"https:\/\/coderbee.net\/index.php\/concurrent\/20131110\/566","title":{"rendered":"False Sharing \u4f2a\u5171\u4eab &#8211; \u8bd1"},"content":{"rendered":"<p>\u7ffb\u8bd1\u81ea\uff1a<a href=\"http:\/\/mechanical-sympathy.blogspot.com\/2011\/07\/false-sharing.html\">http:\/\/mechanical-sympathy.blogspot.com\/2011\/07\/false-sharing.html<\/a><\/p>\n<h3>\u4f2a\u5171\u4eab<\/h3>\n<p>\u5185\u5b58\u5728\u7f13\u5b58\u7cfb\u7edf\u91cc\u662f\u4ee5\u7f13\u5b58\u884c\u4e3a\u5355\u4f4d\u5b58\u50a8\u7684\u3002\u7f13\u5b58\u884c\u662f\u5927\u5c0f\u4e3a2\u7684\u6574\u6570\u5e42\u7684\u8fde\u7eed\u5b57\u8282\uff0c\u5178\u578b\u662f32-256\u5b57\u8282\u3002\u6700\u666e\u904d\u7684\u7f13\u5b58\u884c\u5927\u5c0f\u662f64\u5b57\u8282\u3002\u4f2a\u5171\u4eab\u662f\u4e2a\u672f\u8bed\uff0c\u7528\u4e8e\u5f53\u591a\u4e2a\u7ebf\u7a0b\u4e0d\u77e5\u4e0d\u89c9\u5730\u76f8\u4e92\u5f71\u54cd\u5bf9\u65b9\u7684\u6027\u80fd\uff0c\u5728\u4fee\u6539\u4f7f\u7528\u76f8\u540c\u7f13\u5b58\u884c\u7684\u4f9d\u8d56\u53d8\u91cf\u65f6\u3002\u7f13\u5b58\u884c\u4e0a\u7684\u5199\u51b2\u7a81\u662f\u5e76\u884c\u6267\u884c\u7ebf\u7a0b\u5728SMP\uff08\u5bf9\u79f0\u591a\u5904\u7406\u5668 \uff09\u7cfb\u7edf\u4e0a\u83b7\u5f97\u4f38\u7f29\u6027\u7684\u5355\u4e00\u6700\u5927\u5236\u7ea6\u56e0\u7d20\u3002\u6211\u5df2\u542c\u5230\u4f2a\u5171\u4eab\u88ab\u63cf\u8ff0\u4e3a\u65e0\u58f0\u7684\u6027\u80fd\u6740\u624b\uff0c\u56e0\u4e3a\u5728\u67e5\u770b\u4ee3\u7801\u65f6\uff0c\u5b83\u975e\u5e38\u4e0d\u660e\u663e\u3002<\/p>\n<p>\u4e3a\u4e86\u8fbe\u5230\u6309\u7ebf\u7a0b\u6570\u91cf\u7684\u7ebf\u6027\u4f38\u7f29\u6027\uff0c\u6211\u4eec\u5fc5\u987b\u786e\u4fdd\u6ca1\u6709\u4e24\u4e2a\u7ebf\u7a0b\u5199\u540c\u4e00\u4e2a\u53d8\u91cf\u6216\u540c\u4e00\u4e2a\u7f13\u5b58\u884c\u3002\u4e24\u4e2a\u7ebf\u7a0b\u5199\u540c\u4e00\u4e2a\u53d8\u91cf\u53ef\u4ee5\u5728\u4ee3\u7801\u5c42\u9762\u8ddf\u8e2a\u5230\u3002\u4e3a\u4e86\u77e5\u9053\u72ec\u7acb\u53d8\u91cf\u662f\u5426\u5171\u4eab\u540c\u4e00\u4e2a\u7f13\u5b58\u884c\uff0c\u6211\u4eec\u9700\u8981\u77e5\u9053\u5185\u5b58\u5e03\u5c40\uff0c\u6216\u8005\u8ba9\u5de5\u5177\u544a\u8bc9\u6211\u4eec\u3002 Intel VTune\u662f\u8fd9\u6837\u7684\u4e00\u4e2a\u5206\u6790\u5de5\u5177\u3002\u5728\u8fd9\u7bc7\u6587\u7ae0\uff0c\u6211\u5c06\u89e3\u91caJava\u5bf9\u8c61\u7684\u5185\u5b58\u5e03\u5c40\u548c\u5982\u4f55\u901a\u8fc7\u586b\u5145\u7f13\u5b58\u884c\u6765\u907f\u514d\u4f2a\u5171\u4eab\u3002<br \/>\n<!--more--><\/p>\n<p><img decoding=\"async\" src=\"\/\/coderbee.net\/wp-content\/uploads\/2013\/11\/false-sharing.png\" alt=\"false-sharing\"  \/><br \/>\n\u4e0a\u56fe\u5c55\u793a\u4e86\u4f2a\u5171\u4eab\u7684\u95ee\u9898\u3002\u7ebf\u7a0bA\u5141\u8bb8\u5728Core 1\uff0c\u60f3\u66f4\u65b0\u53d8\u91cfX\uff0c\u8fd0\u884c\u5728Core 2\u7684\u7ebf\u7a0b\u60f3\u66f4\u65b0\u53d8\u91cfY\u3002\u4e0d\u5e78\u7684\u662f\u8fd9\u4e24\u4e2a\u70ed\u95e8\u53d8\u91cf\u5c5e\u4e8e\u540c\u4e00\u4e2a\u7f13\u5b58\u884c\u3002\u6bcf\u4e2a\u7ebf\u7a0b\u5c06\u7ade\u4e89\u7f13\u5b58\u884c\u7684\u6240\u6709\u6743\uff0c\u8fd9\u6837\u5b83\u4eec\u53ef\u4ee5\u66f4\u65b0\u5b83\u3002\u5982\u679cCore 1\u83b7\u5f97\u6240\u6709\u6743\uff0c\u7f13\u5b58\u5b50\u7cfb\u7edf\u9700\u8981\u4f5c\u5e9fCore 2\u5bf9\u5e94\u7684\u7f13\u5b58\u884c\u3002\u5f53Core 2\u83b7\u5f97\u6240\u6709\u6743\u5e76\u6267\u884c\u5b83\u7684\u66f4\u65b0\uff0cCore 1 \u5c06\u88ab\u544a\u8bc9\u53bb\u4f5c\u5e9f\u5b83\u7684\u7f13\u5b58\u884c\u62f7\u8d1d\u3002\u8fd9\u5c06\u6765\u6765\u56de\u56de\u5f80\u8fd4\u8ba9L3\u7f13\u5b58\u6781\u5927\u5f71\u54cd\u6027\u80fd\u3002\u5982\u679c\u7ade\u4e89\u7684Core\u5904\u4e8e\u4e0d\u540c\u7684sockets\uff0c\u90a3\u4e48\u9700\u8981\u989d\u5916\u7684\u8de8Socket\u4e92\u8fde\uff0c\u8fd9\u4e2a\u95ee\u9898\u5c06\u66f4\u52a0\u6076\u5316\u3002<\/p>\n<h3>Java \u5185\u5b58\u5e03\u5c40<\/h3>\n<p>\u5bf9\u4e8eHotSpot JVM\uff0c\u6240\u6709\u5bf9\u8c61\u90fd\u6709\u4e00\u4e2a2-\u5b57\uff08world\uff09\u7684\u5934\u3002\u7b2c\u4e00\u4e2a\u662f\u201c\u6807\u8bb0\uff08mark\uff09\u201d\u5b57\uff0c\u670924\u6bd4\u7279\u4f4d\u7528\u4e8e\u54c8\u5e0c\u7801\u548c8\u6bd4\u7279\u4f4d\u7528\u4e8e\u6807\u8bb0\uff0c\u4f8b\u5982\u9501\u72b6\u6001\uff0c\u6216\u4ea4\u6362\u7528\u4e8e\u9501\u5bf9\u8c61\u3002\u7b2c\u4e8c\u4e2a\u5b57\u662f\u6307\u5411\u5bf9\u8c61\u6240\u5c5e\u7c7b\u7684\u5f15\u7528\u3002\u6570\u7ec4\u6709\u989d\u5916\u7684\u5b57\u7528\u4e8e\u6570\u7ec4\u7684\u5927\u5c0f\u3002\u6bcf\u4e2a\u5bf9\u8c61\u5bf9\u9f50\u52308\u5b57\u8282\u7684\u7c92\u5ea6\u8fb9\u754c\uff0c\u4e3a\u4e86\u6027\u80fd\u3002\u56e0\u800c\u4e3a\u4e86\u66f4\u9ad8\u6548\u586b\u5145\uff0c\u5bf9\u8c61\u7684\u5b57\u6bb5\u5c06\u88ab\u91cd\u6392\u5e8f\uff0c\u4ece\u58f0\u660e\u7684\u987a\u5e8f\u5230\u4e0b\u9762\u7684\u57fa\u4e8e\u5b57\u8282\u5927\u5c0f\u7684\u987a\u5e8f\uff1a<\/p>\n<ol>\n<li>doubles (8) and longs (8)<\/li>\n<li>ints (4) and floats (4)<\/li>\n<li>shorts (2) and chars (2)<\/li>\n<li>booleans (1) and bytes (1)<\/li>\n<li>references (4\/8)<\/li>\n<li>&lt;repeat for sub-class fields><\/li>\n<\/ol>\n<p>\uff08\u8bd1\u6ce8\uff1a\u4e0a\u9762\u5c0f\u62ec\u53f7\u91cc\u7684\u6570\u5b57\u8868\u793a\u5360\u7528\u7684\u5b57\u8282\u6570\u3002\uff09<\/p>\n<p>\u6709\u4e86\u8fd9\u4e9b\u77e5\u8bc6\uff0c\u6211\u4eec\u53ef\u4ee5\u75287\u4e2along\u5728\u4efb\u610f\u5b57\u6bb5\u4e4b\u95f4\u586b\u5145\u7f13\u5b58\u884c\u3002\u5728Disruptor\u91cc\uff0c\u6211\u4eec\u5728RingBuffer\u6e38\u6807\u548cBatchEventProcessor\u5e8f\u53f7\u5668\u5468\u56f4\u586b\u5145\u7f13\u5b58\u884c\u3002<\/p>\n<p>\u4e3a\u4e86\u5c55\u793a\u6027\u80fd\u5f71\u54cd\uff0c\u8ba9\u6211\u4eec\u7528\u4e00\u4e9b\u7ebf\u7a0b\u5404\u81ea\u66f4\u65b0\u81ea\u5df1\u72ec\u7acb\u7684\u8ba1\u6570\u5668\u3002\u8fd9\u4e9b\u8ba1\u6570\u5668\u5c06\u662f<code>volatile<\/code>\u4fee\u9970\u7684<code>long<\/code>\uff0c\u8fd9\u6837\u5c06\u53ef\u4ee5\u770b\u5230\u4ed6\u4eec\u7684\u8fdb\u5c55\u3002<\/p>\n<pre><code class=\"java\">public final class FalseSharing\r\n    implements Runnable\r\n{\r\n    public final static int NUM_THREADS = 4; \/\/ change\r\n    public final static long ITERATIONS = 500L * 1000L * 1000L;\r\n    private final int arrayIndex;\r\n\r\n    private static VolatileLong[] longs = new VolatileLong[NUM_THREADS];\r\n    static\r\n    {\r\n        for (int i = 0; i &lt; longs.length; i++)\r\n        {\r\n            longs[i] = new VolatileLong();\r\n        }\r\n    }\r\n\r\n    public FalseSharing(final int arrayIndex)\r\n    {\r\n        this.arrayIndex = arrayIndex;\r\n    }\r\n\r\n    public static void main(final String[] args) throws Exception\r\n    {\r\n        final long start = System.nanoTime();\r\n        runTest();\r\n        System.out.println(\"duration = \" + (System.nanoTime() - start));\r\n    }\r\n\r\n    private static void runTest() throws InterruptedException\r\n    {\r\n        Thread[] threads = new Thread[NUM_THREADS];\r\n\r\n        for (int i = 0; i &lt; threads.length; i++)\r\n        {\r\n            threads[i] = new Thread(new FalseSharing(i));\r\n        }\r\n\r\n        for (Thread t : threads)\r\n        {\r\n            t.start();\r\n        }\r\n\r\n        for (Thread t : threads)\r\n        {\r\n            t.join();\r\n        }\r\n    }\r\n\r\n    public void run()\r\n    {\r\n        long i = ITERATIONS + 1;\r\n        while (0 != --i)\r\n        {\r\n            longs[arrayIndex].value = i;\r\n        }\r\n    }\r\n\r\n    public final static class VolatileLong\r\n    {\r\n        public volatile long value = 0L;\r\n        public long p1, p2, p3, p4, p5, p6; \/\/ comment out\r\n    }\r\n}\r\n<\/code><\/pre>\n<h3>\u7ed3\u679c<\/h3>\n<p>\u8fd0\u884c\u4e0a\u9762\u7684\u4ee3\u7801\uff0c\u9010\u6e10\u589e\u52a0\u7ebf\u7a0b\u6570\u91cf\u548c\u6dfb\u52a0\/\u79fb\u9664\u7f13\u5b58\u884c\u586b\u5145\uff0c\u6211\u5f97\u5230\u7684\u7ed3\u679c\u5982\u4e0b\u56fe\u6240\u793a\u3002\u8fd9\u662f\u8861\u91cf\u8fd0\u884c\u5728\u6211\u76844\u6838 Nehalem \u4e0a\u7684\u6d4b\u8bd5\u6301\u7eed\u65f6\u95f4\u3002<br \/>\n<img decoding=\"async\" src=\"https:\/\/coderbee.net\/wp-content\/uploads\/2013\/11\/false-sharing-test-result.png\" alt=\"false-sharing-test-result\"  \/><\/p>\n<p>\u4f2a\u5171\u4eab\u7684\u5f71\u54cd\u662f\u6e05\u6670\u53ef\u89c1\u7684\uff0c\u901a\u8fc7\u5b8c\u6210\u6d4b\u8bd5\u9700\u8981\u7684\u65f6\u95f4\u7684\u589e\u52a0\u3002\u6ca1\u6709\u7f13\u5b58\u884c\u7ade\u4e89\uff0c\u6211\u4eec\u53ef\u4ee5\u8fbe\u5230\u63a5\u8fd1\u6309\u7ebf\u7a0b\u7ebf\u6027\u7684\u6269\u5c55\u3002<\/p>\n<p>\u8fd9\u4e0d\u662f\u4e00\u4e2a\u5b8c\u7f8e\u7b49\u6d4b\u8bd5\uff0c\u56e0\u4e3a\u6211\u4eec\u4e0d\u80fd\u786e\u5b9aVolatileLongs\u5c06\u5e03\u5c40\u5230\u5185\u5b58\u7684\u54ea\u91cc\u3002\u4ed6\u4eec\u662f\u72ec\u7acb\u7684\u53d8\u91cf\u3002\u7136\u800c\uff0c\u7ecf\u9a8c\u663e\u793a\u540c\u65f6\u5206\u914d\u7684\u5bf9\u8c61\u503e\u5411\u4e8e\u540c\u5730\u534f\u4f5c\uff08co-located\uff09\u3002<\/p>\n<p>\u73b0\u5728\u4f60\u77e5\u9053\uff0c\u4f2a\u5171\u4eab\u662f\u65e0\u58f0\u7684\u6027\u80fd\u6740\u624b\u4e86\u3002<\/p>\n<h3>\u53e6\u4e00\u4e2a\u6d4b\u8bd5\u4f8b\u5b50<\/h3>\n<p>\u8fd9\u662f\u6765\u81ea\u4f5c\u8005\u7684\u53e6\u4e00\u7bc7\u6587\u7ae0\uff1a<a href=\"http:\/\/mechanical-sympathy.blogspot.com\/2011\/08\/false-sharing-java-7.html\">http:\/\/mechanical-sympathy.blogspot.com\/2011\/08\/false-sharing-java-7.html<\/a>\uff0c\u7531\u4e8e\u76f8\u5173\uff0c\u5c31\u653e\u4e00\u8d77\u4e86\u3002<\/p>\n<p>\u770b\u8d77\u6765Java 7\u53d8\u5f97\u66f4\u806a\u660e\u4e86\uff0c\u4f1a\u6d88\u9664\u6216\u91cd\u6392\u5e8f\u672a\u4f7f\u7528\u7684\u5b57\u6bb5\uff0c\u8fd9\u91cd\u65b0\u5f15\u5165\u4f2a\u5171\u4eab\u3002\u4f5c\u8005\u8ba4\u4e3a\u4e0b\u9762\u7684\u4ee3\u7801\u662f\u6700\u53ef\u9760\u7684\uff1a<\/p>\n<pre><code class=\"java\">import java.util.concurrent.atomic.AtomicLong;\r\n\r\npublic final class FalseSharing\r\n    implements Runnable\r\n{\r\n    public final static int NUM_THREADS = 4; \/\/ change\r\n    public final static long ITERATIONS = 500L * 1000L * 1000L;\r\n    private final int arrayIndex;\r\n\r\n    private static PaddedAtomicLong[] longs = new PaddedAtomicLong[NUM_THREADS];\r\n    static\r\n    {\r\n        for (int i = 0; i &lt; longs.length; i++)\r\n        {\r\n            longs[i] = new PaddedAtomicLong();\r\n        }\r\n    }\r\n\r\n    public FalseSharing(final int arrayIndex)\r\n    {\r\n        this.arrayIndex = arrayIndex;\r\n    }\r\n\r\n    public static void main(final String[] args) throws Exception\r\n    {\r\n        final long start = System.nanoTime();\r\n        runTest();\r\n        System.out.println(\"duration = \" + (System.nanoTime() - start));\r\n    }\r\n\r\n    private static void runTest() throws InterruptedException\r\n    {\r\n        Thread[] threads = new Thread[NUM_THREADS];\r\n\r\n        for (int i = 0; i &lt; threads.length; i++)\r\n        {\r\n            threads[i] = new Thread(new FalseSharing(i));\r\n        }\r\n\r\n        for (Thread t : threads)\r\n        {\r\n            t.start();\r\n        }\r\n\r\n        for (Thread t : threads)\r\n        {\r\n            t.join();\r\n        }\r\n    }\r\n\r\n    public void run()\r\n    {\r\n        long i = ITERATIONS + 1;\r\n        while (0 != --i)\r\n        {\r\n            longs[arrayIndex].set(i);\r\n        }\r\n    }\r\n\r\n    public static long sumPaddingToPreventOptimisation(final int index)\r\n    {\r\n        PaddedAtomicLong v = longs[index];\r\n        return v.p1 + v.p2 + v.p3 + v.p4 + v.p5 + v.p6;\r\n    }\r\n\r\n    public static class PaddedAtomicLong extends AtomicLong\r\n    {\r\n        public volatile long p1, p2, p3, p4, p5, p6 = 7L;\r\n    }\r\n}\r\n<\/code><\/pre>\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<\/p>\n<p><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>\u7ffb\u8bd1\u81ea\uff1ahttp:\/\/mechanical-sympathy.blogspot. &hellip; <a href=\"https:\/\/coderbee.net\/index.php\/concurrent\/20131110\/566\">\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":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[121],"tags":[102,103],"_links":{"self":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/566"}],"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=566"}],"version-history":[{"count":9,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/566\/revisions"}],"predecessor-version":[{"id":1611,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/566\/revisions\/1611"}],"wp:attachment":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/media?parent=566"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/categories?post=566"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/tags?post=566"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}