www.久久久久|狼友网站av天堂|精品国产无码a片|一级av色欲av|91在线播放视频|亚洲无码主播在线|国产精品草久在线|明星AV网站在线|污污内射久久一区|婷婷综合视频网站

當(dāng)前位置:首頁 > 芯聞號(hào) > 充電吧
[導(dǎo)讀]釋放Linux操作系統(tǒng)文件緩存?? 轉(zhuǎn)自:http://pthread.blog.163.com/blog/static/1693081782011111402639863/ 自從工作了,再

釋放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
本站聲明: 本文章由作者或相關(guān)機(jī)構(gòu)授權(quán)發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點(diǎn),本站亦不保證或承諾內(nèi)容真實(shí)性等。需要轉(zhuǎn)載請(qǐng)聯(lián)系該專欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請(qǐng)及時(shí)聯(lián)系本站刪除。
換一批
延伸閱讀

9月2日消息,不造車的華為或?qū)⒋呱龈蟮莫?dú)角獸公司,隨著阿維塔和賽力斯的入局,華為引望愈發(fā)顯得引人矚目。

關(guān)鍵字: 阿維塔 塞力斯 華為

倫敦2024年8月29日 /美通社/ -- 英國汽車技術(shù)公司SODA.Auto推出其旗艦產(chǎn)品SODA V,這是全球首款涵蓋汽車工程師從創(chuàng)意到認(rèn)證的所有需求的工具,可用于創(chuàng)建軟件定義汽車。 SODA V工具的開發(fā)耗時(shí)1.5...

關(guān)鍵字: 汽車 人工智能 智能驅(qū)動(dòng) BSP

北京2024年8月28日 /美通社/ -- 越來越多用戶希望企業(yè)業(yè)務(wù)能7×24不間斷運(yùn)行,同時(shí)企業(yè)卻面臨越來越多業(yè)務(wù)中斷的風(fēng)險(xiǎn),如企業(yè)系統(tǒng)復(fù)雜性的增加,頻繁的功能更新和發(fā)布等。如何確保業(yè)務(wù)連續(xù)性,提升韌性,成...

關(guān)鍵字: 亞馬遜 解密 控制平面 BSP

8月30日消息,據(jù)媒體報(bào)道,騰訊和網(wǎng)易近期正在縮減他們對(duì)日本游戲市場(chǎng)的投資。

關(guān)鍵字: 騰訊 編碼器 CPU

8月28日消息,今天上午,2024中國國際大數(shù)據(jù)產(chǎn)業(yè)博覽會(huì)開幕式在貴陽舉行,華為董事、質(zhì)量流程IT總裁陶景文發(fā)表了演講。

關(guān)鍵字: 華為 12nm EDA 半導(dǎo)體

8月28日消息,在2024中國國際大數(shù)據(jù)產(chǎn)業(yè)博覽會(huì)上,華為常務(wù)董事、華為云CEO張平安發(fā)表演講稱,數(shù)字世界的話語權(quán)最終是由生態(tài)的繁榮決定的。

關(guān)鍵字: 華為 12nm 手機(jī) 衛(wèi)星通信

要點(diǎn): 有效應(yīng)對(duì)環(huán)境變化,經(jīng)營業(yè)績(jī)穩(wěn)中有升 落實(shí)提質(zhì)增效舉措,毛利潤率延續(xù)升勢(shì) 戰(zhàn)略布局成效顯著,戰(zhàn)新業(yè)務(wù)引領(lǐng)增長 以科技創(chuàng)新為引領(lǐng),提升企業(yè)核心競(jìng)爭(zhēng)力 堅(jiān)持高質(zhì)量發(fā)展策略,塑強(qiáng)核心競(jìng)爭(zhēng)優(yōu)勢(shì)...

關(guān)鍵字: 通信 BSP 電信運(yùn)營商 數(shù)字經(jīng)濟(jì)

北京2024年8月27日 /美通社/ -- 8月21日,由中央廣播電視總臺(tái)與中國電影電視技術(shù)學(xué)會(huì)聯(lián)合牽頭組建的NVI技術(shù)創(chuàng)新聯(lián)盟在BIRTV2024超高清全產(chǎn)業(yè)鏈發(fā)展研討會(huì)上宣布正式成立。 活動(dòng)現(xiàn)場(chǎng) NVI技術(shù)創(chuàng)新聯(lián)...

關(guān)鍵字: VI 傳輸協(xié)議 音頻 BSP

北京2024年8月27日 /美通社/ -- 在8月23日舉辦的2024年長三角生態(tài)綠色一體化發(fā)展示范區(qū)聯(lián)合招商會(huì)上,軟通動(dòng)力信息技術(shù)(集團(tuán))股份有限公司(以下簡(jiǎn)稱"軟通動(dòng)力")與長三角投資(上海)有限...

關(guān)鍵字: BSP 信息技術(shù)
關(guān)閉
關(guān)閉