首頁>技術>

FS Cache

FS-Cache是一種核心功能,網路檔案系統或其他檔案系統可以透過它來快取資料到本地磁碟空間,減少網路傳輸的資料,從而提升效能。這在網路速度比較慢時會得到比較好的效果。

FS-Cache 可以被任何希望新增本地快取的檔案系統使用,例如:AFS、NFS、CIFS和Isofs。

FS-Cache 對於客戶端檔案系統是透明存在的,當這項功能開啟的時候,透過快取請求檔案對於客戶端是無感知的。可以參考下圖,FS-Cache可以認為是網路檔案系統和快取後端的中間介質:

看一個更詳細的圖,FS-Cache為網路檔案系統提供了一個快取工具,從而讓快取對使用者無感知

FS-Cache並不遵循在允許訪問之前將所有完全開啟的每個netfs檔案完全載入到快取記憶體中,主要有以下幾個原因:

沒有Cache 也應該能夠正常操作被訪問的檔案的大小不應該受限於Cache的空間大小所有已經開啟的檔案大小不應該受限於Cache的空間大小不應該強制使用者為了一個檔案操作(訪問檔案的一小部分)將整個檔案全部進行下載快取

FS-Cache提供的能力如下:

1. More than one cache can be used at once. Caches can be selected explicitly by use of tags. 一次可以使用多個Cache,不同的Cache使用不同的tag區分2. Caches can be added / removed at any time. Cache可以在任何時間被移除或者新增3. The netfs is provided with an interface that allows either party to withdraw caching facilities from a file (required for (2)). 網路檔案系統提供介面,能夠允許其他方刪除檔案cache相關的能力4. The interface to the netfs returns as few errors as possible, preferring rather to let the netfs remain oblivious. 網路檔案系統儘量不要返回錯誤5. Cookies are used to represent indices, files and other objects to the netfs. The simplest cookie is just a NULL pointer - indicating nothing cached there. 使用cookie表示檔案系統的目錄、檔案、其他物件6. The netfs is allowed to propose - dynamically - any index hierarchy it desires, though it must be aware that the index search function is recursive, stack space is limited, and indices can only be children of indices.7. Data I/O is done direct to and from the netfs’s pages. The netfs indicates that page A is at index B of the data-file represented by cookie C, and that it should be read or written. The cache backend may or may not start I/O on that page, but if it does, a netfs callback will be invoked to indicate completion. The I/O may be either synchronous or asynchronous.8. Cookies can be “retired” upon release. At this point FS-Cache will mark them as obsolete and the index hierarchy rooted at that point will get recycled.9. The netfs provides a “match” function for index searches. In addition to saying whether a match was made or not, this can also specify that an entry should be updated or deleted.10. As much as possible is done asynchronously.

FS-Cache維護了一個網路檔案系統資料的全部索引,該資訊可以位於一個或者多個cache中,如下圖所示:

In the example above, you can see two netfs’s being backed: NFS and AFS. These have different index hierarchies:* The NFS primary index contains per-server indices. Each server index is indexed by NFS file handles to get data file objects. Each data file objects can have an array of pages, but may also have further child objects, such as extended attributes and directory entries. Extended attribute objects themselves have page-array contents.* The AFS primary index contains per-cell indices. Each cell index contains per-logical-volume indices. Each of volume index contains up to three indices for the read-write, read-only and backup mirrors of those volumes. Each of these contains vnode data file objects, each of which contains an array of pages.
CacheFiles介紹

CacheFiles,是屬於Linux Kernel的一個模組,主要用於快取已經掛載的檔案系統,CacheFiles 是一個快取後端,當一個檔案系統掛載到本地時,可以基於CacheFiles做一個快取目錄,CacheFi 使用一個使用者空間的守護程序進行cache管理,例如收割陳舊的節點和剔除,這個守護程序被稱為cachefilesd

快取的檔案系統和資料完整性與後端服務的檔案系統一樣好,由於不同檔案系統的日誌記錄介面都是特殊定義的,因此CacheFiles不會嘗試記錄任檔案系統日誌

CacheFiles 會建立一個混雜的字元裝置”/dev/cachefiles”,用於與守護程序進行通訊,這個裝置一次開啟只能做一次事情,當它開啟時,至少存在部分快取,守護程序開啟 併發送指令用於控制快取,CacheFiles目前只能用於一個單獨的快取

CacheFiles會嘗試維護檔案系統一定比例的空閒空間,可能會透過剔除部分cache使用者縮小cache的大小,用於釋放空間,這就意味著可以在同一介質上存放靈活的實時資料,可能會擴充套件來使用空閒的空間,也可能收縮

Requiremenets

使用CacheFiles 需要以下依賴

dnotify: 對檔案訊號進行監聽extended attribute(xattrs)openat() and friendsbmap() support on files in the filesystem (FIBMAP ioctl)the use of bmap() to detect a partial page at the end of the file配置

配置檔案 /etc/cachefilesd.conf,配置檔案的主要內容為:

brun <N>%, bcull <N>%, bstop <N>%, frun <N>%, fcull <N>%, fstop <N>%Configure the culling limits. Optional. See the section on culling The defaults are 7% (run), 5% (cull) and 1% (stop) respectively.The commands beginning with a ‘b’ are file space (block) limits, those beginning with an ‘f’ are file count limits(也可以限制檔案數量).dir <path> 存放快取的根目錄Specify the directory containing the root of the cache. Mandatory.tag <name> Specify a tag to FS-Cache to use in distinguishing multiple caches. Optional. The default is “CacheFiles”.debug <mask>  用於開啟日誌Specify a numeric bitmask to control debugging in the kernel module. Optional. The default is zero (all off). The following values can be OR’d into the mask to collect various information:1	Turn on trace of function entry (_enter() macros)2	Turn on trace of function exit (_leave() macros)4	Turn on trace of internal debug points (_debug())This mask can also be set through sysfs, eg:echo 5 >/sys/modules/cachefiles/parameters/debug
啟動服務

啟動守護程序,該守護程序開啟 cache 裝置(/dev/cachefiles),配置cache,並開始進行cache,此時cache繫結fscache,cache開始執行。具體的啟動命令和引數如下所示:

The daemon is run as follows:/sbin/cachefilesd [-d]* [-s] [-n] [-f <configfile>]The flags are:-dIncrease the debugging level. This can be specified multiple times and is cumulative with itself.-sSend messages to stderr instead of syslog.-nDon’t daemonise and go into background.-f <configfile>Use an alternative configuration file rather than the default one.
快取剔除

快取偶爾需要進行清理,用於釋放空間,這裡主要將近期未被使用的cache進行清理,基於檔案的訪問時間進行判斷,如果空目錄沒有在使用也會被清理。Cache的清理是基於配置的當前檔案系統的block比例和檔案比例,主要有6個限制,如下所示

brun, frunIf the amount of free space and the number of available files in the cache rises above both these limits, then culling is turned off.當快取中空閒的block和file 都高於該值時,不進行快取剔除bcull, fcullIf the amount of available space or the number of available files in the cache falls below either of these limits, then culling is started.當快取中可以使用的block或者files 有一個低於該值時,進行快取剔除bstop, fstopIf the amount of available space or the number of available files in the cache falls below either of these limits, then no further allocation of disk space or files is permitted until culling has raised things above these limits again.當快取中可以使用的block或者files有一個低於該值時,除非快取剔除機制進行了快取剔除,否則不會再分配磁碟空間,

通常配置是這樣

0 <= bstop < bcull < brun < 1000 <= fstop < fcull < frun < 100

需要注意,這些值是表示的可以使用的空間和檔案,並不是100 減去使用df 檢視的資訊,使用者空間的守護程序掃描cache,來建立一個需要提出的物件表,基於最少使用原則進行剔除。“ A new scan of the cache is started as soon as space is made in the table”,如果物件的atimes(最後訪問時間)發生了變化,不會進行剔除,或者核心模組通知說該檔案仍然在使用,也不會刪除該cache

快取結構

會存在兩個目錄

cache/graveyard/

活動的cache 物件會存放在 cache/ 目錄。CacheFile的核心模組會將不再使用或者剔除的物件移動到graveyard 目錄,守護程序會在graveyard進行刪除,守護程序使用dnotify來監控graveyard目錄,然後會將graveyard存在的物件刪除。

CacheFiles 模組將索引物件使用目錄的方式進行表示,目錄名稱可能是”I….”或者”J….”沒有子物件的資料物件會以檔案的方式進行表示,有子物件的資料物件會以目錄的形式進行表示,檔名稱可能是”D…”或者”E….”。如果表示目錄,那麼會有一個叫”data”的檔案在該目錄,使用者真實的儲存資料特殊的物件,同資料物件類似,不過檔名是以”S….”或者”T…”的形式

如果一個物件有子物件,那麼他會以目錄的形式表示,在該目錄下會有一系列子目錄,子目錄的名稱以@+子物件的雜湊值命名,如下所示

 /INDEX    /INDEX     /INDEX                            /DATA FILES/=========/==========/=================================/================cache/@4a/I03nfs/@30/Ji000000000000000--fHg8hi8400cache/@4a/I03nfs/@30/Ji000000000000000--fHg8hi8400/@75/Es0g000w...DB1rycache/@4a/I03nfs/@30/Ji000000000000000--fHg8hi8400/@75/Es0g000w...N22rycache/@4a/I03nfs/@30/Ji000000000000000--fHg8hi8400/@75/Es0g000w...FP1ry

如果檔名稱太長,超過了NAME_MAX的大小,那麼會被分成多份,第一份被用於建立巢狀目錄,最後一份會位於最後一個目錄,每個中間目錄的名稱會以”+”作為字首,例如:

J1223/@23/+xy...z/+kl...m/Epqr
Note that keys are raw data, and not only may they exceed NAME_MAX in size, they may also contain things like ‘/’ and NUL characters, and so they may not be suitable for turning directly into a filename.To handle this, CacheFiles will use a suitably printable filename directly and “base-64” encode ones that aren’t directly suitable. The two versions of object filenames indicate the encoding:OBJECT TYPE	PRINTABLE	ENCODEDIndex	“I…”	“J…”Data	“D…”	“E…”Special	“S…”	“T…”Intermediate directories are always “@” or “+” as appropriate.Each object in the cache has an extended attribute label that holds the object type ID (required to distinguish special objects) and the auxiliary data from the netfs. The latter is used to detect stale objects in the cache and update or retire them.Note that CacheFiles will erase from the cache any file it doesn’t recognise or any file of an incorrect type (such as a FIFO file or a device file).

11
最新評論
  • BSA-TRITC(10mg/ml) TRITC-BSA 牛血清白蛋白改性標記羅丹明
  • C#配置檔案載入異常