Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


CPU Usage using T-SQL


CPU Usage using T-SQL

Author
Message
sanketahir1985
sanketahir1985
SSC-Enthusiastic
SSC-Enthusiastic (197 reputation)SSC-Enthusiastic (197 reputation)SSC-Enthusiastic (197 reputation)SSC-Enthusiastic (197 reputation)SSC-Enthusiastic (197 reputation)SSC-Enthusiastic (197 reputation)SSC-Enthusiastic (197 reputation)SSC-Enthusiastic (197 reputation)

Group: General Forum Members
Points: 197 Visits: 1274
MarkusB (12/1/2008)
What exactly are you looking for?
The CPU usage by SQL server in general or the CPU usage for a query?

You can find some information about the general CPU usage in sys.dm_os_ring_buffers. But be aware that you only get info about the last 4 hours or so.
Here's an example.
   DECLARE @ts_now BIGINT
   SELECT @ts_now = cpu_ticks / CONVERT(FLOAT, cpu_ticks_in_ms) FROM sys.dm_os_sys_info
   
   SELECT record_id,
      DATEADD(ms, -1 * (@ts_now - [timestamp]), GETDATE()) AS EventTime,
      SQLProcessUtilization,
      SystemIdle,
      100 - SystemIdle - SQLProcessUtilization AS OtherProcessUtilization
   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 '% %') AS x
      Wink AS y
   ORDER BY record_id DESC



For CPU usage by query you can query sys.dm_exec_query_stats. But again you will only get info about query plans which are in cache.

SELECT TOP 10 (total_worker_time * 1.0) / 1000000 AS CPU_Utilized_in_Seconds, text,* 
FROM sys.dm_exec_query_stats
CROSS APPLY sys.dm_exec_sql_text(sql_handle)





Is there any script for SQL 2000 as above?

Sanket Ahir
Don't run behind the success, Try to be eligible & success will run behind u......
rajesh.sharma
rajesh.sharma
SSC Journeyman
SSC Journeyman (95 reputation)SSC Journeyman (95 reputation)SSC Journeyman (95 reputation)SSC Journeyman (95 reputation)SSC Journeyman (95 reputation)SSC Journeyman (95 reputation)SSC Journeyman (95 reputation)SSC Journeyman (95 reputation)

Group: General Forum Members
Points: 95 Visits: 184
MarkusB , Thanks a lot for your query.

Rajesh11Sept.
MudLuck
MudLuck
SSC-Enthusiastic
SSC-Enthusiastic (135 reputation)SSC-Enthusiastic (135 reputation)SSC-Enthusiastic (135 reputation)SSC-Enthusiastic (135 reputation)SSC-Enthusiastic (135 reputation)SSC-Enthusiastic (135 reputation)SSC-Enthusiastic (135 reputation)SSC-Enthusiastic (135 reputation)

Group: General Forum Members
Points: 135 Visits: 519
Lost and have been chasing this CPU query down for several days THANKS!!!w00t



jasoningram
jasoningram
SSC Journeyman
SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)SSC Journeyman (77 reputation)

Group: General Forum Members
Points: 77 Visits: 100
Excellent script Markus!!! Huge help with what I was wanting to do.

I made some minor tweaks to it to make it compatible with SQL 2008. I thought I'd post it back to save the next guy a few minutes. Again, thanks Markus for posting this script.


/***************************************************************************************************************
Description: This script will display CPU usage on the server. The data comes from
   dm_os_ring_buffers which only stored data for the past 4 HOURS. Within the ring buffers, data is averaged
   at the minute level. There are variables limit the results to a smaller time window and for hiding the
   details.

Sources:
   http://www.sqlservercentral.com/Forums/Topic611107-146-2.aspx
   http://social.msdn.microsoft.com/Forums/en-US/sqldatabaseengine/thread/abbf67ab-fc8b-4567-b6d4-1c605bc9866c/
***************************************************************************************************************/

/* Variables */
DECLARE
   @StartTime DATETIME = '01/01/1900 00:00:00'
   ,@EndTime DATETIME = '01/01/2100 23:59:59'
   ,@ShowDetails BIT = 1 -- 1 = True, 0 = False




/* Script Begin */
/* Be careful modifying anything below */


-- Find the timestamp for current server time
DECLARE @ts_now BIGINT
SELECT @ts_now = cpu_ticks / (cpu_ticks / ms_ticks)
FROM sys.dm_os_sys_info;

DECLARE @Results TABLE
   (record_ID BIGINT NOT NULL
   ,EventTime datetime NOT NULL
   ,SQLProcessUtilization tinyint NOT NULL
   ,SystemIdle tinyint NOT NULL
   ,OtherProcessUtilization tinyint NOT NULL
)

INSERT INTO
   @Results
(   
   record_ID
   ,EventTime
   ,SQLProcessUtilization
   ,SystemIdle
   ,OtherProcessUtilization
)
SELECT
record_id
,DATEADD(ms, -1 * (@ts_now - [timestamp]), GETDATE()) AS EventTime
,SQLProcessUtilization
,SystemIdle
,100 - SystemIdle - SQLProcessUtilization AS OtherProcessUtilization
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 '% %'
         AND DATEADD(ms, -1 * (@ts_now - [timestamp]), GETDATE()) BETWEEN @StartTime AND @EndTime
) AS x
) AS y


--Return details
IF @ShowDetails = 1
BEGIN
   SELECT
      record_ID
    ,EventTime
    ,SQLProcessUtilization
    ,SystemIdle
    ,OtherProcessUtilization
   FROM @Results
END


--Return average
SELECT
   AVG(SQLProcessUtilization)
   ,MIN(EVENTTIME) StartTime
   ,MAX(EVENTTIME) EndTime
FROM
   @Results      






MarkusB
MarkusB
SSCarpal Tunnel
SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)

Group: General Forum Members
Points: 4449 Visits: 4208
Hi Jason,

nice work.

Markus Bohse
rafaelsrocha
rafaelsrocha
Forum Newbie
Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)

Group: General Forum Members
Points: 3 Visits: 238
SQL 2008...

declare @a as bigint, @b as bigint

   select @a=sum(cntr_value)
   from sys.dm_os_performance_counters
   where object_name = 'SQLServer:Resource Pool Stats' and cntr_type = 537003264
   
   select distinct @b=cntr_value
   from sys.dm_os_performance_counters
   where object_name = 'SQLServer:Resource Pool Stats' and cntr_type = 1073939712

   select cast(@a as float) / cast(@b as float)*100 as [cpu]
go
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search