釋放Linux操作系統(tǒng)文件緩存??
轉(zhuǎn)自:http://pthread.blog.163.com/blog/static/1693081782011111402639863/
自從工作了,再也沒有更新過這個(gè)技術(shù)博客。一來工作了沒什么好寫的,二來確實(shí)也挺忙。最近稍微有點(diǎn)空閑,先開一個(gè)寫一點(diǎn)吧。
記得在公司做新人習(xí)題的時(shí)候,題目是通過網(wǎng)絡(luò)和本地分別讀取一個(gè)約12G的大文件,從中讀取每一行,對(duì)每行特定的幾個(gè)字段,調(diào)用分詞庫分詞并統(tǒng)計(jì)詞頻。當(dāng)時(shí)遇到一個(gè)很郁悶的事情就是,12G的文件讀取一次了之后,系統(tǒng)中有緩存;然后第二次再次運(yùn)行的時(shí)候,因?yàn)橛芯彺娴挠绊?,性能差異挺?本地讀取幾乎三倍性能差距)。但是當(dāng)時(shí)的開發(fā)機(jī)器上,自己只有普通用戶權(quán)限,無法通過修改/proc/sys/vm/drop_cache來達(dá)到目的。所以最后還是沒有搞定這個(gè)問題。
后來發(fā)現(xiàn)Linux的一個(gè)系統(tǒng)調(diào)用:
?#include
?int posix_fadvise(int fd, off_t offset, off_t len, int advice);
有一個(gè)選項(xiàng)POSIX_FADV_DONTNEED可以做這件事情。網(wǎng)上找了下好像挺多人也遇到這個(gè)無問題的,所以就把我的解決辦法放到這里。于是寫了一個(gè)小工具,一次批量清除文件在系統(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選項(xiàng)用于將數(shù)據(jù)寫回硬盤。因?yàn)閙an 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選項(xiàng)的話,那么臟頁面不會(huì)被釋放,緩存也就不會(huì)被釋放掉啦。所以使用dcache的時(shí)候應(yīng)當(dāng)清楚何時(shí)使用--sync選項(xiàng)。
在公司機(jī)器上做了一下實(shí)驗(yàn),用一個(gè)4kw+行的文本文件,8GB做實(shí)驗(yàn)。
先 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工具釋放對(duì)應(yīng)文件在系統(tǒng)中的緩存:
dcache 5kw.txt
drop cache of plsi_index.5kw.txt success.
再次使用free -m看到cached果然被釋放了8GB,說明工具確實(shí)起到了作用。
?free -m
? ? ? ? ? ? ?total ? ? ? used ? ? ? free ? ? shared ? ?buffers ? ?
cached
Mem: ? ?64334 ? ?50477 ? ? ?13856 ? ? ? ? ?0 ? ? ? ?204 ? ? ?48346