library cache lock和library cache bin实验_2.0

library cache lock和library cache bin实验

1. 首先执行calling过程,在calling过程中调用pining过程
SQL> create or replace PROCEDURE pining IS
  2  BEGIN
  3    NULL;
  4  END;
  5  /

Procedure created.

SQL>
SQL> create or replace procedure calling is
  2  begin
  3    pining;
  4    dbms_lock.sleep(3000);
  5  end;
  6  /

Procedure created.

SQL> select sid from v$mystat where rownum<2;

       SID
----------
        41

SQL> grant all on dbms_lock to scott;

Grant succeeded.

SQL> call calling();

hang.....................

2.session 2执行
SQL> select sid from v$mystat where rownum<2;

       SID
----------
        38
SQL>    
SQL> alter procedure pining compile;   

hang.......................

3.session 3
SQL> select sid from v$mystat where rownum<2;

       SID
----------
        46

SQL> drop procedure pining;  

hang.......................

4.查询
SQL> select event#,name,parameter1,parameter2,parameter3,wait_class#,wait_class from v$event_name where name like 'library cache%';

    EVENT# NAME                           PARAMETER1      PARAMETER2      PARAMETER3                WAIT_CLASS# WAIT_CLASS
---------- ------------------------------ --------------- --------------- ------------------------- ----------- -------------------------
       286 library cache pin              handle address  pin address     100*mode+namespace                  4 Concurrency
       287 library cache lock             handle address  lock address    100*mode+namespace                  4 Concurrency
       288 library cache load lock        object address  lock address    100*mask+namespace                  4 Concurrency
       289 library cache: mutex X         idn             value           where                               4 Concurrency
       290 library cache: mutex S         idn             value           where                               4 Concurrency
      1040 library cache revalidation                                                                         0 Other
      1041 library cache shutdown                                                                             0 Other

7 rows selected.

SQL>   

SQL> select s.sid,s.sql_id,s.status,s.username,s.event,s.blocking_session,s.last_call_et,s.p1,s.p1raw,s.p2,s.p2raw,s.p3,s.p3raw from v$session s where s.EVENT like 'library%';

       SID SQL_ID        STATUS   USERNAME   EVENT                BLOCKING_SESSION LAST_CALL_ET         P1 P1RAW                    P2 P2RAW                    P3 P3RAW
---------- ------------- -------- ---------- -------------------- ---------------- ------------ ---------- ---------------- ---------- ---------------- ---------- ----------------
        38 bvy6nfztw6b8u ACTIVE   SYS        library cache lock                 46          257 1915540152 00000000722CD2B8 1913174216 000000007208B8C8 3.8633E+14 00015F5E00010003
        46 az5qprppsq5fa ACTIVE   SYS        library cache pin                  41         1223 1915540152 00000000722CD2B8 1914076336 0000000072167CB0 3.8633E+14 00015F5E00010003

P1 列是Library Cache Handle Address,Pn字段是10进制表示,PnRaw字段是16进制表示


排查:
我们看到,library cache pin等待的对象的handle地址为:00000000722CD2B8
通过这个地址,我们查询X$KGLOB视图就可以得到对象的具体信息:
Note: X$KGLOB--[K]ernel [G]eneric [L]ibrary Cache Manager [OB]ject
col KGLNAOWN for a10
col KGLNAOBJ for a20
select ADDR,KGLHDADR,KGLHDPAR,KGLNAOWN,KGLNAOBJ,KGLNAHSH,KGLHDOBJ
from X$KGLOB
where KGLHDADR ='00000000722CD2B8';

SQL> col KGLNAOWN for a10
SQL> col KGLNAOBJ for a20
SQL> select ADDR,KGLHDADR,KGLHDPAR,KGLNAOWN,KGLNAOBJ,KGLNAHSH,KGLHDOBJ
  2  from X$KGLOB
  3  where KGLHDADR ='00000000722CD2B8';

ADDR             KGLHDADR         KGLHDPAR         KGLNAOWN   KGLNAOBJ               KGLNAHSH KGLHDOBJ
---------------- ---------------- ---------------- ---------- -------------------- ---------- ----------------
00007FBEE017CC88 00000000722CD2B8 00000000722CD2B8 SYS        PINING               1587262593 0000000074304078

这里KGLNAHSH代表该对象的Hash Value
由此我们知道,在PINING对象上正经历library cache pin的等待.
然后我们引入另外一个内部视图X$KGLPN:
Note:X$KGLPN--[K]ernel [G]eneric [L]ibrary Cache Manager object [P]i[N]s
                
select a.sid,a.username,a.program,b.addr,b.KGLPNADR,b.KGLPNUSE,b.KGLPNSES,b.KGLPNHDL,
b.kGLPNLCK, b.KGLPNMOD, b.KGLPNREQ
from v$session a,x$kglpn b
where a.saddr=b.kglpnuse and b.kglpnhdl = '00000000722CD2B8' and b.KGLPNMOD<>0;

SQL> select a.sid,a.username,a.program,b.addr,b.KGLPNADR,b.KGLPNUSE,b.KGLPNSES,b.KGLPNHDL,
  2  b.kGLPNLCK, b.KGLPNMOD, b.KGLPNREQ
  3  from v$session a,x$kglpn b
  4  where a.saddr=b.kglpnuse and b.kglpnhdl = '00000000722CD2B8' and b.KGLPNMOD<>0;

       SID USERNAME   PROGRAM                                          ADDR             KGLPNADR         KGLPNUSE         KGLPNSES         KGLPNHDL         KGLPNLCK         KGLPNMOD   KGLPNREQ
---------- ---------- ------------------------------------------------ ---------------- ---------------- ---------------- ---------------- ---------------- ---------------- ---------- ----------
        41 SYS        sqlplus@wang (TNS V1-V3)                         00007FBEE0335460 0000000072124DE0 0000000090E6F3C0 0000000090E6F3C0 00000000722CD2B8 00000000721253E0         2          0

通过联合v$session,可以获得当前持有该handle的用户信息,对于我们的测试sid=41的用户正持有该handle:
SQL> select s.inst_id,s.sid,s.serial#,s.sql_id,s.status,s.username,s.event,s.blocking_session,s.WAIT_TIME,s.SECONDS_IN_WAIT,s.LAST_CALL_ET from gv$session s where s.sid=41;

   INST_ID        SID    SERIAL# SQL_ID        STATUS   USERNAME   EVENT                BLOCKING_SESSION  WAIT_TIME SECONDS_IN_WAIT LAST_CALL_ET
---------- ---------- ---------- ------------- -------- ---------- -------------------- ---------------- ---------- --------------- ------------
         1         41        363 1shkx2jasndx8 ACTIVE   SYS        PL/SQL lock timer                              0            1733         1733

接着查询等待hold_sid 的sql,如下:
SQL> select sql_id,sql_text from v$sql where sql_id='1shkx2jasndx8';

SQL_ID        SQL_TEXT
------------- ----------------------------------------
1shkx2jasndx8 call calling()

这里我们得到这个用户正在执行calling这个存储过程,接下来的工作就应该去检查calling在作什么了.我们这个calling作的工作是dbms_lock.sleep(3000),这也就是PL/SQL lock timer正在等待的原因,至此就找到了Library Cache Pin的原因.


简化查询步骤:
获得library cache pin 等待对象的信息:
select addr, kglhdadr, kglhdpar, kglnaown, kglnaobj, kglnahsh, kglhdobj
  from x$kglob
 where kglhdadr in
       (select p1raw from v$session_wait where event like 'library%')

获得持有等待对象的session信息:
select a.PADDR,
       a.sid,
       a.SERIAL#,
       a.PROGRAM,
       a.SQL_ADDRESS,
       a.STATUS,
       a.SQL_HASH_VALUE,
       b.addr,
       b.kglpnadr,
       b.kglpnuse,
       b.kglpnses,
       b.kglpnhdl,
       b.kglpnlck,
       b.kglpnmod,
       b.kglpnreq
  from x$kglpn b, v$session a
 where a.SADDR = b.kglpnuse
   and b.kglpnmod <> 0
   and b.kglpnhdl in (select p1raw from v$session_wait where event like 'library%');


获得持有等待对象的会话执行的代码:
select *
  from v$sqltext
 where (address, hash_value) in
       (select sql_address, sql_hash_value
          from v$session
         where sid in (select a.SID
                         from x$kglpn b, v$session a
                        where a.SADDR = b.kglpnuse
                          and b.kglpnmod <> 0
                          and b.kglpnhdl in
                              (select p1raw
                                 from v$session_wait
                                where event like 'library%')))
 order by piece;
 
总结可用如下sql查询:
select  a.event,
         a.sid,
         a.SERIAL#,
         a.username,
         a.machine,
         a.wait_time,
         a.seconds_in_wait,
         a.state,
         a.blocking_session,
         p.kglpncnt,
         p.kglpnmod,
         p.kglpnreq,
         b.kglnaown,
         b.kglnaobj,
         b.kglfnobj,
         b.kglhdobj
    from v$session a, x$kglpn p, x$kglob b
   where p.kglpnhdl in (select kglpnhdl  from x$kglpn where kglpnreq <>0)
      and p.kglpnhdl=b.kglhdadr
      and a.P1RAW=rawtohex(p.kglpnhdl)
order by seconds_in_wait desc;

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/31397003/viewspace-2156482/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/31397003/viewspace-2156482/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值