CPU學(xué)習(xí) (Cache Coherence)
在2004年寫的一篇文章x86匯編語言學(xué)習(xí)手記(1)中,曾經(jīng)涉及到gcc編譯的代碼默認(rèn)16字節(jié)棧對齊的問題。之所以這樣做,主要是性能優(yōu)化方面的考慮?! 〈蠖鄶?shù)現(xiàn)代cpu都o(jì)ne-die了l1和l2cache。對于l1 cache,大多是write though的;l2 cache則是write back的,不會立即寫回memory,這就會導(dǎo)致cache和memory的內(nèi)容的不一致;另外,對于mp(multi processors)的環(huán)境,由于cache是cpu私有的,不同cpu的cache的內(nèi)容也存在不一致的問題,因此很多mp的的計算架構(gòu),不論是ccnuma還是smp都實現(xiàn)了cache coherence的機制,即不同cpu的cache一致性機制。 cache coherence的一種實現(xiàn)是通過cache-snooping協(xié)議,每個cpu通過對bus的snoop實現(xiàn)對其它cpu讀寫cache的監(jiān)控: 首先,cache line是cache和memory之間數(shù)據(jù)傳輸?shù)淖钚卧! ?. 當(dāng)cpu1要寫cache時,其它cpu就會檢查自己cache中對應(yīng)的cache line,如果是dirty的,就write back到memory,并且會將cpu1的相關(guān)cache line刷新;如果不是dirty的,就invalidate該cache line. 2. 當(dāng)cpu1要讀cache時,其它cpu就會將自己cache中對應(yīng)的cache line中標(biāo)記為dirty的部分write back到memory,并且會將cpu1的相關(guān)cache line刷新。 所以,提高cpu的cache hit rate,減少cache和memory之間的數(shù)據(jù)傳輸,將會提高系統(tǒng)的性能?! ∫虼?,在程序和二進制對象的內(nèi)存分配中保持cache line aligned就十分重要,如果不保證cache line對齊,出現(xiàn)多個cpu中并行運行的進程或者線程同時讀寫同一個cache line的情況的概率就會很大。這時cpu的cache和memory之間會反復(fù)出現(xiàn)write back和refresh情況,這種情形就叫做cache thrashing。 為了有效的避免cache thrashing,通常有以下兩種途徑: 1. 對于heap的分配,很多系統(tǒng)在malloc調(diào)用中實現(xiàn)了強制的alignment.
2. 對于stack的分配,很多編譯器提供了stack aligned的選項?! ‘?dāng)然,如果在編譯器指定了stack aligned,程序的尺寸將會變大,會占用更多的內(nèi)存。因此,這中間的取舍需要仔細(xì)考慮,下面是我在google上搜索到的一段討論:one of our customers complained about the additional code generated to
maintain the stack aligned to 16-byte boundaries, and suggested us to
default to the minimum alignment when optimizing for code size. this
has the caveat that, when you link code optimized for size with code
optimized for speed, if a function optimized for size calls a
performance-critical function with the stack misaligned, the
performance-critical function may perform poorly.





