In terms of monitoring
latches, you’ve already been introduced to some of the DMVs. There’s
more to monitoring latches than just DMVs, though. Performance Monitor
also offers useful information about latches, as do extended events,
which also provide information about spinlocks. Another option is to
use memory dumps, but those are not covered here.
DMVs
The DMVs covered earlier are a useful point of reference. You should familiarize yourself with the contents of sys.dm_os_wait_stats, sys.dm_os_latch_stats, and sys.dm_os_spinlock_stats, and be comfortable with the output they provide. In addition, sys.dm_os_waiting_tasks will display a list of any tasks that are currently waiting on a resource, providing a useful session_id column that can be used to hook into other useful DMVs for information about sessions and the like.
sys.dm_os_wait_stats
This DMV has five columns.
- wait_type
- waiting_tasks_count
- wait_time_ms
- max_wait_time_ms
- signal_wait_time_ms
The first three were described earlier. max_wait_time_ms shows the largest wait time for a single wait since the DMV was cleared. signal_wait_time_ms is less relevant for latches, although it does get used if threads hit spinlock barriers.
sys.dm_os_latch_stats
This DMV has four columns.
- latch_class
- waiting_requests_count
- wait_time_ms
- max_wait_time_ms
These columns have all been described earlier.
sys.dm_os_spinlock_stats
This DMV has six columns.
- name
- collisions
- spins
- spins_per_collision
- sleep_time
- backoffs
A collision is recorded when a spinlock tries to
acquire a resource but finds it unavailable. As a result, the spinlock
starts spinning. This increases the spins but the collision has already
been recorded. Usefully, this DMV also provides a spins_per_collision column, saving the user from doing the calculation.
I’m sure you can imagine that the number of spins
is potentially quite large. Let’s just say that it’s a good thing that
this column is a bigint type, which handles numbers up to 19 digits
long. I don’t think the correct technical term is actually
“gazillions,” but it feels right when you take a look at this DMV on
busy systems that have been up for a while.
The sleep_time and backoffs columns simply report the amount of time that has been spent sleeping on spinlocks, and the number of backoffs.
Performance Monitor
Performance Monitor provides several useful counters to keep an eye on. Figure 1 shows a typical screenshot containing the list of counters in the SQLServer:Latches category for a machine.
Table 1 describes these counters.
TABLE 1: Useful Performance Monitor Counters
COUNTER |
DESCRIPTION |
Average Latch Wait Time (ms) |
Average latch wait time (in milliseconds) for latch requests that had to wait |
Latch Waits/sec |
Number of latch requests that could not be granted immediately and had to wait before being granted |
Number of SuperLatches |
Number of latches that are currently SuperLatches |
SuperLatch Demotions/sec |
Number of SuperLatches that have been demoted to regular latches |
SuperLatch Promotions/sec |
Number of latches that have been promoted to SuperLatches |
Total Latch Wait Time (ms) |
Total latch wait time (in milliseconds) for latch requests that had to wait in the last second |
These performance counter values are also available using the DMV sys.dm_os_performance_counters (see Figure 2) (code file Ch7Monitoring.sql):
SELECT *
FROM sys.dm_os_performance_counters
WHERE object_name LIKE '%Latches%';
Note that although the object_name field appears to end in the string ’Latches’,
this field is actually stored as nchar(256), rather than nvarchar(256),
so there is a large amount of whitespace at the end, and that last % is needed.
Extended Events
If you open the New Session
Wizard for Extended Events from SQL Server 2012 Management Studio, you
will reach a screen from which you select the events you wish to
capture. After reaching this, first scroll the Event library section to
reveal the Channel drop-down box. Then, as shown in Figure 3, check Debug, which is unchecked by default.
Now you can search for spinlock and latch to find a list of extended events related to these areas, as described in Table 2.
TABLE 2: Spinlock and Latch Extended Events
EXTENDED EVENT |
DESCRIPTION |
spinlock_backoff |
Spinlock backoff |
spinlock_backoff_warning |
Occurs when a spinlock backoff warning is sent to the Error Log |
latch_acquire_time |
Time taken to acquire a latch |
latch_demoted |
Occurs when a SuperLatch is demoted to an ordinary latch |
latch_promoted |
Occurs when a latch is promoted to a SuperLatch |
latch_suspend_begin |
Occurs when the executing task must suspend while waiting for a latch to become available in the requested mode |
latch_suspend_end |
Occurs when the executing task is resumed after waiting for a latch |
latch_suspend_warning |
Occurs when there is a timeout waiting for a latch possibly causing performance problems |
You should now be able to create an XE
session collecting these events. Bear in mind that you would typically
expect to see many more latch_acquire_time events occurring than the other event types, and you might not want to bother collecting them.