[20190402]Library Cache mutex.txt

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
column a noprint new_value v_a;
--select mod ( &&3 ,3) a  from dual ;
--alter session set optimizer_index_cost_adj= &&3;
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

--//执行许多次,就可以在 x$mutex_sleep_history有记录,出现MUTEX_TYPE='Library Cache'的情况
$ seq 150 | xargs -I {}  -P 150 bash -c  "sqlplus -s -l scott/book @m2.txt 1e6 f2_150 {} >/dev/null"

SYS@book> @ mutexy 16 a31kd5tkdvvmm
      HASH SUM_SLEEPS   SUM_GETS LOCATION                       MUTEX_TYPE           MUTEX_ADDR       SQLID         KGLNAOWN C100
---------- ---------- ---------- ------------------------------ -------------------- ---------------- ------------- -------- ---------------------------------------
1692266099  161187946  600188278 kksfbc [KKSCHLPIN1]            Cursor Pin           000000007C88E330 a31kd5tkdvvmm          SELECT DEPTNO FROM DEPT WHERE DEPTNO=10
1692266099    9215322 1.8927E+11 kksLockDelete [KKSCHLPIN6]     Cursor Pin           000000007C88E330 a31kd5tkdvvmm          SELECT DEPTNO FROM DEPT WHERE DEPTNO=10
1692266099    5019588 1.0546E+11 kksfbc [KKSCHLFSP2]            Cursor Pin           000000007C88E330 a31kd5tkdvvmm          SELECT DEPTNO FROM DEPT WHERE DEPTNO=10
1692266099    1351526   63672414 kgllkc1   57                   Library Cache        000000007C957128 a31kd5tkdvvmm          SELECT DEPTNO FROM DEPT WHERE DEPTNO=10
1692266099     499588    9089718 kkslce [KKSCHLPIN2]            Cursor Pin           000000007C957128 a31kd5tkdvvmm          SELECT DEPTNO FROM DEPT WHERE DEPTNO=10
1692266099     212212    3838112 kglGetHandleReference 124      Library Cache        000000007C957128 a31kd5tkdvvmm          SELECT DEPTNO FROM DEPT WHERE DEPTNO=10
1692266099     131248    1919076 kgllkdl1  85                   Library Cache        000000007C957128 a31kd5tkdvvmm          SELECT DEPTNO FROM DEPT WHERE DEPTNO=10
1692266099      10118    1814754 kglhdgn2 106                   Library Cache        000000007C957128 a31kd5tkdvvmm          SELECT DEPTNO FROM DEPT WHERE DEPTNO=10
8 rows selected.

--//探究MUTEX_TYPE='Library Cache',MUTEX_ADDR='000000007C957128'在哪里,还有1个LOCATION问题?
--//我执行许多次才出现这样的情况.

2.分析:

SYS@book> @ fcha 000000007C957128
Find in which heap (UGA, PGA or Shared Pool) the memory address 000000007C957128 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...

LOC KSMCHPTR           KSMCHIDX   KSMCHDUR KSMCHCOM           KSMCHSIZ KSMCHCLS   KSMCHTYP KSMCHPAR
--- ---------------- ---------- ---------- ---------------- ---------- -------- ---------- ----------------
SGA 000000007C956FB8          1          1 KGLHD                   560 recr             80 00

--//执行如下,也可以获得类似结果:
SELECT * FROM x$ksmsp WHERE TO_NUMBER ('000000007C957128', 'xxxxxxxxxxxxxxxx') between TO_NUMBER(KSMCHPTR, 'xxxxxxxxxxxxxxxx') and TO_NUMBER(KSMCHPTR, 'xxxxxxxxxxxxxxxx')+KSMCHSIZ -1;
ADDR                   INDX    INST_ID   KSMCHIDX   KSMCHDUR KSMCHCOM         KSMCHPTR           KSMCHSIZ KSMCHCLS   KSMCHTYP KSMCHPAR
---------------- ---------- ---------- ---------- ---------- ---------------- ---------------- ---------- -------- ---------- ----------------
00007F3CEDE1A5B8      16459          1          1          1 KGLHD            000000007C956FB8        560 recr             80 00

SYS@book> @ sharepool/shp4 a31kd5tkdvvmm 0
TEXT           KGLHDADR         KGLHDPAR         C40                                        KGLHDLMD   KGLHDPMD   KGLHDIVC KGLOBHD0         KGLOBHD6           KGLOBHS0   KGLOBHS6   KGLOBT16   N0_6_16        N20   KGLNAHSH KGLOBT03        KGLOBT09
-------------- ---------------- ---------------- ---------------------------------------- ---------- ---------- ---------- ---------------- ---------------- ---------- ---------- ---------- --------- ---------- ---------- ------------- ----------
子游标句柄地址 000000007C7F8B90 000000007C956FE8 SELECT DEPTNO FROM DEPT WHERE DEPTNO=10           0          0          0 000000007C4468D8 000000007C88E3D8       4528       8088       3072     15688      15688 1692266099 a31kd5tkdvvmm          0
父游标句柄地址 000000007C956FE8 000000007C956FE8 SELECT DEPTNO FROM DEPT WHERE DEPTNO=10           0          0          0 000000007CA7BFE8 00                     4720          0          0      4720       4720 1692266099 a31kd5tkdvvmm      65535
--//可以发现MUTEX_ADDR='000000007C957128'的地址与父游标句柄的地址比较接近.实际上从KSMCHCOM=KGLHD也可以确定,

SYS@book> @ fcha 000000007C956FE8
Find in which heap (UGA, PGA or Shared Pool) the memory address 000000007C956FE8 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...

LOC KSMCHPTR           KSMCHIDX   KSMCHDUR KSMCHCOM           KSMCHSIZ KSMCHCLS   KSMCHTYP KSMCHPAR
--- ---------------- ---------- ---------- ---------------- ---------- -------- ---------- ----------------
SGA 000000007C956FB8          1          1 KGLHD                   560 recr             80 00

SELECT * FROM x$ksmsp WHERE TO_NUMBER ('000000007C956FE8', 'xxxxxxxxxxxxxxxx') between TO_NUMBER(KSMCHPTR, 'xxxxxxxxxxxxxxxx') and TO_NUMBER(KSMCHPTR, 'xxxxxxxxxxxxxxxx')+KSMCHSIZ-1;
ADDR                   INDX    INST_ID   KSMCHIDX   KSMCHDUR KSMCHCOM         KSMCHPTR           KSMCHSIZ KSMCHCLS   KSMCHTYP KSMCHPAR
---------------- ---------- ---------- ---------- ---------- ---------------- ---------------- ---------- -------- ---------- ----------------
00007F3CEDDFBC00      12611          1          1          1 KGLHD            000000007C956FB8        560 recr             80 00

--//library cache mutex结构体在父游标句柄地址里面.
--//0x7C957128=2090168616 0x7C956FB8=2090168248 2090168616-2090168248 = 368,在父游标句柄地址偏移368字节处.

3.知道地址就可以模拟问题产生:
--//session 1:
SCOTT@book> @ spid

SID    SERIAL# PROCESS                  SERVER    SPID       PID  P_SERIAL# C50
---------- ---------- ------------------------ --------- ------ ------- ---------- --------------------------------------------------
        32         35 10708                    DEDICATED 10709       26         84 alter system kill session '32,35' immediate;
--//32=0x20

--//session 2:
SYS@book> oradebug setmypid
Statement processed.

SYS@book> oradebug peek 0x000000007C957128 8
[07C957128, 07C957130) = 00000000 00000000

SYS@book> oradebug poke 0x000000007C957128 8 0x0000004400000020
BEFORE: [07C957128, 07C957130) = 00000000 00000000
AFTER:  [07C957128, 07C957130) = 00000020 00000044

--//session 1:
SCOTT@book> @ m2.txt 1 c1 0
1 row created.
Commit complete.
--//挂起!!注第一次执行会挂起,第2次就不会了.

--//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 0000004400000020 0000000000000039 1692266099 2.9206E+11         57         32         33         38 library cache: mutex X ACTIVE   WAITING        35878972              36 Concurrency
--//出现library cache: mutex X 等待.

SYS@book> oradebug poke 0x000000007C957128 8 0x0
BEFORE: [07C957128, 07C957130) = 00000020 00000044
AFTER:  [07C957128, 07C957130) = 00000000 00000000

--//再次执行,因为该光标已经cache. PL/sql语句中的sql语句与sqlplus执行的sql语句方式不同.

SYS@book> select * from v$open_cursor where sql_id='a31kd5tkdvvmm';
SADDR             SID USER_NAME ADDRESS          HASH_VALUE SQL_ID        SQL_TEXT                                LAST_SQL_ACTIVE_TIM SQL_EXEC_ID CURSOR_TYPE
---------------- ---- --------- ---------------- ---------- ------------- --------------------------------------- ------------------- ----------- --------------------
000000008635DE10   32 SCOTT     000000007C956FE8 1692266099 a31kd5tkdvvmm SELECT DEPTNO FROM DEPT WHERE DEPTNO=10                                 PL/SQL CURSOR CACHED
--//第一次执行就cache了.第2次执行不会在library cache上出现阻塞.

SYS@book> oradebug poke 0x000000007C957128 8 0x0000004400000020
BEFORE: [07C957128, 07C957130) = 00000000 00000000
AFTER:  [07C957128, 07C957130) = 00000020 00000044

SCOTT@book> @ m2.txt 1 c1 0
1 row created.
Commit complete.
PL/SQL procedure successfully completed.
11 rows updated.
Commit complete.

--//可以正常执行.但是退出会出现问题.
SCOTT@book> quit

--//挂起!!
SYS@book> @ wait
P1RAW            P2RAW            P3RAW                    P1         P2         P3        SID    SERIAL#       SEQ# EVENT                  STATUS   STATE   WAIT_TIME_MICRO SECONDS_IN_WAIT WAIT_CLASS
---------------- ---------------- ---------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------------------- -------- ------- --------------- --------------- -----------
0000000064DDEE73 0000004400000020 0000000000000055 1692266099 2.9206E+11         85         32         35         57 library cache: mutex X ACTIVE   WAITING        17103974              17 Concurrency

--//还有1个情况是阻塞无法查询x$mutex_sleep_history视图,
SYS@book> @ mutexy 6 a31kd5tkdvvmm
--//挂起!!取消后正常!!
SYS@book> oradebug poke 0x000000007C957128 8 0x00
BEFORE: [07C957128, 07C957130) = 00000020 00000044
AFTER:  [07C957128, 07C957130) = 00000000 00000000
----

HASH SUM_SLEEPS   SUM_GETS LOCATION                       MUTEX_TYPE           MUTEX_ADDR       SQLID         KGLNAOWN C100
---------- ---------- ---------- ------------------------------ -------------------- ---------------- ------------- -------- ---------------------------------------
1692266099  161187946  600188278 kksfbc [KKSCHLPIN1]            Cursor Pin           000000007C88E330 a31kd5tkdvvmm          SELECT DEPTNO FROM DEPT WHERE DEPTNO=10
1692266099    9215322 1.8927E+11 kksLockDelete [KKSCHLPIN6]     Cursor Pin           000000007C88E330 a31kd5tkdvvmm          SELECT DEPTNO FROM DEPT WHERE DEPTNO=10
1692266099    5019588 1.0546E+11 kksfbc [KKSCHLFSP2]            Cursor Pin           000000007C88E330 a31kd5tkdvvmm          SELECT DEPTNO FROM DEPT WHERE DEPTNO=10
1692266099    1351526   63672414 kgllkc1   57                   Library Cache        000000007C957128 a31kd5tkdvvmm          SELECT DEPTNO FROM DEPT WHERE DEPTNO=10
1692266099     499588    9089718 kkslce [KKSCHLPIN2]            Cursor Pin           000000007C957128 a31kd5tkdvvmm          SELECT DEPTNO FROM DEPT WHERE DEPTNO=10
1692266099     212212    3838112 kglGetHandleReference 124      Library Cache        000000007C957128 a31kd5tkdvvmm          SELECT DEPTNO FROM DEPT WHERE DEPTNO=10
6 rows selected.

--//感觉在11g下出现Library Cache的情况性很小.library cache主要出现在sql语句登录第1-3次执行时需要通过哈希定位检查父游标是
--//否存在,以及是否要硬解析的情况,如果cache就不再需要了,不同会话相同语句同时执行的可能性在这里发生阻塞的可能性很小,更容
--//易出现cursor: pin S.(我的测试也说明这个问题),加上session_cached_cursors作用,更难在这里出现阻塞.
--//当然一些应用开始执行的语句可能都是一样的,可能密集的登录时偶尔出现不会成为主要矛盾.
--//不像10g,采用是latch caceh latch,而且还有数量限制.而11g是采用mutex,library cache mutex的结构体在生成的父游标句柄里面,
--//等于打散到共享池的许多地方,出现碰撞的概率降低了.

4.附上相关脚本:
$ cat tpt/cha
channels2.sql  channels3.sql  channels.sql
[oracle@gxqyydg4 IP=100.78 ~/sqllaji ] $ cat tpt/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 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
         and kglobt03= decode('&&2','',kglobt03,lower('&&2'))
GROUP BY kglnaobj
        ,kglobt03
        ,kglnaown
        ,kglnahsh
        ,location
        ,mutex_type
                ,MUTEX_ADDR
ORDER BY sum_sleeps DESC ) where rownum<= &1;

$ cat sharepool/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;

[20190402]Library Cache mutex.txt的更多相关文章

  1. Troubleshooting 'library cache: mutex X' Waits.

    What is a 'library cache: mutex X' wait? The mutex feature is a mechanism to control access to in me ...

  2. library cache: mutex X

    我们先来看看 library cache: mutex X . 是个什么东西 The library cache mutex is acquired for similar purposes that ...

  3. Troubleshooting 'library cache: mutex X' Waits. (Doc ID 1357946.1)

    In this Document   Purpose   Troubleshooting Steps   What is a 'library cache: mutex X' wait?   What ...

  4. 11g等待事件之library cache: mutex X

    11g等待事件之library cache: mutex X 作者: dbafree 日期: 2012 年 07 月 01 日发表评论 (0)查看评论   library cache: mutex X ...

  5. Oracle数据库大量library cache: mutex X及latch: shared pool问题排查一例

    业务系统数据库夯住,数据库内大量的library cache: mutex X及latch: shared pool等待,alert日志信息如下 Tue Sep :: WARNING: inbound ...

  6. [20190319]shared pool latch与library cache latch的简单探究.txt

    [20190319]shared pool latch与library cache latch的简单探究.txt --//昨天看Oracle DBA手记3:数据库性能优化与内部原理解析.pdf 电子书 ...

  7. Using dbms_shared_pool.purge to remove a single task from the library cache

    我们都知道可是使用 alter system flush shared_pool 来清除shared pool 信息,当时不能指定清除某个对象.因为在系统繁忙的时侯 使用 alter system f ...

  8. 共享池之八:软解析、硬解析、软软解析 详解一条SQL在library cache中解析涉及的锁

    先来张大图: 结合上图来说明一下解析的各个步骤涉及的锁. 软解析.硬解析.软软解析区别的简单说明: 为了将用户写的sql文本转化为oracle认识的且可执行的语句,这个过程就叫做解析过程. 解析分为硬 ...

  9. 共享池之六:shared pool latch/ library cache latch /lock pin 简介

    latch:library cache --desc v$librarycache; latch:library cache用于保护hash bucket.library cache lock保护HA ...

随机推荐

  1. 实现网站页面的QQ临时会话,分享到空间微博等按钮.

    一 qq临时会话 要实现qq临时会话首先要到qq在线状态官网开通qq在线状态,其中临时对话也分为加密和未加密. 1.1:加密模式 <a target="_blank" hre ...

  2. Python2中文处理纪要

    python2不是以unicode作为基本代码字符类型,碰到乱码的几率是远远高于python3,但即便如此,相信很多人,也不想随意的迁移到python3,这里就总结几个我平常碰到的问题及解法. 文件中 ...

  3. Linux系统的数据写入机制--延迟写入

    我们都知道,在Linux关机的之前都会要运行一个命令那就是sync,这个命令是同步的意思,那为什么要运行这个?而且之前的数据改变我们已经看见了,为什么还要运行这个命令?要回答这个问题就要说一下Linu ...

  4. MySQL 隔离级别

    一.事务特性 1.原子性 事务是一个原子操作单元,事务中包含的所有操作要么都做,要么都不做,没有第三种情况. 2.一致性 事务操作前和操作后都必须满足业务规则约束,比如说A向B转账,转账前和转账后AB ...

  5. 初学Java Web(8)——过滤器和监听器

    什么是过滤器 过滤器就是 Servlet 的高级特性之一,就是一个具有拦截/过滤功能的一个东西,在生活中过滤器可以是香烟滤嘴,滤纸,净水器,空气净化器等,在 Web 中仅仅是一个实现了 Filter ...

  6. 【c#】RabbitMQ学习文档(五)Topic(主题。通配符模式)

    (本实例都是使用的Net的客户端,使用C#编写),说明,中文方括号[]表示名词. 在上一个教程中,我们改进了我们的日志记录系统. 没有使用只能够进行虚拟广播的[Fanout]交换机,而是使用了[Dir ...

  7. gcc编译基本用法~2

    编译简单的 C 程序 C 语言经典的入门例子是 Hello World,下面是一示例代码: ;} 我们假定该代码存为文件‘hello.c’.要用 gcc 编译该文件,使用下面的命令: $ gcc -g ...

  8. ES6 系列之 Babel 是如何编译 Class 的(下)

    前言 ES5 寄生组合式继承 function Parent (name) { this.name = name; } Parent.prototype.getName = function () { ...

  9. .Net语言 APP开发平台——Smobiler学习日志:HoverBackColor属性的简单介绍

    一.属性介绍 设置按钮按下时的背景色,默认设置为“Gray” 二.举例介绍 以Button控件为例 mobiler窗体设计界面见图1 将该属性设置为“255, 255, 128”,表示RGB颜色,属性 ...

  10. java开发环境配置——Maven

    前篇讲了jdk的安装,这篇讲一下包管理工具Maven,Maven主要是用来统一管理项目引用的jar包,还有用来打包的. Maven官网下载地址:http://maven.apache.org/down ...