You are not logged in.

#1 2011-06-08 13:18:31

walterjwhite
Member
Registered: 2011-05-01
Posts: 207

control groups - automatically put processes in a control group

Hi all,

I recently came across control groups and it is a great idea to limit process resource usage.  The only downside is that you must know the PID, is there any way to easily configure that based on the process name or filename?

For example, I would like pacman or any cron task to run with low priority.  I would have to search for the PID and echo that into the PID list for that control group.  That is definitely doable, but seems cumbersome.


Do you guys have any other ideas?


Thanks,

Walter

Offline

#2 2011-06-08 13:28:52

ChoK
Member
From: France
Registered: 2008-10-01
Posts: 346

Re: control groups - automatically put processes in a control group

You can use pidof to get the PID from a process' name.


Ah, good taste! What a dreadful thing! Taste is the enemy of creativeness.
Picasso
Perfection is reached, not when there is no longer anything to add, but when there is no longer anything to take away.
Saint Exupéry

Offline

#3 2011-06-08 14:01:53

walterjwhite
Member
Registered: 2011-05-01
Posts: 207

Re: control groups - automatically put processes in a control group

Hi Chok,

That is a good start - I need to wait till cron invokes the tasks and then grab the PID and put that into the list of PIDs.  I need to explore options for how to do that part.


Walter

Offline

#4 2011-06-08 14:21:46

ChoK
Member
From: France
Registered: 2008-10-01
Posts: 346

Re: control groups - automatically put processes in a control group

If you start cron in its own cgroup, I'm pretty sure everything started by it should be in the same cgroup


Ah, good taste! What a dreadful thing! Taste is the enemy of creativeness.
Picasso
Perfection is reached, not when there is no longer anything to add, but when there is no longer anything to take away.
Saint Exupéry

Offline

#5 2011-06-08 14:38:31

walterjwhite
Member
Registered: 2011-05-01
Posts: 207

Re: control groups - automatically put processes in a control group

Hi Chok,

Actually, I think I will do it a bit differently.

I will make my own service that utilizes watch.  It will take a configuration that will look for processes and automatically assign them to a group when they're running.  The service can be started/stopped at any point in time.


Walter

Offline

#6 2011-06-08 19:59:49

walterjwhite
Member
Registered: 2011-05-01
Posts: 207

Re: control groups - automatically put processes in a control group

Hi Chok,

I made a primitive daemon to automatically prioritize tasks.  I setup a configuration directory in /etc/cgroups/groups/group/dev.  Within the dev folder, all of those settings get copied to /dev/cgroups, right now, I use pidof to match the process name of the process names in process-names of the group directory.

I created a simple rc script and executable script which does all of the work, it runs every 2 seconds by default (the default from watch).

How can we attach files?

Offline

#7 2011-06-09 09:27:13

ChoK
Member
From: France
Registered: 2008-10-01
Posts: 346

Re: control groups - automatically put processes in a control group

I'm not sure of what you mean by attaching files. If you want to display your file you can use the [ code ][ /code ] tag (without spaces).


Ah, good taste! What a dreadful thing! Taste is the enemy of creativeness.
Picasso
Perfection is reached, not when there is no longer anything to add, but when there is no longer anything to take away.
Saint Exupéry

Offline

#8 2011-06-09 11:31:05

walterjwhite
Member
Registered: 2011-05-01
Posts: 207

Re: control groups - automatically put processes in a control group

Hi,

This is what I have so far.  It needs some work, like not re-adding an existing PID to the cgroups.procs:

/usr/sbin/cgroups

#!/bin/bash

GROUPS_DIRECTORY="/etc/cgroups/groups"
GROUPS_DEV_DIRECTORY="/dev/cgroups"
LOGFILE="/var/log/cgroups.log"

function setup_cgroups {
    if [ ! -e "/dev/cgroups" ]
    then
        mkdir /dev/cgroups
        mount /dev/cgroups
    fi
}

function configure {
    # cgroups
    # configuration is in /etc/cgroups
    for GROUP in `ls ${GROUPS_DIRECTORY}`
    do        
        GROUP_DIRECTORY="${GROUPS_DIRECTORY}/${GROUP}"
        DEV_DIRECTORY="${GROUPS_DEV_DIRECTORY}/${GROUP}"
    
        echo -e "copying group configuration:$GROUP\n" >> $LOGFILE
        if [ ! -e "$DEV_DIRECTORY" ]
        then
            mkdir $DEV_DIRECTORY
                
        fi
        
        cp -R $GROUP_DIRECTORY/dev/* $DEV_DIRECTORY
    done
}

function monitor {
    # cgroups
    # configuration is in /etc/cgroups
    for GROUP in `ls ${GROUPS_DIRECTORY}`
    do
        GROUP_DIRECTORY="${GROUPS_DIRECTORY}/${GROUP}"
        DEV_DIRECTORY="${GROUPS_DEV_DIRECTORY}/${GROUP}"

        while read processName
        do
            PID=`pidof "$processName" | sed "s/ /\n/g"`
            
            if [ -n "$PID" ]
            then
                echo -e "throttling process:$PID.\n" >> $LOGFILE
                echo $PID >> $DEV_DIRECTORY/cgroup.procs
            fi
        
        done < $GROUP_DIRECTORY/process-names
    done
}

if [ "$1" == "watch" ]
then
    echo `date`" monitoring" >> $LOGFILE
    monitor
else
    setup_cgroups
    configure
    watch /usr/sbin/cgroups watch &> /dev/null &
fi

exit 0

/etc/rc.d/cgroups

#!/bin/bash

. /etc/rc.conf
. /etc/rc.d/functions

[ -f /etc/cgroups ]

PID=`pidof -o %PPID watch /usr/sbin/cgroups`
case "$1" in
  start)
    stat_busy "Starting cgroups"
    
    [ -z "$PID" ] && /usr/sbin/cgroups
    if [ $? -gt 0 ]; then
      stat_fail
    else
      add_daemon cgroups
      stat_done
    fi
    ;;
  stop)
    stat_busy "Stopping cgroups"
    [ ! -z "$PID" ] && kill $PID &> /dev/null
    if [ $? -gt 0 ]; then
      stat_fail
    else
      rm_daemon cgroups
      stat_done
    fi
    ;;
  restart)
    $0 stop
    $0 start
    ;;
  *)
    echo "usage: $0 {start|stop|restart}"  
esac
exit 0

sample process list (by-name)
/etc/cgroups/groups/background-tasks/process-names

cron
pacman

Let me know what you think.


Thanks,

Walter

Offline

#9 2011-06-09 12:22:12

ChoK
Member
From: France
Registered: 2008-10-01
Posts: 346

Re: control groups - automatically put processes in a control group

This seems very nice. It's probably worth a wiki entry.


Ah, good taste! What a dreadful thing! Taste is the enemy of creativeness.
Picasso
Perfection is reached, not when there is no longer anything to add, but when there is no longer anything to take away.
Saint Exupéry

Offline

#10 2011-06-09 16:51:58

adee
Member
From: The Moon
Registered: 2009-11-10
Posts: 110

Re: control groups - automatically put processes in a control group

You can also take a look at ulatencyd. You can write custom rules to it

Offline

Board footer

Powered by FluxBB