Veli-Pekka Tätilä wrote:

> Hi,
> Is there a module or some sample code that will let one find out the CPU 
> usage of a given process in percentages? This figure is highly useful and 
> can be viewed in XP's task manager. However, none of the other tools I've 
> seen, including XP's tasklist and Sysinternal's process tools, let you get 
> at the CPU Usage as such. Many do offer the CPU time, however. I'd like to 
> experiment with changing process priorities in case of serius peaks in CPU 
> usage. Thus, a metric of how well the system performs and which processes 
> hog the CPU the most would be just what I need, I guess.
> 
> I've installed some related Win32 modules using PPM including
> perflib and process:: info, memory and perf.
> 
> I've Googled and read related bits of MSDN with little luck so far. seems 
> there are two performance interfaces an NT specific one and the other based 
> on Windows management instrumentation. However, both seem highly abstract 
> and complex to work with and might not even offer the CPU usage as such.
> 
> To be sure, I launched the perfmon application in XP and went through the 
> list of counters. You do get to user and kernel times on a per process basis 
> as well as elapsed CPU time, but again I didn't see anything related to the 
> CPU usage in the list. In Task manager's view/choose column dialog, the 
> entry goes by the name CPU Usage and is shown by default.
> 
> So, does either WMI or the NT performance counters let me access the CPU 
> Usage directly? If this is not the case, what kind of workarounds or other 
> means do I have? For example, what do the 3rd party task manager 
> replacements use?

I doubt you will find any reference to % CPU usage since that's a function
of time.  You can get the current usage at time A, wait for interval I seconds,
get usage at time B and compute the difference in CPU usage over wall clock
time B - A for both system and user.

do forever
        T1 = get time
                B1 = get process stats (including system/user time and
                     for each process you are interested in)
        sleep for Interval
        T2 = get time
                A1 = get process stats (including system/user time and
                     for each process you are interested in)
        WTD = T2 - T1
        foreach process n=1..N
                X = System CPU time diff = An - Bn (System CPU load)
                Y = User   CPU time diff = An - Bn (User   CPU load)
                (repeat for processes 2, 3, 4, ...)
                %CPU = (X + Y) / WTD
        endfor
enddo

You'll proably want to use a hi res clock for this.

> I've checked the Win32 API functions in MSDN and as expected, did not find 
> the CPu usage there, either. Even though I found references to some 
> previously undocumented and interesting NT funcs like 
> NtQuerySystemInformation. Another thought that struck me would be to monitor 
> which Win32 API functions taskmgr.exe itself employs, using some free Win32 
> API monitors out there. However, this job seems difficult as I really don't 
> know what kind of activity I should be looking for, that is by which name 
> the function might go by.
> 
> Any help greatly appreciated as usual.
_______________________________________________
Perl-Win32-Users mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to