釋放Linux操作系統(tǒng)文件緩存??
轉(zhuǎn)自:http://pthread.blog.163.com/blog/static/1693081782011111402639863/
自從工作了,再也沒有更新過這個技術(shù)博客。一來工作了沒什么好寫的,二來確實也挺忙。最近稍微有點空閑,先開一個寫一點吧。
記得在公司做新人習題的時候,題目是通過網(wǎng)絡和本地分別讀取一個約12G的大文件,從中讀取每一行,對每行特定的幾個字段,調(diào)用分詞庫分詞并統(tǒng)計詞頻。當時遇到一個很郁悶的事情就是,12G的文件讀取一次了之后,系統(tǒng)中有緩存;然后第二次再次運行的時候,因為有緩存的影響,性能差異挺大(本地讀取幾乎三倍性能差距)。但是當時的開發(fā)機器上,自己只有普通用戶權(quán)限,無法通過修改/proc/sys/vm/drop_cache來達到目的。所以最后還是沒有搞定這個問題。
后來發(fā)現(xiàn)Linux的一個系統(tǒng)調(diào)用:
?#include
?int posix_fadvise(int fd, off_t offset, off_t len, int advice);
有一個選項POSIX_FADV_DONTNEED可以做這件事情。網(wǎng)上找了下好像挺多人也遇到這個無問題的,所以就把我的解決辦法放到這里。于是寫了一個小工具,一次批量清除文件在系統(tǒng)中的緩存。
?
#define _FILE_OFFSET_BITS 64
#define __USE_XOPEN2K
#include
#include
#include
#include
#include
#include
#include
#include
const struct option dcache_options[] = {
{"sync",0,NULL,'s'},
{"help",0,NULL,'h'},
{NULL,0,NULL,0}
};
void usage(char* proc_name,int exit_code)
{
printf("dcache is an utility to drop file cache.n"
"usage:%s [-s] filen"
"t-s,--sync, sync data before drop cache.n"
"t-h,--help, print help.n",proc_name);
exit(exit_code);
}
int dcache(int fd, int sync_data)
{
off_t off,len;
struct stat buf;
int save_errno;
save_errno = errno;
if (sync_data) {
if (fsync(fd) < 0) {
printf("%sn",strerror(errno));
errno = save_errno;
return -1;
}
}
if (fstat(fd,&buf) < 0) {
printf("%sn",strerror(errno));
errno = save_errno;
return -1;
}
off = 0;
len = buf.st_size;
if (posix_fadvise(fd,off,len,POSIX_FADV_DONTNEED) < 0) {
printf("%sn",strerror(errno));
errno = save_errno;
return -1;
}
return 0;
}
int main(int argc, char* argv[])
{
int c,fd;
char* file;
int long_index = 0;
int print_help = 0;
int sync_data = 0;
while ((c = getopt_long(argc,argv,"sh",dcache_options,&long_index)) != -1) {
switch (c) {
case 's':
sync_data = 1;
break;
case 'h':
print_help = 1;
break;
default:
printf("unknown option -%cn",c);
usage(argv[0],EXIT_FAILURE);
break;
}
}
if (print_help) {
usage(argv[0],EXIT_SUCCESS);
}
if (optind >= argc) {
printf("file name requiredn");
exit(EXIT_FAILURE);
}
for (c = optind; c < argc; ++c) {
file = argv[c];
if ((fd = open(file,O_RDWR)) < 0) {
printf("open %s failed.n",file);
} else {
printf("drop cache of %s %s.n",file,dcache(fd,sync_data) == 0?"success":"failed");
close(fd);
}
}
exit(EXIT_SUCCESS);
}
?
使用方法:
dcache -h
dcache is an utility to drop file cache.
usage:dcache [-s] file
? ? ? ? -s,--sync, sync data before drop cache.
? ? ? ? -h,--help, print help.
--sync選項用于將數(shù)據(jù)寫回硬盤。因為man posix_fadvise說了:
?POSIX_FADV_DONTNEED ?attempts ?to ?free ?cached ?pages ?associated with the specified region. ?This is useful, for ?example, whilestreaming large files. ?A program may periodically request the kernel to free cached data that
has already ?been ?used, ?so ?that?more useful cached pages are not discarded instead.
?Pages ?that ?have ?not ?yet ?been ?written ?out ?will be unaffected, so if the application wishes to guarantee that pages will be
?released, it should call fsync(2) or fdatasync(2) first.
說POSIX_FADV_DONTNEED只釋放clean頁面,dirty頁面,并不受此影響,所以如果你是寫了文件而沒有用--sync選項的話,那么臟頁面不會被釋放,緩存也就不會被釋放掉啦。所以使用dcache的時候應當清楚何時使用--sync選項。
在公司機器上做了一下實驗,用一個4kw+行的文本文件,8GB做實驗。
先 free -m看一下cached總數(shù)為48345M
? ? ? ? ? ? ?total ? ? ? used ? ? ? free ? ? shared ? ?buffers ? ? cached
Mem: ? ? 64334 ? ?50469 ? ?13864 ? ? ? ? ?0 ? ? ? ?196 ? ? ?48345
然后wc -l 5kw.txt,讀取一遍文件,wc輸出文件行數(shù)為
44731963 5kw.txt
然后再free -m一遍看內(nèi)存情況:
? ? ? ? ? ? ?total ? ? ? used ? ? ? free ? ? shared ? ?buffers ? ? cached
Mem: ? ? 64334 ? 58802 ? ? 5532 ? ? ? ? ?0 ? ? ? ?204 ? ? ? ?56670
可以看到,cached page增加了8325M,與我們文件大小接近。然后使用dcache工具釋放對應文件在系統(tǒng)中的緩存:
dcache 5kw.txt
drop cache of plsi_index.5kw.txt success.
再次使用free -m看到cached果然被釋放了8GB,說明工具確實起到了作用。
?free -m
? ? ? ? ? ? ?total ? ? ? used ? ? ? free ? ? shared ? ?buffers ? ?
cached
Mem: ? ?64334 ? ?50477 ? ? ?13856 ? ? ? ? ?0 ? ? ? ?204 ? ? ?48346