CPU-, Memory- and Disk-Usage

Started by mueer01, February 24, 2023, 04:59:10 AM

Previous topic - Next topic

mueer01

Hello,

is there any function in WinBatch, to get the total CPU-, Memory- and Disk-Usage ?
(Those values, that can be seen in the TaskManager.)

Regards,
Erhard

td

Haven't tried it but you could download the Nuget package system.diagnostics.performancecounter.7.0.0.nupkg here:

https://www.nuget.org/packages/System.Diagnostics.PerformanceCounter/

Nuget packages are glorified Zip files so you can unzip the contents with most zip file utility programs. The .net assembly (.dll) you want is in the system.diagnostics.performancecounter.7.0.0\lib\net462 folder. You can look at examples of .Net programming in WIL in the Tech Database if you are not familiar with it. You can likely find some c# examples of using the assembly with a Web search. With some limitations and restrictions, C# examples can often be translated to WIl.

But as previously mentioned I have not tried it so I don't know if the assembly will work with WinBatch.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

stanl

Might take multiple queries, but maybe WMI would provide that.

td

Might be possible for at least some metrics. Here's a link to some WMI script examples. At least one of these might work to obtain CPU usage per process on an ongoing basis.

https://techsupt.winbatch.com/webcgi/webbatch.exe?techsupt/nftechsupt.web+WinBatch/WMI+Total~CPU~Usage.txt
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

stanl

Quote from: td on February 26, 2023, 02:35:51 PM
Might be possible for at least some metrics. Here's a link to some WMI script examples. At least one of these might work to obtain CPU usage per process on an ongoing basis.

https://techsupt.winbatch.com/webcgi/webbatch.exe?techsupt/nftechsupt.web+WinBatch/WMI+Total~CPU~Usage.txt


Yep. When in doubt check out the Tech Database. I guess the purpose behind the Op's ask may be worthwhile pursuing. Are we talking about a static dip for specific metrics for a specific PID, for a PID's or a rolling/polling script, possibly as a WB Service.

td

The OP uses the term "Total" but it is unclear if per process, per logical core, or system processor is the intended target. 
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

td

MSFT's site has a page that discusses using WMI to collect performance data here:

https://learn.microsoft.com/en-us/windows/win32/wmisdk/monitoring-performance-data

Here is a quick and dirty mash-up of the MSFT and Tech Database articles. Be forewarned; It can occasionally produce unusual percentages.

Code (winbatch) Select
strComputer = "."
objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" : strComputer : "\root\cimv2")
objRefresher = CreateObject("WbemScripting.Swbemrefresher")
objProcessor = objRefresher.AddEnum(objWMIService, "Win32_PerfRawData_PerfOS_Processor").ObjectSet

WindowOnTop( "", 1 )
BoxOpen("Performance data (refreshed every 3+ seconds)", "Initiating Process...")
Decimals(2)
Percent = ""
Stamp = ""
bFirst = 1
While @TRUE
    objRefresher.Refresh
        ForEach RefreshItem In objRefresher
            ForEach perf_instance2 In RefreshItem.ObjectSet
               if  StriCmp( perf_instance2.Name, "_Total") == 0       ;;
                  ;; Using sampling intervals prevent representational errors
                  ;; when performing floating point division on very large
                  ;; 64-bit integers.
                  if bFirst
                     Percent1 =  perf_instance2.PercentProcessorTime
                     Stamp1 =  perf_instance2.TimeStamp_Sys100NS
                     bFirst = 0
                  else
                     Percent2 = perf_instance2.PercentProcessorTime
                     Stamp2 = perf_instance2.TimeStamp_Sys100NS
                     
                     ; CounterType - PERF_100NSEC_TIMER_INV
                     ; Formula - (1- ((N2 - N1) / (D2 - D1))) x 100
                     PercentProcessorTime = (1 - ((Percent2 - Percent1)/((Stamp2-Stamp1)*1.0)))*100.0
                     BoxText("Press Escape Key to Cancel Processing":@crlf:@crlf:" Percent Total Process Time ":PercentProcessorTime:"%%")
                     Percent1 = Percent2
                     Stamp1 = Stamp2
                  endIf               
               endif
           Next
        Next
    if WaitForKeyEx("{ESCAPE}", 3) then break
EndWhile

objProcessor = 0
objRefresher = 0
objWMIService = 0
Exit
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade