[20171117]nocache的編譯.txt
[20171117]nocache的編譯.txt
--//昨天別人問nocache的編譯問題,原始的版本編譯後cachestats無法顯示檔名,實際上修改原始碼很簡單
--//做一個記錄.
1.首先簡單介紹nocache:
nocache - minimize filesystem caching effects
---------------------------------------------
The `nocache` tool tries to minimize the effect an application has on
the Linux file system cache. This is done by intercepting the `open`
and `close` system calls and calling `posix_fadvise` with the
`POSIX_FADV_DONTNEED` parameter. Because the library remembers which
pages (ie., 4K-blocks of the file) were already in file system cache
when the file was opened, these will not be marked as "don't need",
because other applications might need that, although they are not
actively used (think: hot standby).
Use case: backup processes that should not interfere with the present
state of the cache.
2.下載連結:
--//百度實際是太.......
3.解壓:
$ unzip nocache-master.zip -d /tmp/
--//這樣解壓到/tmp/nocache-master目錄.
4.修改原始碼:
--//cachestats.c
59 if(st.st_size == 0) {
60 printf("%-40s ",argv[1]);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
61 printf("pages in cache: %d/%d (%.1f%%) [filesize=%.1fK, "
62 "pagesize=%dK]\n", 0, 0, 0.0,
63 0.0, PAGESIZE / 1024);
64 return EXIT_SUCCESS;
65 }
66
67 pages = (st.st_size + PAGESIZE - 1) / PAGESIZE;
68 pageinfo = calloc(sizeof(*pageinfo), pages);
69 if(!pageinfo)
70 exiterr("calloc");
71
72 file = mmap(NULL, st.st_size, PROT_NONE, MAP_SHARED, fd, 0);
73 if(file == MAP_FAILED)
74 exiterr("mmap");
75 if(mincore(file, st.st_size, pageinfo) == -1)
76 exiterr("mincore");
77
78 i = j = 0;
79 while(i < pages)
80 if(pageinfo[i++] & 1)
81 j++;
82
83 if(quiet) {
84 if(j == i)
85 return EXIT_SUCCESS;
86 return EXIT_FAILURE;
87 }
88
89 printf("%-40s ",argv[1]);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
90 printf("pages in cache: %d/%d (%.1f%%) [filesize=%.1fK, "
91 "pagesize=%dK]\n", j, i, 100.0 * j / i,
92 1.0 * st.st_size / 1024, PAGESIZE / 1024);
--//注:下劃線那行是我增加的.(注:前面是行號)
5.編譯與安裝:
# make
cc -Wall -o cachedel cachedel.c
cc -Wall -o cachestats cachestats.c
cc -Wall -fPIC -c -o nocache.o nocache.c
cc -Wall -fPIC -c -o fcntl_helpers.o fcntl_helpers.c
cc -Wall -fPIC -c -o pageinfo.o pageinfo.c
cc -Wall -pthread -shared -Wl,-soname,nocache.so -o nocache.so nocache.o fcntl_helpers.o pageinfo.o -ldl
sed 's!##libdir##!$(dirname "$0")!' <nocache.in >nocache
chmod a+x nocache
# make install
sed 's!##libdir##!/usr/local/lib!' <nocache.in >nocache.global
install -pm 0644 nocache.so /usr/local/lib
install -pm 0755 nocache.global /usr/local/bin/nocache
install -pm 0755 cachedel cachestats /usr/local/bin
install -pm 0644 man/cachedel.1 man/cachestats.1 man/nocache.1 /usr/local/share/man/man1
6.使用就很簡單了:
--//唯一的缺點就是不能使用副檔名,只能一個一個查詢,不過很好解決,你透過xargs+find結合在一起就ok了.
--//例子:
# find . -name "*.c" -print0 | xargs -0 -I{} cachestats {}
./pageinfo.c pages in cache: 1/1 (100.0%) [filesize=3.6K, pagesize=4K]
./cachestats.c pages in cache: 1/1 (100.0%) [filesize=2.7K, pagesize=4K]
./nocache.c pages in cache: 4/4 (100.0%) [filesize=13.2K, pagesize=4K]
./cachedel.c pages in cache: 1/1 (100.0%) [filesize=1.1K, pagesize=4K]
./fcntl_helpers.c pages in cache: 1/1 (100.0%) [filesize=0.9K, pagesize=4K]
# find /mnt/ramdisk/book -name "*.dbf" -print0 | xargs -0 -I{} cachestats {}
/mnt/ramdisk/book/sysaux01.dbf pages in cache: 240642/240642 (100.0%) [filesize=962568.0K, pagesize=4K]
/mnt/ramdisk/book/tea01.dbf pages in cache: 10242/10242 (100.0%) [filesize=40968.0K, pagesize=4K]
/mnt/ramdisk/book/users01.dbf pages in cache: 65538/65538 (100.0%) [filesize=262152.0K, pagesize=4K]
/mnt/ramdisk/book/undotbs01.dbf pages in cache: 275202/275202 (100.0%) [filesize=1100808.0K, pagesize=4K]
/mnt/ramdisk/book/temp01.dbf pages in cache: 104851/105986 (98.9%) [filesize=423944.0K, pagesize=4K]
/mnt/ramdisk/book/system01.dbf pages in cache: 194562/194562 (100.0%) [filesize=778248.0K, pagesize=4K]
/mnt/ramdisk/book/example01.dbf pages in cache: 88642/88642 (100.0%) [filesize=354568.0K, pagesize=4K]
7.簡單測試:
--//如果瞭解linux作業系統就知道,linux會充分使用記憶體,如果你看見free列很少,注意cached列的顯示.而不要以為記憶體耗盡.
# free -m
total used free shared buffers cached
Mem: 129161 103417 25743 0 1077 98465
-/+ buffers/cache: 3875 125285
Swap: 30718 2 30715
# dd if=/dev/zero of=1024m.dd bs=1M count=1024
1024+0 records in
1024+0 records out
1073741824 bytes (1.1 GB) copied, 1.8016 seconds, 596 MB/s
# free -m
total used free shared buffers cached
Mem: 129161 104501 24660 0 1078 99489
-/+ buffers/cache: 3933 125227
Swap: 30718 2 30715
--//可以free列減少1024M,cached列增加1024M.
# cachestats 1024m.dd
1024m.dd pages in cache: 262144/262144 (100.0%) [filesize=1048576.0K, pagesize=4K]
--//全部cache,可以看出linux 檔案系統設計儘量使用剩餘記憶體.
# cachedel 1024m.dd
# cachestats 1024m.dd
1024m.dd pages in cache: 0/262144 (0.0%) [filesize=1048576.0K, pagesize=4K]
# free -m
total used free shared buffers cached
Mem: 129161 103418 25743 0 1078 98465
-/+ buffers/cache: 3874 125286
Swap: 30718 2 30715
--//可以free列又回到原來的數量.
--//如果dd採用direct IO看看.
# dd if=/dev/zero of=1024m.dd bs=1M count=1024 oflag=direct
1024+0 records in
1024+0 records out
1073741824 bytes (1.1 GB) copied, 1.18574 seconds, 906 MB/s
# free -m
total used free shared buffers cached
Mem: 129161 103417 25743 0 1079 98465
-/+ buffers/cache: 3873 125287
Swap: 30718 2 30715
--//可以發現free列沒有變化.
--//如果你複製大檔案使用快取實際上是浪費,因為僅僅1次.使用nocache命令的顯示出來了.
# nocache cp 1024m.dd 1024m.ddx
# free -m
total used free shared buffers cached
Mem: 129161 103422 25738 0 1080 98465
-/+ buffers/cache: 3877 125283
Swap: 30718 2 30715
--//可以發現free列沒有變化(這一點點可以忽略).
--//如果執行如下,cache會消耗2048M.
# cp 1024m.dd 1024m.ddx
# free -m
total used free shared buffers cached
Mem: 129161 105527 23633 0 1081 100513
-/+ buffers/cache: 3932 125228
Swap: 30718 2 30715
--//注意看free列變化,25738-23633=2105M.
# ls -1 1024m.dd* | xargs -I{} cachestats {}
1024m.dd pages in cache: 262144/262144 (100.0%) [filesize=1048576.0K, pagesize=4K]
1024m.ddx pages in cache: 262144/262144 (100.0%) [filesize=1048576.0K, pagesize=4K]
--//全部快取.
# ls -1 1024m.dd* | xargs -I{} cachedel {}
# free -m
total used free shared buffers cached
Mem: 129161 103425 25735 0 1082 98465
-/+ buffers/cache: 3878 125282
Swap: 30718 2 30715
--//總之有了這個小工具瞭解檔案在linux作業系統快取的情況幫助很大.
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/267265/viewspace-2147398/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- [20170221]nocache工具的小測試.txt
- [20170224]nocache工具的小測試2.txt
- Nginx 的編譯並打包成二.txtNginx編譯
- Oracle Sequence NocacheOracle
- apache22_編譯支援mpmr.txtApache編譯
- CMakeLists.txt --- 匯入介面庫(預編譯庫)編譯
- oracle hint_cache_nocacheOracle
- 編譯器的編譯基本過程編譯
- [20191125]編譯vmtouch.txt編譯
- SCSS 的編譯CSS編譯
- 編譯器的自展和自舉、交叉編譯編譯
- cmake編譯指定自己編譯的庫路徑編譯
- Java編譯與反編譯Java編譯
- [20121004]11G下編譯bbed.txt編譯
- Mac平臺反編譯Unity編譯的安卓apkMac編譯Unity安卓APK
- 編譯編譯
- python的編譯Python編譯
- OCI程式的編譯編譯
- Java程式碼的編譯與反編譯那些事兒Java編譯
- hadoop編譯—+2.x編譯Hadoop編譯
- gcc 編譯器與 clang 編譯器GC編譯
- 一張圖解析 編譯器編譯流程圖解編譯
- ubuntu下編譯交叉編譯工具鏈Ubuntu編譯
- [轉]andriod的apk檔案相關的編譯反編譯工具APK編譯
- [譯]iOS編譯器iOS編譯
- 程式碼線上編譯器(上)- 編輯及編譯編譯
- [譯] 優化 Swift 的編譯時間優化Swift編譯
- FreeBSD中的GNU C編譯器--編譯器GCC(轉)編譯GC
- 檢視已經編譯過的NGINX當時的編譯引數編譯Nginx
- 開源編譯工具和編譯軟體編譯
- Make編譯之編譯32bit ffmpeg編譯
- N1064編譯鏈編譯編譯
- 翻譯的未來:翻譯機器和譯後編譯編譯
- webpack的編譯&構建Web編譯
- 對預編譯的理解編譯
- 關於庫的編譯編譯
- PHP的編譯安裝PHP編譯
- 編譯PHP的錯誤編譯PHP