关于缓存行填充的拼图

时间:2018-01-11 13:00:57

标签: java caching

我读过一篇关于缓存行填充的文章,网址是: https://mechanical-sympathy.blogspot.com/2011/07/false-sharing.html

它有一个这样的例子:

public final class FalseSharing implements Runnable {
    public final static int NUM_THREADS = 4; // change
    public final static long ITERATIONS = 500L * 1000L * 1000L;
    private final int arrayIndex;

    private static VolatileLong[] longs = new VolatileLong[NUM_THREADS];
    static {
        for (int i = 0; i < longs.length; i++) {
            longs[i] = new VolatileLong();
        }
    }

    public FalseSharing(final int arrayIndex) {
        this.arrayIndex = arrayIndex;
    }

    public static void main(final String[] args) throws Exception {
        final long start = System.nanoTime();
        runTest();
        System.out.println("duration = " + (System.nanoTime() - start));
    }

    private static void runTest() throws InterruptedException {
        Thread[] threads = new Thread[NUM_THREADS];

        for (int i = 0; i < threads.length; i++) {
            threads[i] = new Thread(new FalseSharing(i));
        }

        for (Thread t : threads) {
            t.start();
        }

        for (Thread t : threads) {
            t.join();
        }
    }

    public void run() {
        long i = ITERATIONS + 1;
        while (0 != --i) {
            longs[arrayIndex].value = i;
        }
    }

    public final static class VolatileLong {
        public volatile long value = 0L;
        public long p1, p2, p3, p4, p5, p6; // comment out
    }
}

问题1: 如果我想避免错误共享,我应该确保VolatileLong对象是64字节,长值0L是8字节,p1,p2,p3,p4,p5,p6是48字节,那么剩下的8字节究竟是什么?

问题2: 我执行了这个程序,结果是:22951146607 如果我在VolatileLong中删除变量p6,结果是:19457942328,它比p6小,而它应该在没有p6的情况下遭受错误共享。当然,结果每次都不同,但通常p6的时间多于没有,它没有显示缓存行填充的优势。

1 个答案:

答案 0 :(得分:1)

来自VolatileLong的{​​{1}}中缺少的额外8个字节由object header获取。因此VolatileLong的完整大小为64字节,即使源代码中只有56个字节可见。

最后,使用适当的micro benchmark harness执行测试以获得可靠的结果会更有效。