0

I've read an article about cache line padding, the url is : https://mechanical-sympathy.blogspot.com/2011/07/false-sharing.html

It has an Example like this:

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
    }
}

Question 1: If I want to avoid false sharing, I should make sure the VolatileLong object is 64 bytes, the long value 0L is 8 bytes, p1, p2, p3, p4, p5, p6 are 48 bytes, then what is the rest 8 bytes exactly?

Question 2: I've executed this program, the result is:22951146607 If I remove the variable p6 in VolatileLong, the result is: 19457942328, it's less than with p6, while it's supposed to suffer from false sharing for without p6. Of course, the result is different every time, but usually the time with p6 is more than without, it doesn't show the advantage for cache line padding.

Kayaman
  • 72,141
  • 5
  • 83
  • 121
gesanri
  • 237
  • 1
  • 3
  • 10

1 Answers1

1

The additional 8 bytes that looks like it's missing from VolatileLong is taken by the object header. So the full size of VolatileLong is 64 bytes, even if only 56 bytes are visible in the source code.

Lastly it would be more efficient to perform the testing with a proper micro benchmark harness to get reliable results.

Kayaman
  • 72,141
  • 5
  • 83
  • 121
  • I've read about the object header, while I have a 64-bit architectures, so the head has taken 16 bits space? So in this program, in order to avoid the cache line padding, I should delete p6, that's right? @Kayaman – gesanri Jan 12 '18 at 02:08
  • That depends on whether you're using compressed OOPs or not. It's described in the linked post. – Kayaman Jan 12 '18 at 06:22