[20241107]nocache的编译.txt
--//原来的测试环境不存在,需要建立nocache工具了解文件缓存情况,学习OS相关知识。
--//实际上linux对这些工具从应用角度讲不重要,如果有用,linux实用程序里面应该包含类似工具。可惜一直不提供。
--//一般这类安装,我都会写安装笔记,我看了以前的安装笔记,重复操作并记录操作细节。
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/
Archive: nocache-master.zip
ce5c18701b1499195d0de7380037efee27a17722
creating: /tmp/nocache-master/
inflating: /tmp/nocache-master/COPYING
inflating: /tmp/nocache-master/Makefile
inflating: /tmp/nocache-master/README
linking: /tmp/nocache-master/README.md -> README
inflating: /tmp/nocache-master/cachedel.c
inflating: /tmp/nocache-master/cachestats.c
inflating: /tmp/nocache-master/fcntl_helpers.c
inflating: /tmp/nocache-master/fcntl_helpers.h
creating: /tmp/nocache-master/man/
inflating: /tmp/nocache-master/man/cachedel.1
inflating: /tmp/nocache-master/man/cachestats.1
inflating: /tmp/nocache-master/man/nocache.1
inflating: /tmp/nocache-master/nocache.c
inflating: /tmp/nocache-master/nocache.in
inflating: /tmp/nocache-master/pageinfo.c
inflating: /tmp/nocache-master/pageinfo.h
creating: /tmp/nocache-master/t/
inflating: /tmp/nocache-master/t/basic.t
inflating: /tmp/nocache-master/t/ls.t
inflating: /tmp/nocache-master/t/maxfd.t
inflating: /tmp/nocache-master/t/testlib.sh
finishing deferred symbolic links:
/tmp/nocache-master/README.md -> README
--//这样解压到/tmp/nocache-master目录.
4.修改源代码:
--//原始的版本编译后cachestats无法显示文件名,我自己做了修改。也许仅仅一个一个查询的原因。
# cd /tmp/nocache-master/
--//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/nocache.1 man/cachestats.1 man/cachedel.1 /usr/local/share/man/man1
6.简单测试:
SCOTT@book01p> select rowid,dept.* from dept;
ROWID DEPTNO DNAME LOC
------------------ ---------- ------------------------------ -------------
AAASmfAAMAAAACDAAA 10 ACCOUNTING NEW YORK
AAASmfAAMAAAACDAAB 20 RESEARCH DALLAS
AAASmfAAMAAAACDAAC 30 SALES CHICAGO
AAASmfAAMAAAACDAAD 40 OPERATIONS BOSTON
SCOTT@book01p> @ rowid AAASmfAAMAAAACDAAA
DATA_OBJECT_ID FILE BLOCK ROW ROWID_DBA DBA TEXT
-------------- ---------- ---------- ---------- -------------------- -------------------- --------------------------------------------------
76191 12 131 0 0x3000083 12,131 alter system dump datafile 12 block 131 ;
--//rowid=AAASmfAAMAAAACDAAA,在dba=12,131。
SCOTT@book01p> select * from dept where rowid='AAASmfAAMAAAACDAAA';
DEPTNO DNAME LOC
---------- ------------------------------ -------------
10 ACCOUNTING NEW YORK
SYS@book> alter system flush BUFFER_CACHE;
System altered.
SYS@book> alter system flush BUFFER_CACHE;
System altered.
SYS@book01p> alter system flush BUFFER_CACHE;
System altered.
SYS@book01p> alter system flush BUFFER_CACHE;
System altered.
$ cachestats -v /u01/oradata/BOOK/book01p/users01.dbf | head -6
/u01/oradata/BOOK/book01p/users01.dbf pages in cache: 112/128962 (0.1%) [filesize=515848.0K, pagesize=4K]
cache map:
0: |x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|
32: |x|x|x|x|x|x|x|x| | | | | | | | | | | | | | | | | | | | | | | | |
64: | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
--//无论我如何刷新都缓存这些数据块,pagesize=4K。相当于缓存了40*4=160K。在pdb下执行也一样。
--//注:11g下不是这样的情况。
$ cachedel /u01/oradata/BOOK/book01p/users01.dbf
$ cachestats -v /u01/oradata/BOOK/book01p/users01.dbf | grep x
$ cachestats -v /u01/oradata/BOOK/book01p/users01.dbf | head -14
/u01/oradata/BOOK/book01p/users01.dbf pages in cache: 0/128962 (0.0%) [filesize=515848.0K, pagesize=4K]
cache map:
0: | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
32: | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
64: | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
96: | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
128: | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
160: | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
192: | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
224: | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
256: | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
288: | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
320: | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
SCOTT@book01p> select * from dept where rowid='AAASmfAAMAAAACDAAA';
DEPTNO DNAME LOC
---------- ------------------------------ -------------
10 ACCOUNTING NEW YORK
$ cachestats -v /u01/oradata/BOOK/book01p/users01.dbf | head -14
/u01/oradata/BOOK/book01p/users01.dbf pages in cache: 2/128962 (0.0%) [filesize=515848.0K, pagesize=4K]
cache map:
0: | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
32: | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
64: | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
96: | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
128: | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
160: | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
192: | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
224: | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
256: | | | | | | |x|x| | | | | | | | | | | | | | | | | | | | | | | | |
288: | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
320: | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
--// 0 1 2 3 4 5 6 7
--//在6,7的位置。256+6 256+7= 262,263.
--//pagesize=4K ,而数据块8K,这样dba=12,131对应4K就是262,263.
--//cachestats命令有一个小缺陷,不支持文件扩展名查询。
$ cachestats /u01/oradata/BOOK/book01p/sys*.dbf
/u01/oradata/BOOK/book01p/sysaux01.dbf pages in cache: 8634/133122 (6.5%) [filesize=532488.0K, pagesize=4K]
--//仅仅查询1条文件,通过xargs+find(ls)结合在一起就ok了.
$ ls -1 /u01/oradata/BOOK/book01p/sys*.dbf | xargs -IQ cachestats Q
/u01/oradata/BOOK/book01p/sysaux01.dbf pages in cache: 8634/133122 (6.5%) [filesize=532488.0K, pagesize=4K]
/u01/oradata/BOOK/book01p/system01.dbf pages in cache: 48376/74242 (65.2%) [filesize=296968.0K, pagesize=4K]
$ ls -1 /u01/oradata/BOOK/book01p/sys*.dbf | xargs -IQ bash -c "cachestats -v Q|head -6"
/u01/oradata/BOOK/book01p/sysaux01.dbf pages in cache: 8634/133122 (6.5%) [filesize=532488.0K, pagesize=4K]
cache map:
0: |x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|
32: |x|x|x|x|x|x|x|x| | | | | | | | | | | | | | | | | | | | | | | | |
64: | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
/u01/oradata/BOOK/book01p/system01.dbf pages in cache: 48376/74242 (65.2%) [filesize=296968.0K, pagesize=4K]
cache map:
0: |x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|
32: |x|x|x|x|x|x|x|x| | | | | | | | | | | | | | | | | | | | | | | | |
64: | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
--//确实启动后前面40块(4K),OS已经缓存,alter system flush BUFFER_CACHE;无法清除。
7.再来谈谈filesystemio_options参数:
SYS@book> @ pvalid filesystemio_options
Display valid values for multioption parameters matching "filesystemio_options"...
PAR# PARAMETER ORD VALUE DEFAULT
------ -------------------- --- ------------------------------ -------
431 filesystemio_options 1 ASYNCH
filesystemio_options 2 DIRECTIO
filesystemio_options 3 SETALL
filesystemio_options 4 NONE
--//FILESYTEMIO_OPTIONS can be set to one of the following values:
--//ASYNCH: enable asynchronous I/O on file system files, which has no timing requirement for transmission.
--// 在文件系统文件上启用异步I/O,在数据传送上没有计时要求。
--//DIRECTIO: enable direct I/O on file system files, which bypasses the buffer cache.
--// 在文件系统文件上启用直接I/O,绕过buffer cache。
--//SETALL: enable both asynchronous and direct I/O on file system files.
--// 在文件系统文件上启用异步和直接I/O。
--//NONE: disable both asynchronous and direct I/O on file system files.
--// 在文件系统文件上禁用异步和直接I/O。
--//测试filesystemio_options=DIRECTIO的情况:
SYS@book> alter system set filesystemio_options=DIRECTIO scope=spfile;
System altered.
--//需要重启数据库才生效。
--//测试:
$ cachedel /u01/oradata/BOOK/book01p/users01.dbf
$ cachestats -v /u01/oradata/BOOK/book01p/users01.dbf | grep x
SCOTT@book01p> select * from dept where rowid='AAASmfAAMAAAACDAAA';
DEPTNO DNAME LOC
---------- ------------------------------ -------------
10 ACCOUNTING NEW YORK
$ cachestats -v /u01/oradata/BOOK/book01p/users01.dbf | grep x
$ cachestats /u01/oradata/BOOK/book01p/users01.dbf
/u01/oradata/BOOK/book01p/users01.dbf pages in cache: 0/128962 (0.0%) [filesize=515848.0K, pagesize=4K]
$ ls -1 /u01/oradata/BOOK/book01p/*.dbf | xargs -IQ cachestats Q
*/
/u01/oradata/BOOK/book01p/sysaux01.dbf pages in cache: 0/133122 (0.0%) [filesize=532488.0K, pagesize=4K]
/u01/oradata/BOOK/book01p/system01.dbf pages in cache: 0/74242 (0.0%) [filesize=296968.0K, pagesize=4K]
/u01/oradata/BOOK/book01p/temp01.dbf pages in cache: 0/78338 (0.0%) [filesize=313352.0K, pagesize=4K]
/u01/oradata/BOOK/book01p/undotbs01.dbf pages in cache: 0/62722 (0.0%) [filesize=250888.0K, pagesize=4K]
/u01/oradata/BOOK/book01p/users01.dbf pages in cache: 0/128962 (0.0%) [filesize=515848.0K, pagesize=4K]
--//实际上没有1个数据文件在OS的缓存。
--//你可以发现pages in cache: 0/128962 (0.0%),也就是操作系统没有缓存对应的文件数据块在操作系统内存中。仅仅在数据库的数
--//据缓存有数据块。
SYS@book01p> @ bh 12 131
INST_ID HLADDR DBARFIL DBABLK CLASS CLASS_TYPE STATE TCH CR_SCN_BAS CR_SCN_WRP CR_UBA_FIL CR_UBA_BLK CR_UBA_SEQ BA LE_ADDR OBJECT_NAME
---------- ---------------- ---------- ---------- ---------- ------------------ ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------------- ---------------- --------------------
1 00000000786E2D88 12 131 1 data block xcur 1 0 0 0 0 0 0000000088120000 00 DEPT
--//我以前遇到一位前辈上线很喜欢这样设置filesystemio_options=setall,与DIRECTIO类似,差别仅仅是否启用异步IO。
--//如果磁盘IO能力强劲,这样设置没有什么问题。因为OS不缓存,这样设置可以适当加大sga的数据缓存的设计。
--//另外的情况如果设置ASYNCH或none,操作系统缓存发挥很大作用,会很大程序掩盖不良sql语句的大量磁盘读写的情况。
--//很多情况下的读写先经过OS的数据缓存,并非直接来之磁盘IO。
--//一旦os缓存放不下,问题马上暴露,多块读以及单块读的时间显著上升。一些项目上线开始单块读 经常是1,2ms。
--//到一定从程度上升到5,6ms甚至更高,就是这个原因。
--//实际上采用asm非常类似filesystemio_options=setall的情况,操作系统不会缓存对应的数据块,如果磁盘IO能力不强,效果比采用
--//filesystem的数据文件还要慢。特别一些上rac的系统,如果购买的存储性能不好,多块读以及单块读的时间很大。
8.另外还有一个vmtouch工具,功能更加强大一些,我很少用。
--//参考 [20191125]编译vmtouch.txt
--//从介绍可以看出vmtouch是一个管理和控制Unix和类Unix文件系统缓存的工具。
--//vmtouch的主要功能如下:
--//查看一个文件(或者目录)哪些部分在内存中;
--//把文件调入内存;
--//把文件清除出内存;
--//把文件锁住在内存中而不被换出到磁盘上;