Posted on 2011-03-22 10:02
MiweiDev 閱讀(1081)
評論(0) 編輯 收藏 引用 所屬分類:
雜談
(http://blog.liuw.name/669)
五一期間看了一篇文章,Memory Barriers: a Hardware View for Software Hackers,對于Memory Barriers得到了更加深入的理解。
Cache本身的更新是遵守MESI(Modified,Exclusive,Shared,Invalid)協議的。CPU之間的Cache信息更新通過消息傳遞來完成。
但是現在CPU的設計中,在Cache之外加入了Store Buffer和Invalidate Queue。Store
Buffer的加入,使得CPU對某內存單元的更新不能馬上反映到Cache中;Invalidate
Queue的存在,使得其他CPU對Cache的invalidate操作不能馬上反映到Cache中。Store Buffer和Invalidate
Queue提高了性能,但是也就導致了Cache的不一致。
因此需要引入Memory Barriers。Store Buffer和Invalidate Queue應該分別對應使用wmb和rmb。當然直接使用通用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應該配對使用,比如說一方使用了rmb另外一方對應使用wmb。在Linux內核中,還存在著Data
Dependence Memory
Barrier,這是一個較弱的rmb。具體見Linux內核代碼的Documentation/memory-barriers.txt。