Posted on 2011-03-22 10:02
MiweiDev 閱讀(1087)
評論(0) 編輯 收藏 引用 所屬分類:
雜談
(http://blog.liuw.name/669)
五一期間看了一篇文章,Memory Barriers: a Hardware View for Software Hackers,對于Memory Barriers得到了更加深入的理解。
Cache本身的更新是遵守MESI(Modified,Exclusive,Shared,Invalid)協(xié)議的。CPU之間的Cache信息更新通過消息傳遞來完成。
但是現(xiàn)在CPU的設(shè)計(jì)中,在Cache之外加入了Store Buffer和Invalidate Queue。Store
Buffer的加入,使得CPU對某內(nèi)存單元的更新不能馬上反映到Cache中;Invalidate
Queue的存在,使得其他CPU對Cache的invalidate操作不能馬上反映到Cache中。Store Buffer和Invalidate
Queue提高了性能,但是也就導(dǎo)致了Cache的不一致。
因此需要引入Memory Barriers。Store Buffer和Invalidate Queue應(yīng)該分別對應(yīng)使用wmb和rmb。當(dāng)然直接使用通用mb也是可以的。
Roughly speaking, a “rmb” marks only the invalidate queue and a “wmb” marks only the store buffer, while a “mb” does both.
一般來說,Memory Barriers應(yīng)該配對使用,比如說一方使用了rmb另外一方對應(yīng)使用wmb。在Linux內(nèi)核中,還存在著Data
Dependence Memory
Barrier,這是一個(gè)較弱的rmb。具體見Linux內(nèi)核代碼的Documentation/memory-barriers.txt。