-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy path18_collect_cpu_utilization_stats.sql
More file actions
228 lines (206 loc) · 6.65 KB
/
18_collect_cpu_utilization_stats.sql
File metadata and controls
228 lines (206 loc) · 6.65 KB
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
/*
Copyright 2026 Darling Data, LLC
https://www.erikdarling.com/
*/
SET ANSI_NULLS ON;
SET ANSI_PADDING ON;
SET ANSI_WARNINGS ON;
SET ARITHABORT ON;
SET CONCAT_NULL_YIELDS_NULL ON;
SET QUOTED_IDENTIFIER ON;
SET NUMERIC_ROUNDABORT OFF;
SET IMPLICIT_TRANSACTIONS OFF;
SET STATISTICS TIME, IO OFF;
GO
USE PerformanceMonitor;
GO
/*
CPU utilization statistics collector
Collects CPU utilization events from sys.dm_os_ring_buffers (SCHEDULER_MONITOR)
Tracks SQL Server CPU vs other process CPU utilization over time
Point-in-time samples stored by ring buffer - not cumulative
*/
IF OBJECT_ID(N'collect.cpu_utilization_stats_collector', N'P') IS NULL
BEGIN
EXECUTE(N'CREATE PROCEDURE collect.cpu_utilization_stats_collector AS RETURN 138;');
END;
GO
ALTER PROCEDURE
collect.cpu_utilization_stats_collector
(
@debug bit = 0 /*Print debugging information*/
)
WITH RECOMPILE
AS
BEGIN
SET NOCOUNT ON;
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
DECLARE
@rows_collected bigint = 0,
@start_time datetime2(7) = SYSDATETIME(),
@current_ms_ticks bigint =
(
SELECT
osi.ms_ticks
FROM sys.dm_os_sys_info AS osi
),
@max_sample_time datetime2(7) = NULL,
@error_message nvarchar(4000);
BEGIN TRY
BEGIN TRANSACTION;
/*
Ensure target table exists
*/
IF OBJECT_ID(N'collect.cpu_utilization_stats', N'U') IS NULL
BEGIN
/*
Log missing table before attempting to create
*/
INSERT INTO
config.collection_log
(
collection_time,
collector_name,
collection_status,
rows_collected,
duration_ms,
error_message
)
VALUES
(
@start_time,
N'cpu_utilization_stats_collector',
N'TABLE_MISSING',
0,
0,
N'Table collect.cpu_utilization_stats does not exist, calling ensure procedure'
);
/*
Call procedure to create table
*/
EXECUTE config.ensure_collection_table
@table_name = N'cpu_utilization_stats',
@debug = @debug;
/*
Verify table now exists
*/
IF OBJECT_ID(N'collect.cpu_utilization_stats', N'U') IS NULL
BEGIN
RAISERROR(N'Table collect.cpu_utilization_stats still missing after ensure procedure', 16, 1);
RETURN;
END;
END;
/*
Get the most recent sample time to avoid duplicate collection
*/
SELECT
@max_sample_time = MAX(cus.sample_time)
FROM collect.cpu_utilization_stats AS cus;
/*
Collect CPU utilization data from ring buffers
Only collects samples newer than the most recent sample we have
On first run (NULL max_sample_time), looks back 1 hour to populate initial data
Avoids duplicate collection of same ring buffer events
*/
INSERT INTO
collect.cpu_utilization_stats
(
sample_time,
sqlserver_cpu_utilization,
other_process_cpu_utilization
)
SELECT
sample_time =
DATEADD
(
SECOND,
-((@current_ms_ticks - t.timestamp) / 1000),
@start_time
),
sqlserver_cpu_utilization =
t.record.value('(Record/SchedulerMonitorEvent/SystemHealth/ProcessUtilization)[1]', 'integer'),
other_process_cpu_utilization =
CASE
WHEN (100 -
t.record.value('(Record/SchedulerMonitorEvent/SystemHealth/SystemIdle)[1]', 'integer') -
t.record.value('(Record/SchedulerMonitorEvent/SystemHealth/ProcessUtilization)[1]', 'integer')) < 0
THEN 0
ELSE 100 -
t.record.value('(Record/SchedulerMonitorEvent/SystemHealth/SystemIdle)[1]', 'integer') -
t.record.value('(Record/SchedulerMonitorEvent/SystemHealth/ProcessUtilization)[1]', 'integer')
END
FROM
(
SELECT
dorb.timestamp,
record =
CONVERT(xml, dorb.record)
FROM sys.dm_os_ring_buffers AS dorb
WHERE dorb.ring_buffer_type = N'RING_BUFFER_SCHEDULER_MONITOR'
) AS t
WHERE DATEADD
(
SECOND,
-((@current_ms_ticks - t.timestamp) / 1000),
@start_time
) > ISNULL(@max_sample_time, DATEADD(HOUR, -1, @start_time))
ORDER BY
t.timestamp DESC
OPTION(RECOMPILE);
SET @rows_collected = ROWCOUNT_BIG();
/*
Log successful collection
*/
INSERT INTO
config.collection_log
(
collector_name,
collection_status,
rows_collected,
duration_ms
)
VALUES
(
N'cpu_utilization_stats_collector',
N'SUCCESS',
@rows_collected,
DATEDIFF(MILLISECOND, @start_time, SYSDATETIME())
);
IF @debug = 1
BEGIN
RAISERROR(N'Collected %d CPU utilization stats rows', 0, 1, @rows_collected) WITH NOWAIT;
END;
COMMIT TRANSACTION;
END TRY
BEGIN CATCH
IF @@TRANCOUNT > 0
BEGIN
ROLLBACK TRANSACTION;
END;
SET @error_message = ERROR_MESSAGE();
/*
Log the error
*/
INSERT INTO
config.collection_log
(
collector_name,
collection_status,
duration_ms,
error_message
)
VALUES
(
N'cpu_utilization_stats_collector',
N'ERROR',
DATEDIFF(MILLISECOND, @start_time, SYSDATETIME()),
@error_message
);
RAISERROR(N'Error in CPU utilization stats collector: %s', 16, 1, @error_message);
END CATCH;
END;
GO
PRINT 'CPU utilization stats collector created successfully';
PRINT 'Collects CPU utilization events from sys.dm_os_ring_buffers (SCHEDULER_MONITOR ring buffer)';
PRINT 'Tracks SQL Server CPU vs other process CPU utilization over time';
GO