Linux Kernel: buffers和cached的區別

The page cache caches pages of files to optimize file I/O. The buffer cache caches disk blocks to optimize block I/O. 

Page Cache緩存文件內容以優化文件I/O,Buffer Cache緩存磁盤blocks以優化block I/O。 

註釋:Block 塊是用來管理磁盤空間的,而Page 頁是針對內存管理,從磁盤讀出的數據就緩存在內存頁中。當一個塊被調入到內存中,它要被存儲在一個緩衝區中。 

 Prior to Linux kernel version 2.4, the two caches were distinct: Files were in the page cache, disk blocks were in the buffer cache. Given that most files are represented by a filesystem on a disk, data was represented twice, once in each of the caches. Many Unix systems follow a similar pattern. 

Linux kernel 2.4以前,這兩個cache的使用是有明顯區別的:文件的內容在Page Cache中緩衝,(管理基於磁盤的文件系統的VFS所訪問的)blocks在Buffer Cache中緩衝。鑑於大多數的文件都是由基於磁盤的文件系統來存儲和表示(represented )的,這樣數據就被CACHE了兩次,每個緩存(PageCache & BufferCache)中各表示一次。許多Unix系統都遵循此類的模式。 

 This is simple to implement, but with an obvious ineleganceand inefficiency. Starting with Linux kernel version 2.4, the contents of the two caches were unified. The VM subsystem now drives I/O and it does so out of the page cache. If cached data has both a file and a block representation—as most data does—the buffer cache will simply point into the page cache; thus only one instance of the data is cached in memory. The page cache is what you picture when you think of a disk cache: It caches file data from a disk to make subsequent I/O faster. 

這樣易於實現,但存在明顯的槽點(CACHE重複)和低效。從Linux kernel 2.4(2.4.10)開始,兩個cache中的內容統一了(buffer cache不再真正的存在,實際上buffer cache不再獨立分配,而是在page cache中用專門的buffer page來替代,buffer page在形式上就是緩衝區描述符,稱爲buffer_head)。現在由VM子系統來驅動I/O(並獨立於Page Cache)。如果緩衝的數據既有文件表示(file representation)又有塊表示(block representation)——大多數數據都是如此——那麼buffer cache就簡單的指向page cache;這樣就僅有一份數據被緩衝在內存中了。當你想象一個磁盤緩存,page cache即是:它緩衝磁盤中的文件數據,以使後續的I/O操作更快。 

註釋:每個緩衝區與一個塊對應,它相當於磁盤塊在內存中的表示。在內存頁中,有一種叫專門用途的頁面叫“緩衝區頁”,用來存放塊緩衝區。而每個塊緩存區由兩部分組成:緩衝區首部(用數據結構buffer_head表示)及真正的緩衝區內容(即所存儲的數據,這些數據就放在剛剛說到的緩衝區頁中)。而文件在內存中由file結構體表示,而磁盤塊在內存中是由緩衝區來進行表示的。 

 The buffer cache remains, however, as the kernel still needs to perform block I/O in terms of blocks, not pages. As most blocks represent file data, most of the buffer cache is represented by the page cache. But a small amount of block data isn't file backed—metadata and raw block I/O for example—and thus is solely represented by the buffer cache. 

Buffer cache還被保留,因爲內核還要以block爲單位(而不是page)來進行block I/O操作。由於大多數情況下block就已經代表了文件數據,所以大多數的buffer cache由page cache來代替了。但還有少量block數據不是文件內容本身——例如文件系統的元數據(metadata)和裸設備(raw) block I/O——這些還是在buffer cache中緩衝的。 

註釋:由於內核處理塊時需要一些信息(比如將CACHE中的髒數據寫入到磁盤中,而數據是被文件系統組織存儲在block中的),如塊屬於哪個設備與塊對應於哪個緩衝區。所以每個緩衝區都有一個緩衝區描述符,稱爲buffer_head。它包含了內核操作緩衝區所需要的全部信息。通過buffer_head 可以快速的定位page中獨立的blocak在磁盤上的邏輯地址。 

 See also my answer to What is the difference between Buffers and Cached columns in /proc/meminfo output?


圖1:page cache 和 buffer cache 的關係


圖2:the buffer cache


原文鏈接:Linux Kernel: What is the major difference between the buffer cache and the page cache?

參考文章:block(塊),page(頁),buffer cache(塊緩衝)區別與聯繫  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章