-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy path14_collect_memory_stats.sql
More file actions
334 lines (308 loc) · 10.4 KB
/
14_collect_memory_stats.sql
File metadata and controls
334 lines (308 loc) · 10.4 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
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/*
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
/*
Memory statistics collector
Collects memory usage from sys.dm_os_memory_clerks, sys.dm_os_process_memory, and sys.dm_os_sys_memory
*/
IF OBJECT_ID(N'collect.memory_stats_collector', N'P') IS NULL
BEGIN
EXECUTE(N'CREATE PROCEDURE collect.memory_stats_collector AS RETURN 138;');
END;
GO
ALTER PROCEDURE
collect.memory_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(),
@previous_buffer_pool_mb decimal(19,2),
@previous_plan_cache_mb decimal(19,2);
BEGIN TRY
BEGIN TRANSACTION;
/*
Ensure target table exists
*/
IF OBJECT_ID(N'collect.memory_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'memory_stats_collector',
N'TABLE_MISSING',
0,
0,
N'Table collect.memory_stats does not exist, calling ensure procedure'
);
/*
Call procedure to create table
*/
EXECUTE config.ensure_collection_table
@table_name = N'memory_stats',
@debug = @debug;
/*
Verify table now exists
*/
IF OBJECT_ID(N'collect.memory_stats', N'U') IS NULL
BEGIN
ROLLBACK TRANSACTION;
RAISERROR(N'Table collect.memory_stats still missing after ensure procedure', 16, 1);
RETURN;
END;
END;
/*
Get previous collection values for pressure detection
A 20%+ drop in buffer pool or plan cache indicates memory pressure
*/
SELECT
@previous_buffer_pool_mb = ms.buffer_pool_mb,
@previous_plan_cache_mb = ms.plan_cache_mb
FROM collect.memory_stats AS ms
WHERE ms.collection_id =
(
SELECT
MAX(ms2.collection_id)
FROM collect.memory_stats AS ms2
);
/*
Collect memory statistics from various DMVs
Aggregates memory clerk data into useful categories
Calculates pressure warnings based on comparison to previous collection
*/
WITH
memory_clerks AS
(
SELECT
buffer_pool_mb =
SUM
(
CASE
WHEN mc.type = N'MEMORYCLERK_SQLBUFFERPOOL'
THEN mc.pages_kb
ELSE 0
END
) / 1024.0,
plan_cache_mb =
SUM
(
CASE
WHEN mc.type IN
(
N'CACHESTORE_SQLCP',
N'CACHESTORE_OBJCP'
)
THEN mc.pages_kb
ELSE 0
END
) / 1024.0,
other_memory_mb =
SUM
(
CASE
WHEN mc.type NOT IN
(
N'MEMORYCLERK_SQLBUFFERPOOL',
N'CACHESTORE_SQLCP',
N'CACHESTORE_OBJCP'
)
THEN mc.pages_kb
ELSE 0
END
) / 1024.0,
total_memory_mb = SUM(mc.pages_kb) / 1024.0
FROM sys.dm_os_memory_clerks AS mc
WHERE mc.pages_kb > 0
),
process_memory AS
(
SELECT
physical_memory_in_use_mb = pm.physical_memory_in_use_kb / 1024.0,
memory_utilization_percentage = pm.memory_utilization_percentage
FROM sys.dm_os_process_memory AS pm
),
system_memory AS
(
SELECT
available_physical_memory_mb = sm.available_physical_memory_kb / 1024.0,
total_physical_memory_mb = sm.total_physical_memory_kb / 1024.0
FROM sys.dm_os_sys_memory AS sm
),
system_info AS
(
SELECT
committed_target_memory_mb = si.committed_target_kb / 1024.0
FROM sys.dm_os_sys_info AS si
)
INSERT INTO
collect.memory_stats
(
buffer_pool_mb,
plan_cache_mb,
other_memory_mb,
total_memory_mb,
physical_memory_in_use_mb,
available_physical_memory_mb,
memory_utilization_percentage,
total_physical_memory_mb,
committed_target_memory_mb,
buffer_pool_pressure_warning,
plan_cache_pressure_warning
)
SELECT
buffer_pool_mb = mc.buffer_pool_mb,
plan_cache_mb = mc.plan_cache_mb,
other_memory_mb = mc.other_memory_mb,
total_memory_mb = mc.total_memory_mb,
physical_memory_in_use_mb = pm.physical_memory_in_use_mb,
available_physical_memory_mb = sm.available_physical_memory_mb,
memory_utilization_percentage = pm.memory_utilization_percentage,
total_physical_memory_mb = sm.total_physical_memory_mb,
committed_target_memory_mb = si.committed_target_memory_mb,
buffer_pool_pressure_warning =
CASE
WHEN @previous_buffer_pool_mb IS NOT NULL
AND mc.buffer_pool_mb < (@previous_buffer_pool_mb * 0.80)
THEN 1
ELSE 0
END,
plan_cache_pressure_warning =
CASE
WHEN @previous_plan_cache_mb IS NOT NULL
AND mc.plan_cache_mb < (@previous_plan_cache_mb * 0.80)
THEN 1
ELSE 0
END
FROM memory_clerks AS mc
CROSS JOIN process_memory AS pm
CROSS JOIN system_memory AS sm
CROSS JOIN system_info AS si
OPTION(RECOMPILE);
SET @rows_collected = ROWCOUNT_BIG();
/*
Debug output for pressure warnings
*/
IF @debug = 1
BEGIN
DECLARE
@current_buffer_pool_mb decimal(19,2),
@current_plan_cache_mb decimal(19,2),
@buffer_warning bit,
@plan_warning bit;
SELECT
@current_buffer_pool_mb = ms.buffer_pool_mb,
@current_plan_cache_mb = ms.plan_cache_mb,
@buffer_warning = ms.buffer_pool_pressure_warning,
@plan_warning = ms.plan_cache_pressure_warning
FROM collect.memory_stats AS ms
WHERE ms.collection_id =
(
SELECT
MAX(ms2.collection_id)
FROM collect.memory_stats AS ms2
);
IF @buffer_warning = 1
BEGIN
DECLARE @buffer_msg nvarchar(500) =
N'WARNING: Buffer pool dropped from ' +
CONVERT(nvarchar(20), @previous_buffer_pool_mb) + N' MB to ' +
CONVERT(nvarchar(20), @current_buffer_pool_mb) + N' MB (>20% drop)';
RAISERROR(@buffer_msg, 0, 1) WITH NOWAIT;
END;
IF @plan_warning = 1
BEGIN
DECLARE @plan_msg nvarchar(500) =
N'WARNING: Plan cache dropped from ' +
CONVERT(nvarchar(20), @previous_plan_cache_mb) + N' MB to ' +
CONVERT(nvarchar(20), @current_plan_cache_mb) + N' MB (>20% drop)';
RAISERROR(@plan_msg, 0, 1) WITH NOWAIT;
END;
END;
/*
Log successful collection
*/
INSERT INTO
config.collection_log
(
collector_name,
collection_status,
rows_collected,
duration_ms
)
VALUES
(
N'memory_stats_collector',
N'SUCCESS',
@rows_collected,
DATEDIFF(MILLISECOND, @start_time, SYSDATETIME())
);
IF @debug = 1
BEGIN
RAISERROR(N'Collected %d memory stats rows', 0, 1, @rows_collected) WITH NOWAIT;
END;
COMMIT TRANSACTION;
END TRY
BEGIN CATCH
IF @@TRANCOUNT > 0
BEGIN
ROLLBACK TRANSACTION;
END;
DECLARE
@error_message nvarchar(4000) = ERROR_MESSAGE();
/*
Log the error
*/
INSERT INTO
config.collection_log
(
collector_name,
collection_status,
duration_ms,
error_message
)
VALUES
(
N'memory_stats_collector',
N'ERROR',
DATEDIFF(MILLISECOND, @start_time, SYSDATETIME()),
@error_message
);
RAISERROR(N'Error in memory stats collector: %s', 16, 1, @error_message);
END CATCH;
END;
GO
PRINT 'Memory stats collector created successfully';
PRINT 'Includes pressure warnings for 20%+ drops in buffer pool or plan cache';
GO