0. summary
1. 什么是mutex
2. Oracle 11gR2中包含的mutex
3. mutex的工作方式
. 3.1 影響mutex工作的隱含參數(shù)
. 3.1.1 10g/11.2.0.1模式
. 3.1.2 patch 6904068模式
. 3.2 mutex的spin和mutex_spin_count控制
1. 什么是mutex
可以認為是一種更加輕量級的latch, 兩者的區(qū)別在于:mutex是內(nèi)存結(jié)構(gòu)本身所包含瀑踢,隨著內(nèi)存的分配而分配鹃操、銷毀而銷毀仓坞,latch是一種獨立的內(nèi)存結(jié)構(gòu)颜说」焊冢基于這個特性,mutex保護的內(nèi)存結(jié)構(gòu)更細粒度门粪,并發(fā)性喊积、支持性要好于latch.
與shared latch一樣可使mutex用cas模式申請latch. 因此也可以把mutex當成一種shared latch, 只是其粒度更細,更加輕量級玄妈。mutex采用spin+FIRO策略乾吻,而shared latch采用spin+FIFO策略髓梅。更多詳細關于mutex的原理可以參見:
從10.2.0.2版本開始,oracle的mutex機制逐步替代了傳統(tǒng)的library cache的一些機制绎签。
2. Oracle 11gR2中包含的mutex
#### 10.2.0.5 ####
SYS@panda>select name,level# from v$latch where name like 'library cache%';
NAME LEVEL#
-------------------------------------------------- ----------
library cache 5
library cache lock 6
library cache load lock 5
library cache pin allocation 3
library cache hash chains 9
library cache lock allocation 3
library cache pin 6
#### 11.2.0.4 ####
SYS@bbed>select name,level# from v$latch where name like 'library cache%';
NAME LEVEL#
---------------------------------------------------------------- ----------
library cache load lock 5
不同版本中枯饿,對于library cache的latch變化較大,特別是從Oracle 10gR2(10.2.0.2)開始诡必,引入了mutex來替代cursor的latch. 在該版本上通過隱含參數(shù)_kks_use_mutex_pin的調(diào)整可以限制是否使用Mutex機制來實現(xiàn)Cursor Pin.
NAME VALUE DESCRIB
------------------------------ -------------------- ------------------------------------------------------------
_kks_use_mutex_pin TRUE Turning on this will make KKS use mutex for cursor pins.
mutex與latch的對應關系如下:
mutex | latch | mutex wait event |
---|---|---|
cursor pin | library cache pin | cursor: pin S奢方、cursor: pin X、cursor: pin S on X |
cursor parent | library cache lock | cursor: mutex X爸舒、cursor: mutex S |
cursor stat | library cache lock | cursor: mutex S |
library cache | library cache | library cache: mutex X蟋字、library cache: mutex S |
hash table | library cache (child cursor) | cursor: mutex X、cursor: mutex S |
cursor parent | library cache lock (child cursor) | cursor: mutex X碳抄、cursor: mutex S |
3. mutex的工作方式
- 申請mutex.
- 如果成功愉老,則持有該mutex
- 如果失敗,則進行spin. spin的過程就是在線等待mutex, 不斷發(fā)起mutex gets, 直到獲得mutex或者達到spin_count限制為止剖效。
- 依據(jù)工作模式的不同選擇yiled還是sleep.
- 若達到sleep限制或者被主動喚醒或者完成yiled, 則重復1)~4)步,直到獲得為止焰盗。
3.1 影響mutex工作的隱含參數(shù)
以下參數(shù)在11.2.0.2以上才有
PANDA@bbed>show parameter mutex
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
_mutex_spin_count integer 255 ---- 設置mutex spin count, 默認255
_mutex_wait_scheme integer 2 ---- 控制mutex的工作模式璧尸,默認為2. 可選擇0、1熬拒、2
_mutex_wait_time integer 1 ---- 設置在工作模式1爷光、2下的sleep時間
PANDA@bbed>show parameter yield
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
_wait_yield_hp_mode string yield ---- 設置mutex yield的優(yōu)先級
_wait_yield_mode string yield ---- 這是yield的工作方式,默認為yield. 可選擇yield和sleep. 表示mutex在失敗后先yield還是sleep
_wait_yield_sleep_freq integer 100 ---- sleep的次數(shù)澎粟,與_wait_yield_sleep_time_msecs一起構(gòu)成sleep時間
_wait_yield_sleep_time_msecs integer 1 ---- 每次yield后進入sleep的sleep時間
_wait_yield_yield_freq integer 20 ---- yield的次數(shù)
_mutex_wait_scheme=1 ---- 在mutex gets失敗后總是簡單yield兩次蛀序,然后進入sleep, sleep的時間由_mutex_wait_time設置。
_mutex_wait_scheme=2 ---- 傳統(tǒng)模式活烙,和模式1的區(qū)別在于徐裸,隨著時間推移,sleep time等待的時間越來越長啸盏。_mutex_wait_time設置了其最長的等待時間重贺。
_mutex_wait_scheme=0 ---- 最為靈活的工作方式,在mutex gets失敗后回懦,依據(jù)_wait_yield_mode選擇進入yield還是進入sleep, yield次數(shù)受到_wait_yield_yield_freq控制气笙。sleep time受_wait_yield_sleep_time_msecs*_wait_yield_sleep_freq的控制∏釉危可以通過參數(shù)控制使scheme 0獲得任意工作方式包括10g工作方式潜圃。
由于mutex采用了傳統(tǒng)的spin+sleep機制而不是latch的spin+post機制,所以sleep time的設置對于mutex性能具有重要地位舟茶,一般來說由于mutex的沖突遠比latch要小谭期,所以合適的yield和更小的sleep選擇是恰當?shù)尿让铡R话闱闆r不需要改動,默認都是1ms.
不同模式的比較可以參考:
3.1.1 10g/11.2.0.1模式
不包含sleep, 只在簡單yield之后進行不斷的mutex spin. 顯然崇堵,在沖突不發(fā)生的情況下會有最好的性能型诚,但是在有沖突的時候可能會消耗很高的CPU資源。
3.1.2 patch 6904068模式
Resolving Issues Where High CPU utilization and 'cursor: pin S' waits are Seen Due to Bug 6904068 (文檔 ID 1476113.1)
This fix is intended for one off patches only and usually uses "_first_spare_parameter" to indicate the number of centiseconds
to wait when pausing for "cursor: pin S" waits. A value of 0 mimicks current behaviour which is to yield between tries.
對10g模式的一種修正鸳劳,增加了sleep部分以降低CPU消耗狰贯,sleep時間收到參數(shù)_first_spare_parameter的控制
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
_first_spare_parameter integer
在Solaris SPARC 10.2.0.4這個參數(shù)是_second_spare_parameter.
3.2 mutex的spin和mutex_spin_count控制
mutex的spin次數(shù)受mutex_spin_count控制,默認為255. 該參數(shù)可以動態(tài)修改赏廓。
SYS@bbed>alter system set "_mutex_spin_count"=2000;
System altered.
某些時候涵紊,該值可能需要適當增大來獲取更好的性能。設置方法可參考latch的spin count設置方法幔摸。v$mutex_sleep_history視圖包含相關的gets和sleep信息摸柄。
SYS@bbed>select gets,sleeps,mutex_type from v$mutex_sleep_history;
GETS SLEEPS MUTEX_TYPE
---------- ---------- --------------------------------
1 3 Cursor Pin
6328 1 Library Cache
關于mutex_spin_conut設置對mutex響應性能的影響可參考: