Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > CPU limits

Reply
Thread Tools

CPU limits

 
 
alexxx.magni@gmail.com
Guest
Posts: n/a
 
      11-07-2007
Inspired by the recent posting of "FAQ 8.39 How do I set CPU limits?",
I hoped to solve this problem, that I always had with computationally
intensive scripts - namely that the CPU usage goes to 100%, and a very
noisy fan starts.
I'd much prefer to be able to set my script's CPU usage at - say - 75%
or similar...

In the FAQ I saw that this possibility is in the BSD::Resource module,
but unfortunately I work in Linux.
Moreover, I'm not certain that even under BSD this could be possible:
setrlimit seems to allow you just to kill processes going beyond your
given threshold.

Any hint?


Thanks!

Alessandro Magni

 
Reply With Quote
 
 
 
 
all mail refused
Guest
Posts: n/a
 
      11-07-2007
On 2007-11-07, <> wrote:

> I'd much prefer to be able to set my script's CPU usage at - say - 75%
> or similar...
>
> In the FAQ I saw that this possibility is in the BSD::Resource module,
> but unfortunately I work in Linux.
> Moreover, I'm not certain that even under BSD this could be possible:
> setrlimit seems to allow you just to kill processes going beyond your
> given threshold.


You should be able to use BSD::Resource on linux to get at
the ulimit and setrlimit() sort of stuff. This can limit
total CPU usage of a program - killing it when exceeded.

If you want to ration CPU usage to some fraction of the total,
or to a certain one of several CPUs etc that's a different
question that I don't know the answer to.

--
Elvis Notargiacomo master AT barefaced DOT cheek
http://www.notatla.org.uk/goen/
 
Reply With Quote
 
 
 
 
xhoster@gmail.com
Guest
Posts: n/a
 
      11-07-2007
"" <> wrote:
> Inspired by the recent posting of "FAQ 8.39 How do I set CPU limits?",
> I hoped to solve this problem, that I always had with computationally
> intensive scripts - namely that the CPU usage goes to 100%, and a very
> noisy fan starts.
> I'd much prefer to be able to set my script's CPU usage at - say - 75%
> or similar...
>
> In the FAQ I saw that this possibility is in the BSD::Resource module,
> but unfortunately I work in Linux.


BSD::Resource works under Linux. Or at least some (I expect all or most)
parts of it do. So I don't think that that is a problem.

> Moreover, I'm not certain that even under BSD this could be possible:
> setrlimit seems to allow you just to kill processes going beyond your
> given threshold.


Yes, that is the problem. I don't know of any facility on any OS that does
this (but I haven't looked very hard.) I guess you could write a
baby-sitter process which goes into a loop where it sleeps for 3N
microseconds, then issues a kill STOP to your real process, then sleeps for
N microseconds, then issues a kill CONT to your real process. That should
restrict it to 75% CPU time. If your main program uses other types of
signals, this might not work very well.

Xho

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
 
Reply With Quote
 
elsiddik
Guest
Posts: n/a
 
      11-07-2007
On Nov 7, 11:40 am, "alexxx.ma...@gmail.com" <alexxx.ma...@gmail.com>
wrote:
> Inspired by the recent posting of "FAQ 8.39 How do I set CPU limits?",
> I hoped to solve this problem, that I always had with computationally
> intensive scripts - namely that the CPU usage goes to 100%, and a very
> noisy fan starts.
> I'd much prefer to be able to set my script's CPU usage at - say - 75%
> or similar...
>
> In the FAQ I saw that this possibility is in the BSD::Resource module,
> but unfortunately I work in Linux.
> Moreover, I'm not certain that even under BSD this could be possible:
> setrlimit seems to allow you just to kill processes going beyond your
> given threshold.
>
> Any hint?
>
> Thanks!
>
> Alessandro Magni


you can use BSD::Resource on linux.
or try this script :

#!/usr/bin/perl
#Description: These subs allow you control how much % CPU maximum
will use your script. CPU_start() must be called once when you script
start.
#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>;
}


zaher el siddik
http://www.unixshells.nl/

 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      11-08-2007

Quoth "" <>:
> Inspired by the recent posting of "FAQ 8.39 How do I set CPU limits?",
> I hoped to solve this problem, that I always had with computationally
> intensive scripts - namely that the CPU usage goes to 100%, and a very
> noisy fan starts.
> I'd much prefer to be able to set my script's CPU usage at - say - 75%
> or similar...
>
> In the FAQ I saw that this possibility is in the BSD::Resource module,
> but unfortunately I work in Linux.


As others have mentioned, BSD::Resource works perfectly well under
Linux.

> Moreover, I'm not certain that even under BSD this could be possible:
> setrlimit seems to allow you just to kill processes going beyond your
> given threshold.


setrlimit sends a trappable signal when you reach your soft limit. The
problem is there is no rlimit for 'percentage of CPU used': I'm not even
sure it's a terribly well-defined property. Apart from anything else,
you don't really want your script to be using 75% CPU; you just want the
CPU to be 25% idle. Have you looked to see if there is some ACPI stuff
you could use to request this? I remember reading about some 'laptop
mode' for Linux which cut down the amount of CPU time available to
preserve power.

Ben

 
Reply With Quote
 
Ivan Novick
Guest
Posts: n/a
 
      11-08-2007
On Nov 7, 11:10 am, all mail refused <elvis-85...@notatla.org.uk>
wrote:
> On 2007-11-07, alexxx.ma...@gmail.com <alexxx.ma...@gmail.com> wrote:


> You should be able to use BSD::Resource on linux to get at
> the ulimit and setrlimit() sort of stuff. This can limit
> total CPU usage of a program - killing it when exceeded.
>
> If you want to ration CPU usage to some fraction of the total,
> or to a certain one of several CPUs etc that's a different
> question that I don't know the answer to.


You can use a VM and assign only one of the CPU's to the VM

Regards,
Ivan Novick
http://www.0x4849.net

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FAQ 8.39 How do I set CPU limits? PerlFAQ Server Perl Misc 0 04-08-2011 10:00 PM
How CPU Features Affect CPU Performance Ian Front Page News 0 02-18-2010 03:28 PM
How , system cpu and user cpu times calculates pavunkumar C Programming 1 02-27-2009 08:29 AM
HELP! Trying to understand the logic behind CPU core speeds and whole CPU speed. dimon Computer Support 4 11-10-2006 04:01 PM
Pentium CPU vs Intel Celeron CPU and the Wireless Mouse kirk lives! Computer Support 4 05-02-2004 06:59 PM



Advertisments