[20241107]nocache的編譯.txt

lfree發表於2024-11-08
[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的主要功能如下:

--//檢視一個檔案(或者目錄)哪些部分在記憶體中;
--//把檔案調入記憶體;
--//把檔案清除出記憶體;
--//把檔案鎖住在記憶體中而不被換出到磁碟上;

相關文章