Win has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

I'm looking to assess CPU usage on a Windows 2000 machine. Basically, I have a Perl program that is on a group of networked machines. This program is designed to run in the background on each of the machines. I want the sleep time within the  For (1..200) {  #some code here;  sleep 20;  } arrangement to be determined by the level of CPU usage. The greater the level of CPU usage the greater the sleep time within the loop.

In particular what modules would people recommend?

Update:
It looks like Win32::Process::Info is the module to go for. From different nodes that I have read it has become apparent that there is a wide range of information that can be returned with use of this module. I am very keen to automate the assessment of CPU usage in order that the level of CPU usage (over a period of about 2 minutes) within a PC node can determine the availability of this node (within a distributed computer network). Can anyone please suggest what info I need to return using Win32::Process::Info in order to do this.


Update B:

I need to take another look at: Win32-Process-Memory

Replies are listed 'Best First'.
Re: CPU usage Windows OP
by tirwhan (Abbot) on Mar 21, 2006 at 12:42 UTC

    I don't know about Windows, under *NIX I'd set the process priority (otherwise also called the nice-level) and let the operating system scheduler handle the rest. That's what it's there for after all, and a hand-rolled solution involving sleep is almost certain to be inferior in every respect.


    All dogma is stupid.
      Seconded. For Win32 I suggest using Win32::Process which has a a SetPriorityClass($priority) method.


      holli, /regexed monk/
        Thanks. ppm presents three options for me:
        1. Win32-Process-Info [1.006] Get information on arbitrary process +es in Wi~ 2. Win32-Process-Memory [0.20] read and write memory of other windo +ws proce~ 3. Win32-Process-Perf [1.02] Performance counter for processes
        I am assuming that it is the first of these that are required.
Re: CPU usage Windows OP
by salva (Canon) on Mar 21, 2006 at 13:11 UTC
    as tirwhan has already suggested, using the process priority to control its scheduling is your best option.

    Anyway, if you still want to control yourself when the script does the heavy work, using sleep with a variable delay is a bad idea because it could end running at the worst moment, when the load in the machine is very high. A better aproach is to use something like...

    while(1) { if (cpu_load() < $limit) { do_heavy() } sleep 10; }
Re: CPU usage Windows OP
by Thelonius (Priest) on Mar 21, 2006 at 14:12 UTC
    Actually the Win32:Process module that holli recommended is bundled with libwin32 and is included in ActivePerl already.
Re: CPU usage Windows OP
by BrowserUk (Patriarch) on Mar 21, 2006 at 19:53 UTC

    The following script will display instantaneous system usage percent once per second. This is usually a little higher than the figure displayed by the Task Manager which does some smoothing of the instantaneous values:

    #! perl -slw use strict; use Win32::API::Prototype; ApiLink( 'kernel32', q[ BOOL GetSystemTimes( LPFILETIME lpIdleTime, LPFILETIME lpKernelTime, LPFILETIME lpUserTime ) ] ) or die $^E; sub SystemTimes { my( $idleTicks, $kernelTicks, $userTicks ) =( chr( 0 ) x 8 ) x 3; GetSystemTimes( $idleTicks, $kernelTicks, $userTicks ) or die $^E +; return map{ my( $lo, $hi ) = unpack 'VV', $_; ( $hi * 2**32 + $lo ); } $idleTicks, $kernelTicks, $userTicks; } use constant { IDLE=>0, KERNEL=>1, USER=>2 }; $|=1; my @last = SystemTimes; while( sleep 1 ) { my @now = SystemTimes; my @deltas = map{ $now[ $_ ] - $last[ $_ ] } IDLE, KERNEL, USER; my $busy = $deltas[ KERNEL ] + $deltas[ USER ]; my $pcUsage = ( $busy - $deltas[ IDLE ] )* 100 /( $deltas[ IDLE ] +|| 100e5 ); printf "\rCPU usage(%%): %6.3f ", $pcUsage; @last = @now; } __END__ C:\test>SysTimes.pl CPU usage(%): 25.490

    However, this is still the wrong approach to solving your problem. Imagine that your sleep times out, and you obtain the cpu usage value just as your browser is downloading a large image. At that instance, your cpu usage might be 100%, so your program will decide the system is too busy and go back to sleep for 20 seconds.

    However, the user spends that 20 seconds looking at the exsquisite curves and fine detail of the bodyform shown in the image he just loaded (a Ferrari F430 Spyder say :), and so for those twenty seconds the cpu is essentially idle, and your process is sleeping, while the user is studying.

    Your process wakes up just as the user zooms the image for a closeup of the air intake. Again the instantaneous cpu usage is at or close to 100%, so your process goes back to sleep. And the next 20 seconds or so the cpu is again idle whilst the user studies the screen.

    The correct way to do this is by using SetPriorityClass() on your process and setting it to IDLE_PRIORITY_CLASS. That will allow your process to use upto 100% of the cpu when nothing else is using the cpu, but will never slow any other processes down by preventing them from access. It's far more accurate than anything than you could code yourself; it will avoid imposing calculation overhead upon the system as the scheduler already has all the information it needs available to it; and it is just much easier.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.