Get-DbaCpuUsage
Is this a feature OR bug: Enhancement
https://trello.com/c/gJhptCU0
Simply returns a present value of records from the ring buffer (T-SQL) that holds the CPU usage for SQL Server. Don't have blog post reference for code I've used but can return SQL Server CPU usage, System Idle, and then calculated "Other CPU usage". Which the last column would be CPU usage for anything else running on the system.
Additional comments
I have a runspace script that handles it but if you want to convert over for dbatools, I do not mind at all. I have not had the time due to a change/enhancement of duties.
@wsmelton - you mentioned having code for this. Can you paste me the T-SQL and I can create this command?
Below is script that I use to extract CPU Usage from sys.dm_os_ring_buffers This is set to get the last hour but usually the last 4 hours is available
DECLARE @SQLString NVARCHAR(500), @ParmDefinition NVARCHAR(500), @ts_now BIGINT, @dt_max BIGINT, @dt_min BIGINT
If (@@microsoftversion / 16777216) > 9 SET @SQLString = N'SELECT @ts_nowOUT = cpu_ticks / CONVERT (float, ( cpu_ticks / ms_ticks )) FROM sys.dm_os_sys_info' ELSE SET @SQLString = N'SELECT @ts_nowOUT = cpu_ticks / CONVERT(FLOAT, cpu_ticks_in_ms) FROM sys.dm_os_sys_info'
SET @ParmDefinition = N'@ts_nowOUT bigint OUTPUT' EXECUTE sp_executesql @SQLString, @ParmDefinition, @ts_nowOUT = @ts_now OUTPUT;
With RingBufferSchedulerMonitor
as (
SELECT
timestamp,
CONVERT(xml, record) AS record
FROM sys.dm_os_ring_buffers
WHERE (ring_buffer_type = N'RING_BUFFER_SCHEDULER_MONITOR')
AND (record LIKE '%
), RingBufferSchedulerMonitorValues as
(
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,
DATEADD(ss, (-1 * (@ts_now - [timestamp]))/1000, GETDATE()) AS EventTime
FROM RingBufferSchedulerMonitor
)
Select
SERVERPROPERTY('ServerName') as ServerName,
record_id,
EventTime,
SQLProcessUtilization,
SystemIdle,
100 - SystemIdle - SQLProcessUtilization AS OtherProcessUtilization
From RingBufferSchedulerMonitorValues
WHERE EventTime> DATEADD(MINUTE, -60, GETDATE()) ;
Get-DbaCpuUsage now exists 👍