zoukankan      html  css  js  c++  java
  • (转载)library cache lock和library cache pin到底是什么

    (http://www.dbsnake.net/library-cache-lock-and-pin.html)

    可能有很多朋友从来就没有搞清楚过到底什么是library cache lock和library cache pin,它们到底是enqueue还是latch?它们的作用是什么?

    这里我尝试对上述问题做一番解释,这些解释可能是有问题的,因为里面包含了我的一些猜测。

    最近连续写了一些基于我的猜测、没有确凿证据的文章,这也许不太合适。

    我们通常说的library cache locklibrary cache pinenqueue,不是latch,它们是两种DDL lock。但需要我们注意的是,在11gR1之前,Oracle中又存在名为library cache locklibrary cache pinlatch

    是不是感觉很混乱?没关系,我们一点一点往下看。很抱歉,我这里引用了大量英文,因为我觉得如果翻译出来就失去了原先的味道。

    1、  作为enqueuelibrary cache locklibrary cache pin的作用是什么?

    Both library cache lock and library cache pin are provided to access objects in the library cache.Library cache lock manages concurrency between processes, whereas library cache pin manages cache coherence. In order to access an object in library cache, a process must first lock the library cache object handle, and then pin the object data heap itself. Requests for both library cache lock and library cache pin will wait until granted. This is a possible source of contention, because there is no NOWAIT request mode.

    By acquiring a library cache lock on the library cache object handle, a process can prevent other processes from accessing the object, or even finding out what type it is. It can even maintain a dependency on an object without preventing other processes from accessing the object. Acquiring a library cache lock is also the only way to locate an object in cache—a process locates and locks an object in a single operation.

    If the process wants to actually examine or modify the object, then it must acquire a library cache pin on the object data heap itself (after acquiring a library cache lock on the library cache object handle).Pinning the object causes information about the object and its data heaps to be loaded into memory if they were not already there. This information is guaranteed to remain in memory at least until the pin is released. Locks and pins are externalized in X$KGLLK and X$KGLPN, respectively.

    2、  作为enqueuelibrary cache locklibrary cache pin有哪几种lock mode

    a)         Library cache lock有三种lock mode,分别是share、exclusive和null。A process acquires a share library cache lock if it intends only to read the object. For example, it wants to reference the object during compilation. A process acquires an exclusive library cache lock if it intends to create or modify the object. For example, it wants to drop the object from the database. Null library cache locks are a special case. They are acquired on objects that are to be executed like child cursor, procedure, function, package, or type body. You can use them to maintain an interest on an object for a long period of time (session persistency), and to detect if the object becomes invalid. You can break null library cache lock at any time. This is used as a mechanism to notify a session that an executable object is no longer valid. If a null library cache lock is broken, and thus the object is invalidated, then it is an indication to the user who was holding the null library cache lock that the object needs to be recompiled. A null library cache lock is acquired during the parse phase of SQL statement execution and is held as long as the shared SQL area for that statement remains in the shared pool. A null library cache lock does not prevent any DDL operation, and can be broken to allow conflicting DDL operations, hence the term “breakable parse lock.” A null library cache lock on an object is broken when there is an exclusive library cache pin on the object.

    b)         Library cache pin有两种lock mode,分别是share和exclusive。 When a process pins an object data heap that is not in memory, the process can determine whether the data heap is to be loaded in the PGA or SGA. An object must be pinned in Exclusive mode if it is to be modified. However, the process first will always pin the object in Share mode, examine it for errors and security checks, and then, if necessary, (such as needing modification) pin it in Exclusive mode. An object is never pinned in Exclusive mode if only read access is required.This is because all dependent transient objects (cursors) are invalidated (null locks broken) when an object is unpinned from Exclusive mode. The effect would be unnecessary recompilation and reparsing of all dependent packages, procedures, and functions.

    3、  作为latchlibrary cache locklibrary cache pin的作用是什么?

    这是一个很纠结的问题,既然已经有了作为enqueue的library cache lock和library cache pin,为什么在11gR1以前,Oracle里还有同名latch,而且明显这些同名latch是在被使用:

    Connected to Oracle Database 10g Enterprise Edition Release 10.2.0.5.0

    Connected as ipra

    SQL> select name,level#,gets,misses,sleeps,immediate_gets,immediate_misses from v$latch where name like ‘library%’;

    NAME                                                   LEVEL#       GETS     MISSES     SLEEPS IMMEDIATE_GETS IMMEDIATE_MISSES

    ————————————————– ———- ———- ———- ———- ————– —————-

    library cache                                               5    9221760       1608        800           2596           76766

    library cache lock                                          6   13548247        582          6              0                0

    library cache lock allocation                               3     208273          0          0              0                0

    library cache pin                                           6    4207462        193          0              2                0

    library cache hash chains                                   9          0          0          0              0                0

    library cache pin allocation                                3      57276          0          0              0                0

    library cache load lock                                     5      24848          0          0              1                0

    7 rows selected

    从结果里我们可以看到,对于10.2.0.5而言,Oracle存在7种跟library cache相关的latch,除了library cache hash chains latch之外,其他的跟library cache相关的latch,Oracle都有使用。

    那么library cache lock latch、library cache pin latch以及大家最耳熟能详的library cache latch等等,这些latch是做什么用的呢?

    也许我们可以从下面的一段文字中找到答案:

    The library cache latches serialize access to the objects in the library cache. Access to library cache objects always occurs through library cache locks. Because locking an object is not an atomic instruction, a library cache latch is acquired before the library cache lock request and is released after it. For most operations, the library cache latches are used, and therefore they can become a point of contention.

    If an object is not in memory, then a library cache lock cannot be acquired on it. In order to prevent multiple processes to request the load of the same object simultaneously, another latch must be acquired before the load request. This is the library cache load lock latch. The library cache load lock latch is taken and held until a library cache load lock is allocated, then the latch is released. Loading of the object is performed under the library cache load lock and not under the library cache load lock latch as it may take quite a long time.

    这里提到了几点值得我们关注:

    a)        Oracle使用上述library cache latches(包括library cache latch、library cache lock latch、library cache pin latch、library cache pin allocation latch、library cache load lock latch)的目的是控制并发访问library cache object所需要的相关的enqueue或者是为了控制并发访问library cache中的相关的内存结构,比如用相关的library cache lock latch控制并发获得library cache lock。这里我猜测Oraclelibrary cache lock latch控制并发获得library cache lock,用library cache pin latch控制并发获得library cache pin,用library cache load lock latch控制并发获得library cache load locklibrary cache latch去控制并发访问library cache object handle中的某些结构,如library cache object handle中的flag中的special status flag (special status flags are protected by the library cache latch. Examples of these flags indicate that: The object is valid; The object is authorized; The object has compilation errors)。

    b)         Library cache load lock是另外一种enqueue。The session tries to find the library cache load lock for the database object so that it can load the object. The library cache load lock is always obtained in Exclusive mode, so that no other process can load the same object. If the library cache load lock is busy the session will wait on this event until the lock becomes available.

    好了,现在我们来验证一下,还是上述10.2.0.5的环境,我将上述sql(select name,level#,gets,misses,sleeps,immediate_gets,immediate_misses from v$latch where name like ‘library%’)马上再执行一遍,这是软解析,必然要获得library cache lock,不需要获得library cache load lock,所以对应的latch应该表现为library cache lock latch的gets增加,library cache load lock latch的gets不变:

    SQL> select name,level#,gets,misses,sleeps,immediate_gets,immediate_misses from v$latch where name like ‘library%’;

    NAME                                                   LEVEL#       GETS     MISSES     SLEEPS IMMEDIATE_GETS IMMEDIATE_MISSES

    ————————————————– ———- ———- ———- ———- ————– —————-

    library cache                                               5    9222166       1608        800           2596           76766

    library cache lock                                          6   13548760        582          6              0                0

    library cache lock allocation                               3     208287          0          0              0                0

    library cache pin                                           6    4207656        193          0              2                0

    library cache hash chains                                   9          0          0          0              0                0

    library cache pin allocation                                3      57278          0          0              0                0

    library cache load lock                                     5      24848          0          0              1                0

    7 rows selected

    从结果里我们可以看到,library cache lock latch的gets从13548247递增到了13548760,library cache pin latch的gets从4207462递增到了4207656,但library cache load lock latch的gets还是保持24848不变。

    现在我们来让library cache load lock latch的gets发生变化,这是非常容易的事情,我们只需要执行一个需要硬解析的sql就可以了:

    SQL> select * from scott.emp_temp;

    EMPNO ENAME      JOB         MGR HIREDATE          SAL      COMM DEPTNO  ISINSPECT

    —– ———- ——— —– ———– ——— ——— —— ———-

    SQL> select name,level#,gets,misses,sleeps,immediate_gets,immediate_misses from v$latch where name like ‘library%’;

    NAME                                                   LEVEL#       GETS     MISSES     SLEEPS IMMEDIATE_GETS IMMEDIATE_MISSES

    ————————————————– ———- ———- ———- ———- ————– —————-

    library cache                                               5    9223549       1608        800           2596           76766

    library cache lock                                          6   13550296        582          6              0                0

    library cache lock allocation                               3     208348          0          0              0                0

    library cache pin                                           6    4208118        193          0              2                0

    library cache hash chains                                   9          0          0          0              0                0

    library cache pin allocation                                3      57294          0          0              0                0

    library cache load lock                                     5      24856          0          0              1                0

    7 rows selected

    由于我们执行了一个需要硬解析的sql,导致Oracle需要获得library cache load lock以便load相关信息到这个sql的子cursor的heap 6中,而要获得library cache load lock,必须先持有library cache load lock latch。从上述结果中我们可以看到,此时library cache load lock latch的gets已经发生了变化,从24848递增到了24856。

    接下来我们再来看一看上述library cache latches的子latch情况:

    SQL> show parameter cpu_count

    NAME                                 TYPE        VALUE

    ———————————— ———– ——————————

    cpu_count                            integer     2

    这里cpu的个数为2,显然上述library cache latches的子latch应该为3:

    SQL> select name,level#,gets,misses,sleeps,immediate_gets,immediate_misses from v$latch_children where name like ‘library%’;

    NAME                                                   LEVEL#       GETS     MISSES     SLEEPS IMMEDIATE_GETS IMMEDIATE_MISSES

    ————————————————– ———- ———- ———- ———- ————– —————-

    library cache                                               5    3274551       1301         94            187                0

    library cache                                               5    2218356        116         80            933                0

    library cache                                               5    3731320        191        626           1476           76766

    library cache lock                                          6    5339737        362          3              0                0

    library cache lock                                          6    6223353        194          3              0                0

    library cache lock                                          6    1987799         26          0              0                0

    library cache pin                                           6    1484918        184          0              0                0

    library cache pin                                           6     891695          3          0              2                0

    library cache pin                                           6    1831837          6          0              0                0

    library cache pin allocation                                3      23177          0          0              0                0

    library cache pin allocation                                3       8272          0          0              0                0

    library cache pin allocation                                3      25849          0          0              0                0

    library cache lock allocation                               3      75900          0          0              0                0

    library cache lock allocation                               3      28229          0          0              0                0

    library cache lock allocation                               3     104237          0          0              0                0

    library cache hash chains                                   9          0          0          0              0                0

    library cache hash chains                                   9          0          0          0              0                0

    library cache hash chains                                   9          0          0          0              0                0

    18 rows selected

    注意,结果里并没有library cache load lock latch,说明library cache load lock latch没有children,它是一个solitary类型的latch。

    从10.2.0.2开始,Oracle将_kks_use_mutex_pin的默认值改成了true,这意味着从10.2.0.2开始,Oracle里将再不会有针对cursor的library cache pin等待,取而代之的是mutex等待,具体表现为cursor: pin *等待,如cursor: pin S wait on X。

    这里需要我们了解的是:

    a)         从10.2.0.2开始,Oracle只是用mutex替代了针对cursor的library cache pin,这并不代表从10.2.0.2开始Oracle里就没有library cache pin等待了。比如这个例子里的library cache pin等待就发生在10.2.0.4中:http://www.dbsnake.net/solve-library-cache-pin.html

    b)         Mutex和latch是互相独立,没有任何关系的:Latches and mutexes are independent mechanisms i.e. a process can hold a latch and a mutex at the same time. In the case of process death, latches are always cleaned up before mutexes. There is no generic mutex deadlock detection (unlike latches). There is no mutex/latch hierarchy.

    从11gR1开始,Oracle用mutex替换了library cache latches,并引了一个新的等待事件:library cache: mutex *,我们来看一下这个知识点:

    Connected to Oracle Database 11g Enterprise Edition Release 11.2.0.1.0

    Connected as nbs

    SQL>  select name,level#,gets,misses,sleeps,immediate_gets,immediate_misses from v$latch where name like ‘library%’;

    NAME                                                                 LEVEL#       GETS     MISSES     SLEEPS IMMEDIATE_GETS IMMEDIATE_MISSES

    —————————————————————- ———- ———- ———- ———- ————– —————-

    library cache load lock                                                   5          0          0          0             0                0

    SQL> select name,level#,gets,misses,sleeps,immediate_gets,immediate_misses from v$latch_children where name like ‘library%’;

    NAME                                                                 LEVEL#       GETS     MISSES     SLEEPS IMMEDIATE_GETS IMMEDIATE_MISSES

    —————————————————————- ———- ———- ———- ———- ————– —————-

    从结果里我们可以看到,在11.2.0.1里,各种library cache latches都没有了,只剩下了library cache load lock latch,而且Oracle还没有使用这个latch,因为gets是0。

  • 相关阅读:
    成功引爆
    pecompact2脱壳手记
    象棋
    今天小雨
    出错了,怎么办?
    设计模式——Adapter模式
    表设计中应注意的2点
    设计模式——Singleton模式
    设计模式——Facade模式
    设计模式——Strategy模式
  • 原文地址:https://www.cnblogs.com/taowang2016/p/2920439.html
Copyright © 2011-2022 走看看