473,698 Members | 2,411 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_BUS Y AS float)
* @@TIMETICKS
/ 10000.00
/ CAST(DATEDIFF (s, SP2.Login_Time, GETDATE()) AS float)) AS
CPUBusyPct
FROM
master..SysProc esses 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 14014
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_BUS Y AS float)
* @@TIMETICKS
/ 10000.00
/ CAST(DATEDIFF (s, SP2.Login_Time, GETDATE()) AS float)) AS
CPUBusyPct
FROM
master..SysProc esses 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_perfo rmance_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****@sommarsk og.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_BUS Y AS float)
* @@TIMETICKS
/ 10000.00
/ CAST(DATEDIFF (s, SP2.Login_Time, GETDATE()) AS float)) AS
CPUBusyPct
FROM
master..SysProc esses 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_perfo rmance_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****@sommarsk og.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_BUS Y AS float)
* @@TIMETICKS
/ 10000.00
/ CAST(DATEDIFF (s, SP2.Login_Time, GETDATE()) AS float)) AS
CPUBusyPct
FROM
master..SysProc esses 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_perfo rmance_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****@sommarsk og.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
11943
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 like array arrayListOfTablesToProcess = { "orders", "phone",
18
4606
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 an end-user in an organization and the database, through the exclusive use of stored procedures which are authored by the organization or by software developers. All development work at the application software level may thereby be conducted...
2
18702
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 sequentially call the *.sql files? i.e. call schemaVersionCheck.sql call addFieldToLoanTable.sql call updateLoanTrigger.sql
3
3756
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 to Below Normal, but I'm still getting reports that the service is making a noticable impact on overall machine performance. I stuck some Thread.Sleep calls in a various places in the code, but that doesn't help much, since the service makes...
2
1219
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 before XML was here. Thanks---
3
2458
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 representing the "Idle" process. I would figure out what percentage of time the idle process has been running since my function last was called. Then the average CPU utilization would be 100 - average idle percentage. I figure out the average idle...
0
1389
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
2248
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 However it doesnt work in Windows XP... i wanna know wat class to use to get CPU utilization in win xp... also Processid is to be compared to get CPU utilization for Process to get info from both classes?
1
6626
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 memory utilization decrease. Why is this happening? PS: CPU Utilization is taken from Process\% Processor Time Memory Utilization is taken from Process\Working Set (I want to measure the physical memory it uses) Any help is appreciated....
0
8685
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8612
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8905
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8880
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6532
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5869
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4373
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2342
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.