473,386 Members | 1,786 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Capture CPU Utilization in TSQL

Happy New Year everyone!

I would like to capture CPU Utilization % using TSQL. I know this can
be done using PerfMon but I would like to run TSQL command (maybe once
every 5 minutes) and see what is the CPU Utilization at that instant so
that I can insert the value in a table and run reports based on the
data.

I have spent a good amount of time scouring google groups but this is
all I have found:
SELECT
(CAST(@@CPU_BUSY AS float)
* @@TIMETICKS
/ 10000.00
/ CAST(DATEDIFF (s, SP2.Login_Time, GETDATE()) AS float)) AS
CPUBusyPct
FROM
master..SysProcesses AS SP2
WHERE
SP2.Cmd = 'LAZY WRITER'

Problem is this gives me total amount of time CPU in %) has been busy
since the server last started. What I want is the % for the instant -
the same number we see in Task Manager and PerfMon.

Any help would be appreciated.

Thanks

Jan 2 '07 #1
3 13986
SQLJunkie (vs******@gmail.com) writes:
I have spent a good amount of time scouring google groups but this is
all I have found:
SELECT
(CAST(@@CPU_BUSY AS float)
* @@TIMETICKS
/ 10000.00
/ CAST(DATEDIFF (s, SP2.Login_Time, GETDATE()) AS float)) AS
CPUBusyPct
FROM
master..SysProcesses AS SP2
WHERE
SP2.Cmd = 'LAZY WRITER'

Problem is this gives me total amount of time CPU in %) has been busy
since the server last started. What I want is the % for the instant -
the same number we see in Task Manager and PerfMon.
Performance counters are in sysperfinfo on SQL 2000 and
sys.dm_os_performance_counters on SQL 2005, but I could find the item
you are looking for in these views.

But I saw in Books Online for SQL 2005 that these values are cumultative. To
get the present value, sample with some interval. I guess you could to
the same: query @@CPU_BUSY twice with a second or so in between.

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Jan 2 '07 #2
Thanks for your quick response Erland. I ran the following script but I
don't think this is the correct value. But I cannot find anything
meaningful???

DECLARE
@CPUBusy1 bigint
, @CPUBusy2 bigint
, @TimeTicks1 bigint
, @TimeTicks2 bigint

SELECT
@CPUBusy1 = @@CPU_BUSY
, @TimeTicks1 = @@TIMETICKS

WAITFOR DELAY '0:00:01'

SELECT
@CPUBusy2 = @@CPU_BUSY
, @TimeTicks2 = @@TIMETICKS

SELECT
@CPUBusy1 AS CPUBusy1
, @CPUBusy2 AS CPUBusy2
, @CPUBusy2 - @CPUBusy1 AS CPUDiff
, @TimeTicks1 AS TimeTicks1
, @TimeTicks2 AS TimeTicks2
, @TimeTicks2 - @TimeTicks1 AS TimeTicksDiff

Thanks for your time and help!
Vishal
Erland Sommarskog wrote:
SQLJunkie (vs******@gmail.com) writes:
I have spent a good amount of time scouring google groups but this is
all I have found:
SELECT
(CAST(@@CPU_BUSY AS float)
* @@TIMETICKS
/ 10000.00
/ CAST(DATEDIFF (s, SP2.Login_Time, GETDATE()) AS float)) AS
CPUBusyPct
FROM
master..SysProcesses AS SP2
WHERE
SP2.Cmd = 'LAZY WRITER'

Problem is this gives me total amount of time CPU in %) has been busy
since the server last started. What I want is the % for the instant -
the same number we see in Task Manager and PerfMon.

Performance counters are in sysperfinfo on SQL 2000 and
sys.dm_os_performance_counters on SQL 2005, but I could find the item
you are looking for in these views.

But I saw in Books Online for SQL 2005 that these values are cumultative. To
get the present value, sample with some interval. I guess you could to
the same: query @@CPU_BUSY twice with a second or so in between.

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Jan 3 '07 #3
Thanks everyone for reading this and your responses. I was able to find
the correct solution in another Google post:

DECLARE
@CPU_BUSY int
, @IDLE int

SELECT
@CPU_BUSY = @@CPU_BUSY
, @IDLE = @@IDLE

WAITFOR DELAY '000:00:01'

SELECT
(@@CPU_BUSY - @CPU_BUSY)/((@@IDLE - @IDLE + @@CPU_BUSY - @CPU_BUSY) *
1.00) *100 AS CPUBusyPct
This solution was posted by Gert-Jan Strik on Tues, Jan 14 2003 6:55
PM.
Here is the URL for the thread:
http://groups.google.com/group/comp....67728586773e8b

Thanks again,

Vishal
SQLJunkie wrote:
Thanks for your quick response Erland. I ran the following script but I
don't think this is the correct value. But I cannot find anything
meaningful???

DECLARE
@CPUBusy1 bigint
, @CPUBusy2 bigint
, @TimeTicks1 bigint
, @TimeTicks2 bigint

SELECT
@CPUBusy1 = @@CPU_BUSY
, @TimeTicks1 = @@TIMETICKS

WAITFOR DELAY '0:00:01'

SELECT
@CPUBusy2 = @@CPU_BUSY
, @TimeTicks2 = @@TIMETICKS

SELECT
@CPUBusy1 AS CPUBusy1
, @CPUBusy2 AS CPUBusy2
, @CPUBusy2 - @CPUBusy1 AS CPUDiff
, @TimeTicks1 AS TimeTicks1
, @TimeTicks2 AS TimeTicks2
, @TimeTicks2 - @TimeTicks1 AS TimeTicksDiff

Thanks for your time and help!
Vishal
Erland Sommarskog wrote:
SQLJunkie (vs******@gmail.com) writes:
I have spent a good amount of time scouring google groups but this is
all I have found:
SELECT
(CAST(@@CPU_BUSY AS float)
* @@TIMETICKS
/ 10000.00
/ CAST(DATEDIFF (s, SP2.Login_Time, GETDATE()) AS float)) AS
CPUBusyPct
FROM
master..SysProcesses AS SP2
WHERE
SP2.Cmd = 'LAZY WRITER'
>
Problem is this gives me total amount of time CPU in %) has been busy
since the server last started. What I want is the % for the instant -
the same number we see in Task Manager and PerfMon.
Performance counters are in sysperfinfo on SQL 2000 and
sys.dm_os_performance_counters on SQL 2005, but I could find the item
you are looking for in these views.

But I saw in Books Online for SQL 2005 that these values are cumultative. To
get the present value, sample with some interval. I guess you could to
the same: query @@CPU_BUSY twice with a second or so in between.

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Jan 3 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Steve | last post by:
Hi; I have been writing a lot of short tsql scripts to fix a lot of tiny database issues. I was wondering if a could make an array of strings in tsql that I could process in a loop, something...
18
by: mountain man | last post by:
Greetings to all database professionals and laymen, Let us make a bold assumption that we have developed a software tool for the SQL Server environment which simply acts as an interface between...
2
by: dynoweb | last post by:
I have several *.sql files with schema/data changes to be applied to our current database. Is there a way to create a TSQL script that could be run from the SQL Query Analyzer that would...
3
by: Dathon | last post by:
I have a Windows service that's built with .NET. The process is meant to run in the background and not suck up too much CPU time. I set the thread priority for the various threads in the service...
2
by: Learner | last post by:
Hi, Can some one provide me with a summary and a URL which will do the trick I.e. EXACTLY what is the utilization of XML and HOW does it help as compared to how (whatever it does) it was done...
3
by: john | last post by:
I don't want to know what the CPU utilization is right now. I want to get the average utilization over the last, for example, hour. So I came up with a method where I would get a Process object...
0
by: felixpj | last post by:
Hi All, Could any one help me how to capture the CPU utilization of a blocking process in MSSQL Server 2000 and how can i get the details of the blocked process. Thanks in Advance Felix
0
by: PRR | last post by:
with WMI class Win32_Process i can get most details of Processes running one Pc.. except CPU utilization... THe class for CPU utilization u need to use Win32_PerfFormattedData_PerfProc_Process ...
1
by: Kaheru | last post by:
memory utilization increase? This is because when i try to keep track of the CPU utilization and memory utilization of my FTP server process (ftpserver.exe), the CPU utilization increase, but the...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.