tracking cpu usage of a process

tracking cpu usage of a process

Post by atchdj » Sat, 05 Oct 2002 01:41:16



Does anyone know how to find out with a script the
cpu usage of a process.  The same cpu usage you would see
in task manager by process.
 
 
 

tracking cpu usage of a process

Post by Philip Nunn [MS » Sat, 05 Oct 2002 02:20:04


This should do what you want.  It will only work on XP/.NET because it uses
the cooked counter provider.  It can be changed to work on win2k with some
additional work though.

<sample>
set cimv2 = GetObject("winmgmts:root\cimv2")

'Create a refresher
set refresher = CreateObject("WbemScripting.SWbemRefresher")

'Add a the processor instance for all processors on the machine to the
'refresher
set Processor = refresher.Add (cimv2,
"Win32_PerfFormattedData_PerfOS_Processor='_Total'").Object

'Add an enumerator of all processes to the refresher
set Processes = refresher.AddEnum (cimv2,
"Win32_PerfFormattedData_PerfProc_Process").ObjectSet

'Refresh once so we have base values to use in our cooking
refresher.Refresh

'Loop around, refreshing every 2 seconds as we go
for I=1 to 10
  WScript.Sleep 2000
  refresher.Refresh

  'Display the Processor utilization
WScript.Echo "Processor Utilization is ", Processor.PercentProcessorTime,
"%"

  'Iterate through the collection of processes
  for each process in Processes
    WScript.Echo process.Name, process.PercentProcessorTime, "%"
  next
next

</sample>

--
-Philip

This posting is provided "As Is" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm


Quote:> Does anyone know how to find out with a script the
> cpu usage of a process.  The same cpu usage you would see
> in task manager by process.


 
 
 

tracking cpu usage of a process

Post by atchdj » Sat, 05 Oct 2002 04:09:57


Thank you for you response but I have already found this
information on the Microsoft website.  I need to use this
on NT4 and Win2k.  Could you please should me how to
change this script to work in Win2k.

Dave

Quote:>-----Original Message-----
>This should do what you want.  It will only work on

XP/.NET because it uses
Quote:>the cooked counter provider.  It can be changed to work
on win2k with some
>additional work though.

><sample>
>set cimv2 = GetObject("winmgmts:root\cimv2")

>'Create a refresher
>set refresher = CreateObject

("WbemScripting.SWbemRefresher")
Quote:

>'Add a the processor instance for all processors on the
machine to the
>'refresher
>set Processor = refresher.Add (cimv2,
>"Win32_PerfFormattedData_PerfOS_Processor='_Total'").Objec
t

>'Add an enumerator of all processes to the refresher
>set Processes = refresher.AddEnum (cimv2,
>"Win32_PerfFormattedData_PerfProc_Process").ObjectSet

>'Refresh once so we have base values to use in our cooking
>refresher.Refresh

>'Loop around, refreshing every 2 seconds as we go
>for I=1 to 10
>  WScript.Sleep 2000
>  refresher.Refresh

>  'Display the Processor utilization
>WScript.Echo "Processor Utilization is ",

Processor.PercentProcessorTime,
Quote:>"%"

>  'Iterate through the collection of processes
>  for each process in Processes
>    WScript.Echo process.Name,

process.PercentProcessorTime, "%"

- Show quoted text -

>  next
>next

></sample>

>--
>-Philip

>This posting is provided "As Is" with no warranties, and
confers no rights.
>Use of included script samples are subject to the terms
specified at
>http://www.microsoft.com/info/cpyright.htm



>> Does anyone know how to find out with a script the
>> cpu usage of a process.  The same cpu usage you would
see
>> in task manager by process.

>.

 
 
 

tracking cpu usage of a process

Post by Philip Nunn [MS » Sun, 06 Oct 2002 02:16:15


Below is a similar script that gets the cpu usage for a single process on
win2k.  It uses the "Performance Counter Provider", a high perf provider,
but the calculations are not done for you. You can find the formulas for
each counter by looking up the value of the countertype property qualifier
under "WMI Performance Counter Types" in MSDN.

If you want a solution to work on NT4 as well, you can use the "Performance
Monitoring Provider" (described in MSDN).  It is not a high performance
provider but it will do all the calculations for you.

<sample>
'Get % Processor Time
'Connect to Local Machine
set wmi_service =
GetObject("winmgmts:{impersonationlevel=impersonate}!\root\cimv2")

sObjectPath =
"Win32_PerfRawData_PerfProc_Process.Name='<proc_name_from_taskmgr>'"
wscript.echo sObjectPath

set perf_instance1 = wmi_service.get( sObjectPath )
N1 = perf_instance1.PercentProcessorTime
D1 = perf_instance1.TimeStamp_Sys100NS

while true

 'Sleep for one second = 1000 ms
 wscript.sleep(1000)

 set perf_instance2 = wmi_service.get( sObjectPath )
 N2 = perf_instance2.PercentProcessorTime
 D2 = perf_instance2.TimeStamp_Sys100NS

 ' CounterType - PERF_100NSEC_TIMER
 ' Formula = ((N2 - N1) / (D2 - D1)) x 100

 if ( 0 = (D2-D1)  ) then
  wscript.echo "divide by zero"
 else
  PercentProcessorTime = ((N2 - N1) / (D2 - D1)) * 100
  wscript.echo "% Processor Time = " , PercentProcessorTime
 end if

 N1 = N2
 D1 = D2
wend

set wmi_service = nothing
</sample>

--
-Philip

This posting is provided "As Is" with no warranties, and confers no rights.


> Thank you for you response but I have already found this
> information on the Microsoft website.  I need to use this
> on NT4 and Win2k.  Could you please should me how to
> change this script to work in Win2k.

> Dave
> >-----Original Message-----
> >This should do what you want.  It will only work on
> XP/.NET because it uses
> >the cooked counter provider.  It can be changed to work
> on win2k with some
> >additional work though.

> ><sample>
> >set cimv2 = GetObject("winmgmts:root\cimv2")

> >'Create a refresher
> >set refresher = CreateObject
> ("WbemScripting.SWbemRefresher")

> >'Add a the processor instance for all processors on the
> machine to the
> >'refresher
> >set Processor = refresher.Add (cimv2,
> >"Win32_PerfFormattedData_PerfOS_Processor='_Total'").Objec
> t

> >'Add an enumerator of all processes to the refresher
> >set Processes = refresher.AddEnum (cimv2,
> >"Win32_PerfFormattedData_PerfProc_Process").ObjectSet

> >'Refresh once so we have base values to use in our cooking
> >refresher.Refresh

> >'Loop around, refreshing every 2 seconds as we go
> >for I=1 to 10
> >  WScript.Sleep 2000
> >  refresher.Refresh

> >  'Display the Processor utilization
> >WScript.Echo "Processor Utilization is ",
> Processor.PercentProcessorTime,
> >"%"

> >  'Iterate through the collection of processes
> >  for each process in Processes
> >    WScript.Echo process.Name,
> process.PercentProcessorTime, "%"
> >  next
> >next

> ></sample>

> >--
> >-Philip

> >This posting is provided "As Is" with no warranties, and
> confers no rights.
> >Use of included script samples are subject to the terms
> specified at
> >http://www.microsoft.com/info/cpyright.htm



> >> Does anyone know how to find out with a script the
> >> cpu usage of a process.  The same cpu usage you would
> see
> >> in task manager by process.

> >.