SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


CPU Usage using T-SQL


CPU Usage using T-SQL

Author
Message
Madhusudhana Reddy
Madhusudhana Reddy
Grasshopper
Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)

Group: General Forum Members
Points: 17 Visits: 18
Hi,

Please help me, how can I get CPU usage in % using T-SQL?


Regards
- MSR
MarkusB
MarkusB
SSCertifiable
SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)

Group: General Forum Members
Points: 5801 Visits: 4208
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
) 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)



Markus Bohse
sgambale
sgambale
SSC-Enthusiastic
SSC-Enthusiastic (145 reputation)SSC-Enthusiastic (145 reputation)SSC-Enthusiastic (145 reputation)SSC-Enthusiastic (145 reputation)SSC-Enthusiastic (145 reputation)SSC-Enthusiastic (145 reputation)SSC-Enthusiastic (145 reputation)SSC-Enthusiastic (145 reputation)

Group: General Forum Members
Points: 145 Visits: 265
If anyone knows a command from sql server that provides cpu utilization %, please post it.
I need to save this in a table on an hourly basis, in order to report the peak cpu utilization each month.

I think I may have to use a vbscript and perfmon to do this.

Steve
M&M
M&M
Hall of Fame
Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)

Group: General Forum Members
Points: 3537 Visits: 3906
You can consider using the DMV's available in SQL Server 2005

You could also use some tools which includes Microsoft Add-on tools like Performance Dashboard reports which are extremely useful in getting CPU usage reports. However, with performance dashboard reports you would get current information only.

Please check below links

http://www.microsoft.com/downloads/details.aspx?familyid=1d3a4a0d-7e0c-4730-8204-e419218c1efc&displaylang=en


http://www.sql-server-performance.com/articles/per/bm_performance_dashboard_2005_p1.aspx

M&M
sgambale
sgambale
SSC-Enthusiastic
SSC-Enthusiastic (145 reputation)SSC-Enthusiastic (145 reputation)SSC-Enthusiastic (145 reputation)SSC-Enthusiastic (145 reputation)SSC-Enthusiastic (145 reputation)SSC-Enthusiastic (145 reputation)SSC-Enthusiastic (145 reputation)SSC-Enthusiastic (145 reputation)

Group: General Forum Members
Points: 145 Visits: 265
The dashboard might be good, but I doubt I am able to have it installed. I'll see.

Since the link to the dashboard says it uses the dynamic mgt views, there ought to be a way to obtain cpu utilization by querying those views, but I have not found one yet.
I only saw a column for cpu ticks, which is not helpful.

Do you know of any views I can query?

Steve
M&M
M&M
Hall of Fame
Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)

Group: General Forum Members
Points: 3537 Visits: 3906
Steve,

The DMVs updated by MarkusB are pretty good. Even the performance dashboard gives charts similar to the output. The first DMV gives CPU usage (SQL and non SQL) in the last 4 hours. Pretty hand.

You could make use of that query for collecting the CPU usage.

I checked the performance dashboard for CPU and it has a bar graph for SQL and non SQL. Just like the query.

M&M
sgambale
sgambale
SSC-Enthusiastic
SSC-Enthusiastic (145 reputation)SSC-Enthusiastic (145 reputation)SSC-Enthusiastic (145 reputation)SSC-Enthusiastic (145 reputation)SSC-Enthusiastic (145 reputation)SSC-Enthusiastic (145 reputation)SSC-Enthusiastic (145 reputation)SSC-Enthusiastic (145 reputation)

Group: General Forum Members
Points: 145 Visits: 265
Yes, now I realize the MarkusB query is good.

Do we know if perfmon would give the same results if I looked for processor objects and counters?

I am not familiar with how he gets the information:
record.value('(./Record/SchedulerMonitorEvent/SystemHealth/SystemIdle)[1]', 'int')

What is this schedulermonitorevent?

I'm looking for overall cpu utilization of the machine, so with his query, I would modify it to save 100 - SystemIdle.
Alex S-483693
Alex S-483693
Grasshopper
Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)

Group: General Forum Members
Points: 17 Visits: 117
The query from sys.dm_os_ring_buffers returns an XML column. The expression "record.value" pulls out some information from a given location within the XML and the "RecordMonitorEvent" is part of the XML returned from the sys.dm_os_ring_buffers table. The whole parameter is basically telling the "record.value" where to find the value to extract from the XML.

If the query just returned the XML then it would be hard to read so it has been formatted nicely so you can read it using the XML queries.

Alex
jonas.rosenthal 22680
jonas.rosenthal 22680
Grasshopper
Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)

Group: General Forum Members
Points: 11 Visits: 23
Hi...I get an ambiguous column error message with the record.value column reference...not sure why.

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,
BlackHawk-17
BlackHawk-17
Valued Member
Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)

Group: General Forum Members
Points: 62 Visits: 401
You can set up a PerfMon log to populate a SQL table.

Type PerfMon at a command prompt and go into help. After that, if you need more info, Google/Bing on PerfMon and SQL Logging.

Regards;
Greg
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