in reply to CPU utilization

Presumably you have a loop reading the log file like:
while (my $logline = <LOG>) { # parse it }
Making a process max out at a certain percentage of CPU is not easy to do (because it's generally not a useful thing to do; if the CPU isn't otherwise used, why not use 100%? and if there are other things running, setting different priorities works out better) especially I think on Windows. It may be sufficient for your purposes just to put in a line like sleep 1 unless $. % 100 which makes perl stop for at least a second every 100 lines.

However, you may be able to get the linux code you posted to work by getting rid of sub Cat entirely and making CPU_used like:

sub CPU_used { (map $_->[0]+$_->[1], [times])[0] }
But unless I'm totally misunderstanding it, you should be using CPU_max(.2) to get a max of 20% CPU use.

Update: note that calling CPU_max every iteration is counterproductive; I'd figure out how many lines you process per second and make your code:

CPU_start(); while (my $logline = <LOG>) { # parse it CPU_max(.2) unless $. % $lines_per_second; }
(using your lines per second figure, and adjusting if needed for finer granularity.)

Replies are listed 'Best First'.
Re^2: CPU utilization
by contact_chatan (Initiate) on Feb 24, 2005 at 12:03 UTC
    Hi, Its not working as it is taking 100% cpu utilization Thanks , Chatanya Agrawal
      Is this ActiveState's perl? Which windows operating system are you using? Just before the sleep call in CPU_max, try putting in a
      print "CPU_max: cpu: $cpu real: $real\n";
      and run it for a little while to see how it goes.

      You might also try getting rid of the sleep function and instead putting use Time::HiRes "sleep"; at the top of your code.


        Yes i am already doing it and it is printing value like:
        start: MAX: 0.2 REAL: 0.15625CPU: 0.031
        start: MAX: 0.2 REAL: 0.15625CPU: 0.031
        start: MAX: 0.2 REAL: 0.15625CPU: 0.031
        start: MAX: 0.2 REAL: 0.171875CPU: 0.047
        Wat should i do and it is utilizing more than 80% of CPU.Please can you check this code at your end>Also do you have yahoo or MSN messenger ID
        Thanks,

        Chatanya Agrawal