Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

perl cpu usage on windows machines

by leonidlm (Pilgrim)
on Jul 14, 2008 at 09:18 UTC ( [id://697418]=perlquestion: print w/replies, xml ) Need Help??

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

Hi all,
I noticed that a perl script is consuming a really big amount of CPU when it is running. Even a small and simple script (perform HTTP get) is taking a huge amount of CPU and can reach the 100% barrier easily.
Am I missing something ? Should some benchmarks be done ?

Replies are listed 'Best First'.
Re: perl cpu usage on windows machines
by CountZero (Bishop) on Jul 14, 2008 at 09:36 UTC
    Even "a small and simple script" can eat all your CPU cycles, for instance by running in a tight loop.

    What kind of OS are you running your script under? Does it happen with all your Perl scripts? Can we see some code?

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      The script is run under windows 2003. This is an example of the script:
      #!/usr/bin/perl use strict; use Warnings; use HTTP::Status; use HTTP::Response; use HTTP::Request; use LWP::UserAgent; if (@ARGV < 2) { print "USAGE: IsPageAvailable [-r] <MonitorName> <URL> <Hosts file +> [Http::Host]\n"; } else { # Treat the -r option, in wich case the screap will search in the +response returned to SUCCEED # string and treat the returned monitor accoringly. my $checkResponse = 0; if ($ARGV[0] eq "-r") { $checkResponse = 1; shift @ARGV; } my ($strMonitorName, $strURL, $hosts, $httpHost) = @ARGV; # Use the configuration file only if specefied. if (defined($hosts)) { # Generate the computerName and domainName variables used late +r to search in the hosts file my $computerName = lc($ENV{'COMPUTERNAME'}); $strURL =~ /^http.*\/\/(.+?)[\:\/]/; my $domainName = $1; my $ip; # Convert the domain using the hosts file supplied open (HOSTS, '<', $hosts) or die "Cant open configuration file + $hosts\n"; END: while (my $line = <HOSTS>) { if ($line =~ /$computerName:$domainName:(.+)/) { $ip = $1; chomp($ip); last END; } } close(HOSTS); # No entries in the hosts found die "No entries in $hosts file found for this machine ($comput +erName, $domainName)\n" if (!defined($ip)); # Substitute in the URL to the found $ip $strURL =~ s/$domainName/$ip/; } # Perform the GET command print "Performing GET on $strURL\n"; my $ua = LWP::UserAgent->new; $ua->timeout(15); # We can define the httpHost my $request; if (defined($httpHost)) { $request = HTTP::Request->new(GET => $strURL, HTTP::Headers->n +ew(Host => $httpHost)); } else { $request = HTTP::Request->new(GET => $strURL); } my $response = $ua->request($request); my $nCode = $response->code; my $nMonitorValue; my $nReturnCode = $response->code; print "Return Code = $nReturnCode\n"; # Check the returned code and format the opcmon command if ($nReturnCode == 200) { if ($checkResponse) { if ($response->content =~ /SUCCEEDED/m) { $nMonitorValue = 0; } else { $nMonitorValue = 1; } } else { $nMonitorValue = 0; } } else { $nMonitorValue = 1; } # Send the opcmon print "opcmon value = $nMonitorValue\n"; my $strCmd ="opcmon $strMonitorName=$nMonitorValue"; my $exit = system($strCmd); exit $exit; }
        What does the opcmon command do? Could it be that this command maxes out your CPU?

        CountZero

        A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

        How long does the script run?

        There is only one loop in the script and it should run very quickly unless the hosts file is huge. The remainder of the script ought to be I/O bound, although a virus checker may gobble up chunks of cpu depending on how it is configured.

        You could insert a few print statements for light weight profiling or trot out Devel::SmallProf to get a handle on where the time is going.


        Perl is environmentally friendly - it saves trees
Re: perl cpu usage on windows machines
by moritz (Cardinal) on Jul 14, 2008 at 09:21 UTC
    That's not the normal case, so you can assume that there's something wrong with your scripts.
Re: perl cpu usage on windows machines
by Perlbotics (Archbishop) on Jul 14, 2008 at 15:58 UTC
    Hi,
    does this problem occour with this paticular script or with other (potentially web-using) Perl applications? Can you identify the "hotspot" within your program?
    E.g., when reading the DB file (NFS/SMB?), when downloading the document (non-blocking I/O somewhere?), when calling the external tool?
    Some changes to the Perl installation or the OS lately?

    Perlbotics

Re: perl cpu usage on windows machines
by jethro (Monsignor) on Jul 14, 2008 at 17:06 UTC
    More interesting would be to know how much time your script needs to run? If it uses 100% CPU for a millisecond no harm is done

    I tested your script on my linux machine without the opcmon call and with the website http://www.ora.com. Needed a tenth of a second to run.

    You could put a few print statements like print "I'm here now at point ...\n" in your script to check whether the webaccess or the call to opcmon or something else entirely is using too much time

      Thank you all for participating. I will profile my script soon, meanwhile I hear an opinion from one of our system administrators saying that windows treating processes differently from unix - If CPU is quite idle windows will prefer to "push on it" (= 100% CPU) to finish the script as fast as possible.
      Can someone confirm this opinion ?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://697418]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (2)
As of 2024-04-19 22:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found