• <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 右席 閱讀(16769) 評論(1)  編輯 收藏 引用 所屬分類: 搬磚之路
            久久中文精品无码中文字幕| 久久噜噜电影你懂的| 色播久久人人爽人人爽人人片aV| 亚洲国产精品综合久久一线| 久久无码高潮喷水| 久久99国产精品久久99| 久久精品国产精品亚洲| 午夜精品久久久久久中宇| 久久久久国产精品| 亚洲AV无码1区2区久久| 精品久久综合1区2区3区激情| 婷婷久久久亚洲欧洲日产国码AV | 亚洲午夜久久久久久久久久| 久久99国内精品自在现线| 亚洲午夜精品久久久久久app| 欧美精品一区二区精品久久 | 久久无码AV一区二区三区| 国产精品毛片久久久久久久| 久久久亚洲AV波多野结衣| 国产精品免费久久久久电影网| 久久久久久无码Av成人影院| 亚洲国产天堂久久久久久| 国产午夜精品久久久久九九电影 | 国产成人无码精品久久久免费| 精品国产青草久久久久福利| 一本久久综合亚洲鲁鲁五月天亚洲欧美一区二区 | 国产精品99久久久久久董美香| 久久久久亚洲av成人网人人软件| 亚洲成色999久久网站| 国产日产久久高清欧美一区| 国产精品禁18久久久夂久| 久久久久无码精品国产不卡| 国产69精品久久久久观看软件| 欧美麻豆久久久久久中文| 欧美午夜A∨大片久久| 午夜精品久久久久久影视777| 久久精品国产一区二区三区不卡| 精品久久久久久国产牛牛app | 久久99久久99精品免视看动漫 | 色综合久久综精品| 狠狠色伊人久久精品综合网|