Pages

Wednesday, October 10, 2012

Find the CPU Utilization of SQL Server at particular time

Find the CPU Utilization of SQL Server


Sometimes by seeing the task manager we will be in confused state about the CPU utilization of SQL Server. If CPU is 100% utlizised then how much contribution is made by SQL Server in that is the big question.

Below query will give the results which shows the CPU utilization (SQL Server utilization and Other process utilization).

-----------------------------------------------------------------------------------------------------------
DECLARE @ts_now bigint = (SELECT cpu_ticks/(cpu_ticks/ms_ticks)FROM sys.dm_os_sys_info);

SELECT  TOP (30) SQLProcessUtilization AS [SQL Server Process CPU Utilization],
               SystemIdle AS [System Idle Process],
               100 - SystemIdle - SQLProcessUtilization AS [Other Process CPU Utilization],
               DATEADD(ms, -1 * (@ts_now - [timestamp]), GETDATE()) AS [Event Time]
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
                  WHERE ring_buffer_type = N'RING_BUFFER_SCHEDULER_MONITOR'
                  AND record LIKE '%<SystemHealth>%') AS x
        ) AS y
ORDER BY record_id DESC;
-----------------------------------------------------------------------------------------------------------
Hope the above query helps

Thanks.