2.7 Library cache Latch
Library cache latch用来控制对library cache object的并发访问。前面已经提到,我们要访问library cache object之前必须获得library cache lock, lock不是一个原子操作(原子操作就是在操作程中不会被打破的操作,很明显这里的lock可以被打破), Oracle为了保护这个lock,引入了library cache latch机制,也就是说在获得library cache lock之前,需要先获得library cache latch,当获得library cache lock之后就释放library cache latch。
如果某个library cache object没有在内存中,那么这个lock就不能被获取,这个时候需要获得一个library cache load lock latch,然后再获取一个library cache load lock,当load lock获得之后就释放library cache load lock latch。
library cache latch受隐含参数_KGL_LATCH_COUNT的控制,默认值为大于等于系统中CPU个数的最小素数,但是Oracle对其有一个硬性限制,该参数不能大于67。
注意:我们去查询_kgl_latch_count有时候显示为0,这是一个bug。
Oracle利用下面算法来确定library cache object handle是由哪个子latch来保护的:
latch# = mod(bucket#, #latches)
也就是说用哪个子latch去保护某个handle是根据那个handle所在的bucket号,以及总共有多少个子latch来进行hash运算得到的。
有关其他内容,参考: http://www.linuxidc.com/Linux/2011-07/38714.htm
三. Dictionary Cache
Table definition against which an application user suppose to do a query, it include table’s associated Index and Columns and privilege information regarding table as we as columns.
/* Formatted on 2011/6/21 15:01:52 (QP5 v5.163.1008.3004) */
SELECT *
FROM X$KSMSS
WHERE KSMSSNAM LIKE 'PL/SQL%' AND KSMSSLEN <> 0;
四. Control Structure
This contain the information regarding Internal Latch and Locks (Data Structure), it also contain buffer header, the process session and transaction arrays.
The size of these arrays depends on the setting of Initialisation parameter of Init.ora file and can’t be changed without shutting down the database.
4.1 Shared Pool Chunks
Have close looks of Shared Pool, For that we should have a close look at x$ksmsp each row in this table shows a chunk of shared pool :
SQL>select * from X$ksmsp

When each shared pool chunk is allocated the code is passed to a function that does the work of allocation and this address is visible to KSMCHCOM column,which describe the purpose of allocation. This chunk is supposed to be larger then the size of the object as it also contains the header information.
The column KSMCHCLS represent the class, there are basically four type of classes:
Freeabl : can be freed only contain the objects needed for the session call.
Free : free and not contained by valid object.
Recr : contain by temporary objects.
Perm : contained by the permanent object.
/* Formatted on 2011/6/21 16:14:50 (QP5 v5.163.1008.3004) */
SELECT KSMCHCLS CLASS,
COUNT (KSMCHCLS) NUM,
SUM (KSMCHSIZ) SIZ,
TO_CHAR ( ( (SUM (KSMCHSIZ) / COUNT (KSMCHCLS) / 1024)), '999,999.00')
|| 'k'
"AVG SIZE"
FROM X$KSMSP
GROUP BY KSMCHCLS;

注意:
在生产库上查询X$KSMSP时,要看下系统的繁忙或者说是负载高低,因为可能会导致db hang 住。
So the overall summary :
/* Formatted on 2011/6/21 16:06:38 (QP5 v5.163.1008.3004) */
SELECT KSMCHCOM name,
COUNT (KSMCHCOM),
SUM (DECODE (KSMCHCLS, 'recr', KSMCHSIZ)) RECREATABLE,
SUM (DECODE (KSMCHCLS, 'freeabl', KSMCHSIZ)) FREEABLE
FROM x$ksmsp
GROUP BY KSMCHCOM;
4.2 LRU List
When a process starts it allocate some memory and when it’ fails to allocated required memory, Then it try to remove chunk containing recreatable object from shared pool to get the desired size of chunk, and removing these object from memory is based on LRU(Least Recent Used) means those objects that are frequently pinned kept in the memory and those are unpinned we generally remove those objects from the memory. Object those required again known as transient and other known as recurrent.
ORA-04331 ‘unable to allocate x bytes of shared pool’ when all the free memory fully exhausted (Later we will discuss the shared pool fragmentation).
There is one list maintained which known as Reserved List, it generally 5% of the total size of Shared Pool and reserved size is defined by SHARED_POOL_RESERVED_SIZE parameter in Init.ora parameter file.
With the help of v$shared_pool_reserved we can see reserved size, here REQUEST_MISS shows the number of times the request miss for a large chunk.
4.3 SHARED_POOL size calculation
The shared pool size is highly application dependent. To determine the shared pool size for a production system it is generally necessary to develop the application run it on a test environment (should be enough like production system) get some test result and on the basis of that calculate the shared pool size. There are some few step which we should consider while calculating the shared pool:
4.3.1 STORED OBJECTS
The amount of shared pool that needs to be allocated for objects that are stored in the database like packages and views is easy to measure. You can just measure their size directly with the following statement:
/* Formatted on 2011/6/21 16:32:28 (QP5 v5.163.1008.3004) */
SELECT SUM (sharable_mem) FROM V$DB_OBJECT_CACHE;
4.3.2 SQL
The amount of memory needed to store sql statements in the shared pool is more difficult to measure because of the needs of dynamic sql. If an application has no dynamic sql then the amount of memory can simply be measured after the application has run for a while by just selecting it out of the shared pool as follows:
/* Formatted on 2011/6/21 16:34:02 (QP5 v5.163.1008.3004) */
SELECT SUM (sharable_mem) FROM v$sqlarea;
If the application has a moderate or large amount of dynamic sql like most applications do, then a certain amount of memory will be needed for the shared sql plus more for thedynamic sql. Sufficient memory should be allocated so that the dynamic sql does not age the shared sql out of the shared pool.
Approximated memory could be calculated by the following:
/* Formatted on 2011/6/21 16:35:06 (QP5 v5.163.1008.3004) */
SELECT SUM (sharable_mem)
FROM v$sqlarea
WHERE executions > 5;
The remaining memory in v$sqlarea is for dynamic sql.
4.3.3 PER-USER PER-CURSOR MEMORY
You will need to allow around 250 bytes of memory in the shared pool per concurrent user for each open cursor that the user has whether the cursor is shared or not. During the peak usage time of the production system, you can measure this as follows:
/* Formatted on 2011/6/21 16:36:55 (QP5 v5.163.1008.3004) */
SELECT SUM (250 * users_opening) FROM v$sqlarea;
In a test system you can measure it by selecting the number of open cursors for a test user and multiplying by the total number of users:
/* Formatted on 2011/6/21 16:37:20 (QP5 v5.163.1008.3004) */
SELECT 250 * VALUE bytes_per_user
FROM v$sesstat s, v$statname n
WHERE s.statistic# = n.statistic#
AND n.name = 'opened cursors current'
AND s.sid = 130;
-- replace 130 with session id of user being measured
The per-user per-cursor memory is one of the classes of memory that shows up as 'library cache' in v$sgastat.
4.3.4 MTS
If you are using multi-threaded server, then you will need to allow enough memory for all the shared server users to put their session memory in the shared pool. This can be measured for one user with the following query:
/* Formatted on 2011/6/21 16:39:31 (QP5 v5.163.1008.3004) */
SELECT VALUE sess_mem
FROM v$sesstat s, v$statname n
WHERE s.statistic# = n.statistic#
AND n.name = 'session uga memory'
AND s.sid = 23;
-- replace 23 with session id of user being measured
A more conservative value to use is the maximum session memory that was ever allocated by the user:
/* Formatted on 2011/6/21 16:40:22 (QP5 v5.163.1008.3004) */
SELECT VALUE sess_max_mem
FROM v$sesstat s, v$statname n
WHERE s.statistic# = n.statistic#
AND n.name = 'session uga memory max'
AND s.sid = 23;
-- replace 23 with session id of user being measured
To select this value for all the currently logged on users the following query can be used:
/* Formatted on 2011/6/21 16:40:56 (QP5 v5.163.1008.3004) */
SELECT SUM (VALUE) all_sess_mem
FROM v$sesstat s, v$statname n
WHERE s.statistic# = n.statistic# AND n.name = 'session uga memory max';
4.3.5 OVERHEAD
You will need to add a minimum of 30% overhead to the values calculated above to allow for unexpected and unmeasured usage of the shared pool.
Estimating Procedure (From Metalink)
/* Formatted on 2011/6/21 16:46:24 (QP5 v5.163.1008.3004) */
SET SERVEROUTPUT ON;
DECLARE
object_mem NUMBER;
shared_sql NUMBER;
cursor_mem NUMBER;
mts_mem NUMBER;
used_pool_size NUMBER;
free_mem NUMBER;
pool_size VARCHAR2 (100);
BEGIN
-- Stored objects (packages, views)
SELECT SUM (sharable_mem) INTO object_mem FROM v$db_object_cache;
-- Shared SQL -- need to have additional memory if dynamic SQL used
SELECT SUM (sharable_mem) INTO shared_sql FROM v$sqlarea;
-- User Cursor Usage -- run this during peak usage
SELECT SUM (250 * users_opening) INTO cursor_mem FROM v$sqlarea;
-- For a test system -- get usage for one user, multiply by # users
-- select (250 * value) bytes_per_user
-- from v$sesstat s, v$statname n
-- where s.statistic# = n.statistic#
-- and n.name = 'opened cursors current'
-- and s.sid = 25; -- where 25 is the sid of the process
-- MTS memory needed to hold session information for shared server users
-- This query computes a total for all currently logged on users (run
-- during peak period). Alternatively calculate for a single user and
-- multiply by # users.
SELECT SUM (VALUE)
INTO mts_mem
FROM v$sesstat s, v$statname n
WHERE s.statistic# = n.statistic# AND n.name = 'session uga memory max';
-- Free (unused) memory in the SGA: gives an indication of how much memory
-- is being wasted out of the total allocated.
SELECT bytes
INTO free_mem
FROM v$sgastat
WHERE name = 'free memory';
-- For non-MTS add up object, shared sql, cursors and 30% overhead.
used_pool_size := ROUND (1.3 * (object_mem + shared_sql + cursor_mem));
-- For MTS add mts contribution also.
-- used_pool_size := round(1.3*(object_mem+shared_sql+cursor_mem+mts_mem));
SELECT VALUE
INTO pool_size
FROM v$parameter
WHERE name = 'shared_pool_size';
-- Display results
DBMS_OUTPUT.put_line ('Obj mem: ' || TO_CHAR (object_mem) || ' bytes');
DBMS_OUTPUT.put_line ('Shared sql:' || TO_CHAR (shared_sql) || ' bytes');
DBMS_OUTPUT.put_line ('Cursors: ' || TO_CHAR (cursor_mem) || ' bytes');
DBMS_OUTPUT.put_line ('MTS session:' || TO_CHAR (mts_mem) || ' bytes');
DBMS_OUTPUT.put_line (
'Free memory:'
|| TO_CHAR (free_mem)
|| ' bytes '
|| '('
|| TO_CHAR (ROUND (free_mem / 1024 / 1024, 2))
|| 'M)');
DBMS_OUTPUT.put_line (
'Shared poolutilization (total): '
|| TO_CHAR (used_pool_size)
|| ' bytes '
|| '('
|| TO_CHAR (ROUND (used_pool_size / 1024 / 1024, 2))
|| 'M)');
--Technical Reports Compendium, Volume I, 1996
--Shared Pool Internals
DBMS_OUTPUT.put_line (
'Shared pool allocation (actual): '
|| pool_size
|| ' bytes'
|| '('
|| TO_CHAR (ROUND (pool_size / 1024 / 1024, 2))
|| 'M)');
DBMS_OUTPUT.put_line (
'Percentage Utilized:'
|| TO_CHAR (ROUND (used_pool_size / pool_size * 100))
|| '%');
END;
