LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 12-21-2004, 09:55 PM   #1
student04
Member
 
Registered: Jan 2004
Location: USA
Distribution: macOS, OpenBSD
Posts: 669

Rep: Reputation: 34
limit cpu % usage for a process


Hey,

I know that it is possible to renice a process so that its priority over the CPU usage gets reduced and that it gets less CPU usage when another program needs it (given that it has a higher nice value), but is it also possible to limit the % of CPU resources no matter what nice value it has?

Thanks.
 
Old 12-21-2004, 11:14 PM   #2
trickykid
LQ Guru
 
Registered: Jan 2001
Posts: 24,149

Rep: Reputation: 269Reputation: 269Reputation: 269
Someone just asked this yesterday: http://www.linuxquestions.org/questi...hreadid=268605
 
Old 12-21-2004, 11:29 PM   #3
student04
Member
 
Registered: Jan 2004
Location: USA
Distribution: macOS, OpenBSD
Posts: 669

Original Poster
Rep: Reputation: 34
thanks.... mandrake doesn't have an rpm for that. Seems like only SUSE does

*edit
maybe i do have it... ug, who knows.. i type 'ulimit' and it says 'unlimited'. I type 'man ulimit' and an almost empy man page comes up.. but there are man pages on the net

thanks again

Last edited by student04; 12-21-2004 at 11:41 PM.
 
Old 12-21-2004, 11:57 PM   #4
student04
Member
 
Registered: Jan 2004
Location: USA
Distribution: macOS, OpenBSD
Posts: 669

Original Poster
Rep: Reputation: 34
ok i'm still a little lost here...
Code:
[alex@localhost alex]$ ulimit -a
core file size        (blocks, -c) 0
data seg size         (kbytes, -d) unlimited
file size             (blocks, -f) unlimited
max locked memory     (kbytes, -l) unlimited
max memory size       (kbytes, -m) unlimited
open files                    (-n) 1024
pipe size          (512 bytes, -p) 8
stack size            (kbytes, -s) 8192
cpu time             (seconds, -t) unlimited
max user processes            (-u) 4094
virtual memory        (kbytes, -v) unlimited
[alex@localhost alex]$
Code:
[alex@localhost alex]$ help ulimit
ulimit: ulimit [-SHacdflmnpstuv] [limit]
    Ulimit provides control over the resources available to processes
    started by the shell, on systems that allow such control.  If an
    option is given, it is interpreted as follows:

        -S      use the `soft' resource limit
        -H      use the `hard' resource limit
        -a      all current limits are reported
        -c      the maximum size of core files created
        -d      the maximum size of a process's data segment
        -f      the maximum size of files created by the shell
        -l      the maximum size a process may lock into memory
        -m      the maximum resident set size
        -n      the maximum number of open file descriptors
        -p      the pipe buffer size
        -s      the maximum stack size
        -t      the maximum amount of cpu time in seconds
        -u      the maximum number of user processes
        -v      the size of virtual memory

    If LIMIT is given, it is the new value of the specified resource;
    the special LIMIT values `soft', `hard', and `unlimited' stand for
    the current soft limit, the current hard limit, and no limit, respectively.
    Otherwise, the current value of the specified resource is printed.
    If no option is given, then -f is assumed.  Values are in 1024-byte
    increments, except for -t, which is in seconds, -p, which is in
    increments of 512 bytes, and -u, which is an unscaled number of
    processes.
[alex@localhost alex]$
That's as close to a man page as i have gotten, but none seem to hint towards cpu % limits.. except for the cpu time, but that's time not amount.. or am i wrong? I'm really -> Like, i haven't seen anywhere on the web (in all non-useful man pages) anything about cpu % limitations. Just a lot of stuff about core dumps.

Are you sure ulimit has this functionality? ...

Last edited by student04; 12-22-2004 at 04:18 PM.
 
Old 12-22-2004, 04:44 PM   #5
student04
Member
 
Registered: Jan 2004
Location: USA
Distribution: macOS, OpenBSD
Posts: 669

Original Poster
Rep: Reputation: 34
http://www.uwsg.iu.edu/hypermail/lin...08.2/0075.html
http://www.uwsg.iu.edu/hypermail/lin...08.2/0085.html
http://www.linuxquestions.org/questi...?threadid=8674
http://linuxdev.hasw.de/cgi-bin/man2html?ulimit+3 (not very useful manpage)

Through all my searching and reading I have not been able to confirm that ulimit can limit the amount (%) of cpu a process or user can consume, instead only the amount of cpu TIME a process can consume. But this is not what I am looking for... I would like to know if anybody knows if it is possible AT ALL in linux to allow one process a maximum of x% of my cpu. From the first two links above, it seems like it is NOT possible. Bummer.

I run folding@home and would like it to run continuously, however limited to no more than 60% of my cpu - it already reniced itself to 19, but that still allows it to consume 100% of my cpu when nothing else is using it.

Please correct me if I am wrong with any of this. Thanks.

Last edited by student04; 12-22-2004 at 04:46 PM.
 
Old 01-14-2005, 12:06 PM   #6
gthom
LQ Newbie
 
Registered: Jan 2005
Location: france (gap)
Distribution: mdk 10.1
Posts: 1

Rep: Reputation: 0
I think that and.sourceforge.net is the program you are searching for.
Notice i never use and test it.
I need it but as it must be runned as root, i think that the code as to be checked.

regards
 
Old 01-14-2005, 04:19 PM   #7
student04
Member
 
Registered: Jan 2004
Location: USA
Distribution: macOS, OpenBSD
Posts: 669

Original Poster
Rep: Reputation: 34
Quote:
Originally posted by gthom
I think that and.sourceforge.net is the program you are searching for.
What program?
 
Old 01-14-2005, 05:14 PM   #8
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
AND - auto nice daemon

Its sourceforge website is what he meant by and.sourceforge.net

Seeing as how it's simply an automated re-nice program, I don't believe it has the ability to limit cpu usage to a percentage. Otherwise, you would have come across that option while searching through the nice documentation. I would bet, for something like this, you'd need to incorporate some new process tracking in the kernel's scheduler that is accessible through user space (either system calls or through the /proc filesystem).

One thing I'd like to ask though: why do you want to limit it to a flat 60% max? If the computer isn't doing anything useful, why not let it consume the whole load? If it was nice'ed to 19, then as soon as you move the mouse/type/whatever, the handlers for those events take over and folding@home gets the shaft so to speak. The only reason I could think would possibly be heat generation from the processor, but surely it's not that bad. I'm not saying trying to limit a process's CPU to a flat percentage is necessarily bad, but I just don't see the need for it (just my opinion).

Last edited by Dark_Helmet; 01-14-2005 at 05:26 PM.
 
Old 01-14-2005, 05:23 PM   #9
student04
Member
 
Registered: Jan 2004
Location: USA
Distribution: macOS, OpenBSD
Posts: 669

Original Poster
Rep: Reputation: 34
Ok, i thought it was 'and' but what was written was slightly confusing.

Quote:
Originally posted by Dark_Helmet
One thing I'd like to ask though: why do you want to limit it to a flat 60% max? If the computer isn't doing anything useful, why not let it consume the whole load? If it was nice'ed to 19, then as soon as you move the mouse/type/whatever, the handlers for those events take over and folding@home gets the shaft so to speak. The only reason I could think would possibly be heat generation from the processor, but surely it's not that bad.
Yea it's the heat.. I have a laptop and it smoked a week or two ago, and one of my guesses was from overheating (and the other from it being a little bit too dusty). And my laptop, after a while, seems to lag to some degree leaving folding@home on. Maybe it's the memory usage? Who knows.
 
Old 01-14-2005, 05:35 PM   #10
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
Well, a poor man's approach might be to write an application that basically does nothing; it just sits idle. Kick it off along with folding@home, and tweak with it until you get approximately 60%.

One idea might be to have a cron job that runs every minute, checks whether a file exists, if it does, run an instance of the idle program. After so many seconds, the program ends, and folding@home can resume again. Nice the idle process to 18 and have it do nothing for 24 seconds (40% of 1 minute). When you want the idle process to run, kick off a shell script that touch'es the trigger file, and just rm that same file when you want idling to stop. To be honest though, I'm not sure which system call can be used to idle a process without allowing premption. Maybe a good question on the programming forum...
 
Old 01-26-2005, 07:04 AM   #11
houdelou
Member
 
Registered: Nov 2004
Location: Canada
Distribution: Ubuntu
Posts: 44

Rep: Reputation: 15
I know there is a kernel patch for limiting %cpu usage on www.freshmeat.net and another here http://www.tls-technologies.com/CPU/cpu-download.html but I've never used them so I cannot tell if they work flawlessly. Also, you have to apply the patch and then rebuild another kernel to use it so it is not a simple solution.
 
Old 01-26-2005, 08:00 AM   #12
student04
Member
 
Registered: Jan 2004
Location: USA
Distribution: macOS, OpenBSD
Posts: 669

Original Poster
Rep: Reputation: 34
Hey, that's sweet. Thanks.
 
Old 11-13-2009, 01:33 PM   #13
jowagner
LQ Newbie
 
Registered: Oct 2007
Posts: 6

Rep: Reputation: 0
solving the heat problem

Quote:
Originally Posted by student04 View Post
Yea it's the heat..
It has been a few years, but maybe the following solutions helps someone else:
PID_FOLDATHOME=12345 # the process id of the process to be
# limited to 60% CPU usage over 5 seconds
while True; do
kill -s SIGSTOP $PID_FOLDATHOME
sleep 2
kill -s SIGCONT $PID_FOLDATHOME
sleep 3
done

This alternates between 0% and up to 100% with a 40 - 60 pulse resulting in max. 60% usage in the long run. If your heat sink is very small, your fan controller might still pick up the changes and annoy you with oscillating fan speed. If so, change it to sleep 1 + sleep 1 (for 50%) or sleep 1 + sleep 2 (for 67%), or maybe your sleep command supports fractional durations in which case you can use sleep 0.4 + sleep 0.6 (don't use very small numbers like 0.04 + 0.06 though; it will cause overhead).

JJ

Last edited by jowagner; 11-13-2009 at 01:36 PM. Reason: grammar and spelling
 
Old 07-23-2012, 02:50 PM   #14
Andy Alt
Member
 
Registered: Jun 2004
Location: Minnesota, USA
Distribution: Slackware64-stable, Manjaro, Debian64 stable
Posts: 528

Rep: Reputation: 167Reputation: 167
cpulimit is now available in some repos.

Quote:
Cpulimit is a tool which limits the CPU usage of a process (expressed in percentage, not in CPU time). It is useful to control batch jobs, when you don't want them to eat too many CPU cycles. The goal is prevent a process from running for more than a specified time ratio. It does not change the nice value or other scheduling priority settings, but the real CPU usage. Also, it is able to adapt itself to the overall system load, dynamically and quickly.
The control of the used cpu amount is done sending SIGSTOP and SIGCONT POSIX signals to processes.
All the children processes and threads of the specified process will share the same percent of CPU.
 
Old 07-23-2012, 09:42 PM   #15
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,662
Blog Entries: 4

Rep: Reputation: 3942Reputation: 3942Reputation: 3942Reputation: 3942Reputation: 3942Reputation: 3942Reputation: 3942Reputation: 3942Reputation: 3942Reputation: 3942Reputation: 3942
Usually, you do want a process to use as much CPU time as is "fairly" available for it ... rather than, say, allow the CPU itself to sit idle. If there's nothing else going on, letting a process have 100% of the otherwise-wasted resource is a Good Thing.

The CPU is usually a "barely used" resource anyway.
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to get %CPU usage of a process devershetty Linux - Newbie 1 08-18-2008 01:52 AM
process cpu usage vanv101 Fedora 9 06-24-2005 05:53 PM
limit cpu usage by mysql eduac Linux - Software 3 06-08-2004 05:08 PM
How can I limit the CPU usage of a PID? ideasman Linux - Software 1 04-01-2004 07:15 AM
CPU usage of a process asharma Solaris / OpenSolaris 7 03-29-2003 02:17 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 02:50 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration