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.
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.
niceonly 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.
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%.
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
-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.
cgroups were created for exactly this reason.
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.
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:
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):
As you can see the process is still consuming all the idle CPU time.