ORACLE數(shù)據(jù)庫異步IO介紹
異步IO概念
Linux 異步 I/O (AIO)是 Linux 內(nèi)核中提供的一個增強的功能。它是Linux 2.6 版本內(nèi)核的一個標準特性,當然我們在2.4 版本內(nèi)核的補丁中也可以找到它。AIO 背后的基本思想是允許進程發(fā)起很多 I/O 操作,而不用阻塞或等待任何操作完成。稍后或在接收到 I/O 操作完成的通知時,進程就可以檢索 I/O 操作的結(jié)果。
Linux IO模型(I/O models)分同步IO模型(synchronous models)和異步IO模型(asynchronous models)。 在同步IO中,線程啟動一個IO操作然后就立即進入等待狀態(tài),直到IO操作完成后才醒來繼續(xù)執(zhí)行。而異步IO方式中,線程發(fā)送一個IO請求到內(nèi)核,然后繼 續(xù)處理其他的事情,內(nèi)核完成IO請求后,將會通知線程IO操作完成了
如果IO請求需要大量時間執(zhí)行的話,異步文件IO方式可以顯著提高效率,因為在線程等待 的這段時間內(nèi),CPU將會調(diào)度其他線程進行執(zhí)行,如果沒有其他線程需要執(zhí)行的話,這段時間將會浪費掉(可能會調(diào)度操作系統(tǒng)的零頁線程)。如果IO請求操作 很快,用異步IO方式反而還低效,還不如用同步IO方式。
其它關(guān)于異步IO與同步IO的細節(jié),可以參考Boost application performance using asynchronous I/O這篇文章,網(wǎng)上很多"Linux異步IO"的文章其實是翻譯自這篇文章。如果了解更多關(guān)于異步IO的細節(jié),可以細讀這篇文章。
異步IO好處
異步I/O的優(yōu)點:異步I/O是和同步I/O相比較來說的,如果是同步I/O,當一個I /O操作執(zhí)行時,應(yīng)用程序必須等待,直到此I/O執(zhí)行完。相反,異步I/O操作在后臺運行,I/O操作和應(yīng)用程序可以同時運行,提高了系統(tǒng)性能;使用異步 I/O會提高I/O流量,如果應(yīng)用是對裸設(shè)備進行操作,這種優(yōu)勢更加明顯, 因此像數(shù)據(jù)庫,文件服務(wù)器等應(yīng)用往往會利用異步I/O,使得多個I/O操作同時執(zhí)行. 而且從官方文檔來看,ORACLE也是推薦ORACLE數(shù)據(jù)庫啟用異步IO的這個功能的。
With synchronous I/O, when an I/O request is submitted to the operating system, the writing process blocks until the write is confirmed as complete. It can then continue processing. With asynchronous I/O, processing continues while the I/O request is submitted and processed. Use asynchronous I/O when possible to avoid bottlenecks.
Some platforms support asynchronous I/O by default, others need special configuration, and some only support asynchronous I/O for certain underlying file system types.
Q: 2. What are the benefits of Asynchronous I/O?
A: The implementation of Asynchronous I/O on Red Hat Advanced Server allows Oracle processes to issue multiple I/O requests to disk with a single system call, rather than a large number of single I/O requests. This improves performance in two ways:
First, because a process can queue multiple requests for the kernel to handle, so the kernel can optimize disk activity by recording requests or combining individual requests that are adjacent on disk into fewer and larger requests.
Secondary, because the system does not put the process in sleep state while the hardware processes the request. So, the process is able to perform other tasks until the I/O complete.
This involves making use of I/O capabilities such as:
Asynchronous I/O: Asynchronous I/O does not reduce traffic but allows processes to do other things while waiting for IO to complete.
Direct I/O (bypassing the Operating System's File Caches) : Direct IO does not reduce traffic but may use a shorter code path / fewer CPU cycles to perform the IO.
啟用異步IO
ORACLE數(shù)據(jù)庫是從ORACLE 9i Release 2開始支持異步IO特性的。之前的版本是不支持異步IO特征的。另外在ORACLE 9i R2和 ORACLE 10g R1中默認是禁用異步特性的,直到ORACLE 10g R2才默認啟用異步IO特性。
Q: 4. Can I use Asynchronous I/O with Oracle 8i or Oracle 9i release 1?
A: No. Asynchronous I/O feature is only available with Oracle RDBMS 9i release 2 (Oracle9iR2).
Q: 5. Is Asynchronous I/O active with Oracle RDBMS by default?
A: No. By default, Oracle9iR2 and Oracle10gR1 are shipped with asynchronous I/O support disabled.In 10gR2 asyncIO is enabled by default.
那么如何啟用ORACLE數(shù)據(jù)庫的異步IO特性呢? 我們可以按照下面步驟操作:
1:首先要確認ORACLE數(shù)據(jù)庫所在的系統(tǒng)平臺(操作系統(tǒng))是否支持異步IO
目前流行的Linux/Unix平臺基本上都支持異步IO,但是一些老舊的版本就不一定了??梢运阉饕幌孪嚓P(guān)文檔了解清楚。
2: 檢查是否安裝libaio、libaio-devel相關(guān)包(似乎libaio-devel包不是必須的,測試環(huán)境沒有l(wèi)ibaio-devel似乎也OK,當然最好也一起安裝)
- [root@DB-Server ~]# rpm -qa | grep aio
- libaio-0.3.106-5
- libaio-0.3.106-5
- [root@DB-Server Server]# rpm -ivh libaio-devel-0.3.106-5.i386.rpm
- warning: libaio-devel-0.3.106-5.i386.rpm: Header V3 DSA signature: NOKEY, key ID 1e5e0159
- Preparing... ########################################### [100%]
- 1:libaio-devel ########################################### [100%]
- [root@DB-Server Server]# rpm -ivh libaio-devel-0.3.106-5.x86_64.rpm
- warning: libaio-devel-0.3.106-5.x86_64.rpm: Header V3 DSA signature: NOKEY, key ID 1e5e0159
- Preparing... ########################################### [100%]
- 1:libaio-devel ########################################### [100%]
- [root@DB-Server Server]# rpm -qa | grep libaio
- libaio-0.3.106-5
- libaio-devel-0.3.106-5
- libaio-devel-0.3.106-5
- libaio-0.3.106-5
3:檢查系統(tǒng)是否支持異步I/O
根據(jù)文檔[Note 370579.1] ,可以通過查看slabinfo統(tǒng)計信息查看操作系統(tǒng)中AIO是否運行,slab是Linux的內(nèi)存分配器,AIO相關(guān)的內(nèi)存結(jié)構(gòu)已經(jīng)分配,kiocb值的第二列和第三列非0即是已使用
[root@DB-Server ~]# cat /proc/slabinfo | grep kio
kioctx 62 110 384 10 1 : tunables 54 27 8 : slabdata 11 11 0
kiocb 0 0 256 15 1 : tunables 120 60 8 : slabdata 0 0 0
kiocb值的第二列和第三列非0表示系統(tǒng)已經(jīng)啟用異步IO。如上所示,表示異步I/O沒有在使用。
The kioctx and kiocb are Async I/O data structures that are defined in aio.h. If it shows a non zero value that means async io is enabled. source code loaded /usr/src/linux-/include/linux/aio.h
4:修改、優(yōu)化系統(tǒng)內(nèi)核參數(shù)
Linux從2.6 kernel開始,已經(jīng)取消了對IO size的限制,Oracle建議將aio-max-nr的值設(shè)置為1048576或更高。
[root@DB-Server ~]# cat /proc/sys/fs/aio-max-nr
65536
命令echo 1048576 > /proc/sys/fs/aio-max-nr修改參數(shù),只對當前環(huán)境有效,如果系統(tǒng)重啟過后,則會使用默認值,所以最好修改參數(shù)文件/etc /sysctl.conf。編輯/etc/sysctl.conf 添加或修改參數(shù)fs.aio-max-nr = 1048576,保存后。運行sysctl -p使之生效。
[root@DB-Serveruat ~]# cat /proc/sys/fs/aio-max-nr
1048576
注意aio-max-size參數(shù)從RHEL4開始已經(jīng)不存在了,詳情見文檔Kernel Parameter "aio-max-size" does not exist in RHEL4 / EL4 / RHEL5 /EL5 (文檔 ID 549075.1)。
5:檢查ORACLE軟件是否支持開啟AIO。
如下所示有輸出值,表示ORACLE軟件支持開啟AIO,其實從ORACLE 9i R2開始,ORACLE就已經(jīng)支持開啟異步IO(AIO)了。不過10GR1以前版本需要手動開啟AIO,相對而言要麻煩一些。
- [oracle@DB-Server ~]$ /usr/bin/ldd $ORACLE_HOME/bin/oracle | grep libaio
- libaio.so.1 => /usr/lib64/libaio.so.1 (0x00007f5a247f4000)
- [oracle@DB-Server ~]$ /usr/bin/nm $ORACLE_HOME/bin/oracle | grep io_getevent
- w io_getevents@@LIBAIO_0.4
6:數(shù)據(jù)庫級別啟用異步I/O
將參數(shù)disk_asynch_io設(shè)置為true,其實ORACLE 10g R2中參數(shù)disk_asynch_io默認是為true的。
- SQL> alter system set filesystemio_options = setall scope=spfile;
- System altered.
- SQL> alter system set disk_asynch_io = true scope=spfile;
- System altered.
關(guān)于參數(shù)filesystemio_options有四個值: asynch、directio, setall,none. 一般建議設(shè)置為setall比較合適。
You can use the FILESYSTEMIO_OPTIONS initialization parameter to enable or disable asynchronous I/O or direct I/O on file system files. This parameter is platform-specific and has a default value that is best for a particular platform. It can be dynamically changed to update the default setting.
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.
在文件系統(tǒng)文件上啟用異步I/O,在數(shù)據(jù)傳送上沒有計時要求。
· DIRECTIO: enable direct I/O on file system files, which bypasses the buffer cache.
在文件系統(tǒng)文件上啟用直接I/O,繞過buffer cache。
· SETALL: enable both asynchronous and direct I/O on file system files.
在文件系統(tǒng)文件上啟用異步和直接I/O。
· NONE: disable both asynchronous and direct I/O on file system files.
在文件系統(tǒng)文件上禁用異步和直接I/O。
設(shè)置完成后重啟數(shù)據(jù)庫,驗證異步IO特性是否啟用。如下所示, kiocb的第二、三列都不為0,表示ORACLE的異步IO特性已經(jīng)啟用。
[oracle@DB-Server ~]$ cat /proc/slabinfo | grep kio
kioctx 60 80 384 10 1 : tunables 54 27 8 : slabdata 8 8 0
kiocb 6 30 256 15 1 : tunables 120 60 8 : slabdata 2 2 0
[oracle@DB-Server ~]$
參考資料:
http://www.ibm.com/developerworks/linux/library/l-async/index.html
https://support.oracle.com/epmos/faces/DocumentDisplay?_afrLoop=73665184034240&parent=DOCUMENT&sourceId=223117.1&id=432854.1&_afrWindowMode=0&_adf.ctrl-state=11m9r3dm4l_242
https://support.oracle.com/epmos/faces/DocumentDisplay?_afrLoop=73689176803264&parent=DOCUMENT&sourceId=471846.1&id=225751.1&_afrWindowMode=0&_adf.ctrl-state=11m9r3dm4l_291
http://blog.sina.com.cn/s/blog_465a4a1e0100oizv.html
http://semiter.blog.51cto.com/1234477/1243325
本文轉(zhuǎn)自:http://www.cnblogs.com/kerrycode/
作者:瀟湘隱者
本文版權(quán)歸作者所有,歡迎轉(zhuǎn)載,但未經(jīng)作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接.