CPU assignment bash script

Hi guys,

I'm basically looking for some help with a bash script I've written. It's purpose is to assign process to individual CPU cores once that process hits 15% CPU usage or more. If it drops below 15%, it's unassigned again (using taskset).

My problem is that I can't think of a way to make the script check if the process is already assigned to an invidividual core, and if so to make it skip the assigning process and remove that core from the array of available cores (there is only enough cores in the array to allow each core to have two processes assigned each [there won't be more than that many processes on the machine so it won't ever go over the limit]). As it currently works, a process might be over 15% and already assigned to one CPU, but if another process' status has changed it could be moved from one CPU to another. I'd like this not to happen, so it doesn't switch processes between CPUs all the time.

Keep in mind I'm pretty basic in my knowledge of this stuff so some parts of the script might be a bit iffy as well. Any suggestions to fix other parts would be appreciated too.

This is what I have so far (for a quad core [you can see it's only for processes with ds_i in them]):

CPUS=( 1 2 3 0 1 2 3 0 )

PIDS=( `top -b -n 1 | grep ds_i | egrep '[SDR]   ( [0-9]|1[1-4]) ' | sed -e "s/^ *//" -e "s/ .*$//"` )

# Processes with 14% or less usage
for (( e = 0 ; e < ${#PIDS[@]} ; e++ ))
do
taskset -cp 0-3 ${PIDS[$e]}
chrt -o -p 0 ${PIDS[$e]}
done

unset PIDS


PIDS=( `top -b -n 1 | grep ds_i | egrep '[SDR]   ([1][5-9]|[2-9][0-9]) ' | sed -e "s/^ *//" -e "s/ .*$//"` )
# Processes with 15%+ CPU usage
for (( p = 0 ; p < ${#PIDS[@]} ; p++ ))
do
AFF=`taskset -cp ${PIDS[$p]} | sed -e "s/.* current affinity list: //"`

len=`echo $AFF | wc -c`
len=`expr $len - 1`

if [ $len -lt "2" ]
then

        if [ $AFF -lt "4" ]
        then
                for (( e = 0 ; e < ${#CPUS[@]} ; e++ ))
                do

                        if [[ $AFF == ${CPUS[$e]} ]]
                        then
                        unset CPUS[$e] #Remove used CPU from array
                        CPUS=( ${CPUS
[*]} )
                        unset PIDS[$p] #Remove PID from array
                        break
                        fi
                done
        fi
fi
done

PIDS=( ${PIDS
[*]} )

for (( p = 0 ; p < ${#PIDS[@]} ; p++ ))
do

taskset -cp ${CPUS[0]} ${PIDS[$p]}
chrt -f -p 59 ${PIDS[$p]}
unset CPUS[0]
CPUS=( ${CPUS
[*]} )

done

could you please give me the sample output of below commens first? I can't get the output from my environment.

PIDS=( `top -b -n 1 | grep ds_i | egrep '[SDR]   ( [0-9]|1[1-4]) ' | sed -e "s/^ *//" -e "s/ .*$//"` )

PIDS=( `top -b -n 1 | grep ds_i | egrep '[SDR]   ([1][5-9]|[2-9][0-9]) ' | sed -e "s/^ *//" -e "s/ .*$//"` )

Or give me the top output directly.

top -b -n 1

It just spits out the PIDs like this:

5555
6666
7777