wordpress插件看訪問(wèn)者數(shù)量關(guān)鍵詞優(yōu)化策略
談起內(nèi)存屏障,大家感覺(jué)這個(gè)"玩意兒"很虛,不太實(shí)際,但是內(nèi)核代碼中又廣泛地可以看到起身影。內(nèi)存屏障,英文barrier,這個(gè)"玩意兒"它還不太好去定義它。barrier,中文翻譯為柵欄,柵欄大家都見(jiàn)過(guò),現(xiàn)實(shí)生活中就是防止他人或者動(dòng)物非法闖入而用來(lái)進(jìn)行隔離用的工具。再進(jìn)一步,既然是防止闖入,那就是要保護(hù)柵欄內(nèi)的東西。所以在linux 內(nèi)核中,內(nèi)存屏障用于保護(hù)內(nèi)存的訪問(wèn)。
1、什么需要保護(hù)
下面來(lái)談?wù)剝?nèi)存屏障到底保護(hù)什么。
比如cpu0執(zhí)行 a = 1 這條指令時(shí),假設(shè)a所在cache line已經(jīng)在cpu1的 L1 cache中,cpu0 先要獲取a所在的cache line到cpu0的L1cache中,由于是寫(xiě)操作,需要改寫(xiě)a的值,需要再總線上發(fā)送invalid消息讓其他cpu使無(wú)效其cache中a的值,等待其他cpu應(yīng)答后,cpu0才能改寫(xiě)a的值,這樣才能保證cache一致性。
?等待其他cpu上使無(wú)效消息的應(yīng)答期間造成了cpu0的無(wú)效等待,浪費(fèi)時(shí)間。于是cpu設(shè)計(jì)者開(kāi)始修改CPU設(shè)計(jì),出現(xiàn)了使無(wú)效隊(duì)列以及write buffer這些內(nèi)部部件用來(lái)加速cpu的執(zhí)行。具體內(nèi)容讀者可以查閱其他文章。
提升CPU執(zhí)行效率(硬件層面)和編譯器的優(yōu)化使得指令重排序(軟件層面)給程序員帶來(lái)了負(fù)擔(dān),需要程序去進(jìn)行進(jìn)行內(nèi)存訪問(wèn)順序的維護(hù)及保序。
2、ARM提供的指令
ARM提供了如下指令來(lái)進(jìn)行內(nèi)存屏障的處理:
DMB:Data Memory Barrier,數(shù)據(jù)存儲(chǔ)屏障
DSB:Date Synchronization Barrier,數(shù)據(jù)同步屏障
ISB:Instruction Synchronization Barrier,指令同步屏障
DMB和DSB的一個(gè)本質(zhì)區(qū)別,DMB針對(duì)的是memory的load/store之間;DSB強(qiáng)調(diào)的是同類或不同類事物的先后完成。
Data Memory Barrier (DMB) ensures that all explicit memory accesses that appear in program order before the DMB instruction are observed
before any explicit memory accesses that appear in program order after the DMB instruction.
數(shù)據(jù)內(nèi)存屏障(DMB)確保DMB指令之前的所有顯式內(nèi)存訪問(wèn)在DMB指令開(kāi)始之后的任何顯式內(nèi)存訪問(wèn)之前被觀察到。且DMB指令不影響處理上執(zhí)行的任何其他指令的順序。
Data Synchronization Barrier (DSB),No instruction in program order after this instruction executes until this instruction completes.
數(shù)據(jù)同步屏障(DSB)完成后,其后面的指令才可執(zhí)行??梢?jiàn)DSB影響了其他指令的執(zhí)行。
Instruction Synchronization Barrier (ISB) flushes the pipeline in the processor, so that all instructions following the ISB are fetched from cache or memory,
after the instruction has been completed.
指令同步屏障(ISB)沖刷處理器中的流水線,以便在ISB完成后,從緩存或內(nèi)存中提取ISB之后的所有指令??梢?jiàn)ISB嚴(yán)重影響后續(xù)指令的執(zhí)行。
3、linux內(nèi)核實(shí)現(xiàn)
arch/arm/include/asm/barrier.h/*isb,dsb,dmb匯編指令*/
#define isb(option) __asm__ __volatile__ ("isb " #option : : : "memory")
#define dsb(option) __asm__ __volatile__ ("dsb " #option : : : "memory")
#define dmb(option) __asm__ __volatile__ ("dmb " #option : : : "memory")/*barrier:編譯優(yōu)化屏障,阻止編譯器為了性能優(yōu)化而進(jìn)行指令重排*/
#define barrier() __asm__ __volatile__("": : :"memory")/*內(nèi)存屏障(包括讀和寫(xiě)),用于SMP和UP*/
#define mb() do { dsb(); outer_sync(); } while (0)
/*讀內(nèi)存屏障,用于SMP和UP*/
#define rmb() dsb()
/*寫(xiě)內(nèi)存屏障,用于SMP和UP*/
#define wmb() do { dsb(st); outer_sync(); } while (0)/*osh:outer shareable domain*/
#define dma_rmb() dmb(osh)
#define dma_wmb() dmb(oshst)/*用于SMP場(chǎng)合的內(nèi)存屏障。*/
/*ish:inner shareable domain,在ish范圍內(nèi)客觀測(cè)到結(jié)果*/
#define smp_mb() dmb(ish)
/*用于SMP場(chǎng)合的讀內(nèi)存屏障*/
#define smp_rmb() smp_mb()
/*用于SMP場(chǎng)合的寫(xiě)內(nèi)存屏障*/
/*waits only for stores to complete, and only to the inner shareable domain.*/
#define smp_wmb() dmb(ishst)