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

            前些天組里老司機(jī)@梁希在jvm的項(xiàng)目榨干機(jī)器性能之余,為了檢查下gcc編譯器和Intel Xoen CPU的正確性,寫(xiě)了一組測(cè)試代碼測(cè)試了下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).
            `

            簡(jiǎn)單來(lái)說(shuō)就是一個(gè)可以在CPU亂序執(zhí)行中保證真實(shí)的load/store順序的指令

            0x1
            老司機(jī)寫(xiě)了一個(gè)小程序(注:有誤版)
            // 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個(gè)變量,v1.iv2.ib ,起了2個(gè)線程,一個(gè)順序?qū)懭雟1和v2,一個(gè)讀v1和v2,互相通過(guò)改變b的值來(lái)通訊,然后兩個(gè)線程不停循環(huán)。

            這個(gè)程序會(huì)掛在
            printf("assert error, cnt = %d, icnt = %d, i1 = %d, i2 = %d\n", cnt, icnt, i1, i2); 
            這條斷言上,意思是線程1在順序?qū)懭雟1和v2,但是主線程卻出現(xiàn)讀到 v1=0,v2=1的情況。

            0x2

            然后我?guī)兔θタ戳艘幌拢X(jué)得這種寫(xiě)法甚是粗暴,于是原樣照搬了一個(gè)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;
            }

            因?yàn)槭窃瓨诱瞻幔钥隙ㄟ€是會(huì)掛,但是畢竟語(yǔ)義上更好理解了

            我們先來(lái)分析一下為什么會(huì)掛

            • 線程1對(duì)于v1,v2的寫(xiě)入順序一定是一致的
            • Memory Barrier也保證了他們寫(xiě)入順序?qū)ζ渌€程的可見(jiàn)性(很有迷惑性的一點(diǎn))
            • 但是主線程卻可以讀到 v1=0,v2=1的情況
            • 所以情況就是雖然順序?qū)懭肓耍莿e的線程沒(méi)有看到正確的順序?
            • Intel: 并不是!
            • 原因是搞錯(cuò)了因果關(guān)系,他真正保證的順序是當(dāng)你讀到v2的new value的時(shí)候,那么v1也一定被寫(xiě)入了。
            • 解決方案就是互換上面代碼中我用**星號(hào)**標(biāo)注出的兩行
            • done

            在舊寫(xiě)法中,掛掉的情況是線程1寫(xiě)入v1 = 1,主線程讀v1,沒(méi)有讀到,那么主線程認(rèn)為v1是0,然后線程1繼續(xù)寫(xiě)入v2,主線程讀到了,主線程認(rèn)為v2是1。 然后掛在了斷言上。

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

            0x3

            當(dāng)然,想讓跑通那個(gè)例子不需要那么多的atomic<>,精簡(jiǎn)之后利用c++11的memory_order可以寫(xiě)成如下:

            #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在兩個(gè)線程之間同步,如下圖

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

            看來(lái)Intel的Strong Memory Model已經(jīng)保證了這一點(diǎn),Memory Barrier都不需要了

            (雖然標(biāo)題里面有MemoryBarrier,但是內(nèi)容里面根本沒(méi)涉及的樣子。。)

            posted on 2016-01-19 16:13 右席 閱讀(16780) 評(píng)論(1)  編輯 收藏 引用 所屬分類(lèi): 搬磚之路
            99久久国产精品免费一区二区| 亚洲国产成人精品女人久久久 | 久久久久黑人强伦姧人妻| 99久久精品国产一区二区蜜芽| 99久久国产亚洲高清观看2024| 老司机午夜网站国内精品久久久久久久久| 日本精品久久久久影院日本 | 久久一本综合| 无码人妻久久一区二区三区免费| 狠狠色婷婷综合天天久久丁香 | 久久亚洲AV成人无码软件| 久久精品国产亚洲AV无码偷窥| 精品无码久久久久久久久久| 亚洲欧洲中文日韩久久AV乱码| 激情伊人五月天久久综合| 精品免费久久久久国产一区| 无码久久精品国产亚洲Av影片 | 老男人久久青草av高清| 国产Av激情久久无码天堂| 久久精品人妻一区二区三区| 亚洲精品白浆高清久久久久久| 99久久精品免费观看国产| 日韩精品久久久久久免费| 久久久久国产精品三级网| 久久精品人人做人人爽97| 亚洲国产成人久久综合一区77| 国内精品伊人久久久久AV影院| 合区精品久久久中文字幕一区| 国产69精品久久久久99| 国产成人精品久久二区二区| 久久久久久久97| 久久天天日天天操综合伊人av| 99久久精品国产免看国产一区| 亚洲精品乱码久久久久久按摩 | 中文字幕无码精品亚洲资源网久久| 亚洲&#228;v永久无码精品天堂久久| 国产婷婷成人久久Av免费高清| 人妻精品久久无码区| 亚洲精品高清国产一线久久| 欧美黑人又粗又大久久久| 亚洲精品乱码久久久久久 |