Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

How to limit CPU utilization by ANY process with a perl script?

by unix.95054 (Initiate)
on Feb 24, 2009 at 00:36 UTC ( [id://745869]=perlquestion: print w/replies, xml ) Need Help??

unix.95054 has asked for the wisdom of the Perl Monks concerning the following question:

Hello perl gurus,

I am not into perl (as of now) but really admire the functionalities and flexibilites it provides.

I have a requirement which states as follows:
"No single process in Unix/Linux system can take more than 70% CPU at any given point of time"

I have proposed two solution, which are near to this requirement but not close enough:
1) Use of /etc/security/limits.conf file to set CPU time. For example:

@students hard cpu 2


(This will limit CPU running time to 2 minutes to any member of students group)

2) Second way is to use "cpulimits" freeware program which can limit CPU usage based on either a PID or any program name. Example to this is:

./cpulimit -e perl -l 10


This will limit "perl" to maximum 10% of CPU utilization.

Both of these methods are good in their own way but doesn't solve my purpose. I want
"ANY PROCESS AT ANY TIME SHOULD NOT USE MORE THAN 70% OF CPU".

Can anyone take a look into this and suggest me a possible solution with the help of perl?

Thanks,
VS

Replies are listed 'Best First'.
Re: How to limit CPU utilization by ANY process with a perl script?
by tilly (Archbishop) on Feb 24, 2009 at 04:44 UTC
    This shows how to do what you are asking for with cpulimits and a simple daemon written in shell. I would just use that, though porting it to Perl shouldn't be too hard if you really want to use Perl instead.

      The listed program appears to issue SIGCONT and SIGSTOP to the offending program. This can work, but it just seems messy. Additionally, the application can ignore those signals, correct?

      For a kludge, it seems like it would work. Having worked in academia, however, there are some percentage of users who will try to get around this. On the other hand, that is what the human side of policy is for (lock account, petition to have it reinstated, blah blah blah).

      I still contend that this belongs in the scheduler. That being said, however, if this functionality does not exist in the scheduler, then the suggested solution is reasonable.

      --MidLifeXis

Re: How to limit CPU utilization by ANY process with a perl script?
by bichonfrise74 (Vicar) on Feb 24, 2009 at 01:28 UTC
    Not sure if this is what you want, but in Linux, you can do something like this...
    ps aux | perl -lane 'print if $F[3] > 70'
    This basically prints the CPU utilization if it is more than 70%. We can replace the 'print' with a force kill command if this is the answer to your question.
      ...or, rather than replacing print(1) with kill(1), why not replace with nice(1) instead - it is what it's there for , after all :-)

      A user level that continues to overstate my experience :-))

        However, in the case of an idle system, nice will not limit use to <= 70%. If the system is idle, it will still use as much as it needs to do what it is trying to do.

        This is an issue for the OS scheduler, or each program needs to give up the processor voluntarily if it goes over 70%.

        Update: or use something with SIGSTOP/SIGCONT - see tilly's post below.

        --MidLifeXis

      Hmm, I think I might be misunderstanding what your one-liner is doing, but isn't the fourth element of @F %MEM, not %CPU?

      USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND marc 9764 0.0 1.9 85544 40380 ? S 15:47 0:07 pidgin
      $ ps aux | perl -lane 'print if $F[3] == 1.9' marc 9764 0.0 1.9 85500 40376 ? S 15:47 0:07 pidgin

      And you didn't even know bears could type.

        That's true. In my case, the 4th element is for the CPU. I guess ps gives a different output depending on your Linux flavor.
Re: How to limit CPU utilization by ANY process with a perl script?
by BrowserUk (Patriarch) on Feb 24, 2009 at 16:56 UTC
    1. Method 1: Replace a 3.0 GHz processor with a 2.1 GHz processor. (Adjust as appropriate!)

      Now no process can use more than 70% of the original cpu.

    2. Method 2: Write a cpu-bound monitoring process that consumes 30% of the cpu and run it at real-time priority. This works for my system, but you will probably have to adjust the constants for yours:
      #! perl -slw use strict; our $N ||= 1.75e5; while( 1 ) { Win32::Sleep 1; 1 for 1 .. $N; Win32::Sleep 1; 1 for 1 .. $N; Win32::Sleep 1; 1 for 1 .. $N; }

    Question: Why do you want to limit processes to no more that 70% CPU?


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: How to limit CPU utilization by ANY process with a perl script?
by poolpi (Hermit) on Feb 24, 2009 at 13:27 UTC
Re: How to limit CPU utilization by ANY process with a perl script?
by roboticus (Chancellor) on Feb 24, 2009 at 18:28 UTC
    unix.95054:

    As MidLifeXis and BrowserUK imply, the requirement "ANY PROCESS AT ANY TIME SHOULD NOT USE MORE THAN 70% OF CPU" seems to be incorrectly specified. Rarely do you care what percentage of the CPU is spent on any particular process. Usually you care that the system is always responsive to the console, or that processes aren't CPU or I/O starved or some other criterion. Thus, the original statement appears to be symptom-related rather than requirement-related. You might ask them to restate the requirement in a more meaningful form. Otherwise, you'll have to do ridiculous things like consuming a third of the CPU in a high-priority process or otherwise discarding CPU cycles when they're otherwise unneeded by other processes.

    ...roboticus

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2024-03-29 05:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found