在筆記本上用了2年的SSD,這幾天突然想了個問題--掛載SSD是否該開啟'nobarrier'標(biāo)示?問了朋友和網(wǎng)友,眾說紛紜,查找了一些資料,做了總結(jié)--It depends!
Depending什么?要搞清楚Depending什么,先要明白何謂barrier?
何謂barrier
引用mount manual:
barrier=0 / barrier=1
This disables / enables the use of write barriers in the jbd code. barrier=0 disables, barrier=1 enables (default). This also requires an IO stack which can support barriers, and if jbd gets an error on a barrier write, it will disable barriers again with a warning. Write barriers enforce proper on-disk ordering of journal commits, making volatile disk write caches safe to use, at some performance penalty. If your disks are battery-backed in one way or another, disabling barriers may safely improve performance.
寫的不清不楚.
其實簡單說barrier是保證日志文件系統(tǒng)的WAL(write ahead logging)一種手段--現(xiàn)代日志文件系統(tǒng)如ext4有個journal區(qū),類似數(shù)據(jù)庫領(lǐng)域的redo log,用于意外崩潰后的快速恢復(fù).數(shù)據(jù)寫入磁盤時,理應(yīng)先寫入journal區(qū),再寫入數(shù)據(jù)在磁盤的實際對應(yīng)位置;磁盤廠商為了加快磁盤寫入速度,磁盤都內(nèi)置cache,數(shù)據(jù)一般都先寫入磁盤的cache.
cache能加快寫入速度,當(dāng)然是極好的東西,但磁盤一般會對cache內(nèi)緩存數(shù)據(jù)排序使之最優(yōu)刷新到磁盤,這樣就可能導(dǎo)致要刷新的實際數(shù)據(jù)和journal順序錯亂;一旦系統(tǒng)崩潰,下次開機時磁盤要參考journal區(qū)來恢復(fù),但此時journal記錄順序與數(shù)據(jù)實際刷新順序不同就會導(dǎo)致數(shù)據(jù)反而"恢復(fù)"到不一致了.而barrier如其名--'柵欄',先加一個'柵欄',保證journal總是先寫入記錄,然后對應(yīng)數(shù)據(jù)才刷新到磁盤,這種方式保證了系統(tǒng)崩潰后磁盤恢復(fù)的正確性,但對寫入性能有蠻大影響.
何時開啟SSD'nobarrier'標(biāo)示
前面說道barrier不開啟情況下journal記錄會和數(shù)據(jù)刷新順序不一致,但journal只有恢復(fù)時才會用,所以只要disk無需恢復(fù)就行:
1.BBWC (Battery-Backed Write Cache)
通常企業(yè)級服務(wù)器都有HBA,提供單獨電池,這樣即使系統(tǒng)崩潰,磁盤一直有電供著.
2.FBWC (Flash-Backed Write Cache)
類似BBW,但不用電池而是使用flash做存儲沸呐,掉電時有一個大電容供電醇王,足夠?qū)⒋疟Pcache中的內(nèi)容寫入flash.
3.其他供電方式
UPS呢燥、筆記本電池等等
4.無cache或禁用cache
這樣子寫就是同步的了,自然barrier也就沒什么意義;Percona的《Tuning For Speed: Percona Server and Fusion-io》上說利用ioMemory的無cache特性,自然無需write barrier.
除了以上4種情況,其他情況下SSD不要開啟"nobarrier"標(biāo)示
參考
Barriers and journaling filesystems
Barriers, Caches, Filesystems
Is disabling barriers for ext4 safe on a laptop with battery?