Oracle安裝 – shmmax和shmall設置

一、概述

在Linux上安裝oracle,需要對內核參數進行調整,其中有shmmax和shmall這兩個參數,那這兩個參數是什麼意思,又該如何設置呢?

二、官方文檔

在oracle的官方文檔( //docs.oracle.com/en/database/oracle/oracle-database/19/ladbi/minimum-parameter-settings-for-installation.html#GUID-CDEB89D1-4D48-41D9-9AC2-6AD9B0E944E3 )中對這兩個參數,設置了最小的標準值。
微信截圖_20220406104623.png
shmall – Greater than or equal to the value of shmmax, in pages.
shmmax – Half the size of physical memory in bytes. See My Oracle Support Note 567506.1 for additional information about configuring shmmax.

再根據redhat的官方文檔( //access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html/performance_tuning_guide/sect-red_hat_enterprise_linux-performance_tuning_guide-configuration_tools-configuring_system_memory_capacity ),去查這兩個參數所表達的含義。
微信截圖_20220406113052.png
shmall – Defines the total amount of shared memory pages that can be used on the system at one time. A page is 4096 bytes on the AMD64 and Intel 64 architecture, for example.
shmmax – Defines the maximum size (in bytes) of a single shared memory segment allowed by the kernel.

以上兩段英文翻譯過來:shmmax單個最大共享記憶體段,shmall同一時刻能使用的所有共享記憶體頁。shmmax最小一半的物理記憶體,shmall >= shmmax/4096。

oracle的sga(Shared Global Area)使用的就是共享記憶體,共享記憶體的優勢redhat官方文檔( //access.redhat.com/documentation/en-us/red_hat_enterprise_linux/5/html/tuning_and_optimizing_red_hat_enterprise_linux_for_oracle_9i_and_10g_databases/chap-oracle_9i_and_10g_tuning_guide-setting_shared_memory#sect-Oracle_9i_and_10g_Tuning_Guide-Setting_Shared_Memory-Setting_SHMMAX_Parameter_ )中也有提及。直白點說就是多進程使用共享記憶體交流數據最快。例如:伺服器進程從磁碟讀取數據到sga的buffer cache,dbwn進程從buffer cache將數據寫回到磁碟,操作的是同一片記憶體區域。如果沒有共享記憶體,那麼就需要將伺服器進程操作的這片記憶體複製一份到dbwn所操作的記憶體中去,來完成讀取和寫入操作。
微信截圖_20220406113004.png
Shared memory allows processes to access common structures and data by placing them in shared memory segments. It is the fastest form of inter-process communication available since no kernel involvement occurs when data is passed between the processes. In fact, data does not need to be copied between the processes.
Oracle uses shared memory segments for the Shared Global Area (SGA) which is an area of memory that is shared by Oracle processes. The size of the SGA has a significant impact to Oracle’s performance since it holds database buffer cache and much more.

從上面的官方文檔我們了解了這兩個參數的含義,但是oracle只給了shmmax和shmall的最小值。接下來我們就通過實驗來看看這兩個參數對oracle的影響。

三、實驗

我的實驗機器物理記憶體是1877M,設置SGA_TAEGET為1000M。接下來測試幾個場景。

a. shmmax 200M, shmall 200M

將/etc/sysctl.conf參數設置為
kernel.shmmax = 209715200
kernel.shmall = 51200

oracle啟動直接報錯

SQL> startup nomount pfile='/home/oracle/test.ora'
ORA-27102: out of memory
Linux-x86_64 Error: 28: No space left on device
Additional information: 209715200
Additional information: 1

b. shmmax 1200M, shmall 200M

將/etc/sysctl.conf參數設置為
kernel.shmmax = 1258291200
kernel.shmall = 51200

oracle啟動報跟上面一樣的錯

SQL> startup nomount pfile='/home/oracle/test.ora' 
ORA-27102: out of memory
Linux-x86_64 Error: 28: No space left on device
Additional information: 1035993088
Additional information: 1

從a和b的實驗結果來看,oracle是否能夠正常啟動跟shmmax參數無關,只與shmall有關。shmall不能設置的比SGA_TAEGET小。

c. shmmax 200M, shmall 1200M

將/etc/sysctl.conf參數設置為
kernel.shmmax = 209715200
kernel.shmall = 307200

資料庫能夠正常啟動

SQL> startup nomount pfile='/home/oracle/test.ora'
ORACLE instance started.

Total System Global Area 1043886080 bytes
Fixed Size                  2259840 bytes
Variable Size             327156864 bytes
Database Buffers          708837376 bytes
Redo Buffers                5632000 bytes

查看共享記憶體的資訊

[root@oracletest ~]# ipcs -m

------ Shared Memory Segments --------
key        shmid      owner      perms      bytes      nattch     status      
0x00000000 229376     oracle     640        12582912   18                      
0x00000000 262145     oracle     640        209715200  18                      
0x00000000 294914     oracle     640        209715200  18                      
0x00000000 327683     oracle     640        209715200  18                      
0x00000000 360452     oracle     640        209715200  18                      
0x00000000 393221     oracle     640        197132288  18                      
0x276f5044 425990     oracle     640        2097152    18  

把上面的共享記憶體段bytes全部加起來(12582912+209715200…+2097152)/1024/1024=1002MB。可以看到oracle分配記憶體段的時候,單個共享記憶體段的確沒有超過shmmax(209715200)。總的共享記憶體剛好等於SGA_TAEGET。

d. shmmax 1200M, shmall 1200M

將/etc/sysctl.conf參數設置為
kernel.shmmax = 1258291200
kernel.shmall = 307200

資料庫同樣能夠正常啟動

SQL> startup nomount pfile='/home/oracle/test.ora'
ORACLE instance started.

Total System Global Area 1043886080 bytes
Fixed Size                  2259840 bytes
Variable Size             327156864 bytes
Database Buffers          708837376 bytes
Redo Buffers                5632000 bytes

查看共享記憶體的資訊

[root@oracletest ~]# ipcs -m

------ Shared Memory Segments --------
key        shmid      owner      perms      bytes      nattch     status      
0x00000000 557056     oracle     640        12582912   18                      
0x00000000 589825     oracle     640        1035993088 18                      
0x276f5044 622594     oracle     640        2097152    18 

把上面的共享記憶體段bytes全部加起來(12582912+1035993088+2097152)/1024/1024=1002MB。總的共享記憶體仍然剛好等於SGA_TAEGET。記憶體段的數量卻只有三個,最大的記憶體段達到1035993088/1024/1024=988M

f. shmmax 2400M, shmall 2400M

將/etc/sysctl.conf參數設置為
kernel.shmmax = 2516582400
kernel.shmall = 614400

SQL> startup nomount pfile='/home/oracle/test.ora'
ORACLE instance started.

Total System Global Area 1043886080 bytes
Fixed Size                  2259840 bytes
Variable Size             327156864 bytes
Database Buffers          708837376 bytes
Redo Buffers                5632000 bytes  
[root@oracletest ~]# ipcs -m

------ Shared Memory Segments --------
key        shmid      owner      perms      bytes      nattch     status      
0x00000000 688128     oracle     640        12582912   18                      
0x00000000 720897     oracle     640        1035993088 18                      
0x276f5044 753666     oracle     640        2097152    18 

可以看到f跟e沒啥區別,shmmax這個值你就算設置超過了物理記憶體也不受影響。因為oracle實際上分配的共享記憶體不會超過SGA_TAEGET。

四、總結

  1. 為了讓共享記憶體不至於切分成多個段,建議將shmmax設置比SGA_TAEGET大,shmall=shmmax/4096即可。至於大多少,個人認為隨意。

++本人水平有限,特別是對於共享記憶體這塊,我仍然有很多疑問,比如共享記憶體能否被交換出去?多個共享記憶體段有什麼缺點?暫時就先記錄到這裡,後面了解之後,再來更新此文。如果有專家看到文章錯誤,還望指正。++

Tags: