1 / 48

Latch Internals

Latch Internals. Objectives. After completing this lesson, you should be able to do the following: Describe the internal implementation of latches Detail latch contention Explain how to alleviate latch contention Identify the diagnostic tools for latching issues. Purpose of Latches.

noah
Télécharger la présentation

Latch Internals

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Latch Internals

  2. Objectives • After completing this lesson, you should be able to do the following: • Describe the internal implementation of latches • Detail latch contention • Explain how to alleviate latch contention • Identify the diagnostic tools for latching issues

  3. Purpose of Latches • To serialize access: • Protect data structures within the SGA • Protect shared memory allocations • To serialize execution: • Prevent simultaneous execution of certain “critical” pieces of code • Prevent corruptions

  4. Latch Characteristics • Simple low-level synchronization locks that are designed to ensure the serialization of some operations • Latches differ from enqueues. Access Acquisition Atomicity Enqueues Several Modes FIFO queue No Latches Mostly Exclusive Nondeterministic Yes

  5. Latch Implementation • A latch is implemented as a single memory location. • Latches are designed to be fast and inexpensive locks: • Latches are obtained in an atomic operation. • They are held for a minimal amount of time. • Exclusive latches allow write access to a single process (most common usage). • Shared latches allow concurrent reads by processes to a piece of memory. • Rules are enforced to prevent deadlocks.

  6. Oracle Latch Implementation • Latch attributes: A latch can be defined as a parent, child, or solitary latch. • Latch levels: Each latch is associated with a predefined level to avoid deadlocks. • Latch declaration: Latches are declared through macros with the required parameters that define the latch.

  7. Latch Attributes • A latch can be defined as a solitary latch or as a latch set. • A latch set has a parent and several child latches. • Parent and solitary latches are allocated statically. • Child latches are allocated dynamically. • Both parent and child latches share the same latch name. • The parent latch acts as a master latch. • The latches in a set are chained together to facilitate statistics gathering.

  8. Latch Attributes • The following flags are used when latches are created: • KSLLAPR2: Defines a parent latch allowing two wait gets of child latches • KSLLAPAR: Defines a parent latch not allowing two wait gets of child latches • KSLLALNG: Defines a latch that can be held for a longer period of time • KSLLASHL: Defines a shared latch

  9. Latch Levels • All latches have a level in the range 0–14. • The level is predefined and permanent. • The levels facilitate operations on shared structures and avoid deadlocks. • Usually a process can request a latch X after obtaining latch Y, if and only if level X > level Y.

  10. Latch Declaration • Latches are declared internally with macro definitions. • Some examples of macros that are used are KSLLATDN, KSLLATDV, KSLLTDV2. • For example, the KSLLATDV macro accepts four arguments: • Latch name • Description • Cleanup function address • Latch level • Child latches are allocated dynamically by using the function kslclt().

  11. Internal Latch Structures • The latch instance structure is ksllt and contains: • The OSD latch structure • Latch attributes and level • The recovery information for the latch • Various statistics • The process latching information is the structure kslla: • Embedded in the process state object • Array of latches that are acquired at each level • Information about the state of any current attempt to acquire a latch

  12. Latch Interface Routines: Examples • Functions that are used to acquire latches: • kslgetl() get a latch • kslgetsl() get a shared latch • kslgpl() get parent and children • kslg2c() get two child latches • Functions that are used to free latches: • kslfre() free a latch • kslfred() debugging version of kslfre() • kslf2c() free two child latches

  13. Waiting for a Latch S G A Latch Process A holds a latch Process B Process A Process B waits (spins and sleeps) CPU 1 CPU 2

  14. Latch Acquisition in No-Wait Mode • One attempt to get latch, no spin or sleep • Statistics are updated • Used when many candidate latches are available • If the latch is free, then it is acquired. • If the latch is busy, then try another candidate. • Used to bypass the latch level rule • If the latch is free, then it is acquired. • If the latch is busy, then all higher level latches are released and acquired in the correct order.

  15. Latch Acquisition in Wait Mode • Latch gets with wait • One fast get, no spin • Repeated slow gets with exponential backoff, with spin on multi-CPU • Sleep on latch free wait event between gets • Sleep time approximately doubles with each wait until upper limit is reached.

  16. Latch Release • Free the latch. • Post first process in the list of waiters (if required). • Update the statistics (waiters_woken).

  17. Latch Cleanup Function • Before changing a structure, a process writes its intentions in the latch recovery area. • If the process dies while holding the latch, then the protected structure may be corrupted. • At this point, PMON invokes the latch cleanup function. • Each latch has its own cleanup function. • The cleanup operation restores the state of the protected structure. • After cleanup, PMON releases the latch.

  18. Wait Event: LatchFree Example • The latch free wait event is associated with wait get latches. • It is seen here in Statspack output in the Top-5 Wait Events section: Top 5 Wait Events ~~~~~~~~~~~~~~~~~ Wait % Total Event Waits Time(cs) Wt Time ---------------------------------- --------- --------- ------- latch free 1,380,957 4,872,462 98.76 log file switch completion 228 23,083 .47 log file switch (chckpt incomplete) 427 21,583 .44 db file parallel write 1,740 8,266 .17 log file parallel write 2,214 4,781 .10 --------------------------------------------

  19. Wait Event: LatchFree Example • Wait Events Complete List: Waited for by Foreground processes Wait Events for DB: VIN817 Instance: vin817 Snaps: 41 -42 -> cs - centisecond - 100th of a second -> ms - millisecond - 1000th of a second -> ordered by wait time desc, waits desc (idle events last) Avg Total Wait wait Waits Event Waits Timeouts Time (cs) (ms) /txn ---------------------------------------------------------------------- latch free 1,380,957 1,371,337 4,872,462 35 ###### log file switch completion 228 225 23,083 1012 32.6 log file switch (checkpoint 427 201 21,583 505 61.0 db file parallel write 1,740 0 8,266 48 248.6 log file parallel write 2,214 0 4,781 22 316.3 control file parallel write 4,216 0 1,954 5 602.3 . . .

  20. Wait Event: LatchFree Example • Wait Events Complete List: Waited for by Background processes Avg Total Wait wait Waits Event Waits Timeouts Time (cs) (ms) /txn ------------------------------------------------------------------- db file parallel write 1,740 0 8,266 48 248.6 log file parallel write 2,214 0 4,781 22 316.3 control file parallel write 4,215 0 1,954 5 602.1 direct path read 1,368 0 226 2 195.4 LGWR wait for redo copy 176 0 122 7 25.1 file open 5,472 0 83 0 781.7 log file single write 456 0 61 1 65.1 direct path write 1,368 0 32 0 195.4 db file scattered read 296 0 19 1 42.3 control file sequential read 4,482 0 18 0 640.3 file identify 456 0 13 0 65.1 enqueue 7 0 12 17 1.0 latch free 2 2 4 20 0.3 . . .

  21. Wait Event: LatchFree Example • Latch Activity Latch Activity for DB: VIN817 Instance: vin817 Snaps: 41 -42 Pct Avg Pct Get Get Slps NoWait NoWait Latch Name Requests Miss /Miss Requests Miss ---------------------------------------------------------------------- Token Manager 2,776 0.0 0 active checkpoint queue latch 5,320 0.0 0 cache buffer handles 50 0.0 0 cache buffers chains 127,930,193 48.6 0.0 104,546 0.3 cache buffers lru chain 119,090 0.0 0.0 56,925 0.0 channel handle pool latch 54 0.0 0 channel operations parent lat 81 0.0 0 checkpoint queue latch 104,936 0.0 0.0 0 dictionary lookup 4 0.0 0 dml lock allocation 510 0.0 0 enqueue hash chains 4,969 0.0 0 ...

  22. Wait Event: LatchFree Example • Latch Sleep Breakdown Latch Sleep breakdown for DB: VIN817 Instance: vin817 Snaps: 41 -42 -> ordered by misses desc   Get Spin & Latch Name Requests Misses Sleeps Sleeps 1->4 ------------------------------------------------------------------- cache buffers chains 127,930,193 62,233,216 1,355,874 61032654/106 5731/117861/ 16970/0 library cache 75,022,933 4,088,744 25,042 4076842/3461 /4195/4246/0 latch wait list 11,753 178 31 147/31/0/0/0 shared pool 10,616,656 55 10 45/10/0/0/0 -----------------------------------------------------

  23. Wait Event: LatchFree Example Latch Miss Sources for DB: VIN817 Instance: vin817 Snaps: 41 -42 -> only latches with sleeps are shown -> ordered by name, sleeps desc NoWait Waiter Latch Name Where Misses Sleeps Sleeps -------------------------------------------------------------------- cache buffers chains kcbgcur: kslbegin 0 844,098 ####### cache buffers chains kcbrls: kslbegin 0 505,952 ####### cache buffers chains kcbgtcr: kslbegin 0 3,421 10,566 cache buffers chains kcbbxsv 0 2 0 latch wait list kslfre 3 31 31 library cache kglpnal: child: before pro 0 12,637 234 library cache kglget: child: KGLDSBYD 0 4,506 23,811 library cache kglpnal: child: alloc spac 0 4,486 336 library cache kgllkdl: child: cleanup 0 1,697 271 library cache kglpin 0 1,675 353 shared pool kghalo 0 8 0 shared pool kghupr1 0 2 10 --------------------------------------------------- • Latch Miss Sources

  24. Wait Event: LatchFree Example • Track hot blocks when cache buffers chains latch sleeps are high: • Create a temporary table to hold sleep statistics. • Create a second temporary table using the same query, at a time interval apart. • Join the two tables and determine the difference in sleeps. • Resultant output will show skewing and indicate “hot” addresses. • Determine file#, block# from x$bh.

  25. Wait Event: LatchFree Example HLADDR TCH TS# DBARFIL DBABLK CLASS DECODE(STATE,2,'S ------ ------- --- ------- ------ ----- ----------------- 0254FC8C 113 6 12 25976 1 exclusive current 0254FDC0 113 6 12 25977 1 exclusive current ... (Output truncated) 025504F8 549 6 12 28784 1 exclusive current 0255062C 552 6 12 25995 1 exclusive current 02550760 3203 6 7 353462 1 exclusive current 02550894 3648 6 7 353482 1 exclusive current

  26. Wait Event: LatchFree Example • Find the object by using the following query: SQL> SELECT segment_name, segment_type, owner, 2 tablespace_name 3 FROM sys.dba_extents 4 WHERE file_id = 7 5 AND 353482 between block_id and 6 (block_id + (blocks-1)); SEGMENT_NAME SEGMENT_TYPE OWNER TABLESPACE_NAME ----------------- ------------ --------- ---------------- ORDERS TABLE PRODTFS3 TFS301TS

  27. Using V$ Views • V$SESSION_EVENT and V$SYSTEM_EVENT give aggregate statistics at the session and system level, respectively. • V$SESSION_WAIT gives real-time information on which sessions are currently waiting or have just waited for the event.

  28. Using V$ Views • V$SESSION_WAIT provides the following information regarding the latch free wait event: • P1: Latch address • P2: Latch number • By using the latch number, obtain the latch name from V$LATCH or V$LATCHNAME. • Then drill down to V$LATCH_CHILDREN and V$LATCH_MISSES to get more information.

  29. Latch Contention • Latch contention occurs when a latch is required by several processes at the same time. • As utilization increases, latch waits also increase. • Degradation can be exponential. • Assess contention by using the sleep histograms in V$LATCH. • Look for those latches with the worst impact, not necessarily the worst utilization rate: Impact = SLEEPS * sleep duration

  30. Tuning the Spin Count • Find the balance between: • CPU time that is wasted in spinning • CPU time that is saved in context switches because of spinning • Minimize the total cost of spinning, which is calculated as • _SPIN_COUNT * SLEEPS / MISSES • In small systems with mild contention, usually increasing _SPIN_COUNT helps. • In systems under heavy contention, normally reducing _SPIN_COUNT is more effective.

  31. Highly Used Latches • Library cache: Used to locate matching SQL in the shared pool • Shared pool: Protects shared pool memory allocation • Cache buffers LRU chain: Helps to implement the block replacement policy in the buffer cache • Redo allocation: Manages the allocation of redo log buffer memory for redo entries

  32. Highly Used Latches • Cache buffers chains: Needed when searching for data blocks that are cached in the SGA • Row cache objects: Used when trying to access cached data dictionary information • Redo copy: Used to write redo records into the redo log buffer

  33. Diagnostics: Latch Views • V$LATCH: Summary statistics for all latches (grouped by latch name) • V$LATCH_PARENT: Statistics for parent (master) and solitary latches only • V$LATCH_CHILDREN: Statistics on each child latch • V$LATCHHOLDER: Current holder of latch • V$LATCH_MISSES: Useful information on latch miss statistics

  34. Diagnostics: Latches Dump • Latch information that is provided by the V$ views can also be obtained by using the following command: • Oracle trace files may contain latch information in the process and session state objects. • Event 10005 can also be set to perform latch tracing. SQL> ALTER SESSION SET EVENTS 2 'IMMEDIATE TRACE NAME LATCHES LEVEL 10';

  35. Latch Contention Diagnosis: Example • Analyze the latch free wait event. SQL> SELECT sid, substr(event,1,12) "Event", 2 time_waited, round(average_wait,2) "Average" 3 FROM v$session_event 4 WHERE event ='latch free' 5 AND time_waited != 0 6 ORDER BY time_waited DESC; SID Event TIME_WAITED Average --------- ------------ ----------- --------- 2 latch free 10 .3 15 latch free 2 1

  36. Latch Contention Diagnosis: Example • Determine which latch has the greatest impact. SQL> SELECT latch#, substr(name,1,25) "Latch", sleeps 2 FROM v$latch 3 WHERE sleeps!=0 4 ORDER BY sleeps desc; LATCH# Latch SLEEPS ---------- -------------------------- ------ 97 cache buffers chains 304326 155 library cache 260 92 cache buffers lru chain 233 4 session allocation 2 91 batching SCNs 2

  37. Latch Contention Diagnosis: Example • Collect information for the corresponding child latches. SQL> SELECT addr, gets, misses, sleeps, spin_gets, sleep1, 2 sleep2, sleep3 3 FROM v$latch_children 4 WHERE latch#=97 5 ORDER BY sleeps DESC; ADDR GETS MISSES SLEEPS SPIN_GETS SLEEP1 SLEEP2 SLEEP3 -------- -------- ------- -------- --------- -------- -------- -------- 8025A108 2588104 74632 273209 30 32 53 74517 802514C4 1269415 122 237 16 27 67 12 80259CA0 1252124 90 225 12 20 37 21 8026013C 1180490 96 216 17 19 42 18 80264458 1084178 89 173 23 23 28 15 8025BB78 1054608 69 121 24 11 24 10 8025D52C 1075960 58 85 16 7 27 8 80259BE4 919071 53 77 9 18 19 7

  38. Latch Contention Diagnosis: Example • Find out which buffer makes use of the “hot” latch. SQL> SELECT hladdr, dbarfil, dbablk, class, state, TCH 2 FROM x$bh 3 WHERE hladdr IN 4 ('8025A108','802514C4','80259CA0') 5 ORDER BY TCH DESC; HLADDR DBARFIL DBABLK CLASS STATE TCH -------- ------- ---------- ---------- ------- ------- 8025A108 2 293 20 1 876 802514C4 5 18387 4 3 287 80259CA0 1 35644 1 1 269

  39. Latch Contention Diagnosis: Example • Identify the segments that are associated with these blocks: SQL> SELECT owner, segment_name "Name", segment_type "Type" 2 FROM dba_extents 3 WHERE file_id = 2 4 AND block_id <= 293 5 AND block_id + blocks > 293; OWNER Name Type -------------------- --------- ------------ SYSDUALTABLE SYS C_TS# CLUSTER SCOTT EMPNO_X INDEX

  40. Latch Monitoring Summary • Monitor latches at higher levels first and focus on the latches with the biggest impact. • If it is a solitary latch, then reduce the load on it. • If contention is on a child latch, then: • Identify the hot resource • Eliminate or reduce the load on the hot resource • Advanced Monitoring • Tune spin count • Tune latch wait posting

  41. Summary • In this lesson, you should have learned about: • The internal implementation of latches in the Oracle server • Latch statistics • Monitoring latch contention • Diagnostics tools for dealing with latching issues

  42. Reference • Source: ksl.h,kgsl.h

More Related