• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            posts - 9,  comments - 19,  trackbacks - 0

            0x0

            前些天組里老司機@梁希在jvm的項目榨干機器性能之余,為了檢查下gcc編譯器和Intel Xoen CPU的正確性,寫了一組測試代碼測試了下mfence指令的效果

            `
            mfence Opcode : 0F AE /6

            Performs a serializing operation on all load-from-memory and store-to-memory instructions that were issued prior the MFENCE instruction. This serializing operation guarantees that every load and store instruction that precedes in program order the MFENCE instruction is globally visible before any load or store instruction that follows the MFENCE instruction is globally visible. The MFENCE instruction is ordered with respect to all load and store instructions, other MFENCE instructions, any SFENCE and LFENCE instructions, and any serializing instructions (such as the CPUID instruction).
            Weakly ordered memory types can be used to achieve higher processor performance through such techniques as out-of-order issue, speculative reads, write-combining, and write-collapsing.
            The degree to which a consumer of data recognizes or knows that the data is weakly ordered varies among applications and may be unknown to the producer of this data. The MFENCE instruction provides a performance-efficient way of ensuring load and store ordering between routines that produce weakly-ordered results and routines that consume that data.
            It should be noted that processors are free to speculatively fetch and cache data from system memory regions that are assigned a memory-type that permits speculative reads (that is, the WB, WC, and WT memory types). The PREFETCHh instruction is considered a hint to this speculative behavior. Because this speculative fetching can occur at any time and is not tied to instruction execution, the MFENCE instruction is not ordered with respect to PREFETCHh instructions or any other speculative fetching mechanism (that is, data could be speculatively loaded into the cache just before, during, or after the execution of an MFENCE instruction).
            `

            簡單來說就是一個可以在CPU亂序執行中保證真實的load/store順序的指令

            0x1
            老司機寫了一個小程序(注:有誤版)
            // file: order.c

            #define _GNU_SOURCE
            #include <pthread.h>
            #include <stdio.h>
            #include <stdlib.h>
            #include <assert.h>

            union p64 {
                int i;
                char padding[64];
                long align8;
            };

            volatile union p64 v1, v2;
            int b;

            void *
            run1(void *ignore)
            {
                for (;;) {
                    while (!b);
                    if (v1.i || v2.i) {
                        puts("assert error 1");
                        exit(-1);
                    }
                    v1.i = 1;
                    asm ("sfence": : :"memory");
                    v2.i = 1;
                    asm ("sfence": : :"memory");
                    b = 0; 
                }
            }

            int
            main()
            {
                pthread_t p;
                pthread_create(&p, NULL, run1, NULL);
                int cnt = 0;

                for (;; cnt++) {
                    v1.i = v2.i = 0;
                    asm ("sfence": : :"memory");
                    b = 1;
                    asm ("sfence": : :"memory");
                    int icnt = 0;
                    for (;; icnt++) {
                        int i1 = v1.i;
                        asm ("lfence": : :"memory");
                        int i2 = v2.i;
                        if (i1 && i2)   break;
                        if (i1 < i2) {
                            printf("assert error, cnt = %d, icnt = %d, i1 = %d, i2 = %d\n", cnt, icnt, i1, i2);
                            exit(-1);
                        }
                    }
                }
                return 0;
            }

            大概邏輯是: 一共有3個變量,v1.iv2.ib ,起了2個線程,一個順序寫入v1和v2,一個讀v1和v2,互相通過改變b的值來通訊,然后兩個線程不停循環。

            這個程序會掛在
            printf("assert error, cnt = %d, icnt = %d, i1 = %d, i2 = %d\n", cnt, icnt, i1, i2); 
            這條斷言上,意思是線程1在順序寫入v1和v2,但是主線程卻出現讀到 v1=0,v2=1的情況。

            0x2

            然后我幫忙去看了一下,覺得這種寫法甚是粗暴,于是原樣照搬了一個c++11版:

            #include <stdio.h>
            #include <stdlib.h>
            #include <assert.h>

            #include <atomic>
            #include <thread>

            using namespace std;

            union p64 {
                atomic<int> i;
                char padding[64];
                long align8;
            };

            volatile union p64 v1, v2;
            atomic<int> b;

            void *
            run1()
            {
                int rcnt = 0;
                for (;; rcnt++) {
                    while (!b.load());
                    if (v1.i.load() || v2.i.load()) {
                        puts("assert error 1");
                        exit(-1);
                    }
                    v1.i.store(1);
                    v2.i.store(1);
                    b.store(0);
                }
            }

            int
            main()
            {
                // init
                v1.i.store(0);
                v2.i.store(0);
                thread t(run1);
                int cnt = 0;
                for (;; cnt++) {
                    v1.i.store(0);
                    v2.i.store(0);
                    b.store(1);
                    int icnt = 0;
                    for (;; icnt++) {
                        int b2 = b.load();
                        int i1 = v1.i.load();       // *****
                        int i2 = v2.i.load();       // *****
                        if (i1 && i2)   break;
                        if (i1 < i2) {
                            printf("assert error, cnt = %d, icnt = %d, i1 = %d, i2 = %d\n", cnt, icnt, i1, i2);
                            exit(-1);
                        }
                        if (i1 == 0 && i2 == 0 && b2 == 0) break;
                    }
                }
                return 0;
            }

            因為是原樣照搬,所以肯定還是會掛,但是畢竟語義上更好理解了

            我們先來分析一下為什么會掛

            • 線程1對于v1,v2的寫入順序一定是一致的
            • Memory Barrier也保證了他們寫入順序對其他線程的可見性(很有迷惑性的一點)
            • 但是主線程卻可以讀到 v1=0,v2=1的情況
            • 所以情況就是雖然順序寫入了,但是別的線程沒有看到正確的順序?
            • Intel: 并不是!
            • 原因是搞錯了因果關系,他真正保證的順序是當你讀到v2的new value的時候,那么v1也一定被寫入了。
            • 解決方案就是互換上面代碼中我用**星號**標注出的兩行
            • done

            在舊寫法中,掛掉的情況是線程1寫入v1 = 1,主線程讀v1,沒有讀到,那么主線程認為v1是0,然后線程1繼續寫入v2,主線程讀到了,主線程認為v2是1。 然后掛在了斷言上。

            兩行互換后,主線程首先讀取v2,如果v2已經是1了,那么v1也一定是1,反之亦然。

            0x3

            當然,想讓跑通那個例子不需要那么多的atomic<>,精簡之后利用c++11的memory_order可以寫成如下:

            #include <stdio.h>
            #include <stdlib.h>
            #include <assert.h>

            #include <atomic>
            #include <thread>

            using namespace std;

            union p64 {
                int i;
                char padding[64];
                long align8;
            };

            volatile union p64 v1, v2;
            atomic<int> b;    // variable b as a guard

            void *
            run1()
            {
                int rcnt = 0;
                for (;; rcnt++) {
                    while (!b.load());
                    if (v1.i || v2.i) {
                        puts("assert error 1");
                        exit(-1);
                    }
                    v1.i = 1;
                    v2.i = 1;
                    b.store(0, memory_order_release);
                }
            }
            int
            main()
            {
                // init
                v1.i = 0;
                v2.i = 0;
                thread t(run1);
                int cnt = 0;

                for (;; cnt++) {
                    v1.i = 0;
                    v2.i = 0;
                    b.store(1, memory_order_release);
                    int icnt = 0;
                    for (;; icnt++) {
                        int b2 = b.load(memory_order_acquire);
                        if (b2 != 0) {
                            continue
                        }
                        int i1 = v1.i;
                        int i2 = v2.i;
                        if (i1 && i2)   break;
                        if (i1 < i2) {
                            printf("assert error 2, cnt = %d, icnt =  %d, i1 = %d, i2 = %d\n", cnt, icnt, i1, i2);
                            exit(-1);
                        }
                    }
                }
                return 0;
            }

            利用變量b在兩個線程之間同步,如下圖

             (Thead 1)

               v1.i = 1;
               v2.i = 1;
               
               b.store(0, memory_order_release) <---+
                                                                         |
                                                            synchronize with b
                                                             (happend before)
                                                                         |
                                                                        +----->  b.load(memory_order_acquire)
                                                                                      
                                                                                    i1 = v1.i
                                                                                    i2 = v2.i

                                                                                   (Thread 2)

            我們查看下生成的代碼
            g++ -std=c++11 -pthread -g -O2 order.cpp

             v1.i = 1;
              400be6:       c7 05 d0 10 20 00 01    movl   $0x1,0x2010d0(%rip)        # 601cc0 <v1>
              400bed:       00 00 00 
                    v2.i = 1;
              400bf0:       c7 05 86 10 20 00 01    movl   $0x1,0x201086(%rip)        # 601c80 <v2>
              400bf7:       00 00 00 
                    memory_order __b = __m & __memory_order_mask;
                    __glibcxx_assert(__b != memory_order_acquire);
                    __glibcxx_assert(__b != memory_order_acq_rel);
                    __glibcxx_assert(__b != memory_order_consume);

                    __atomic_store_n(&_M_i, __i, __m);
              400bfa:       c7 05 5c 10 20 00 00    movl   $0x0,0x20105c(%rip)        # 601c60 <b>
              400c01:       00 00 00 
                    b.store(0, memory_order_release);

              

              400a58:       8b 05 02 12 20 00       mov    0x201202(%rip),%eax        # 601c60 <b>
                        int b2 = b.load(memory_order_consume);
                        if (b2 != 0) {
              400a5e:       85 c0                   test   %eax,%eax
              400a60:       75 f3                   jne    400a55 <main+0x55>
                            continue
                        }
                        int i1 = v1.i;
              400a62:       8b 0d 58 12 20 00       mov    0x201258(%rip),%ecx        # 601cc0 <v1>
                        int i2 = v2.i;
              400a68:       44 8b 05 11 12 20 00    mov    0x201211(%rip),%r8d        # 601c80 <v2>

            看來Intel的Strong Memory Model已經保證了這一點,Memory Barrier都不需要了

            (雖然標題里面有MemoryBarrier,但是內容里面根本沒涉及的樣子。。)

            posted on 2016-01-19 16:13 右席 閱讀(16776) 評論(1)  編輯 收藏 引用 所屬分類: 搬磚之路
            久久大香香蕉国产| 久久免费视频6| 久久久久亚洲av无码专区喷水 | 99久久99久久| 91亚洲国产成人久久精品| 精品久久久久久无码中文字幕| 亚洲国产精品综合久久一线| 亚洲AV成人无码久久精品老人| 久久精品国产亚洲AV香蕉| 99久久国产免费福利| 久久香综合精品久久伊人| 996久久国产精品线观看| 久久中文字幕无码专区| 成人久久综合网| 日韩久久久久久中文人妻| 国产成人精品久久综合| 人妻少妇久久中文字幕| 四虎国产精品成人免费久久| 国产高清国内精品福利99久久| 亚洲AV无码久久寂寞少妇| 欧美亚洲另类久久综合婷婷| 久久免费线看线看| 2021久久国自产拍精品| 午夜欧美精品久久久久久久| 三级三级久久三级久久| 亚洲精品99久久久久中文字幕 | 狠狠色噜噜色狠狠狠综合久久| 人人狠狠综合久久亚洲88| 久久99精品国产麻豆| 久久99热这里只频精品6| 久久精品国产一区二区电影| 久久精品国产免费| 国产亚洲欧美成人久久片| 色婷婷综合久久久中文字幕| 午夜精品久久久久久久久| 久久无码AV一区二区三区| 久久亚洲高清综合| 久久亚洲国产最新网站| 亚洲国产成人久久笫一页 | 久久久婷婷五月亚洲97号色 | 午夜久久久久久禁播电影|