Académique Documents
Professionnel Documents
Culture Documents
WHO AM I
Andrey.Nikolaev@rdtex.ru
RDTEX, First Line Support Center
http://andreynikolaev.wordpress.com
"Latch, mutex and beyond"
Specialize in Oracle performance tuning
Over 20 years of Oracle related experience as a research
scientist, developer, DBA, performance consultant, lecturer
Occasionally presented at conferences
RUOUG member
Latches
Mutexes
Access
Several Modes
Operations
Acquisition
FIFO
SIRO
Spinlock based
No
Yes
Yes
Timescale
> Milliseconds
Microseconds
SubMicroseconds
Lifecycle
Dynamic
Static
Dynamic
Free
CPU1:
Holding by CPU 2
Free
CPU2: Get
CPU3:
Spin count ( )
Oracle latches:
CLASSIC SPINLOCKS
Wiki: " spinlock waits in a loop repeatedly checking until the lock
becomes available ".
Introduced by Edsger Dijkstra [4] in 1965. Have been thoroughly
investigated since that time [5].
Many sophisticated spinlock realizations were proposed and evaluated
(TS, TTS, Delay, MCS, Anderson,...).
Two general types:
System spinlocks. Kernel OS threads cannot wait. Major metrics:
atomic operations frequency. Shared bus utilization.
User spinlocks. Oracle latch and mutex. Average lock holding time ~
10 us. It is more efficient to poll a lock rather than pre-empt the thread
doing 1 ms context switch. Metrics: CPU and elapsed times.
9
No spinning
_spin_count=0
_spin_count=2000
SPINLOCK REALIZATIONS
Spinlock:
TS
Pseudocode:
Problems:
while(Test_and_Set(lock));
Bus saturation
while(lock||Test_and_Set(lock));
Invalidation storms on
release (open door,
thundering herds).
Adjustable delay
pre-11.2 mutex
TTS
Oracle latch
Delay
Latch and 11.2
Mutex
THE TOOLS
12
for i in 1..1000000
loop
select dummy into a from latch_contention;
end loop;
To induce the contention for exclusive row cache objects latch protecting
dc_users row cache just add several dynamic RLS policies:
exec dbms_rls.add_policy('system','latch_contention','rls_1',
exec dbms_rls.add_policy('system','latch_contention','rls_2',
Latch wait
16
HOW ORACLE
REQUESTS THE LATCH
RDTEX | Latch internals | RUOUG Seminar
17
kslg2c(l1,l2,trc,why, where)
kslgetsl (laddr,wait,why,where,mode)
kslgpl(laddr,comment,why,where)
kslfre(laddr)
18
V$LATCH_MISSES
Based on X$KSLWSC - No[W]ait and [S]leep [C]ount stats by
Context. Documentation is rather obscure.
The following counters are incremented on each latch sleep:
sleep_count (where the latch was held)
wtr_slp_count (where the latch get was requested)
nwfail_count is incremented only for session, which SID is equal
to _LATCH_MISS_STAT_SID parameter.
longhold_count is incremented for shared latches only, when
someone held the latch at this where for the entire duration of
sleep.
21
v$process
-> x$ksupr
Struct ksupr {
Struct kslla{
ksllt *ksllalat[14];
}
}
Struct ksllt{
struct ksllt {
<Latch>
where and why
Level, latch#, class, other attributes
Statistics
Latch wait list header
25
*nix 64bit
Windows 32bit
7.3.4
92
120
8.0.6
104
104
8.1.7
104
144
104
9.0.1
200
160
9.2.0
196
240
200
10.1.0
256
208
100
160
104
10.2.0 - 11.2.0.3
In 11g first latch word shows the pid of the latch holder
0x00 latch is free
0x12 Oracle process with pid 18 holds the exclusive latch
27
kslltwhr, kslltwhy where and why latch was acquired last time.
kslltwgt, - statistics.
kslltlvl, - attributes.
kslltwtt latch wait time.
29
x$ksllt
GETS
kslltwgt
MISSES
kslltwff
SLEEPS
kslltwsl
SPIN_GETS
ksllthst0
WAIT_TIME
kslltwtt
IMMEDIATE_GETS
kslltngt
IMMEDIATE_MISSES kslltnfa
31
32
LATCH ATTRIBUTES
Each latch have at least the following attributes in kslldt :
Name Latch name as appeared in V$ views
SHR. Is the latch Exclusive or Shared?
PAR. Is the latch Solitary or Parent for the family of child latches?
G2C. Can two child latches be simultaneously requested in wait mode
using kslg2c()
LNG. Is wait posting used for this latch? Obsolete since Oracle 9.2.
UFS. The latch is Ultrafast. It will not increment miss statistics when
STATISTICS_LEVEL=BASIC. 10.2 and above
Level. 0-14. Used to prevent latch deadlocks
Class. 0-7. Spin and wait class assigned to the latch. 9.2 and above.
33
Number of latches
PAR
G2C
LNG
UFS
SHARED
7.3.4.0
53
14
8.0.6.3
80
21
8.1.7.4
152
48
19
9.2.0.8
242
79
37
19
10.2.0.2
385
114
55
47
10.2.0.3
388
117
58
48
10.2.0.4
394
117
59
50
11.1.0.6
496
145
67
81
11.1.0.7
502
145
67
83
11.2.0.1
535
149
70
86
11.2.0.2
551
153
72
91
11.2.0.3
553
154
72
93
34
LATCH LEVELS
Every latch has a level in the range 0-14 to avoid deadlocks.
Process can request a latch X in wait mode after obtaining latch Y, if and
only if level X > level Y
At the same level. Process can request the second G2C child X in wait
mode after obtaining child Y, if and only if the child number of X < child
number of Y
If these rules are broken, the Oracle process raises:
ORA-600[504] - trying to obtain another latch at incompatible level. Note 28104.1.
ORA-600[525] - trying to obtain another latch at the same level. Note 138888.1.
ORA-600[526] - latch child order violation. Note 138889.1.
35
LATCH TREES
Rising level rule leads to trees of processes waiting for and
holding the latches:
ospid: 28067
sid: 1677 pid: 61
holding: 3800729f0
'shared pool' (156) level=7 child=1 whr=1602 kghupr1
waiter: ospid: 129
sid: 72 pid: 45
holding: a154b7120
'library cache' (157) level=5 child=17 kglupc: child
waiter: ospid: 18255 sid: 65 pid: 930
waiter: ospid: 6690
sid: 554 pid: 1654
waiter: ospid: 4685
sid: 879 pid: 1034
Direct SGA access program output for 9.2.0.6 instance with too small
shared pool.
37
LATCH CLASSES
Classes with different spin and wait policies.
Up to 8 classes. By default all the latches belong to class 0
Exception process allocation latch belongs to class 2
Latch can be assigned to class by its number. For example :
_LATCH_CLASS_6='100 12 1000 2000 3000 4000'
_LATCH_CLASSES='4:6'
6
100
12
1000
2000
3000
4000
4000
4000
38
kslgetsl(laddr,0,why,where,mode)
39
LATCH WAITS
40
S G A
Latch
Process B
Process A
CPU 1
CPU 2
Process A
holds a latch
Process B waits
(spins and sleeps)
41
42
43
44
46
1 us
1 sec
1 ms
17 min
1 sec
11.5 days
0.0005 sec
~ 1-2 sec
~10 sec
~ 10-20 sec
~50 sec
~ 10 sec
10sec-17min
48
50
51
53
55
57
X mode get
Held in S mode
Compatible
2*_SPIN_COUNT
Held in X mode
2*_SPIN_COUNT
Blocking mode
2*_SPIN_COUNT
58
LATCH RELEASE
Free the latch kslfre(laddr).
Oracle process releases the latch nonatomically.
Then it sets up memory_barrier perform atomic operation on
address individual to each process.
This requires less bus invalidation and ensures propagation of latch
release to other local caches.
Not fair policy. Spinners on the local CPU board have the
preference.
Then process posts the first process in the list of waiters.
59
LATCH CONTENTION
RDTEX | Latch internals | RUOUG Seminar
60
LATCH CONTENTION
Occurs when a latch is required by several processes at the
same time
Mostly due to bugs or abnormal application behavior.
As utilization increases, latch waits and spins also increase.
Performance degradation can be extremely sharp
To diagnose the latch contention we need to investigate the
latch statistics [8]
61
gets
=
time
misses
gets
sleeps
K =
misses
wait _ time
W =
time
spin _ gets
=
misses
SAMPLED AVERAGES
By sampling of x$ksupr we can estimate:
Length of latch wait queue
Lw
: Ns
U =
S=
L =W
+ 1
Taq = 1 ( N s + W )
64
65
> 10%
67
68
70
SPIN SCALING
My previous work [8] proposed approximate scaling rules to estimate
effect of SPIN_COUNT tuning depending on:
= "sleeps
ratio"=Avg
If "sleeps ratio"K for
exclusive
latchSlps/Miss
is 10% than increase of
spin count
40000 may results in
K 0.1tothen:
If spin is inefficient
10Doubling
times decrease
of "latch free" wait events, and only 10%
the spin count will reduce "sleeps ratio" by 2 and doubles
the spin CPU consumption
increase of CPU consumption.
mileage
may vary
If latch holding timeYour
distribution
has exponential
tail, you have enough
CPU resources and spin is efficient K << 0 .1 then:
Doubling the spin count will square the sleeps ratio coefficient.
This will multiply the spin CPU consumption by (1 + K )
If the spin is already efficient, it is worth to increase the spin count.
73
CPU THRASHING
If CPUs are running at 100% utilization for other reasons, the latch
contention can be just a symptom of this CPU starvation itself.
All wait times will be inflated by wait for CPU. However, latch
holding time should be in normal range.
CPU thrashing is unlikely on Solaris due to latch preemption
control.
To prevent the priority inversion use:
FX priority class on Solaris
HPUX_SCHED_NOAGE on HPUX
SCHED_BATCH policy on Linux
76
77
BIBLIOGRAPHY
1.
2.
3.
4.
5.
6.
7.
8.
9.
Steve Adams. Oracle8i Internal Services for Waits, Latches, Locks, and
Memory. 1999.
Tanel Poder blog, http://blog.tanelpoder.com
Jonathan Lewis, Oracle Core: Essential Internals for DBAs and
Developers. 2011
Edsger Dijkstra, Solution of a Problem in Concurrent Programming
Control CACM. 1965.
M. Herlihy, N. Shavit, The Art of Multiprocessor Programming. 2008.
B. Sinharoy, et al. , Improving Software MP Efficiency for Shared
Memory Systems. Proc. of 29th Hawaii Conference on System Sciences.
1996.
Guy Harrison blog, Using _spin_count to reduce latch contention in 11g.
http://guyharrison.squarespace.com/ 2008.
R. Johnson, et al. A new look at the roles of spinning and blocking.
Proc. of 5th Workshop on Data Management on New Hardware. 2009
A. Nikolaev, Exploring Oracle RDBMS latches using Solaris DTrace.
Proc. of MEDIAS 2011 Conf., http://arxiv.org/abs/1111.0594v1 2011
78
Q/A?
Questions?
Comments?
79
ACKNOWLEDGEMENTS
Thanks to RDTEX Technical Support Centre Director S.P.
Misiura for years of encouragement and support of my
investigations.
Thanks to my colleagues for discussions.
Thanks to all our customers for participating in latch
troubleshooting.
80
THANK YOU!
Andrey Nikolaev
http://andreynikolaev.wordpress.com
Andrey.Nikolaev@rdtex.ru
RDTEX, Moscow, Russia
www.rdtex.ru
Andrey Kriushin
Chairman of Russian Oracle User Group (RuOUG)
Died of heart attack 02.08.2011
82