CPU limits

Discussion in 'Perl Misc' started by alexxx.magni@gmail.com, Nov 7, 2007.

  1. Guest

    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
    , Nov 7, 2007
    #1
    1. Advertising

  2. 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/
    all mail refused, Nov 7, 2007
    #2
    1. Advertising

  3. Guest

    "" <> 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.
    , Nov 7, 2007
    #3
  4. elsiddik Guest

    On Nov 7, 11:40 am, "" <>
    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/
    elsiddik, Nov 7, 2007
    #4
  5. Ben Morrow Guest

    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
    Ben Morrow, Nov 8, 2007
    #5
  6. Ivan Novick Guest

    On Nov 7, 11:10 am, all mail refused <>
    wrote:
    > On 2007-11-07, <> 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
    Ivan Novick, Nov 8, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jack
    Replies:
    0
    Views:
    6,090
  2. Ashish
    Replies:
    1
    Views:
    3,141
    Natty Gur
    Nov 19, 2003
  3. Jerry Camel

    File Upload Limits

    Jerry Camel, Feb 20, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    376
    Ken Cox [Microsoft MVP]
    Feb 21, 2004
  4. pavunkumar

    How , system cpu and user cpu times calculates

    pavunkumar, Feb 27, 2009, in forum: C Programming
    Replies:
    1
    Views:
    348
  5. PerlFAQ Server

    FAQ 8.39 How do I set CPU limits?

    PerlFAQ Server, Apr 8, 2011, in forum: Perl Misc
    Replies:
    0
    Views:
    120
    PerlFAQ Server
    Apr 8, 2011
Loading...

Share This Page