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。
我们来看一下在 Oracle10gR2 中的一个有关library cache latch,
library cache lock, library cache pin 的统计情况
SQL> select name,gets,misses,sleeps from v$latch where name like '%library%';
NAME GETS MISSES SLEEPS
------------------------------ ---------- ---------- ----------
library cache 1937415298 5513866 2346346
library cache lock 734620587 425030 3021
library cache pin 171326029 108613 868
library cache pin allocation 2930490 35 1
library cache lock allocation 5226147 349 2
library cache load lock 720252 1762 34
library cache hash chains 0 0 0
library cache latch受隐含参数_KGL_LATCH_COUNT的控制,默认值为大于等于系统中CPU个数的最小素数,但是Oracle对其有一个硬性限制,该参数不能大于67。注意:我们去查询_kgl_latch_count有时候显示为0,这是一个bug。
那么library cache object handle是由哪个子latch来保护的呢?Oracle利用下面算法来确定:
latch# = mod(bucket#, #latches)(摘自DSI405)
也就是说用哪个子latch去保护某个handle是根据那个handle所在的bucket号,以及总共有多少个子latch来进行hash运算得到的,对此我们不必深究。
latch contention
根据前面的讲解,存在大量硬解析的系统上面就必然引发library cache latch, library cache lock竞争,下面就是一个每秒高达74个硬解析所引发
Library cache lock 成为Top wait event的一个案例。
Load Profile
|
Per Second |
Per Transaction | |
|
Redo size: |
832,040.22 |
10,738.95 |
|
Logical reads: |
56,114.37 |
724.26 |
|
Block changes: |
5,897.48 |
76.12 |
|
Physical reads: |
3,442.07 |
44.43 |
|
Physical writes: |
174.41 |
2.25 |
|
User calls: |
273.48 |
3.53 |
|
Parses: |
82.02 |
1.06 |
|
Hard parses: |
74.53 |
0.96 |
|
Sorts: |
121.00 |
1.56 |
|
Logons: |
0.09 |
0.00 |
|
Executes: |
160.67 |
2.07 |
|
Transactions: |
77.48 |
|
Top 5 Timed Events
|
Event |
Waits |
Time(s) |
Avg Wait(ms) |
% Total Call Time |
Wait Class |
|
library cache lock |
3,603 |
10,588 |
2,939 |
51.7 |
Concurrency |
|
CPU time |
|
7,059 |
|
34.5 |
|
|
db file sequential read |
1,454,620 |
1,932 |
1 |
9.4 |
User I/O |
|
db file scattered read |
863,507 |
1,178 |
1 |
5.7 |
User I/O |
|
log file parallel write |
269,164 |
260 |
1 |
1.3 |
System I/O |
如果shared pool过小,也会引发library cache latch竞争,与此同时还会伴随shared pool latch竞争。下面就是一个由于shared pool过小,导致library cache latch成为top wait event的案例
Load Profile
|
Per Second |
Per Transaction | |
|
Redo size: |
81,060.24 |
32,559.33 |
|
Logical reads: |
11,204.65 |
4,500.55 |
|
Block changes: |
414.99 |
166.69 |
|
Physical reads: |
0.50 |
0.20 |
|
Physical writes: |
5.62 |
2.26 |
|
User calls: |
189.05 |
75.93 |
|
Parses: |
38.51 |
15.47 |
|
Hard parses: |
3.58 |
1.44 |
|
Sorts: |
24.27 |
9.75 |
|
Logons: |
0.02 |
0.01 |
|
Executes: |
85.29 |
34.26 |
|
Transactions: |
2.49 |
|
Top 5 Timed Events
|
Event |
Waits |
Time(s) |
Avg Wait(ms) |
% Total Call Time |
Wait Class |
|
CPU time |
|
6,663 |
|
61.0 |
|
|
latch: library cache |
1,306 |
157 |
120 |
1.4 |
Concurrency |
|
latch: shared pool |
5,527 |
152 |
28 |
1.4 |
Concurrency |
|
log file parallel write |
81,192 |
122 |
2 |
1.1 |
System I/O |
|
log file sync |
74,187 |
120 |
2 |
1.1 |
Commit |
Shared Pool Advisory
- SP: Shared Pool Est LC: Estimated Library Cache Factr: Factor
- Note there is often a 1:Many correlation between a single logical object in the Library Cache, and the physical number of memory objects associated with it. Therefore comparing the number of Lib Cache objects (e.g. in v$librarycache), with the number of Lib Cache Memory Objects is invalid.
|
Shared Pool Size(M) |
SP Size Factr |
Est LC Size (M) |
Est LC Mem Obj |
Est LC Time Saved (s) |
Est LC Time Saved Factr |
Est LC Load Time (s) |
Est LC Load Time Factr |
Est LC Mem Obj Hits |
|
352 |
0.38 |
115 |
15,602 |
547,132 |
0.88 |
82,833 |
7.60 |
13,561,473 |
|
448 |
0.48 |
210 |
20,206 |
559,218 |
0.90 |
70,747 |
6.49 |
13,681,539 |
|
544 |
0.59 |
304 |
25,430 |
564,732 |
0.91 |
65,233 |
5.99 |
13,732,206 |
|
640 |
0.69 |
397 |
29,804 |
585,824 |
0.95 |
44,141 |
4.05 |
13,761,147 |
|
736 |
0.79 |
492 |
31,706 |
606,741 |
0.98 |
23,224 |
2.13 |
13,779,369 |
|
832 |
0.90 |
587 |
33,581 |
614,849 |
0.99 |
15,116 |
1.39 |
13,791,238 |
|
928 |
1.00 |
682 |
35,624 |
619,069 |
1.00 |
10,896 |
1.00 |
13,795,452 |
|
1,024 |
1.10 |
777 |
37,928 |
621,113 |
1.00 |
8,852 |
0.81 |
13,798,083 |
|
1,120 |
1.21 |
872 |
40,162 |
623,021 |
1.01 |
6,944 |
0.64 |
13,799,705 |
|
1,216 |
1.31 |
967 |
42,895 |
624,574 |
1.01 |
5,391 |
0.49 |
13,800,847 |
|
1,312 |
1.41 |
1,062 |
45,835 |
626,304 |
1.01 |
3,661 |
0.34 |
13,801,787 |
|
1,408 |
1.52 |
1,157 |
48,677 |
627,737 |
1.01 |
2,228 |
0.20 |
13,802,717 |
|
1,504 |
1.62 |
1,252 |
52,032 |
636,469 |
1.03 |
1 |
0.00 |
13,803,727 |
|
1,600 |
1.72 |
1,347 |
56,719 |
637,734 |
1.03 |
1 |
0.00 |
13,804,715 |
|
1,696 |
1.83 |
1,442 |
60,885 |
638,457 |
1.03 |
1 |
0.00 |
13,805,622 |
|
1,792 |
1.93 |
1,537 |
63,993 |
638,640 |
1.03 |
1 |
0.00 |
13,806,647 |
|
1,888 |
2.03 |
1,632 |
66,977 |
638,660 |
1.03 |
1 |
0.00 |
13,807,680 |
Library Cache Activity
- "Pct Misses" should be very low
|
Namespace |
Get Requests |
Pct Miss |
Pin Requests |
Pct Miss |
Reloads |
Invali- dations |
|
BODY |
5,545 |
0.20 |
7,806 |
0.40 |
20 |
0 |
|
CLUSTER |
91 |
4.40 |
155 |
3.23 |
1 |
0 |
|
INDEX |
75 |
25.33 |
140 |
34.29 |
29 |
0 |
|
SQL AREA |
660,357 |
26.67 |
3,184,169 |
9.38 |
8,452 |
120 |
|
TABLE/PROCEDURE |
20,169 |
2.45 |
965,511 |
0.30 |
1,268 |
0 |
|
TRIGGER |
202 |
0.00 |
1,472 |
0.20 |
3 |
0 |
我是怎么判断shared pool过小的呢?AWR里面提供了多个指标,这里硬解析不是太高,每秒3个,说明硬解析不会导致该问题,shared pool advice已经说得很明白了,当前shared pool只有928m,Oracle建议把shared pool至少调整到1504m。同时查看 Library cache activtiy,大家注意观察pct miss,SQL AREA的pct miss居然达到26.67%,而且重载了8452次,那么我完全可以肯定就是由于shared pool太小,导致SQL被大量重载,从而引发library cache latch竞争。
Library cache latch竞争还有可能是具有高version_count的SQL导致的,某个session去执行一个具有很高version_count的SQL需要pin住child cursor,而由于child cursor过多,在未pin住child cursor之前不会释放library cache latch,这样当其他session想运行该SQL的时候就会发生library cache latch争用,那么遇到这种情况请检查cursor_sharing参数的设置,另外请查询是否遇到bug,或者由于系统中不同schema出现大量同名的表名,这样请更改设计。
在第二个案例中,由于shared pool设置过小还导致了shared pool latch处于top wait event 中的第二名。Shared pool latch是用来干嘛的呢?Shared pool latch用来保护共享池的结构,在分配,释放共享池空间的时候就会获得该latch,那么在这个案例中,由于共享池太小,在对一个新的SQL进行硬解析的时候需要老化某些对象,为新对象腾出空间,那么这个释放空间的过程就需要获得shared pool latch。当然了,在进行硬解析,也需要获得一个shared pool latch因为硬解析需要申请分配shared pool空间,而分配空间的时候就需要获得该latch。
