且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

[20171117]nocache的编译.txt

更新时间:2022-08-15 08:28:37

[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.下载链接:
--//百度实际是太.......
https://github.com/Feh/nocache
https://codeload.github.com/Feh/nocache/zip/master

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操作系统缓存的情况帮助很大.