cpufreqd: select rule depending on external command

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:

  1. [Rule]
  2. name=CPU Too Hot
  3. acpi_temperature=65-100
  4. cpu_interval=50-100
  5. profile=Performance Low
  6. [/Rule]

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

  1. [Rule]
  2. name=Movie Watcher
  3. programs=xine,mplayer,gmplayer
  4. battery_interval=0-100
  5. acpi_temperature=0-60
  6. profile=Performance High
  7. [/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 [1] and cpufreqd-2.3.3 [2]).

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

  1. [Rule]
  2. name=emerge is running
  3. exec=ps -u root | grep emerge
  4. battery_interval=0-100
  5. acpi_temperature=0-65
  6. cpu_interval=0-100
  7. profile=Performance High
  8. [/Rule]

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