Skip to content

Instantly share code, notes, and snippets.

@petesql
Last active February 18, 2024 02:06
Show Gist options
  • Save petesql/6dee542d010e9cda0ecd15e43b35c8be to your computer and use it in GitHub Desktop.
Save petesql/6dee542d010e9cda0ecd15e43b35c8be to your computer and use it in GitHub Desktop.
SQL Server Get CPU Utilization History
-- Get CPU Utilization History for last 4hrs (one minute intervals)
DECLARE @ts_now bigint = (SELECT ms_ticks FROM sys.dm_os_sys_info WITH (NOLOCK));
SELECT
SQLProcessUtilization,
SystemIdle As SystemIdleProcess,
100 - SystemIdle - SQLProcessUtilization AS OtherProcessCPUUtilization,
DATEADD(ms, -1 * (@ts_now - timestamp), GETDATE()) AS EventTime
FROM ( SELECT record.value('(./Record/@id)[1]', 'int') AS record_id,
record.value('(./Record/SchedulerMonitorEvent/SystemHealth/SystemIdle)[1]', 'int') AS SystemIdle,
record.value('(./Record/SchedulerMonitorEvent/SystemHealth/ProcessUtilization)[1]', 'int') AS SQLProcessUtilization, timestamp
FROM ( SELECT timestamp, CONVERT(xml, record) AS record
FROM sys.dm_os_ring_buffers WITH (NOLOCK)
WHERE ring_buffer_type = N'RING_BUFFER_SCHEDULER_MONITOR'
AND record LIKE N'%<SystemHealth>%') AS x) AS y
ORDER BY record_id DESC OPTION (RECOMPILE);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment