forked from DavidWiseman/sqlserver-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbo.sp_Blocked_Process_Report_Viewer.sql
316 lines (277 loc) · 11.7 KB
/
dbo.sp_Blocked_Process_Report_Viewer.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
USE master
GO
IF OBJECT_ID('sp_blocked_process_report_viewer') IS NULL
EXEC ('
CREATE PROCEDURE dbo.sp_blocked_process_report_viewer
AS
SELECT ''Replace Me''')
GO
ALTER PROCEDURE dbo.sp_blocked_process_report_viewer
(
@Source nvarchar(max),
@Type varchar(10) = 'FILE'
)
AS
/*
Original link: http://michaeljswart.com/2011/04/a-new-way-to-examine-blocked-process-reports/
Author: Michael J. Swart
*/
SET NOCOUNT ON
-- Validate @Type
IF (@Type NOT IN ('FILE', 'TABLE', 'XMLFILE', 'XESESSION'))
RAISERROR ('The @Type parameter must be ''FILE'', ''TABLE'' or ''XMLFILE''', 11, 1)
IF (@Source LIKE '%.trc' AND @Type <> 'FILE')
RAISERROR ('Warning: You specified a .trc trace. You should also specify @Type = ''FILE''', 10, 1)
IF (@Source LIKE '%.xml' AND @Type <> 'XMLFILE')
RAISERROR ('Warning: You specified a .xml trace. You should also specify @Type = ''XMLFILE''', 10, 1)
IF (@Type = 'XESESSION' AND NOT EXISTS (
SELECT *
FROM sys.server_event_sessions es
JOIN sys.server_event_session_targets est
ON es.event_session_id = est.event_session_id
WHERE est.name in ('event_file', 'ring_buffer')
AND es.name = @Source )
)
RAISERROR ('Warning: The extended event session you supplied does not exist or does not have an "event_file" or "ring_buffer" target.', 10, 1);
CREATE TABLE #ReportsXML
(
monitorloop nvarchar(100) NOT NULL,
endTime datetime NULL,
blocking_spid INT NOT NULL,
blocking_ecid INT NOT NULL,
blocked_spid INT NOT NULL,
blocked_ecid INT NOT NULL,
blocked_hierarchy_string as CAST(blocked_spid as varchar(20)) + '.' + CAST(blocked_ecid as varchar(20)) + '/',
blocking_hierarchy_string as CAST(blocking_spid as varchar(20)) + '.' + CAST(blocking_ecid as varchar(20)) + '/',
bpReportXml xml not null,
primary key clustered (monitorloop, blocked_spid, blocked_ecid),
unique nonclustered (monitorloop, blocking_spid, blocking_ecid, blocked_spid, blocked_ecid)
);
DECLARE @SQL NVARCHAR(max);
DECLARE @TableSource nvarchar(max);
-- define source for table
IF (@Type = 'TABLE')
BEGIN
-- everything input by users get quoted
SET @TableSource = ISNULL(QUOTENAME(PARSENAME(@Source,4)) + N'.', '')
+ ISNULL(QUOTENAME(PARSENAME(@Source,3)) + N'.', '')
+ ISNULL(QUOTENAME(PARSENAME(@Source,2)) + N'.', '')
+ QUOTENAME(PARSENAME(@Source,1));
END
-- define source for trc file
IF (@Type = 'FILE')
BEGIN
SET @TableSource = N'sys.fn_trace_gettable(N' + QUOTENAME(@Source, '''') + ', -1)';
END
-- load table or file
IF (@Type IN ('TABLE', 'FILE' ))
BEGIN
SET @SQL = N'
INSERT #ReportsXML(blocked_ecid,blocked_spid,blocking_ecid,blocking_spid,
monitorloop,bpReportXml,endTime)
SELECT blocked_ecid,blocked_spid,blocking_ecid,blocking_spid,
COALESCE(monitorloop, CONVERT(nvarchar(100), endTime, 120), cast(newid() as nvarchar(100))),
bpReportXml,EndTime
FROM ' + @TableSource + N'
CROSS APPLY (
SELECT CAST(TextData as xml)
) AS bpReports(bpReportXml)
CROSS APPLY (
SELECT
monitorloop = bpReportXml.value(''(//@monitorLoop)[1]'', ''nvarchar(100)''),
blocked_spid = bpReportXml.value(''(/blocked-process-report/blocked-process/process/@spid)[1]'', ''int''),
blocked_ecid = bpReportXml.value(''(/blocked-process-report/blocked-process/process/@ecid)[1]'', ''int''),
blocking_spid = bpReportXml.value(''(/blocked-process-report/blocking-process/process/@spid)[1]'', ''int''),
blocking_ecid = bpReportXml.value(''(/blocked-process-report/blocking-process/process/@ecid)[1]'', ''int'')
) AS bpShredded
WHERE EventClass = 137
AND blocking_spid is not null
AND blocked_spid is not null';
EXEC (@SQL);
END
IF (@Type = 'XESESSION')
BEGIN
DECLARE @SessionType sysname;
DECLARE @SessionId int;
DECLARE @SessionTargetId int;
DECLARE @FilenamePattern sysname;
SELECT TOP ( 1 )
@SessionType = est.name,
@SessionId = est.event_session_id,
@SessionTargetId = est.target_id
FROM sys.server_event_sessions es
JOIN sys.server_event_session_targets est
ON es.event_session_id = est.event_session_id
WHERE est.name in ('event_file', 'ring_buffer')
AND es.name = @Source;
IF (@SessionType = 'event_file')
BEGIN
SELECT @filenamePattern = REPLACE( CAST([value] AS sysname), '.xel', '*xel' )
FROM sys.server_event_session_fields
WHERE event_session_id = @SessionId
AND [object_id] = @SessionTargetId
AND name = 'filename'
IF (@filenamePattern not like '%xel')
set @filenamePattern += '*xel';
INSERT #ReportsXML(blocked_ecid,blocked_spid,blocking_ecid,blocking_spid,
monitorloop,bpReportXml,endTime)
SELECT blocked_ecid,blocked_spid,blocking_ecid,blocking_spid,
COALESCE(monitorloop, CONVERT(nvarchar(100), eventDate, 120), cast(newid() as nvarchar(100))),
bpReportXml,eventDate
FROM sys.fn_xe_file_target_read_file ( @filenamePattern, null, null, null)
as event_file_value
CROSS APPLY ( SELECT CAST(event_file_value.[event_data] as xml) )
as event_file_value_xml ([xml])
CROSS APPLY (
SELECT
event_file_value_xml.[xml].value('(event/@timestamp)[1]', 'datetime') as eventDate,
event_file_value_xml.[xml].query('//event/data/value/blocked-process-report') as bpReportXml
) as bpReports
CROSS APPLY (
SELECT
monitorloop = bpReportXml.value('(//@monitorLoop)[1]', 'nvarchar(100)'),
blocked_spid = bpReportXml.value('(/blocked-process-report/blocked-process/process/@spid)[1]', 'int'),
blocked_ecid = bpReportXml.value('(/blocked-process-report/blocked-process/process/@ecid)[1]', 'int'),
blocking_spid = bpReportXml.value('(/blocked-process-report/blocking-process/process/@spid)[1]', 'int'),
blocking_ecid = bpReportXml.value('(/blocked-process-report/blocking-process/process/@ecid)[1]', 'int')
) AS bpShredded
WHERE blocking_spid is not null
AND blocked_spid is not null;
END
ELSE IF (@SessionType = 'ring_buffer')
BEGIN
-- get data from ring buffer
INSERT #ReportsXML(blocked_ecid,blocked_spid,blocking_ecid,blocking_spid,
monitorloop,bpReportXml,endTime)
SELECT blocked_ecid,blocked_spid,blocking_ecid,blocking_spid,
COALESCE(monitorloop, CONVERT(nvarchar(100), bpReportEndTime, 120), cast(newid() as nvarchar(100))),
bpReportXml,bpReportEndTime
FROM sys.dm_xe_session_targets st
JOIN sys.dm_xe_sessions s
ON s.address = st.event_session_address
CROSS APPLY
( SELECT CAST(st.target_data AS XML) )
AS TargetData ([xml])
CROSS APPLY
TargetData.[xml].nodes('/RingBufferTarget/event[@name="blocked_process_report"]')
AS bpNodes(bpNode)
CROSS APPLY
bpNode.nodes('./data[@name="blocked_process"]/value/blocked-process-report')
AS bpReportXMLNodes(bpReportXMLNode)
CROSS APPLY
(
SELECT
bpReportXml = CAST(bpReportXMLNode.query('.') as xml),
bpReportEndTime = bpNode.value('(./@timestamp)[1]', 'datetime'),
monitorloop = bpReportXMLNode.value('(//@monitorLoop)[1]', 'nvarchar(100)'),
blocked_spid = bpReportXMLNode.value('(./blocked-process/process/@spid)[1]', 'int'),
blocked_ecid = bpReportXMLNode.value('(./blocked-process/process/@ecid)[1]', 'int'),
blocking_spid = bpReportXMLNode.value('(./blocking-process/process/@spid)[1]', 'int'),
blocking_ecid = bpReportXMLNode.value('(./blocking-process/process/@ecid)[1]', 'int')
) AS bpShredded
WHERE s.name = @Source
OPTION (MAXDOP 1);
END
END
IF (@Type = 'XMLFILE')
BEGIN
CREATE TABLE #TraceXML (
id int identity primary key,
ReportXML xml NOT NULL
)
SET @SQL = N'
INSERT #TraceXML(ReportXML)
SELECT col FROM OPENROWSET (
BULK ' + QUOTENAME(@Source, '''') + N', SINGLE_BLOB
) as xmldata(col)';
EXEC (@SQL);
CREATE PRIMARY XML INDEX PXML_TraceXML ON #TraceXML(ReportXML);
WITH XMLNAMESPACES
(
'http://tempuri.org/TracePersistence.xsd' AS MY
),
ShreddedWheat AS
(
SELECT
bpShredded.blocked_ecid,
bpShredded.blocked_spid,
bpShredded.blocking_ecid,
bpShredded.blocking_spid,
bpShredded.monitorloop,
bpReports.bpReportXml,
bpReports.bpReportEndTime
FROM #TraceXML
CROSS APPLY
ReportXML.nodes('/MY:TraceData/MY:Events/MY:Event[@name="Blocked process report"]')
AS eventNodes(eventNode)
CROSS APPLY
eventNode.nodes('./MY:Column[@name="EndTime"]')
AS endTimeNodes(endTimeNode)
CROSS APPLY
eventNode.nodes('./MY:Column[@name="TextData"]')
AS bpNodes(bpNode)
CROSS APPLY (
SELECT CAST(bpNode.value('(./text())[1]', 'nvarchar(max)') as xml),
CAST(LEFT(endTimeNode.value('(./text())[1]', 'varchar(max)'), 19) as datetime)
) AS bpReports(bpReportXml, bpReportEndTime)
CROSS APPLY (
SELECT
monitorloop = bpReportXml.value('(//@monitorLoop)[1]', 'nvarchar(100)'),
blocked_spid = bpReportXml.value('(/blocked-process-report/blocked-process/process/@spid)[1]', 'int'),
blocked_ecid = bpReportXml.value('(/blocked-process-report/blocked-process/process/@ecid)[1]', 'int'),
blocking_spid = bpReportXml.value('(/blocked-process-report/blocking-process/process/@spid)[1]', 'int'),
blocking_ecid = bpReportXml.value('(/blocked-process-report/blocking-process/process/@ecid)[1]', 'int')
) AS bpShredded
)
INSERT #ReportsXML(blocked_ecid,blocked_spid,blocking_ecid,blocking_spid,
monitorloop,bpReportXml,endTime)
SELECT blocked_ecid,blocked_spid,blocking_ecid,blocking_spid,
COALESCE(monitorloop, CONVERT(nvarchar(100), bpReportEndTime, 120), 'unknown'),
bpReportXml,bpReportEndTime
FROM ShreddedWheat;
DROP TABLE #TraceXML
END
-- Organize and select blocked process reports
;WITH Blockheads AS
(
SELECT blocking_spid, blocking_ecid, monitorloop, blocking_hierarchy_string
FROM #ReportsXML
EXCEPT
SELECT blocked_spid, blocked_ecid, monitorloop, blocked_hierarchy_string
FROM #ReportsXML
),
Hierarchy AS
(
SELECT monitorloop, blocking_spid as spid, blocking_ecid as ecid,
cast('/' + blocking_hierarchy_string as varchar(max)) as chain,
0 as level
FROM Blockheads
UNION ALL
SELECT irx.monitorloop, irx.blocked_spid, irx.blocked_ecid,
cast(h.chain + irx.blocked_hierarchy_string as varchar(max)),
h.level+1
FROM #ReportsXML irx
JOIN Hierarchy h
ON irx.monitorloop = h.monitorloop
AND irx.blocking_spid = h.spid
AND irx.blocking_ecid = h.ecid
)
SELECT
ISNULL(CONVERT(nvarchar(30), irx.endTime, 120),
'Lead') as traceTime,
SPACE(4 * h.level)
+ CAST(h.spid as varchar(20))
+ CASE h.ecid
WHEN 0 THEN ''
ELSE '(' + CAST(h.ecid as varchar(20)) + ')'
END AS blockingTree,
irx.bpReportXml
FROM Hierarchy h
LEFT JOIN #ReportsXML irx
ON irx.monitorloop = h.monitorloop
AND irx.blocked_spid = h.spid
AND irx.blocked_ecid = h.ecid
ORDER BY h.monitorloop, h.chain;
DROP TABLE #ReportsXML;
END;
GO