Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Setting CPU usage limit with BSD::Resource

by Andre_br (Pilgrim)
on Feb 05, 2006 at 22:03 UTC ( [id://528103]=perlquestion: print w/replies, xml ) Need Help??

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

Hello my esteemed fellow monks,

Iīm in need of solving a CPU-hungry script problem. This script eats 99% CPU during a whole minute, so all the website gets toooo slow when itīs being executed.

Someone here at the chatbox gave me this tip for BSD::Resource , but the documentation isnīt clear at all, and I couldnīt find any tutorial over the net. Has any of you dealed with it successfully?

Iīd like to limit the CPU usage to 50% maximum. Is it possible? By the way, my host's OS is Debian Sarge.

Thanks a lot

Andre

  • Comment on Setting CPU usage limit with BSD::Resource

Replies are listed 'Best First'.
Re: Setting CPU usage limit with BSD::Resource
by tirwhan (Abbot) on Feb 05, 2006 at 22:33 UTC

    There's no way you can set CPU usage to a percentage of CPU time. Anyway, you don't want to do that, because it would mean that your CPU would idle some of the time while at other times it still cannot perform the other (more important) tasks. What you want is for your script to use the CPU 100% when no other task is waiting to be run, but to release the processor when another process needs it.

    The way this is done in *NIX is the process priority (otherwise known as the "nice" value). This is a value between -20 (highest priority, process is prioritized over all other processes) and 19 (lowest priority). Process priority can be set either on the commandline vie the "nice" or "renice" commands, e.g.

    nice -n 10 perlscript.pl # or, if the process is already running renice 10 -p <PID>

    (see the man pages for these commands for more details). Or with the BSD::Resource module method setpriority

    use BSD::Resource; my $success = setpriority(0,10);

    All dogma is stupid.
Re: Setting CPU usage limit with BSD::Resource
by zentara (Archbishop) on Feb 06, 2006 at 13:14 UTC
    I don't want an argument with tirwhan over whether this is good or not, but this works on my linux box, It was posted a few years ago( not my invention).
    #!/usr/bin/perl #Description: These subs allow you control how much % CPU maximum wil +l use your script. CPU_start() must be called once when you script st +art. #This example script will use 30% CPU until Ctrl-C pressed: CPU_start(); CPU_max(30) while 1; use Time::HiRes qw(time); sub CPU_used { (map {$_->[13]+$_->[14]} [split " ", Cat("/proc/self/stat")])[0] } { my %start = (tm => 0, cpu => 0); sub CPU_start { @start{"tm","cpu"} = (time(),CPU_used()) } sub CPU_max { my ($max, $real, $cpu) = ($_[0], time()-$start{tm}, CPU_used()-$start{cpu}); return unless defined($max) and $max > 0; &sleep( $cpu/$max-$real ); }} # # macro used from CPU_used() and CPU_max() # sub sleep { select undef,undef,undef,$_[0] } sub Cat { local *F; open F, "< ".$_[0] or return; local $/ unless wantarray; return <F>; }

    I'm not really a human, but I play one on earth. flash japh

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (7)
As of 2024-04-16 09:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found