Low profile configuration / Limit CPU usage

Support for the Debian/Ubuntu package, created by JCFP.
Forum rules
Help us help you:
  • Are you using the latest stable version of SABnzbd? Downloads page.
  • Tell us what system you run SABnzbd on.
  • Adhere to the forum rules.
  • Do you experience problems during downloading?
    Check your connection in Status and Interface settings window.
    Use Test Server in Config > Servers.
    We will probably ask you to do a test using only basic settings.
  • Do you experience problems during repair or unpacking?
    Enable +Debug logging in the Status and Interface settings window and share the relevant parts of the log here using [ code ] sections.
Post Reply
sinkiss
Newbie
Newbie
Posts: 1
Joined: June 28th, 2011, 5:14 am

Low profile configuration / Limit CPU usage

Post by sinkiss »

Hi, i'm new to this forum, so I hope my post is in the good Topic.

I use and enjoy SABnzbd+ for some time now, and I've faced a CPU usage issue.

I use an old PC, with many different services (Apache, Mysql, Ampache, mediatomb, openvpn, amule-daemon .... most of them use ssl) that run concurently and each of them use quite some CPU.

My download speed is around 3Mo/sec, and I use ssl for usenet access.

The probleme is that when I run a big download batch, sabnzbd+ begin to use CPU intensivly, and when the first downloaded item arrive, par2 began it's CPU usage followded by unrar. 

With the High temperature we reach thi summer, my low profile configuration quickly began to overheat .... and lead the PC to an unwanted poweroff.
That was very disturbing for me, causing all hosted services to poweroff.

Since downloading isn't a Highly important service, it's not very important, for me, to run at maximum CPU rate for par verifyng or decompressing.

So, I've look around and I've found a little binary that can limit cpu usage of any running process or not yet running process (like unrar and par2 that are run on demand).

the name is cpulimit http://cpulimit.sourceforge.net/ , available in ubuntu official release http://doc.ubuntu-fr.org/cpulimit:

Code: Select all

sudo apt-get install cpulimit

You can use it to limit any kind of process running on your system.
It's very simple tu use, everything goes throught the commandline.

To ease the use, I've written a simple init script, to run limit on startup, for the three process, unrar, par2 and sabnzbd+.
Each process is limited to use 40% of the CPU, on a dual core system (that is a 80 in the parameter, ie 2 processors = a maximum of 200 CPU usage, 4 processors = 400 , so 40% CPU on a Quad pro is equal to 160 for cpulimit, read cpulimit documentation .... )

Everything is configurable throught the Variable  at the begining of the script.

There is a dependance on sabnzbd+ startup, cause I need to recover the PID of sabnzbd+, so it should allready be started when it is launched.

Code: Select all

/etc/init.d/cpulimit.sh

Code: Select all

#!/bin/bash
### BEGIN INIT INFO
# Provides:          cpulimit.sh
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start cpulimtit daemon at boot time for unar, par2 and sabnzbd+
# Description:       Enable services CPU usage limitation.
### END INIT INFO
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

DAEMON=/usr/bin/cpulimit
LIMIT_NZB=80
LIMIT_RAR=80
LIMIT_PAR=80
PID_NZB=/var/run/cpulimit_nzb
PID_RAR=/var/run/cpulimit_rar
PID_PAR=/var/run/cpulimit_par
PID_SAB=`ps aux |grep sab | grep -v grep| awk '{print $2}'`
OPTION_NZB=" --pid $PID_SAB --limit $LIMIT_NZB"
OPTION_RAR=" --exe unrar --limit $LIMIT_RAR"
OPTION_PAR=" --exe par2 --limit $LIMIT_PAR"

NAME=cpulimit
COMMENT_RAR=unrar
COMMENT_PAR=par2
COMMENT_NZB=sabnzbplus


DESC="cpulimit"

test -x $DAEMON || exit 0

set -e

depend() {
        after sabnzbdplus
}

case "$1" in

  start)

        echo -n "Starting $DESC: $NAME $COMMENT_NZB"

        start-stop-daemon --start --background --quiet --make-pidfile \
         --pidfile $PID_NZB --exec $DAEMON -- $OPTION_NZB

        echo "."

        echo -n "Starting $DESC: $NAME $COMMENT_RAR"

        start-stop-daemon --start --background --quiet --make-pidfile \
         --pidfile $PID_RAR --exec $DAEMON -- $OPTION_RAR

        echo "."

        echo -n "Starting $DESC: $NAME $COMMENT_PAR"

        start-stop-daemon --start --background --quiet --make-pidfile \
         --pidfile $PID_PAR --exec $DAEMON -- $OPTION_PAR
        echo "."

       ;;

  stop)

        echo -n "Stopping $DESC: $NAME $COMMENT_NZB"

        start-stop-daemon --stop --quiet --oknodo --pidfile $PID_NZB \
         --exec $DAEMON
        rm -rf $PID_NZB

        echo "."

        echo -n "Stopping $DESC: $NAME $COMMENT_RAR"

        start-stop-daemon --stop --quiet --oknodo --pidfile $PID_RAR \
         --exec $DAEMON
        rm -rf $PID_RAR

        echo "."

        echo -n "Stopping $DESC: $NAME $COMMENT_PAR"

        start-stop-daemon --stop --quiet --oknodo --pidfile $PID_PAR \
         --exec $DAEMON
        rm -rf $PID_PAR

        echo "."

       ;;

  restart|force-reload)

        #

        #       If the daemon can reload its config files on the fly

        #       for example by sending it SIGHUP, do it here.

        #

        #       If the daemon responds to changes in its config file

        #       directly anyway, make this a do-nothing entry.

        #

        echo -n "Reloading $DESC configuration..."

        start-stop-daemon --stop --signal 1 --quiet --oknodo --exec $DAEMON

        echo "done."

       ;;
  status)

        ps aux | grep cpulimit

       ;;
  *)

        N=/etc/init.d/$NAME

        # echo "Usage: $N {start|stop|restart|reload|force-reload}" >&2

        echo "Usage: $N {start|stop|restart|force-reload|status}" >&2

        exit 1

       ;;

esac



exit 0



Default value in this script are for a dual core:

Code: Select all

LIMIT_NZB=80
LIMIT_RAR=80
LIMIT_PAR=80
sabnzbd+ 40% CPU usage (that is 80 = 40*2)
unrar 40% CPU usage (that is 80 = 40*2)
par2 40% CPU usage (that is 80 = 40*2)

for example if you want to limit 40% CPU usage on a quad core you will set 160 = 4*40 :

Code: Select all

LIMIT_NZB=160
LIMIT_RAR=160
LIMIT_PAR=160
tu run this script at startup :

Code: Select all

update-rc-d cpulimit.sh defaults

I'm no expert so everything, could/should be improved, this is a first try.
Hope it can help some people out there.
Have a nice day.
User avatar
shypike
Administrator
Administrator
Posts: 19774
Joined: January 18th, 2008, 12:49 pm

Re: Low profile configuration / Limit CPU usage

Post by shypike »

Nice initiative.
I'm moving this to the Ubuntu/Debian section.
kazooless
Newbie
Newbie
Posts: 4
Joined: December 1st, 2011, 10:00 pm

Re: Low profile configuration / Limit CPU usage

Post by kazooless »

Awesome! This is exactly what I was looking for. The "nice" and "ionice" parameters just didn't seem to make a difference for me. I hope this does, but pretty hopeful.
Post Reply