Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

CPU Usage using T-SQL Expand / Collapse
Author
Message
Posted Monday, December 1, 2008 3:21 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Sunday, November 15, 2009 9:29 AM
Points: 1, Visits: 18
Hi,

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


Regards
- MSR
Post #611107
Posted Monday, December 1, 2008 5:38 AM
SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Wednesday, July 16, 2014 6:13 AM
Points: 4,432, Visits: 4,167
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
Post #611223
Posted Wednesday, January 27, 2010 2:23 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Wednesday, July 18, 2012 6:40 AM
Points: 75, 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
Post #854741
Posted Wednesday, January 27, 2010 2:33 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 3:21 PM
Points: 2,268, Visits: 3,777
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


Mohammed Moinudheen
Post #854748
Posted Thursday, January 28, 2010 6:26 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Wednesday, July 18, 2012 6:40 AM
Points: 75, 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
Post #855139
Posted Thursday, January 28, 2010 10:59 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 3:21 PM
Points: 2,268, Visits: 3,777
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.






Mohammed Moinudheen
Post #855493
Posted Thursday, January 28, 2010 12:55 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Wednesday, July 18, 2012 6:40 AM
Points: 75, 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.
Post #855586
Posted Wednesday, March 17, 2010 7:24 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, April 14, 2014 9:29 PM
Points: 3, Visits: 105
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
Post #885172
Posted Thursday, March 18, 2010 12:17 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, July 22, 2010 11:47 AM
Points: 1, Visits: 22
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,

Post #885794
Posted Friday, March 19, 2010 6:34 AM


SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Wednesday, June 4, 2014 7:34 AM
Points: 42, Visits: 398
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
Post #886294
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse