Limiting CPU of BOINC client automatically

Yesterday an article was on the front page of digg, regarding fighting climate change by leaving your computer turned on, and it piqued my interested (after all, your computer being turned on is wasting electricity, so how could it possible counter that horrible effect?), so I took a look and it was just an pseudo advertisement for participating in BOINC for climateprediction.net. Now, while I normally don’t participate in any distributed computing projects, simply because I hate having my server bring up room temperature by multiple degrees, I figured I’d give it a try for the pure challenge of trying to limit the CPU it uses.

So I did some due diligence and found an application for Linux called cpulimit, which lets you specify a process by name, PID or by full path, and this program will limit it to a certain amount of CPU utilization, so I can tell the BOINC client to only use 25% of my CPU, for example. Now, while cpulimit is a pretty neat application, it has one problem, and that is its inability to limit the CPU usage of child processes spawned from one of your existing limited process. This means that in the case of BOINC, I can’t just start it and then tell cpulimit to limit the boinc_client process, since that process kicks off any number of additional processes.

However, what you can do is write a script that will find all processes being run by your boinc user (I did an apt-get install boinc-client, and the Debian package automatically creates a boinc user and runs all of the processes as that user), and throttle them, then just toss that into cron and make sure that none of the processes use more than your specified limit.

Take a look “after the jump” for the bash script that does the above. It’s quite simple, but amazingly I couldn’t find any off-the-shelf ones on Google.

Run this bad boy in cron every 10 minutes, to make sure anytime a new boinc process starts it gets limited:

#!/bin/bash
#limitboinc.sh: Limit percent of cpu used by a user, using cpulimit -- paul@supz.org
LIMIT=25
USER=boinc
PID=$(ps h -o pid -u$USER)
for a in $PID ; do
  echo -n "Checking if process $a is already limited. "
  ps -fC cpulimit | grep $a >/dev/null
  if [ $? -ne 0 ] ; then
    echo -n "starting"
    nohup /usr/bin/cpulimit -p $a -z -l $LIMIT >/dev/null 2>&1 &
  else
    echo -n "cpulimit already running"
  fi
  echo "."
done

Pop this into your crontab:

*/10 * * * * /root/limitboinc.sh >/dev/null 2>&1

Post a Comment

You must be logged in to post a comment.