僞共享和緩存行填充

什麼是僞共享html

關於僞共享講解最清楚的是這篇文章《剖析Disruptor:爲何會這麼快?(三)僞共享》,我這裏就直接摘抄其對僞共享的解釋:java

 

緩存系統中是以緩存行(cache line)爲單位存儲的。緩存行是2的整數冪個連續字節,通常爲32-256個字節。最多見的緩存行大小是64個字節。當多線程修改互相獨立的變量時,如 果這些變量共享同一個緩存行,就會無心中影響彼此的性能,這就是僞共享。緩存行上的寫競爭是運行在SMP系統中並行線程實現可伸縮性最重要的限制因素。有 人將僞共享描述成無聲的性能殺手,由於從代碼中很難看清楚是否會出現僞共享。程序員

爲了讓可伸縮性與線程數呈線性關係,就必須確保不會有兩個線程往同一個變量或緩存行中寫。兩個線程寫同一個變量能夠在代碼中發現。爲了肯定互相獨立的變量 是否共享了同一個緩存行,就須要瞭解內存佈局,或找個工具告訴咱們。Intel VTune就是這樣一個分析工具。本文中我將解釋Java對象的內存佈局以及咱們該如何填充緩存行以免僞共享。算法

cache-line.png

圖1說明了僞共享的問題。在覈心1上運行的線程想更新變量X,同時核心2上的線程想要更新變量Y。不幸的是,這兩個變量在同一個緩存行中。每一個線程都要去 競爭緩存行的全部權來更新變量。若是核心1得到了全部權,緩存子系統將會使核心2中對應的緩存行失效。當核心2得到了全部權而後執行更新操做,核心1就要 使本身對應的緩存行失效。這會來來回回的通過L3緩存,大大影響了性能。若是互相競爭的核心位於不一樣的插槽,就要額外橫跨插槽鏈接,問題可能更加嚴重。緩存

JAVA 7下的方案多線程

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
public final class FalseSharing implements Runnable { 
    public static int NUM_THREADS = 4; // change 
    public final static long ITERATIONS = 500L * 1000L * 1000L; 
    private final int arrayIndex; 
    private static VolatileLong[] longs; 
   
    public FalseSharing(final int arrayIndex) { 
        this.arrayIndex = arrayIndex; 
    
   
    public static void main(final String[] args) throws Exception { 
        Thread.sleep(10000); 
        System.out.println("starting...."); 
        if (args.length == 1) { 
            NUM_THREADS = Integer.parseInt(args[0]); 
        
   
        longs = new VolatileLong[NUM_THREADS]; 
        for (int i = 0; i < longs.length; i++) { 
            longs[i] = new VolatileLong(); 
        
        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; 
        
    
}
?
1
2
3
public class VolatileLongPadding {
    public volatile long p1, p2, p3, p4, p5, p6; // 註釋 
}
?
1
2
3
public class VolatileLong extends VolatileLongPadding {
    public volatile long value = 0L; 
}

 

把padding放在基類裏面,能夠避免優化。(這好像沒有什麼道理好講的,JAVA7的內存優化算法問題,能繞則繞)。不過,這種辦法怎麼看都有點煩,借用另一個博主的話:作個java程序員真難。工具

 

 

JAVA 8下的方案佈局

在JAVA 8中,緩存行填充終於被JAVA原生支持了。JAVA 8中添加了一個@Contended的註解,添加這個的註解,將會在自動進行緩存行填充。以上的例子能夠改成:性能

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
public final class FalseSharing implements Runnable { 
    public static int NUM_THREADS = 4; // change 
    public final static long ITERATIONS = 500L * 1000L * 1000L; 
    private final int arrayIndex; 
    private static VolatileLong[] longs; 
   
    public FalseSharing(final int arrayIndex) { 
        this.arrayIndex = arrayIndex; 
    
   
    public static void main(final String[] args) throws Exception { 
        Thread.sleep(10000); 
        System.out.println("starting...."); 
        if (args.length == 1) { 
            NUM_THREADS = Integer.parseInt(args[0]); 
        
   
        longs = new VolatileLong[NUM_THREADS]; 
        for (int i = 0; i < longs.length; i++) { 
            longs[i] = new VolatileLong(); 
        
        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; 
        
    
}
?
1
2
3
4
5
6
import sun.misc.Contended;
 
@Contended
public class VolatileLong {
    public volatile long value = 0L; 
}

 

執行時,必須加上虛擬機參數-XX:-RestrictContended,@Contended註釋纔會生效。不少文章把這個漏掉了,那樣的話實際上就沒有起做用。優化

 


 

原文連接:http://www.cnblogs.com/Binhua-Liu/p/5620339.html

相關文章
相關標籤/搜索