Windows Performance log and MacroScript

Technical support and scripting issues

Moderators: JRL, Dorian (MJT support)

Post Reply
User avatar
pgriffin
Automation Wizard
Posts: 460
Joined: Wed Apr 06, 2005 5:56 pm
Location: US and Europe

Windows Performance log and MacroScript

Post by pgriffin » Mon Mar 20, 2006 3:42 pm

I would like to be able to log CPU and memory usage on the server of my customer in a format that I can access with MacroScript. I would like to compare the CPU/Memory usage to the log file from MacroScript to find a correlation (if it exists) between CPU spikes and specific commands in my script.

I cannot find (yet) in Windows how to log things to a text file or other readable form?

Thanks in advance.

User avatar
JRL
Automation Wizard
Posts: 3532
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Tue Mar 21, 2006 5:08 am

Paul,

I have been thinking about this since you posted here. The following script uses tasklist.exe which only works in WinXP and later. I would think that someone with VB script experience could write something similar that would work globally in Windows OSs.

The basic concept is based on the assumption that CPU Usage is measured as time that a process has use of the CPU through a measured period of time. In other words during a 10 second time span, any process that uses the CPU for 2 seconds will show a CPU usage of 20%. I don't know how often Microsoft polls running processes' CPU usage to get the percentage that is displayed in Task Manager but my Task Manager screen seems to refresh about every three seconds.

The following script uses Tasklist.exe to gather the CPU time in hours:minutes:seconds, waits a user specified period of time and acquires the CPU time for the process a second time. The first time is subtracted from the second time and the result is divided by the time span between the two. The ratio is then multiplied by 100 to display the result as a percentage.

Hope this makes sense. And I hope I've made a correct assumption about CPU usage measurement.

Tasklist can also provide memory usage. Type the following line in a DOS window command prompt. Substitute an actual process for [process name]

TaskList /fi "imagename eq [process name]"

Later,
Dick



/////////////////script starts here//////////////////

//Use 2 input> as simple method to provide sample data
Input>Secs,Number of Seconds to measure CPU processing
If>Secs=,end

//Process must match process name exactly, eg. firefox.exe or msched.exe
//However it is case insensitive
Input>Process,Process to measure CPU usage over next %Secs% seconds
If>Process=,end
///////////////////////////////////////

//Create multiplier constant for changing "CPU time difference"
//into a "CPU percentage" by dividing 86400 (the number of seconds in 24 hours)
//by the number of seconds specified to be measured. Then multiplying that
//result by 100.
Let>constant={(86400/%Secs%)*100}

//Set "," to the variable "comma" for use in the separate function.
Let>comma=,

//Use tasklist to write process info to file #1
LET>RP_WAIT=1
LET>RP_WINDOWMODE=2
Run>cmd /c tasklist /v /fo csv /fi "imagename eq %Process%" > C:\~CPU_usage_test_1~.txt
Wait>%Secs%

//Use tasklist to write process info to file #2
Run>cmd /c tasklist /v /fo csv /fi "imagename eq %Process%" > C:\~CPU_usage_test_2~.txt

//Acquire info from file #1
ReadLn>C:\~CPU_usage_test_1~.txt,3,CPU_start

//Acquire info from file #2
ReadLn>C:\~CPU_usage_test_2~.txt,3,CPU_end

//delete files
DeleteFile>C:\~CPU_usage_test_1~.txt
DeleteFile>C:\~CPU_usage_test_2~.txt

//break each line of data into variables
Separate>CPU_start,comma,start
Separate>CPU_end,comma,end

//Find which variable contains time
//For some reason this varies
Let>k=5
Repeat>k
add>k,1
Let>test=start_%k%
Separate>test,:,var
If>var_count>1,Continue
Until>k=10

Label>Continue
//Remove quotes from variable value
Separate>start_%k%,",start
Separate>end_%k%,",end

//separate time into hours minutes amd seconds
//and assign to 3 variables
Separate>start_2,:,start
Separate>end_2,:,end

//Convert time into a decimal of a day
Let>start={(%start_3%/86400)+(%start_2%/1440)+(%start_1%/24)}
Let>end={(%end_3%/86400)+(%end_2%/1440)+(%end_1%/24)}

//Subtract start from end to get total CPU time
Let>delta=%end%-%start%

//Multiply "CPU time difference" by "constant" (see above)
//to get CPU percentage of usage during specified time period.
Let>CPU_percent=%delta%*%constant%

MDL>The CPU usage for %Process% for a %Secs% second interval was %CPU_percent% percent.

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Tue Mar 21, 2006 1:33 pm

Does this help:

VBSTART
Sub DumpProcInfo(OutFile)
'Writes ProcessorTime and VirtualBytes (memory use) for each running process to file
'See: http://msdn.microsoft.com/library/defau ... rocess.asp

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colProcess = objWMIService.ExecQuery("Select * from Win32_PerfFormattedData_PerfProc_Process",,48)

Set filesys = CreateObject("Scripting.FileSystemObject")
Set filetxt = filesys.CreateTextFile(OutFile, True)
filetxt.WriteLine("ProcessName,PercentProcessorTime,VirtualBytes")

For Each objItem in colProcess
if objItem.Name "Idle" and objItem.Name "_Total" then
filetxt.WriteLine(objItem.Name & "," & objItem.PercentProcessorTime & "," & objItem.VirtualBytes)
end if
Next

filetxt.Close
End Sub
VBEND

VBRun>DumpProcInfo,c:\procusage.txt

If so I'll add it to Scripts & Tips.

Note other values available:

class Win32_PerfFormattedData_PerfProc_Process : Win32_PerfFormattedData
{
string Caption;
uint32 CreatingProcessID;
string Description;
uint64 ElapsedTime;
uint64 Frequency_Object;
uint64 Frequency_PerfTime;
uint64 Frequency_Sys100NS;
uint32 HandleCount;
uint32 IDProcess;
uint64 IODataBytesPerSec;
uint64 IODataOperationsPerSec;
uint64 IOOtherBytesPerSec;
uint64 IOOtherOperationsPerSec;
uint64 IOReadBytesPerSec;
uint64 IOReadOperationsPerSec;
uint64 IOWriteBytesPerSec;
uint64 IOWriteOperationsPerSec;
string Name;
uint32 PageFaultsPerSec;
uint64 PageFileBytes;
uint64 PageFileBytesPeak;
uint64 PercentPrivilegedTime;
uint64 PercentProcessorTime;
uint64 PercentUserTime;
uint32 PoolNonpagedBytes;
uint32 PoolPagedBytes;
uint32 PriorityBase;
uint64 PrivateBytes;
uint32 ThreadCount;
uint64 Timestamp_Object;
uint64 Timestamp_PerfTime;
uint64 Timestamp_Sys100NS;
uint64 VirtualBytes;
uint64 VirtualBytesPeak;
uint64 WorkingSet;
uint64 WorkingSetPeak;
};

From:
http://msdn.microsoft.com/library/....
Last edited by Marcus Tettmar on Tue Mar 21, 2006 2:48 pm, edited 3 times in total.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

User avatar
JRL
Automation Wizard
Posts: 3532
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Tue Mar 21, 2006 2:40 pm

Marcus,

I tried running this and got an error:

Microsoft VBScript compilation error:1006 Expected ')'
Line 8 column 107

So I tried placing a close paren at the specified location and received another error. Rather than struggle I thought I'd ask.

Thanks,
Dick

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Tue Mar 21, 2006 2:48 pm

Try now. The forum was replacing some code with html for a smilie! I've fallen into that trap a few times and need to remember to disable smilies when I post script code!
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

Me_again
Automation Wizard
Posts: 1101
Joined: Fri Jan 07, 2005 5:55 pm
Location: Somewhere else on the planet

Post by Me_again » Tue Mar 21, 2006 3:00 pm

Update: Darn, you posted while I was working on this one :lol:

Try this, may have to unwrap some lines.

Code: Select all

VBSTART
Sub DumpProcInfo(OutFile)
  'Writes ProcessorTime and VirtualBytes (memory use) for each running process to file
  'See: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/win32_perfformatteddata_perfproc_process.asp

  strComputer = "."
  Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
  Set colProcess = objWMIService.ExecQuery("Select * from Win32_PerfFormattedData_PerfProc_Process",,48)

  Set filesys = CreateObject("Scripting.FileSystemObject")
  Set filetxt = filesys.CreateTextFile(OutFile, True)
  filetxt.WriteLine("ProcessName,PercentProcessorTime,VirtualBytes")

  For Each objItem in colProcess
    if objItem.Name <> "Idle"  and objItem.Name <> "_Total" then
		filetxt.WriteLine(objItem.Name & "," & objItem.PercentProcessorTime & "," & objItem.VirtualBytes)
    end if
  Next

  filetxt.Close
End Sub
VBEND

VBRun>DumpProcInfo,c:\procusage.txt
Something strange, when I copied from the scrolling box there was an html tag in the code and I got the same error JRL did.

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Tue Mar 21, 2006 3:02 pm

Yeh, as I said, the forum inserted HTML for a smilie, because I forgot to disable smilies. I have corrected the code. Please try again.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

User avatar
JRL
Automation Wizard
Posts: 3532
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Tue Mar 21, 2006 3:29 pm

Marcus,

Sorry, to be a bother. I think I see another problem. I intensionally created a tight loop dialog script, compiled it and set it in motion. Task Manager says its CPU usage is 94%. When I run your VB Script I get the following result:

TightLoop,0,38035456
wmiprvse,100,21446656

I'm assuming that "wmiprvse" is the VB Script?

Just asking,
Dick

User avatar
pgriffin
Automation Wizard
Posts: 460
Joined: Wed Apr 06, 2005 5:56 pm
Location: US and Europe

Post by pgriffin » Tue Mar 21, 2006 3:57 pm

Everyone,

Thanks for all the interest in my post. Marcus, I used the VB script with no problem. I am very interested in using more of the values available. My guess is that there is something happening on my customer's server that is causing ODBC calls to bog down their system and, consequently, makes them think there is a problem with MacroScript.

When I get some results by comparing the output of the VB performance monitoring script with the log of my scripts, I'll be sure to repost them.

Once again, thanks to everyone that replied to my post. I learn something each time from the code you guys post.

Paul

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Tue Mar 21, 2006 4:08 pm

JRL wrote:Marcus,

Sorry, to be a bother. I think I see another problem. I intensionally created a tight loop dialog script, compiled it and set it in motion. Task Manager says its CPU usage is 94%. When I run your VB Script I get the following result:

TightLoop,0,38035456
wmiprvse,100,21446656

I'm assuming that "wmiprvse" is the VB Script?

Just asking,
Dick
I don't know what Task Manager does or where it gets its info from but it probably isn't the exact same information that this script outputs. There's also the Win32_PerfRawData_PerfProc_Process object instead of the Win32_PerfFormattedData_PerfProc_Process which is formatted. The Raw version would therefore give more accurate info but would need some calculations to get it into anything useable.

I'm just passing on info freely available on Microsoft's site and I included the URL in the comment of the function. For more info please read what Microsoft has to say.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts