Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Limit CPU usage for an application/process Apps
If you want to decrease the CPU demands for an application you can use this very simple and somewhat ugly trick.

I use it when I convert videos on my MacBook Pro to prevent it from getting too warm. Even if the CPU is IDLE this hack will prevent the application to use the CPU during Sleep Time (in contrast to renice/nice commands which will take all IDLE time).

This means the conversions will take longer time, but I don't care because I run it at night. There is similar C program for Linux called cpulimit, but it wont compile on my Mac.

Note: To get the PID, run first the top -u command in one Terminal window.

Copy and paste the bash script (below) and save it in a text file (e.g cpulimitrob.sh).

Here's the bash script:
#! /bin/bash
# Description:
# 
# If you want to decrease the CPU demands for an application
# you can use this very simple and ugly "hack". 
# I use it when I convert videos on my MacBook Pro
# to prevent it from getting to warm. Even if the CPU is IDLE this hack
# will prevent the application to use the CPU during Sleep Time
# (in contrast to renice/nice commands which will take all IDLE time). 
# This means the conversions will take longer time, but I 
# don't care because I run it at night.
#
# There is similar c program for Linux called cpulimit, but it wont
# compile on my Mac.
#
# Usage:  sudo ./cpulimitrob.sh
#
# Hint: To get the PID, run first top -u command in one Terminal window. 
#
#
echo "Which process ID (PID)? "
read pid

echo "Sleep time in seconds? "
read sleeptime

echo "Run time in seconds (e.g 0.5 or 1 )? "
read runtime

i=1
dot=.

while true
do
if [ $i -eq 1 ]
then
kill -SIGSTOP $pid
sleep $sleeptime
i=0
else
kill -SIGCONT $pid
sleep $runtime
i=1
fi
echo -n $dot
done
To make the file executable and run it type the commands (after going to the directory you saved the file to):

chmod 755 cpulimitrob.sh

sudo ./cpulimitrob.sh

[crarko adds: I haven't tested this one.]
    •    
  • Currently 3.17 / 5
  You rated: 1 / 5 (6 votes cast)
 
[31,739 views]  

Limit CPU usage for an application/process | 16 comments | Create New Account
Click here to return to the 'Limit CPU usage for an application/process' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Limit CPU usage for an application/process
Authored by: Kalak on Feb 02, '11 10:49:15AM

cpulimit has been ported to OSX in the subversion repository. Ironically, the checkout I just compiled form from there used all extra CPU time to run cpulimit. (any coders want to make a patch?)

While this script is a bit of a hack, I'm keeping it around, since I've got a similar use, and I get tired of hearing the cooling fans on my MBP when I'm in no hurry, and this is working more or less as described.

---
--
Kalak
I am, and always will be, an Idiot.



[ Reply to This | # ]
Limit CPU usage for an application/process
Authored by: hamarkus on Feb 02, '11 11:09:46AM

Stupid question, isn't this thermal cycling (at the processor level) also not that great for durability? (The fans are certainly been spared by this method.)



[ Reply to This | # ]
Limit CPU usage for an application/process
Authored by: matsw on Feb 02, '11 11:22:31AM

The script can be very much simplified (variable i is not needed):

while true
do
kill -SIGSTOP $pid
sleep $sleeptime
kill -SIGCONT $pid
sleep $runtime
echo -n $dot
done



[ Reply to This | # ]
Limit CPU usage for an application/process
Authored by: ehlers on Feb 02, '11 11:48:26AM

Have a look at cputhrottle (http://www.willnolan.com/cputhrottle/cputhrottle.html). It's made for Mac OS X.

Tested it with 10.6.6, works fine.

Bernhard



[ Reply to This | # ]
Limit CPU usage for an application/process
Authored by: billbarstad on Feb 03, '11 07:50:59AM

Thanks for this. I'll try it with Handbrake.



[ Reply to This | # ]
Limit CPU usage for an application/process
Authored by: LEgregius on Feb 07, '11 08:42:15PM

cputhrottle seems to be the better of the choices i've tried. It doesn't seem to work as smoothly as cpulimit does on linux, but it's certainly a workable solution. I used it all the time.



[ Reply to This | # ]
Limit CPU usage for an application/process
Authored by: sanderw on Feb 02, '11 11:52:14AM

Basically, this is turning the process on and off according to a defined interval. Definitely ugly, but I can't think of a better way to accomplish your task except perhaps running the process in a VM or binding it to a single core.



[ Reply to This | # ]
Limit CPU usage for an application/process
Authored by: frank_martin on Feb 12, '11 04:50:36PM

How can I bind or limit an application to use a single core?



[ Reply to This | # ]
Limit CPU usage for an application/process
Authored by: S Barman on Feb 02, '11 12:09:04PM
As an old time Unix programmer, I know that this method can be dangerous! Even though signals are stacked for processes, it is possible that those processes that are not interactive that you try to control using SIGSTOP may lose track of signals. Signals are interrupts that are sent to a process when something outside of the program occurs that the process causes--such as trying to write to memory you do not have access to. SIGSTOP and SIGSTART is part of the jobs control that is usually entered from the terminal by pressing CTRL-Z. Programs designed to handle this properly should be able to process SIGSTART in order to ensure the continuation of the program properly. Many non-interactive programs are not programmed to handle SIGSTART--it is assumed that these signals are used by interactive programs and not relevant for background processes. Rather than using these signals, use the "nice" command to lower the priority of the process. Nominally, all processes start at priority 0. The priority can be set in the range of -20 (lowest) to +20 (highest). By lowering the scheduling priority you will let the underlying Mach operating system schedule other processes ahead of the one you want to limit access to CPU resources. It has the effect of lowering the scheduling priority to run the process during times when the operating system is idle, which is what you what to happen. It is a cleaner way to do what you want. There are two ways to run processes at a lower priority. First, start the process using the "nice" command.
nice -n -5 command command_arguments... &
Make sure you add the ampersand to the end of the command so that the command is run in the background. Otherwise, if you want to change the scheduling priority of a running process, use the "renice" command. Find the process identification number (pid) of the running process and run the renice command:
renice -n -5 pid
Just as you lower the scheduling priority, you can also raise the scheduling priority. However, only the superuser can raise the scheduling priority. Thus, at the end of the day if you want to reset the priority of the process back to 0 (nominal), the use "sudo" to run the renice command. Using the process's pid, run:
sudo renice 0 pid
Using "nice" and "renice" is more friendly to most running processes.

[ Reply to This | # ]
Nice does nothing to reduce thermal load
Authored by: hamarkus on Feb 02, '11 12:22:29PM

You are both right and wrong. I just had Handbrake crash on me using this script, which could be an example of the pitfalls of this method.
However, your method, ie, nice, does nothing to keep the total CPU load below 100% (per core), in fact this has already been said in the article, nice is not a solution to reduce the total CPU load.

What might work more elegantly is to simply disable one CPU core via the Developer Tools. I do not know how much this reduces the thermal load but is bound to reduce it somewhat (unless the TurboBoost kicks and completely overcompensates for this, but then you might be able to disable the TurboBoost via the developer tools as well, you might even be able to limit the CPU speed itself, I do not know.

Edited on Feb 02, '11 12:51:30PM by hamarkus



[ Reply to This | # ]
Nice does nothing to reduce thermal load
Authored by: S Barman on Feb 02, '11 01:18:49PM
Just to let you know, I did compile and run cpulimit. You need to download it from http://cpulimit.sourceforge.net/ and make sure you have the latest version of the developer tools.

[ Reply to This | # ]
Nice does nothing to reduce thermal load
Authored by: hamarkus on Feb 02, '11 02:47:21PM

I tried cpulimit, checked it out and compiled it with make and ran it with: cpulimit$ ./cpulimit -l 80 -p 47879

Maybe I did not call it correctly but it did not work, ie, it did not limit my target thread to 80% CPU, cpulimit itself consumed a sizeable chunk of CPU cycles itself and something sucked up my 8 GB of memory. I did not realize this after I had quit my target thread and cpulimit itself, so I am not sure which process gobbled up all my memory, either cpulimit itself or maybe my target thread.



[ Reply to This | # ]
Limit CPU usage for an application/process
Authored by: Auricchio on Feb 02, '11 06:41:42PM

Negative numbers in nice actually allow MORE cpu usage. Positive numbers cut priority down.

---
EMOJO: mojo no longer workin'



[ Reply to This | # ]
Limit CPU usage for an application/process
Authored by: CoolerQ on Feb 02, '11 10:29:34PM

You're not quite right there... first off, it's SIGSTOP and SIGCONT, not SIGSTART, but I'll assume that's a typo. SIGSTOP isn't what is sent by the terminal when you press control-Z; that's SIGTSTP, which stands for "Tty SToP"; the signals are different because SIGSTOP cannot be trapped by the program you are controlling.

Your point about this interfering with the operation of some programs is, however, quite valid.

(Amusingly, Wikipedia has an article for each POSIX signal, including http://en.wikipedia.org/wiki/SIGTSTP )



[ Reply to This | # ]
Limit CPU usage for an application/process
Authored by: BigBadbenny on Feb 02, '11 07:27:29PM

A paid alternative is Coolbook.
It offers cpu undervolting as well as fine control over cpu speed steps, cpu throttling and temp limits.



[ Reply to This | # ]
Limit CPU usage for an application/process
Authored by: BryanD on Feb 04, '11 03:01:37AM

'Though Fan Control can help keep a MacBook Pro cooler, ultimately things that hammer both CPUs can get things so hot that even the fans won't keep the temperature down — and high heat will shorten the lifetime.

There's another trick that I use — just pop out the battery and the CPUs will be limited to 1 GHz (at least this is the case with my MBP Dual 2.33 GHz machine). While you obviously need to keep the unit plugged in (and mine's normally also on a UPS circuit when I do this), it's about impossible to get the unit too hot when it's limited to 1 GHz. Great for those multi-hour processing jobs.



[ Reply to This | # ]