CPU limits
am 07.11.2007 11:40:59 von alexxx.magni
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
Re: CPU limits
am 07.11.2007 20:10:44 von unknown
Post removed (X-No-Archive: yes)
Re: CPU limits
am 07.11.2007 20:46:35 von xhoster
"alexxx.magni@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.
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.
Re: CPU limits
am 07.11.2007 21:21:31 von zouz
On Nov 7, 11:40 am, "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 ;
}
zaher el siddik
http://www.unixshells.nl/
Re: CPU limits
am 08.11.2007 03:42:00 von Ben Morrow
Quoth "alexxx.magni@gmail.com" :
> 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
Re: CPU limits
am 08.11.2007 08:09:11 von Ivan Novick
On Nov 7, 11:10 am, all mail refused
wrote:
> On 2007-11-07, 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