I want my device to use less CPU to save battery. I have a program that uses a large amount of it. nice
won't help as all it does is change scheduling when CPU is at %100, while I don't want it to get that high

- 90,279

- 249
3 Answers
In order to make your CPU use less battery, you need it to run slower. The total amount of CPU instructions needed to execute your program do not depend on the speed at which it runs, so limiting the proportion of CPU time used by your program would make it use more CPU, not less. When your CPU is doing nothing instead of running your program, it's still using power. At best, the pauses during which the CPU is doing nothing will be long enough for it to go into a power saving state; but even then you'll pay a penalty for the extra power state transitions.
Limiting the proportion of CPU time used by your process may have the indirect effect of causing the kernel to switch to a less fast CPU clock speed. Whether this happens depends on the choice of CPU frequency governor and the exact numbers involved. It will happen with the default governor (ondemand), but the rate is difficult to control.
If you want to save power, the best strategy is to let your program use 100% of the available CPU power (so that it finishes as quickly as possible), but switch your CPU to running as slowly as possible (because the amount of energy spent per instruction increases with the CPU speed).
You can configure the CPU frequency through parameters in /sys/devices/system/cpu/cpu0/cpufreq
and its cousins, or with various user interfaces. The cpufreq utilities provide two command line tools: cpufreq-info
, cpufreq-set
. There is a more advanced tool suite called cpupower which is now included in the kernel sources. If you use Gnome, you can use its Cpufreq applet. There are other GUIs.
To save as much battery as possible, run your CPU at its minimum frequency. Run cpufreq-info
to display available frequencies. Run cpufreq-set -c 0 -g powersave
to always run CPU 0 at its minimum frequency (repeat with increasing values for -c
for the other CPUs).

- 829,060
-
some governors can be set to not increase cpu-frequency for nice processes. – ctrl-alt-delor Aug 24 '14 at 12:38
you can use cpulimit for specific PID for example
sudo cpulimit -b -l 80 -p 1183
renice:
renice +19 1183
this will set priority to the lowest possible for 1183

- 2,839
-
That won't help much, if at all. Setting a CPU limit under 95% may cause the CPU to slow down under the default CPU governor (ondemand), which will save energy, but the execution pauses will consume energy for nothing, which may well offset these savings. – Gilles 'SO- stop being evil' May 14 '14 at 00:33
I don't know whether there is an elegant solution for this but you may stop and restart the process with signals:
PID=1234
mode=running
run_time=0.1
stop_time=0.1
while true; do
if [ running = "$mode" ]; then
kill -STOP "$PID"
mode=stopped
sleep "$run_time"
else
kill -CONT "$PID"
mode=running
sleep "$stop_time"
fi
done

- 90,279
-
That won't change how much energy the process consumes. (In fact it will consume very slightly more energy due to the extra scheduling.) – Gilles 'SO- stop being evil' May 14 '14 at 00:32
-
@Gilles That is right only if the unmodified process consumes less that the easy to adapt ratio $run_time / $stop_time. Of course, a pausing CPU (not in a (real) power saving state!) does consume less energy than a working one. This is not a useful strategy instead of but only after frequency limitation (which has a relevant lower limit). – Hauke Laging May 14 '14 at 05:55
kill
processes consuming huge CPU utilization. – Ramesh May 13 '14 at 20:17