cpufreqd: select rule depending on external command

Published by cybso on
This is a post from my original site, which was hosted by the former blog service of the University of Osnabrück. I have moved it to the new site for archiving. Pages linked in this article may no longer work today, and the blog comments under the article no longer exist. Opinions expressed in this article reflect the point of view of the time of publication and do not necessarily reflect my opinion today.

Cpu Frequency Daemon is a very configurable program for throttling the cpu's frequency depending on "rules". A rule tests for battery state, cpu temperatur, active processes and so on.

For example, a rule which throttles your cpu when it's to hot might look like this:

[Rule]
name=CPU Too Hot
acpi_temperature=65-100
cpu_interval=50-100
profile=Performance Low
[/Rule]

And if you want full performance when watching movies you might have a rule like this:

[Rule]
name=Movie Watcher
programs=xine,mplayer,gmplayer
battery_interval=0-100
acpi_temperature=0-60
profile=Performance High
[/Rule]

(For more examples, have a look at the manpage)

As a Gentoo user I want full cpu power when emerge is running. But sadly, "programs=emerge" won't work because emerge' process actually is the python interpreter (and surely you don't want to define "programs=python" as it would fire on your processor for all python scripts):

$ ps a | grep emerge
13423 pts/2    SN+    0:02 /usr/bin/python -O /usr/bin/emerge libc

So what we need is a cpufreqd.conf-statement which executes a shell command and scores the rule depending on the process' exit code (0 is good and anything else is bad). There is already a plugin, "exec", which executes a command before and after testing a rule or switching the profile, although this is not exactly what we want. Nevertheless the code of this plugin (cpufreqd_exec.c) only needs a few changes to get a new options called "exec" which does exactly what we need (I've attached patches for cpufreqd-2.2.1 and cpufreqd-2.3.3).

The rule for switching to "Performance High" when emerge is running now looks like this:

[Rule]
name=emerge is running
exec=ps -u root | grep emerge
battery_interval=0-100
acpi_temperature=0-65
cpu_interval=0-100
profile=Performance High
[/Rule]

(If somebody knows an more efficient method to check wether emerge is running or not, please post it into the comments).