15
  1. I have a program. When it is running, the CPU temperature raise from 50 to 80 Celcius, which is my major concern.

    I can control the CPU frequency to slow it down, but other processes will be slowed down as well which I don't want.

    Is it possible to slow down a particular process without affecting other processes to keep the CPU cool?

    My OS is Ubuntu 10.10.

  2. I tried to set the priority of the process by nice -n 15 myprogram, and am not sure if that will work. The CPU is 77 Celcius high.

    Does nice only set relative priority of a process wrt other processes? I.e., if other processes are not running, will this niced process run fast? I would like to set the process to be running slow all through.

6
  • 1
    What does this process do: heavy input/output? Heavy calculations on the main CPU? Heavy calculations on the GPU (video card) (usually but not limited to programs that calculate images)? Aug 16, 2011 at 22:56
  • The program is pdf2djvu, converting a pdf file to djvu. Is it heavy IO or heavy CPU or heavy GPU? Do you have different ways for different cases?
    – Tim
    Aug 16, 2011 at 23:04
  • 4
    80º is very very high. I live in a hot place of Spain and the maximus temperature of my processor is ~60º. Open the computer, clean all pieces and change the coolers. I think your isn't the CPU use. Aug 17, 2011 at 1:22
  • Try doing both.
    – stribika
    Aug 17, 2011 at 13:17
  • 80° is actually high but not considered extreme depending of the CPU model. most CPU support up to 90°C without issue
    – Kiwy
    Oct 22, 2018 at 15:11

3 Answers 3

21

CPULimit is exactly what you need. You start the program, then run cpulimit against the program name or PID, specifying what percentage you want it limited.

The following command limits the process at PID 7777 to 5% CPU usage.

cpulimit -p 7777 -l 5

Alternatively, you can use the name of the executable:

cpulimit -e myprogram -l 5

Or the absolute path of the executable:

cpulimit -P /path/to/myprogram -l 5

Note the percentage is of all cores; so if you have 4 cores, you could use 400%.

2
  • 1
    Thanks! (1) Is CPULimit only used for already run process? Not used when submitting a program to run? (2) does it fix the percentage of usage of CPU by a process, or set the upper limit for the process and allow its usage to fluctuate between 0 and that limit according to other processes? (3) I also found that after using CPULimit on a process, there is no way to Ctrl+C to cancel the running, because it will soon return back to run automatically. Wonder why?
    – Tim
    Aug 17, 2011 at 0:36
  • 3
    A small caveat about CPULimit: it uses SIGSTOP/SIGCONT to limit the amount of CPU time used by a process (processes exceeding the target CPU usage are stopped; when enough time has passed, the process is sent a SIGCONT and resumes execution). However, there are rare cases in which signals can interfere with system calls, see the GNU C library manual for details. Since CPULimit sends these signals many times a second, there's quite a chance of interrupting. Not a big problem for running pdftk on a desktop, tho. Aug 18, 2011 at 21:13
9

You can renice a running process to give it more or less priority (the so-called "nice value"). Note that the UNIX priority scale is somewhat counter-intuitive: negative values mean a process is favored over concurrent processes, i.e., it has "more" priority.

You can thus try to "slow down" your process given its PID through:

# lower priority of a process
renice +1 "PID"

Every time you run this, the process "nice value" is raised by 1; you can use integer values other than +1 of course.

The command nice allows you to start a process with a +10 nice value adjustment (change this with option -n). For example:

# start a CPU-intensive task with low priority
nice ./cpu-hog

However, the "nice value" only affects how much the scheduler favors running a particular process over others in the system: if your computer is basically idling, raising the "nice value" of one single process will not stop that process from taking 100% CPU. I quote from the getpriority(2) manpage: (Emphasis added by me.)

The degree to which their relative nice value affects the scheduling of processes varies across Unix systems, and, on Linux, across kernel versions. Starting with kernel 2.6.23, Linux adopted an algorithm that causes relative differences in nice values to have a much stronger effect. This causes very low nice values (+19) to truly provide little CPU to a process whenever there is any other _higher priority load on the system._

The reason for this resides in the way processes are run on a UNIX-like kernel: every time the kernel decides to run a process, that process has full control of a CPU core for a certain (fixed and short) span of time. The "nice value" can influence how often the kernel scheduler is willing to give a time slot to a process, but you cannot change the fact that, once scheduled, a process runs undisturbed for a fixed amount of time.

Therefore, short of slowing down your CPU there is no way to make a process run slower if there are no other processes in the system that can contend for CPU access.

5
  • Thanks! Is it possible to make a process run slow from the beginning till the ending, regardless of other processes?
    – Tim
    Aug 16, 2011 at 23:09
  • @Tim I don't think what you ask is possible; I've updated the answer with more details. Hope that makes it more clear. Aug 16, 2011 at 23:41
  • 1
    The other reply suggested CPULimit, which seems to do the impossible thing.
    – Tim
    Aug 17, 2011 at 0:52
  • 1
    Ah, I didn't know about CPULimit - it definitely fills a gap in my UNIX tool set. Aug 18, 2011 at 21:05
  • nice won't do a thing to keep your processor cool. It is not designed to work like that.
    – Ken Sharp
    Dec 4, 2017 at 20:36
4

cgroups were created for exactly this reason.

http://www.kernel.org/doc/Documentation/cgroups/ http://www.serverwatch.com/tutorials/article.php/3921001/Setting-Up-Linux-Cgroups.htm

It takes a little while to familiarise yourself with them, and I believe you need root access to set them up, but it can all be scripted. The newer Ubuntus have a .conf file so that you don't have to write your own script. I'm not sure about 10.10.

A nice place to start is in this answer: https://askubuntu.com/a/94743/170177

Note that cgroups is still under active development so some features may be unavailable in your current kernel.

Using cgroups' cpu.shares does nothing that a nice value wouldn't do. It sounds like you want to actually throttle the processes, which can definitely be done.

You will need to use a script or two, and/or edit /etc/cgconfig.conf to define the parameters you want.

Specifically, you want to edit the values cpu.cfs_period_us and cpu.cfs_quota_us. The process will then be allowed to run for cpu.cfs_quota_us microseconds per cpu.cfs_period_us microseconds.

For example:

If cpu.cfs_period_us = 50000 and cpu.cfs_quota_us = 10000 then the process will receive 20% of the CPU time maximum, no matter what else is going on.

In this screenshot I have given the process 2% of CPU time:

2% CPU time

As far as the process is concerned it is running at 100%.

Settings cpu.shares on the other hand can and will still use 100% of the idle CPU time.

In this similar example I have given the process cpu.shares = 100 (of 1024):

cpu.shares

As you can see the process is still consuming all the idle CPU time.

References:

http://manpages.ubuntu.com/manpages/precise/man5/cgconfig.conf.5.html http://kennystechtalk.blogspot.co.uk/2015/04/throttling-cpu-usage-with-linux-cgroups.html

4
  • Note: systemd does things a little different. Ubuntu Trusty, for example, seems to come with two services to manage cgroups. I'll update when I know more.
    – Ken Sharp
    Dec 6, 2015 at 3:49
  • But you can change the max cpu% when the process is already started, as renice? May 1, 2019 at 14:57
  • 1
    renice does not change the amount of CPU used, it just changes the priority of the process in the scheduler. cgroups can change the CPU limit at any time. Since this answer was posted cgroups have had all kinds of tweaks added and is very powerful.
    – Ken Sharp
    May 2, 2019 at 3:55
  • renice does not change the amount of CPU uses I know it ;) I just posted as an example of a program that changes things at runtime. Thank you for your reply, I'll try cgroups. May 2, 2019 at 9:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .