get actual CPU usage by sql server with direct queryu

Alen Cappelletti 1,047 Reputation points
2024-08-26T16:25:17.15+00:00

Hi, I'm on a "simple" standard edition...
and during my test on DEV, I'm working on Development edition and as you can see below I've default pol.
User's image

In prod only internal.
User's image

Is there a very simple way to trigger a procedure when, at the JOB RUN, SQL Server's CPU usage exceeds XX%?

I don't want to parse the "Xevent Profile"... is something that already doing...
It doesn't seem like there is a "performance counter" specifically for SQL Server CPU usage, or is there? I wouldn't want to use PowerShell...

Thanks ALEN

SQL Server | Other
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Erland Sommarskog 121.8K Reputation points MVP Volunteer Moderator
    2024-08-26T21:35:32.0566667+00:00

    Not really what you are asking for, but here is a query to get the load history. I'm not sure from where I got it; it might have been Glenn Berry's diagnostic queries. You could tailor it to fit your needs and poll it.

    -- Listing 6-1. Getting CPU Load History
    DECLARE
      @now BIGINT;
    	 
    SELECT @now = cpu_ticks / (cpu_ticks / ms_ticks) 
    FROM sys.dm_os_sys_info WITH (NOLOCK);
    ;WITH RingBufferData([timestamp], rec)
    AS
    (
      SELECT [timestamp], CONVERT(XML, record) AS rec 
      FROM sys.dm_os_ring_buffers WITH (NOLOCK)
      WHERE 
        ring_buffer_type = N'RING_BUFFER_SCHEDULER_MONITOR' AND
        record LIKE N'%<SystemHealth>%'
    )
    ,Data(id, SystemIdle, SQLCPU, [timestamp])
    AS
    (
      SELECT
        rec.value('(./Record/@id)[1]', 'int') 
        ,rec.value
         ('(./Record/SchedulerMonitorEvent/SystemHealth/SystemIdle)[1]','int')
        ,rec.value
         ('(./Record/SchedulerMonitorEvent/SystemHealth/ProcessUtilization)[1]','int') 
        ,[timestamp] 
      FROM RingBufferData
    )
    SELECT TOP 256 
      dateadd(MS, -1 * (@now - [timestamp]), getdate()) AS [Event Time] 
      ,SQLCPU AS [SQL Server CPU Utilization] 
      ,SystemIdle AS [System Idle]
      ,100 - SystemIdle - SQLCPU AS [Other Processes CPU Utilization]
    FROM Data
    ORDER BY id desc
    OPTION (RECOMPILE, MAXDOP 1);
    -- 
    ;WITH DBCPU
    AS
    (
        SELECT 
            pa.DBID, DB_NAME(pa.DBID) AS [DB]
            ,SUM(qs.total_worker_time/1000) AS [CPUTime]
        FROM 
            sys.dm_exec_query_stats qs WITH (NOLOCK)
            CROSS APPLY 
            (
                SELECT CONVERT(INT, value) AS [DBID] 
                FROM sys.dm_exec_plan_attributes(qs.plan_handle)
                WHERE attribute = N'dbid'
            ) AS pa
        GROUP BY pa.DBID
    )
    SELECT 
        [DB]
        ,[CPUTime] AS [CPU Time (ms)]
        ,CONVERT(decimal(5,2), 1. *[CPUTime] / 
            SUM([CPUTime]) OVER() * 100.0) AS [CPU Percent]
    FROM DBCPU
    WHERE DBID <> 32767 -- ResourceDB
    ORDER BY [CPUTime] DESC;
    

  2. LiHongMSFT-4306 31,571 Reputation points
    2024-08-27T02:38:51.65+00:00

    Hi @Alen Cappelletti

    It doesn't seem like there is a "performance counter" specifically for SQL Server CPU usage, or is there?

    Yes, there is.

    You could choose:

    • Processor: % Privileged Time
    • Processor: %User Time
    • System: Processor Queue Length

    See this doc for more details: Monitor CPU Usage.

    Best regards,

    Cosmog


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.