欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

[20190328]简单探究sql语句相关mutexes.txt

程序员文章站 2023-04-06 09:46:31
[20190328]简单探究sql语句相关mutexes.txt--//摘要:http://www.askmaclean.com/archives/understanding-oracle-mutex.html虽然Mutex中文翻译为互斥锁,但为了和OS mutex充分的区别,所以我们在本文里称Or ......

[20190328]简单探究sql语句相关mutexes.txt

--//摘要:http://www.askmaclean.com/archives/understanding-oracle-mutex.html

虽然mutex中文翻译为互斥锁,但为了和os mutex充分的区别,所以我们在本文里称oracle mutex为mutex。

oracle中的mutex,类似于latch,是一种低级的串行机制,用以控制对sga中部分共享数据结构的访问控制。
oracle中的串行机制有不少,引入它们的目的是避免一个对象出现下述现象:

    当某些进程在访问该对象时,该资源被重新分配
    当某些进程在修改它时,被其他进程读取
    当某些进程在修改它时,被其他进程修改
    当某些进程在读取它时,被其他进程修改

不同于latch,mutex的使用更灵活,用途更多,例如:

    哪些需要被mutex保护的共享数据结构可以有自己独立的mutex,即一个对象拥有自己独立的mutex,不像latch往往一个需要保护大量
    对象,举例来说,每一个父游标有其对应的mutex, 而每一个子游标也有其对应的mutex

    每一个数据结构可能有一个或多个mutex保护,每一个mutex负责保护其结构的不同部分
    当然一个mutex也可以用来保护多于一个的数据结构

理论上mutex即可以存放在其保护的结构本身中(其实是嵌入在结构里),也可以存放在其他地方。 一般情况下mutex是在数据结构需要被
保护时动态创建出来的。 如是嵌在需要保护结构体内的mutex,则当 所依附的数据结构被清理时 该mutex也将被摧毁。

mutex带来的好处

虽然mutex和latch都是oracle中的串行机制,但是mutex具有一些latch没有的好处

更轻量级且更快

mutex作为latch的替代品,具有更快速获得,更小等优势。 获取一个mutex进需要大约30~35个指令, 而latch则需要150~200个指令。一
个mutex结构的大小大约为16 bytes,而在10.2版本中一个latch需要112个bytes,在更早的版本中是200个bytes。 从200个bytes 精简到
112个是通过减少不必要的统计指标 sleep1~sleep11、waiters_woken, waits_holding_latch等从而实现的。今后我们将看到更多关于
latch的代码优化。

减少伪争用

典型情况下一个latch保护多个对象。 当一个latch保护多个热对象时,并行地对这些对象的频繁访问让latch本身变成性能的串行点。
这也就是我们此处说的伪争用点, 因为争用是发生在这个串行保护的机制上,而不是进程去访问的对象本身。与latch不同, 使用mutex
的情况下oracle开发人员可以为每一个要保护的数据结构创建一个独立的mutex。 这意味着latch的那种伪争用将大大减少,因为每一个
对象均被自己独立拥有的mutex保护

--//我想通过测试说明问题.

1.环境:
--//session 1:
scott@book> @ ver1
port_string         version        banner
------------------- -------------- --------------------------------------------------------------------------------
x86_64/linux 2.4.xx 11.2.0.4.0     oracle database 11g enterprise edition release 11.2.0.4.0 - 64bit production

$ cat m2.txt
set verify off
host sleep $(echo &&3/50| bc -l )
insert into job_times values ( sys_context ('userenv', 'sid') ,dbms_utility.get_time ,'&&2') ;
commit ;
declare
v_id number;
v_d date;
begin
    for i in 1 .. &&1 loop
        --select  1 into v_id from dual ;
        --select  sysdate into v_d from dual ;
        select deptno into v_id from dept where deptno=10;
    end loop;
end ;
/
update job_times set time_ela = dbms_utility.get_time - time_ela where sid=sys_context ('userenv', 'sid') and method='&&2';
commit;
quit

$ seq 150 | xargs -i {}  -p 150 bash -c  "sqlplus -s -l scott/book @m2.txt 1e6 c2_150 {} >/dev/null"

--//执行以上命令后,由于大量的sql执行语句相同,出现cursor: pin s.可以执行多次,避免语句从共享池刷出.
--//注意oracle围绕mutex的视图很少,仅仅v$mutex_sleep,v$mutex_sleep_history,指令少结构体小,这样记录的诊断信息少.

2.首先看看mutex在哪里?
--//首先确定mutex_addr在那里.
--//sesson 1:
scott@book> column location format a40
scott@book> select * from v$mutex_sleep order by 3 ;
mutex_type           location                     sleeps  wait_time
-------------------- -------------------------- -------- ----------
library cache        kglget1   1                       1          0
library cache        kglini1   32                      1          0
library cache        kgldtin1  42                      1          0
library cache        kglati1   45                      2          0
library cache        kglnti1   46                      3          0
library cache        kglllal1 109                      6          0
library cache        kgllldl2 112                      7          0
library cache        kglllal3 111                     10          0
library cache        kglpnal1  90                     17          0
library cache        kgllkdl1  85                     18          0
library cache        kglpndl1  95                     27          0
library cache        kglget2   2                      28          0
library cache        kgllkc1   57                     28          0
library cache        kglpin1   4                      29          0
cursor pin           kkslce [kkschlpin2]             130          0
library cache        kglhdgn2 106                    475          0
cursor pin           kksfbc [kkschlfsp2]          649446          0
cursor pin           kkslockdelete [kkschlpin6]  1109496          0
18 rows selected.
--//仅仅知道mutex_type=cursor pin ,location=kkslockdelete [kkschlpin6],kksfbc [kkschlfsp2]的sleep很高,具体在那里,那个语
--//句引起的问题,明显诊断信息不足,
--//而仅仅mutex出现阻塞sleep后,oracle才会在视图v$mutex_sleep_history留下信息,不然很难定位.

--//session 2:
sys@book> @ mutexy 3
old  21: order by sum_sleeps desc ) where rownum<= &1
new  21: order by sum_sleeps desc ) where rownum<= 3
      hash sum_sleeps   sum_gets location                       mutex_type           mutex_addr       sqlid         kglnaown c100
---------- ---------- ---------- ------------------------------ -------------------- ---------------- ------------- -------- ---------------------------------------
1692266099     770212 1.8476e+10 kkslockdelete [kkschlpin6]     cursor pin           000000007dfacab8 a31kd5tkdvvmm          select deptno from dept where deptno=10
1692266099     408706 9792073012 kksfbc [kkschlfsp2]            cursor pin           000000007dfacab8 a31kd5tkdvvmm          select deptno from dept where deptno=10
 991821498        115      16076 kglhdgn2 106                   library cache        000000007c638348                        reco.oracle.com
--//mutex_addr = 000000007dfacab8

sys@book> @ fcha 000000007dfacab8
find in which heap (uga, pga or shared pool) the memory address 000000007dfacab8 resides...
warning!!! this script will query x$ksmsp, which will cause heavy shared pool latch contention
in systems under load and with large shared pool. this may even completely hang
your instance until the query has finished! you probably do not want to run this in production!
press enter to continue, ctrl+c to cancel...
old  14:     to_number(substr('&1', instr(lower('&1'), 'x')+1) ,'xxxxxxxxxxxxxxxx')
new  14:     to_number(substr('000000007dfacab8', instr(lower('000000007dfacab8'), 'x')+1) ,'xxxxxxxxxxxxxxxx')
old  32:     to_number(substr('&1', instr(lower('&1'), 'x')+1) ,'xxxxxxxxxxxxxxxx')
new  32:     to_number(substr('000000007dfacab8', instr(lower('000000007dfacab8'), 'x')+1) ,'xxxxxxxxxxxxxxxx')
old  50:     to_number(substr('&1', instr(lower('&1'), 'x')+1) ,'xxxxxxxxxxxxxxxx')
new  50:     to_number(substr('000000007dfacab8', instr(lower('000000007dfacab8'), 'x')+1) ,'xxxxxxxxxxxxxxxx')
loc ksmchptr           ksmchidx   ksmchdur ksmchcom           ksmchsiz ksmchcls   ksmchtyp ksmchpar
--- ---------------- ---------- ---------- ---------------- ---------- -------- ---------- ----------------
sga 000000007dfac3f0          1          1 kglh0^64ddee73         4096 recr           4095 000000007e0ae4b0

--//ksmchcom=kglh0^64ddee73 ,ksmchcom有信息kglh0,可以猜测在sql语句的某个父或者子光标的堆0中.

sys@book> @ sharepool/shp4 a31kd5tkdvvmm 0
old  20:  where kglobt03 = '&1'  or kglhdpar='&1' or kglhdadr='&1' or kglnahsh= &2
new  20:  where kglobt03 = 'a31kd5tkdvvmm'  or kglhdpar='a31kd5tkdvvmm' or kglhdadr='a31kd5tkdvvmm' or kglnahsh= 0
text           kglhdadr         kglhdpar         c40                                        kglhdlmd   kglhdpmd   kglhdivc kglobhd0         kglobhd6           kglobhs0   kglobhs6   kglobt16   n0_6_16        n20   kglnahsh kglobt03        kglobt09
-------------- ---------------- ---------------- ---------------------------------------- ---------- ---------- ---------- ---------------- ---------------- ---------- ---------- ---------- --------- ---------- ---------- ------------- ----------
子游标句柄地址 000000007e1b07d8 000000007e3b7830 select deptno from dept where deptno=10           1          2          0 000000007e23f080 000000007dfacb60       4528       8088       3072     15688      15688 1692266099 a31kd5tkdvvmm          0
父游标句柄地址 000000007e3b7830 000000007e3b7830 select deptno from dept where deptno=10           1          0          0 000000007e0ae4b0 00                     4720          0          0      4720       4720 1692266099 a31kd5tkdvvmm      65535

--//与父游标句柄地址的kglobhd0='000000007e0ae4b0',注意这个地址是堆描述符地址.也可以理解为指向堆0的指针,
--//也就是这个mutex结构体在父游标的堆0里面.

sys@book> select * from x$ksmsp where to_number ('000000007e0ae4b0', 'xxxxxxxxxxxxxxxx') between to_number(ksmchptr,'xxxxxxxxxxxxxxxx') and to_number(ksmchptr, 'xxxxxxxxxxxxxxxx')+ksmchsiz-1;
old   1: select * from x$ksmsp where to_number ('&&1', 'xxxxxxxxxxxxxxxx') between to_number(ksmchptr, 'xxxxxxxxxxxxxxxx') and to_number(ksmchptr, 'xxxxxxxxxxxxxxxx')+ksmchsiz
new   1: select * from x$ksmsp where to_number ('000000007e0ae4b0', 'xxxxxxxxxxxxxxxx') between to_number(ksmchptr, 'xxxxxxxxxxxxxxxx') and to_number(ksmchptr, 'xxxxxxxxxxxxxxxx')+ksmchsiz
addr                   indx    inst_id   ksmchidx   ksmchdur ksmchcom         ksmchptr           ksmchsiz ksmchcls   ksmchtyp ksmchpar
---------------- ---------- ---------- ---------- ---------- ---------------- ---------------- ---------- -------- ---------- ----------------
00007f24581306c8       3192          1          1          1 kglda            000000007e0ae448        240 freeabl           0 00

3.人为加锁看看:
--//session 2:
sys@book> @ pt 'select * from v$mutex_sleep_history where mutex_identifier=1692266099 and rownum=1'
old  10:              passing xmltype(cursor( &1 ))
new  10:              passing xmltype(cursor( select * from v$mutex_sleep_history where mutex_identifier=1692266099 and rownum=1 ))
row_num col_name           col_value
------- ------------------ --------------------------
      1 mutex_identifier   1692266099
        sleep_timestamp    2019-03-28 15:36:51.649428
        mutex_type         cursor pin
        gets               599013225
        sleeps             25361
        requesting_session 7
        blocking_session   35
        location           kkslockdelete [kkschlpin6]
        mutex_value        000000230000001e
        p1                 1
        p1raw              00
        p2                 0
        p3                 0
        p4                 0
14 rows selected.
--//mutex_value=000000230000001e.0x00000023=35,表示阻塞的session的sid.  
--//session 1:
scott@book> @ spid
       sid    serial# process                  server    spid       pid  p_serial# c50
---------- ---------- ------------------------ --------- ------ ------- ---------- --------------------------------------------------
       295          7 11295                    dedicated 11296       21          4 alter system kill session '295,7' immediate;
--//sid=295 , 295=0x127.

sys@book> oradebug setmypid
statement processed.

sys@book> oradebug poke 0x000000007dfacab8 8 0x0000012700000127
before: [07dfacab8, 07dfacac0) = 00000000 00000000
after:  [07dfacab8, 07dfacac0) = 00000127 00000127

scott@book> @ m2.txt 1 c1 0
1 row created.
commit complete.
declare
*
error at line 1:
ora-04024: self-deadlock detected while trying to mutex pin cursor 0x07e1b07d8
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ora-06512: at line 8
2 rows updated.
commit complete.
--//注意看下划线,这是自锁.oracle设计够严谨的,什么情况下会出现ora-04024错误.
--//这里的0x07e1b07d8是子游标句柄地址.

--//session 2:
sys@book> @ spid
       sid    serial# process                  server    spid       pid  p_serial# c50
---------- ---------- ------------------------ --------- ------ ------- ---------- --------------------------------------------------
         1         31 11248                    dedicated 11249       24          9 alter system kill session '1,31' immediate;
--//sid=1.
sys@book> oradebug poke 0x000000007dfacab8 8 0x0000000100000127
before: [07dfacab8, 07dfacac0) = 00000127 00000127
after:  [07dfacab8, 07dfacac0) = 00000127 00000001
--//注意一个细节,intel cpu系列的大小头问题. 4个4个字节颠倒的.

sys@book> oradebug peek 0x000000007dfacab8 8
[07dfacab8, 07dfacac0) = 00000127 00000001

--//session 1:
scott@book> @ m2.txt 1 c1 0
1 row created.
commit complete.
--//挂起!!
--//说明:一般测试环境没有用户登录的情况下,下次登录sid不会变化.

--//session 2:
sys@book> @ wait
p1raw            p2raw            p3raw                    p1         p2         p3        sid    serial#       seq# event         status   state   wait_time_micro seconds_in_wait wait_class
---------------- ---------------- ---------------- ---------- ---------- ---------- ---------- ---------- ---------- ------------- -------- ------- --------------- --------------- -----------
0000000064ddee73 0000000100000127 0000000500000000 1692266099 4294967591 2.1475e+10        295         13         30 cursor: pin s active   waiting        50465005              50 concurrency
--//注意p2raw.

sys@book> oradebug poke 0x000000007dfacab8 8 0x000000000000000
before: [07dfacab8, 07dfacac0) = 00000127 00000001
after:  [07dfacab8, 07dfacac0) = 00000000 00000000

--//session 1:
--//执行完成退出.

4.看看一些细节:
--//session 1:
scott@book> @ spid
       sid    serial# process                  server    spid       pid  p_serial# c50
---------- ---------- ------------------------ --------- ------ ------- ---------- --------------------------------------------------
       295         17 11477                    dedicated 11478       21          9 alter system kill session '295,17' immediate;
--//spid=1147811358

--//session 2:
sys@book> oradebug poke 0x000000007dfacab8 8 0x0000000100000127
before: [07dfacab8, 07dfacac0) = 00000000 00000000
after:  [07dfacab8, 07dfacac0) = 00000127 00000001

--//打开新的终端执行如下命令:
$ strace -fttt -p 11478 2>&1 | tee /tmp/m.txt

--//session 1:
scott@book> @ m2.txt 1 c1 0
1 row created.
commit complete.
--//再次挂起.等1分钟以上.
--//session 2:
sys@book> oradebug poke 0x000000007dfacab8 8 0x000000000000000
before: [07dfacab8, 07dfacac0) = 00000127 00000001
after:  [07dfacab8, 07dfacac0) = 00000000 00000000

--//分析/tmp/m.txt.
$ awk '{print $2}' /tmp/m.txt  | uniq -c | egrep "semtimedop| getrusag"
...
      1 getrusage(rusage_self,
      2 getrusage(rusage_self,
      7 getrusage(rusage_self,
      1 getrusage(rusage_self,
    117 semtimedop(309166080,
      2 getrusage(rusage_self,
    181 semtimedop(309166080,
      2 getrusage(rusage_self,
    182 semtimedop(309166080,
      2 getrusage(rusage_self,
    182 semtimedop(309166080,
      2 getrusage(rusage_self,
    ....
    182 semtimedop(309166080,
      2 getrusage(rusage_self,
    167 semtimedop(309166080,
      1 getrusage(rusage_self,
      4 getrusage(rusage_self,
      6 getrusage(rusage_self,
      2 getrusage(rusage_self,
      1 getrusage(rusage_self,
      2 getrusage(rusage_self,
      1 getrusage(rusage_self,
      1 getrusage(rusage_self,
      2 getrusage(rusage_self,
      3 getrusage(rusage_self,
      1 getrusage(rusage_self,
      2 getrusage(rusage_self,
--//大约调用semtimedop 182次后(大约2秒),调用2次getrusage.截取其中1段.
16:04:56.410571 getrusage(rusage_self, {ru_utime={0, 33994}, ru_stime={0, 33994}, ...}) = 0 <0.000020>
16:04:56.410676 getrusage(rusage_self, {ru_utime={0, 33994}, ru_stime={0, 33994}, ...}) = 0 <0.000017>
16:04:56.410785 semtimedop(309166080, 0x7fff83068fd0, 1, {0, 10000000}) = -1 eagain (resource temporarily unavailable) <0.010684>
16:04:56.421565 semtimedop(309166080, 0x7fff83068fd0, 1, {0, 10000000}) = -1 eagain (resource temporarily unavailable) <0.010886>
16:04:56.432559 semtimedop(309166080, 0x7fff83068fd0, 1, {0, 10000000}) = -1 eagain (resource temporarily unavailable) <0.010894>
...
16:04:58.368564 semtimedop(309166080, 0x7fff83068fd0, 1, {0, 10000000}) = -1 eagain (resource temporarily unavailable) <0.010889>
16:04:58.379561 semtimedop(309166080, 0x7fff83068fd0, 1, {0, 10000000}) = -1 eagain (resource temporarily unavailable) <0.010888>
16:04:58.390557 semtimedop(309166080, 0x7fff83068fd0, 1, {0, 10000000}) = -1 eagain (resource temporarily unavailable) <0.010905>
16:04:58.401570 semtimedop(309166080, 0x7fff83068fd0, 1, {0, 10000000}) = -1 eagain (resource temporarily unavailable) <0.010883>
16:04:58.412548 getrusage(rusage_self, {ru_utime={0, 39993}, ru_stime={0, 38994}, ...}) = 0 <0.000019>
16:04:58.412648 getrusage(rusage_self, {ru_utime={0, 39993}, ru_stime={0, 38994}, ...}) = 0 <0.000017>

--//大家可以man semtimedop或者man getrusage表示什么意思.摘要一段:
$ man semtimedop
name
       semop, semtimedop - semaphore operations
       int semtimedop(int semid, struct sembuf *sops, unsigned nsops, struct timespec *timeout);

semtimedop() behaves identically to semop() except that in those cases were the calling process would sleep, the
duration of that sleep is limited by the amount of elapsed time specified by the timespec structure whose address is
passed in the timeout parameter.  if the specified time limit has been reached, semtimedop() fails with errno set to
eagain (and none of the operations in sops is performed).  if the timeout parameter is null, then semtimedop() behaves
exactly like semop().

--//翻译如下:
semtimedop()的行为与semop相同(),但在这些情况下,调用进程将休眠,睡眠的持续时间受时间规格结构指定的经过时间的限制,其地址
是在超时参数中传递。如果已达到指定的时间限制,则semtimedop()失败,errno设置为再次(不执行sop中的操作)。如果超时参数为空
,则semtimedop()行为就像semop()。

16:04:56.410785 semtimedop(309166080, 0x7fff83068fd0, 1, {0, 10000000}) = -1 eagain (resource temporarily unavailable) <0.010684>
--//{0, 10000000} 是 timespec. 前面单位是秒,后面单位是纳秒(毫微秒) 1秒=10^9纳秒, 10000000/10^9 = .01.
--//这样每次调用semtimedop需要0.010xxx秒.
--//我的理解相当于不断spin,检查这个资源是否可用.2秒后调用getrusage.

--//session 2:
sys@book> @ hide mutex
name               description       default_value session_value system_value
------------------ ----------------- ------------- ------------- ------------
_mutex_spin_count  mutex spin count  true          255           255
_mutex_wait_scheme mutex wait scheme true          2             2
_mutex_wait_time   mutex wait time   true          1             1

5.修改这些隐含参数看看:
--//session 2:
sys@book> alter system set "_mutex_wait_time"=100 scope=memory;
system altered.

--//session 1:
scott@book> @ spid
       sid    serial# process                  server    spid       pid  p_serial# c50
---------- ---------- ------------------------ --------- ------ ------- ---------- --------------------------------------------------
        37         79 11990                    dedicated 11991       26         19 alter system kill session '37,79' immediate
--//sid=37 ,37=0x25.

--//session 2:
sys@book> oradebug poke 0x000000007dfacab8 8 0x0000000100000025
before: [07dfacab8, 07dfacac0) = 00000000 00000000
after:  [07dfacab8, 07dfacac0) = 00000025 00000001

--//打开新的终端执行如下命令:
$ strace -fttt -p 11956 2>&1 | tee /tmp/m1.txt
16:45:21.034782 getrusage(rusage_self, {ru_utime={0, 300954}, ru_stime={0, 31995}, ...}) = 0 <0.000034>
16:45:21.034936 semtimedop(309166080, 0x7fff3961cd50, 1, {1, 0}) = -1 eagain (resource temporarily unavailable) <1.000684>
16:45:22.035733 semtimedop(309166080, 0x7fff3961cd50, 1, {1, 0}) = -1 eagain (resource temporarily unavailable) <1.000669>
16:45:23.036517 getrusage(rusage_self, {ru_utime={0, 300954}, ru_stime={0, 31995}, ...}) = 0 <0.000033>
16:45:23.036668 getrusage(rusage_self, {ru_utime={0, 300954}, ru_stime={0, 31995}, ...}) = 0 <0.000031>
16:45:23.036819 semtimedop(309166080, 0x7fff3961cd50, 1, {1, 0}) = -1 eagain (resource temporarily unavailable) <1.000693>
16:45:24.037629 semtimedop(309166080, 0x7fff3961cd50, 1, {1, 0}) = -1 eagain (resource temporarily unavailable) <1.000844>
16:45:25.038587 getrusage(rusage_self, {ru_utime={0, 301954}, ru_stime={0, 31995}, ...}) = 0 <0.000032>
16:45:25.038734 getrusage(rusage_self, {ru_utime={0, 301954}, ru_stime={0, 31995}, ...}) = 0 <0.000035>
16:45:25.038883 semtimedop(309166080, 0x7fff3961cd50, 1, {1, 0}) = -1 eagain (resource temporarily unavailable) <1.000709>
16:45:26.039702 semtimedop(309166080, 0x7fff3961cd50, 1, {1, 0}) = -1 eagain (resource temporarily unavailable) <1.000822>
16:45:27.040680 getrusage(rusage_self, {ru_utime={0, 301954}, ru_stime={0, 31995}, ...}) = 0 <0.000029>
16:45:27.040805 getrusage(rusage_self, {ru_utime={0, 301954}, ru_stime={0, 31995}, ...}) = 0 <0.000017>
16:45:27.040909 semtimedop(309166080, 0x7fff3961cd50, 1, {1, 0}) = -1 eagain (resource temporarily unavailable) <1.000680>
16:45:28.041693 semtimedop(309166080, 0x7fff3961cd50, 1, {1, 0}) = -1 eagain (resource temporarily unavailable) <1.000819>
16:45:29.042627 getrusage(rusage_self, {ru_utime={0, 301954}, ru_stime={0, 31995}, ...}) = 0 <0.000033>
16:45:29.042780 getrusage(rusage_self, {ru_utime={0, 301954}, ru_stime={0, 31995}, ...}) = 0 <0.000034>
--//现在是间隔1秒调用semtimedop. 也就是改变_mutex_wait_time等待时间(单位cs).我的理解在spin 255次不成功sleep 1秒.
--//注:semtimedop , spin次数来源这里(_mutex_spin_count=255),然后sleep,再次唤醒.
--//session 2:
sys@book> alter system set "_mutex_wait_time"=10 scope=memory;
system altered.

--//重新测试:
16:49:32.057570 getrusage(rusage_self, {ru_utime={0, 29995}, ru_stime={0, 18997}, ...}) = 0 <0.000019>
16:49:32.057682 getrusage(rusage_self, {ru_utime={0, 29995}, ru_stime={0, 18997}, ...}) = 0 <0.000019>
16:49:32.057796 semtimedop(309166080, 0x7fffbe3108a0, 1, {0, 100000000}) = -1 eagain (resource temporarily unavailable) <0.100747>
16:49:32.158640 semtimedop(309166080, 0x7fffbe3108a0, 1, {0, 100000000}) = -1 eagain (resource temporarily unavailable) <0.100834>
16:49:32.259582 semtimedop(309166080, 0x7fffbe3108a0, 1, {0, 100000000}) = -1 eagain (resource temporarily unavailable) <0.100928>
16:49:32.360618 semtimedop(309166080, 0x7fffbe3108a0, 1, {0, 100000000}) = -1 eagain (resource temporarily unavailable) <0.100856>
16:49:32.461584 semtimedop(309166080, 0x7fffbe3108a0, 1, {0, 100000000}) = -1 eagain (resource temporarily unavailable) <0.100890>
16:49:32.562583 semtimedop(309166080, 0x7fffbe3108a0, 1, {0, 100000000}) = -1 eagain (resource temporarily unavailable) <0.100924>
16:49:32.663615 semtimedop(309166080, 0x7fffbe3108a0, 1, {0, 100000000}) = -1 eagain (resource temporarily unavailable) <0.100858>
16:49:32.764582 semtimedop(309166080, 0x7fffbe3108a0, 1, {0, 100000000}) = -1 eagain (resource temporarily unavailable) <0.100928>
16:49:32.865619 semtimedop(309166080, 0x7fffbe3108a0, 1, {0, 100000000}) = -1 eagain (resource temporarily unavailable) <0.100855>
16:49:32.966582 semtimedop(309166080, 0x7fffbe3108a0, 1, {0, 100000000}) = -1 eagain (resource temporarily unavailable) <0.100909>
16:49:33.067606 semtimedop(309166080, 0x7fffbe3108a0, 1, {0, 100000000}) = -1 eagain (resource temporarily unavailable) <0.100853>
16:49:33.168602 semtimedop(309166080, 0x7fffbe3108a0, 1, {0, 100000000}) = -1 eagain (resource temporarily unavailable) <0.100809>
16:49:33.269536 semtimedop(309166080, 0x7fffbe3108a0, 1, {0, 100000000}) = -1 eagain (resource temporarily unavailable) <0.100879>
16:49:33.370541 semtimedop(309166080, 0x7fffbe3108a0, 1, {0, 100000000}) = -1 eagain (resource temporarily unavailable) <0.100854>
16:49:33.471520 semtimedop(309166080, 0x7fffbe3108a0, 1, {0, 100000000}) = -1 eagain (resource temporarily unavailable) <0.100872>
16:49:33.572516 semtimedop(309166080, 0x7fffbe3108a0, 1, {0, 100000000}) = -1 eagain (resource temporarily unavailable) <0.100915>
16:49:33.673559 semtimedop(309166080, 0x7fffbe3108a0, 1, {0, 100000000}) = -1 eagain (resource temporarily unavailable) <0.100849>
16:49:33.774533 semtimedop(309166080, 0x7fffbe3108a0, 1, {0, 100000000}) = -1 eagain (resource temporarily unavailable) <0.100899>
16:49:33.875556 semtimedop(309166080, 0x7fffbe3108a0, 1, {0, 100000000}) = -1 eagain (resource temporarily unavailable) <0.100842>
16:49:33.976519 semtimedop(309166080, 0x7fffbe3108a0, 1, {0, 100000000}) = -1 eagain (resource temporarily unavailable) <0.100887>
16:49:34.077528 getrusage(rusage_self, {ru_utime={0, 30995}, ru_stime={0, 18997}, ...}) = 0 <0.000031>
16:49:34.077679 getrusage(rusage_self, {ru_utime={0, 30995}, ru_stime={0, 18997}, ...}) = 0 <0.000031>
16:49:34.077827 semtimedop(309166080, 0x7fffbe3108a0, 1, {0, 100000000}) = -1 eagain (resource temporarily unavailable) <0.100601>
16:49:34.178538 semtimedop(309166080, 0x7fffbe3108a0, 1, {0, 100000000}) = -1 eagain (resource temporarily unavailable) <0.100878>
16:49:34.279542 semtimedop(309166080, 0x7fffbe3108a0, 1, {0, 100000000}) = -1 eagain (resource temporarily unavailable) <0.100854>
16:49:34.380520 semtimedop(309166080, 0x7fffbe3108a0, 1, {0, 100000000}) = -1 eagain (resource temporarily unavailable) <0.100891>
16:49:34.481551 semtimedop(309166080, 0x7fffbe3108a0, 1, {0, 100000000}) = -1 eagain (resource temporarily unavailable) <0.100866>
16:49:34.582536 semtimedop(309166080, 0x7fffbe3108a0, 1, {0, 100000000}) = -1 eagain (resource temporarily unavailable) <0.100886>
16:49:34.683545 semtimedop(309166080, 0x7fffbe3108a0, 1, {0, 100000000}) = -1 eagain (resource temporarily unavailable) <0.100869>
16:49:34.784537 semtimedop(309166080, 0x7fffbe3108a0, 1, {0, 100000000}) = -1 eagain (resource temporarily unavailable) <0.100874>
16:49:34.885552 semtimedop(309166080, 0x7fffbe3108a0, 1, {0, 100000000}) = -1 eagain (resource temporarily unavailable) <0.100868>
16:49:34.986539 semtimedop(309166080, 0x7fffbe3108a0, 1, {0, 100000000}) = -1 eagain (resource temporarily unavailable) <0.100874>
16:49:35.087538 semtimedop(309166080, 0x7fffbe3108a0, 1, {0, 100000000}) = -1 eagain (resource temporarily unavailable) <0.100855>
16:49:35.188519 semtimedop(309166080, 0x7fffbe3108a0, 1, {0, 100000000}) = -1 eagain (resource temporarily unavailable) <0.100890>
16:49:35.289535 semtimedop(309166080, 0x7fffbe3108a0, 1, {0, 100000000}) = -1 eagain (resource temporarily unavailable) <0.100901>
16:49:35.390564 semtimedop(309166080, 0x7fffbe3108a0, 1, {0, 100000000}) = -1 eagain (resource temporarily unavailable) <0.100880>
16:49:35.491562 semtimedop(309166080, 0x7fffbe3108a0, 1, {0, 100000000}) = -1 eagain (resource temporarily unavailable) <0.100836>
16:49:35.592519 semtimedop(309166080, 0x7fffbe3108a0, 1, {0, 100000000}) = -1 eagain (resource temporarily unavailable) <0.100912>
16:49:35.693555 semtimedop(309166080, 0x7fffbe3108a0, 1, {0, 100000000}) = -1 eagain (resource temporarily unavailable) <0.100861>
16:49:35.794537 semtimedop(309166080, 0x7fffbe3108a0, 1, {0, 100000000}) = -1 eagain (resource temporarily unavailable) <0.100892>
16:49:35.895553 semtimedop(309166080, 0x7fffbe3108a0, 1, {0, 100000000}) = -1 eagain (resource temporarily unavailable) <0.100843>
16:49:35.996518 semtimedop(309166080, 0x7fffbe3108a0, 1, {0, 100000000}) = -1 eagain (resource temporarily unavailable) <0.100876>
16:49:36.097508 getrusage(rusage_self, {ru_utime={0, 31995}, ru_stime={0, 19996}, ...}) = 0 <0.000030>
16:49:36.097635 getrusage(rusage_self, {ru_utime={0, 31995}, ru_stime={0, 19996}, ...}) = 0 <0.000027>
--//变成了0.1秒.
--//注意一个细节,如果减去_mutex_wait_time时间,可以发现后面的时间都在0.0006xx-0.00090x之间.
--//取一段计算看看
0.100601+0.100878+0.100854+0.100891+0.100866+0.100886+0.100869+0.100874+0.100868+0.100874+0.100855+0.100890
+0.100901+0.100880+0.100836+0.100912+0.100861+0.100892+0.100843+0.100876= 2.017207
2.017207/20 = .10086035000000000000
--//取平均0.000860秒=860微秒.如果spin=255次的话.每次0.000860/255 = 0.0000034秒
--//另外我修改_mutex_spin_count参数,好像调用semtimedop时间不会变化,不知道我理解错误在哪里?

--//session 2:
sys@book> alter system set "_mutex_wait_time"=1 scope=memory;
system altered.

sys@book> alter system set "_mutex_wait_scheme"=0 scope=memory;
system altered.

$ awk '{print $2}' /tmp/m2.txt  | uniq -c |egrep "sched_yield|select"|head
     99 sched_yield()
      1 select(0,
     99 sched_yield()
      1 select(0,
     99 sched_yield()
      1 select(0,
     99 sched_yield()
      1 select(0,
     99 sched_yield()
      1 select(0,
--//调用99次sched_yield,然后1次seelct.
--//截取一段
16:51:53.763611 sched_yield()           = 0 <0.000025>
16:51:53.763710 sched_yield()           = 0 <0.000020>
16:51:53.763797 sched_yield()           = 0 <0.000025>
16:51:53.763896 sched_yield()           = 0 <0.000023>
16:51:53.763993 sched_yield()           = 0 <0.000023>
16:51:53.764089 sched_yield()           = 0 <0.000023>
16:51:53.764206 select(0, [], [], [], {0, 1000}) = 0 (timeout) <0.001109>
16:51:53.765424 sched_yield()           = 0 <0.000024>
16:51:53.765530 sched_yield()           = 0 <0.000023>
16:51:53.765624 sched_yield()           = 0 <0.000022>
--//如果是_mutex_wait_scheme=0,调用是sched_yield 99次,然后1次select.

$ man sched_yield
sched_yield(2)             linux programmer's manual            sched_yield(2)

name
       sched_yield - yield the processor

synopsis
       #include <sched.h>
       int sched_yield(void);

description
       a process can relinquish the processor voluntarily without blocking by calling sched_yield().  the process will
       then be moved to the end of the queue for its static priority and a new process gets to run.

       note: if the current process is the only process in the highest priority list at that time, this process will
       continue to run after a call to sched_yield().

       posix systems on which sched_yield() is available define _posix_priority_scheduling in <unistd.h>.

--//对于select函数不是很理解.不过里面的时间单位是秒,微妙.
$ man select
name
       select, pselect, fd_clr, fd_isset, fd_set, fd_zero - synchronous i/o multiplexing

description
       select() and pselect() allow a program to monitor multiple file descriptors, waiting until one or more of the
       file descriptors become "ready" for some class of i/o operation (e.g., input possible).  a file descriptor is
       considered ready

       if it is possible to perform the corresponding i/o operation (e.g., read(2)) without blocking.

       the operation of select() and pselect() is identical, with three differences:

       (i)    select() uses a timeout that is a struct timeval (with seconds and microseconds), while pselect() uses a
       struct timespec (with seconds and nanoseconds).

       (ii)   select() may update the timeout argument to indicate how much time was left.  pselect() does not change
       this argument.

       (iii)  select() has no sigmask argument, and behaves as pselect() called with null sigmask.

       three independent sets of file descriptors are watched.  those listed in readfds will be watched to see if
       characters become available for reading (more precisely, to see if a read will not block; in particular,  a  file
       descriptor  is also  ready on end-of-file), those in writefds will be watched to see if a write will not block,
       and those in exceptfds will be watched for exceptions.  on exit, the sets are modified in place to indicate which
       file descriptors actually changed status.  each of the three file descriptor sets may be specified as null if no
       file descriptors are to be watched for the corresponding class of events.

       four macros are provided to manipulate the sets.  fd_zero() clears a set.  fd_set() and fd_clr() respectively add
       and remove a given file descriptor from a set.  fd_isset() tests to see if a file descriptor is part of the set;
       this  is  useful after select() returns.

       nfds is the highest-numbered file descriptor in any of the three sets, plus 1.

       timeout is an upper bound on the amount of time elapsed before select() returns. it may be zero, causing select()
       to return immediately. (this is useful for polling.) if timeout is null (no timeout), select() can block
       indefinitely.

       sigmask  is a pointer to a signal mask (see sigprocmask(2)); if it is not null, then pselect() first replaces the
       current signal mask by the one pointed to by sigmask, then does the 'select' function, and then restores the
       original signal mask.
...
the timeout
    the time structures involved are defined in <sys/time.h> and look like

      struct timeval {
          long    tv_sec;         /* seconds */
          long    tv_usec;        /* microseconds */
      };

--//修改_mutex_wait_scheme=1看看.
--//session 2:
sys@book> alter system set "_mutex_wait_scheme"=1 scope=memory;
system altered.

$ awk '{print $2}' /tmp/m3.txt  | uniq -c
$ awk '{print $2}' /tmp/m3.txt  | uniq -c | egrep "select|getrusage"
...
      2 getrusage(rusage_self,
      7 getrusage(rusage_self,
      1 getrusage(rusage_self,
   1387 select(0,
      2 getrusage(rusage_self,
   1684 select(0,
      2 getrusage(rusage_self,
   1675 select(0,
      2 getrusage(rusage_self,
   1635 select(0,
      2 getrusage(rusage_self,
   1615 select(0,
      2 getrusage(rusage_self,
   1617 select(0,
      2 getrusage(rusage_self,
   1616 select(0,
      2 getrusage(rusage_self,
   1615 select(0,
      2 getrusage(rusage_self,
   1616 select(0,
      2 getrusage(rusage_self,
   1619 select(0,
      2 getrusage(rusage_self,
   1611 select(0,
      2 getrusage(rusage_self,
    766 select(0,
      1 getrusage(rusage_self,
      4 getrusage(rusage_self,

--//截取一段:
17:06:45.648350 sched_yield()           = 0 <0.000037>
17:06:45.648570 select(0, [], [], [], {0, 1000}) = 0 (timeout) <0.001082>
17:06:45.649750 select(0, [], [], [], {0, 1000}) = 0 (timeout) <0.001115>
17:06:45.650979 select(0, [], [], [], {0, 1000}) = 0 (timeout) <0.001111>
...
17:06:47.290475 select(0, [], [], [], {0, 1000}) = 0 (timeout) <0.001099>
17:06:47.291645 select(0, [], [], [], {0, 1000}) = 0 (timeout) <0.001138>
17:06:47.292862 getrusage(rusage_self, {ru_utime={0, 61990}, ru_stime={0, 67989}, ...}) = 0 <0.000021>
17:06:47.292968 getrusage(rusage_self, {ru_utime={0, 61990}, ru_stime={0, 67989}, ...}) = 0 <0.000018>
17:06:47.293111 select(0, [], [], [], {0, 1000}) = 0 (timeout) <0.001095>

--//还原:
--//session 2:
sys@book> alter system set "_mutex_wait_scheme"=2 scope=memory;
system altered.

--//视乎_mutex_wait_scheme参数调整mutex的方式.

6.总结:
--//不小心写的太长.对于os,oracle等许多概念不是很清楚,做这探究难度太大...
--//网上找了一段资料:

* _mutex_spin_count (integer)
- this sets the number of times to spin before yielding/waiting.

* _mutex_wait_scheme (integer)
- in 11.2 this controls which wait scheme to use. it can be set to one

of the three wait schemes described above thus:
_mutex_wait_scheme = 0                        – always yield
_mutex_wait_scheme = 1 & _mutex_wait_time = t – always sleep for t milli-seconds (default)
_mutex_wait_scheme = 2 & _mutex_wait_time = t – exp backoff with maximum sleep

--//注:_mutex_wait_scheme = 2,我并没有看到指数回退-每次迭代我们都会睡得更多的时间的情况.

7.附上测试的脚本:
$ cat mutexy.sql
column kglnaown format a20
column mutex_type format a20
column kglnaobj format a100
column location format a30
  select * from (
  select kglnahsh hash
        ,sum (sleeps) sum_sleeps
        ,sum (gets) sum_gets
        ,location
        ,mutex_type
        ,mutex_addr
        ,kglobt03 sqlid
        ,kglnaown
        ,replace(kglnaobj,chr(13)) c100
    --,substr (kglnaobj, 1, 140) object
    from x$kglob, x$mutex_sleep_history
   where kglnahsh = mutex_identifier
group by kglnaobj
        ,kglobt03
        ,kglnaown
        ,kglnahsh
        ,location
        ,mutex_type
                ,mutex_addr
order by sum_sleeps desc ) where rownum<= &1;
--//查询x$mutex_sleep_history信息,主要v$mutex_sleep_history没有mutex_addr字段信息.

 $ cat fcha.sql
--------------------------------------------------------------------------------
--
-- file name:   fcha.sql (find chunk address) v0.2
-- purpose:     find in which heap (uga, pga or shared pool) a memory address resides
--
-- author:      tanel poder
-- copyright:   (c) http://blog.tanelpoder.com | @tanelpoder
--
-- usage:       @fcha <addr_hex>
--              @fcha f6a14448
--
-- other:       this would only report an uga/pga chunk address if it belongs
--              to *your* process/session (x$ksmup and x$ksmpp do not see other
--              session/process memory)
--
--------------------------------------------------------------------------------

prompt find in which heap (uga, pga or shared pool) the memory address &1 resides...
prompt
prompt warning!!! this script will query x$ksmsp, which will cause heavy shared pool latch contention
prompt in systems under load and with large shared pool. this may even completely hang
prompt your instance until the query has finished! you probably do not want to run this in production!
prompt
pause  press enter to continue, ctrl+c to cancel...


select
    'sga' loc,
    ksmchptr,
    ksmchidx,
    ksmchdur,
    ksmchcom,
    ksmchsiz,
    ksmchcls,
    ksmchtyp,
    ksmchpar
from
    x$ksmsp
where
    to_number(substr('&1', instr(lower('&1'), 'x')+1) ,'xxxxxxxxxxxxxxxx')
    between
        to_number(ksmchptr,'xxxxxxxxxxxxxxxx')
    and to_number(ksmchptr,'xxxxxxxxxxxxxxxx') + ksmchsiz - 1
union all
select
    'uga',
    ksmchptr,
    null,
    null,
    ksmchcom,
    ksmchsiz,
    ksmchcls,
    ksmchtyp,
    ksmchpar
from
    x$ksmup
where
    to_number(substr('&1', instr(lower('&1'), 'x')+1) ,'xxxxxxxxxxxxxxxx')
    between
        to_number(ksmchptr,'xxxxxxxxxxxxxxxx')
    and to_number(ksmchptr,'xxxxxxxxxxxxxxxx') + ksmchsiz - 1
union all
select
    'pga',
    ksmchptr,
    null,
    null,
    ksmchcom,
    ksmchsiz,
    ksmchcls,
    ksmchtyp,
    ksmchpar
from
    x$ksmpp
where
    to_number(substr('&1', instr(lower('&1'), 'x')+1) ,'xxxxxxxxxxxxxxxx')
    between
        to_number(ksmchptr,'xxxxxxxxxxxxxxxx')
    and to_number(ksmchptr,'xxxxxxxxxxxxxxxx') + ksmchsiz - 1
/

$ cat shp4.sql
column n0_6_16 format 99999999
select decode (kglhdadr,
               kglhdpar, '父游标句柄地址',
               '子游标句柄地址')
          text,
       kglhdadr,
       kglhdpar,
       substr(kglnaobj,1,40) c40,
           kglhdlmd,
           kglhdpmd,
           kglhdivc,
       kglobhd0,
       kglobhd6,
       kglobhs0,kglobhs6,kglobt16,
       kglobhs0+kglobhs6+kglobt16 n0_6_16,
           kglobhs0+kglobhs1+kglobhs2+kglobhs3+kglobhs4+kglobhs5+kglobhs6+kglobt16 n20,
           kglnahsh,
           kglobt03 ,
           kglobt09
  from x$kglob
 where kglobt03 = '&1'  or kglhdpar='&1' or kglhdadr='&1' or kglnahsh= &2;

$ cat spid.sql
column sid     new_value newsid
column serial#  new_value newserial
column spid    new_value newspid

set verify off
/* formatted on 2019/3/28 17:19:16 (qp5 v5.252.13127.32867) */
select s.sid
      ,s.serial#
      ,s.process
      ,s.server
      ,p.spid
      ,p.pid
      ,p.serial# p_serial#
      ,   'alter system kill session '''
       || s.sid
       || ','
       || s.serial#
       || ''''
       || ' immediate;'
          c50
  from v$session s, v$process p
 where     s.sid in (select sid from v$mystat where rownum = 1)
       and s.paddr = p.addr;

$ cat hide.sql
col name format a40
col description format a66
col session_value format a22
col default_value format a22
col system_value format a22

select
   a.ksppinm  name,
   a.ksppdesc description,
   b.ksppstdf default_value,
   b.ksppstvl session_value,
   c.ksppstvl system_value
from x$ksppi a, x$ksppcv b, x$ksppsv c
where a.indx = b.indx
 and a.indx = c.indx
 and lower(a.ksppinm) like lower('%&1%')
order by 1;

$ cat pt.sql
--http://orasql.org/2013/04/02/sqlplus-tips-2/
-- show output
set termout on
-- but without echo
set echo off
-- without newpage on start:
set embedded on
-- scrolling control
set pause on
-- two lines between rows:
set newpage 2
-- text for prompt after each page:
set pause "press enter to view next row..."
-- new page on new "row_num"
break on row_num skip page

-- main query:
select *
from
   xmltable( 'for $a at $i in /rowset/row
                 ,$r in $a/*
                   return element row{
                                     element row_num{$i}
                                    ,element col_name{$r/name()}
                                    ,element col_value{$r/text()}
                                    }'
             passing xmltype(cursor( &1 ))
             columns
                row_num   int
--               ,col_name  varchar2(30)
               ,col_name  varchar2(30)
               ,col_value varchar2(100)
      );
-- disabling pause and breaks:
set pause off
clear breaks