1. 程式人生 > >系統技術非業餘研究 » Linux下新系統呼叫sync_file_range

系統技術非業餘研究 » Linux下新系統呼叫sync_file_range

我們在做資料庫程式或者IO密集型的程式的時候,通常在更新的時候,比如說資料庫程式,希望更新有一定的安全性,我們會在更新操作結束的時候呼叫fsync或者fdatasync來flush資料到持久裝置去。而且通常是以頁面為單位,16K一次或者4K一次。 安全性保證了,但是效能就有很大的損害。而且我們更新的時候,通常是更新檔案的某一個頁面,那麼由於是更新覆蓋操作,對檔案系統的元資料來講的話,無需變更,所以我們通常不大關心元資料是否寫入。 當更新非常頻繁的時候,我們時候能夠有其他方法減少效能損失。sync_file_range同學出場了。

Linux下系統呼叫sync_file_range只在核心2.6.17及更高版本是可用的, 我們常用的RHEL 5U4是支援的。
這篇文章有他的介紹:

http://lwn.net/Articles/178199/
也可以man sync_file_range下他的具體作用, 但是請注意,sync_file_range是不可移植的。

sync_file_range – sync a file segment with disk

sync_file_range() permits fine control when synchronising the open file referred to by the file descriptor fd with disk.

offset is the starting byte of the file range to be synchronised. nbytes specifies the length of the range to be synchronised, in bytes; if nbytes is zero, then all bytes from offset through to the end of file are synchronised. Synchronisation is in units of the system page size: offset is rounded down to a page boundary; (offset+nbytes-1) is rounded up to a page boundary.

sync_file_range可以讓我們在做多個更新後,一次性的刷資料,這樣大大提高IO的效能。 具體的實現在fs/sync.c裡面,有興趣的同學可以圍觀下。

著名的fio測試工具支援sync_file_range來做sync操作,我們在決定在我們的應用中使用該syscall之前不妨先fio測試一把。

祝大家玩的開心。

Post Footer automatically generated by wp-posturl plugin for wordpress.

No related posts.