[20171117]nocache的編譯.txt

lfree發表於2017-11-17

[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/,如需轉載,請註明出處,否則將追究法律責任。

相關文章