CPU assignment bash script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting CPU assignment bash script
# 1  
Old 05-19-2010
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]):

Code:
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

# 2  
Old 05-19-2010
could you please give me the sample output of below commens first? I can't get the output from my environment.

Code:
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.

Code:
top -b -n 1

# 3  
Old 05-19-2010
It just spits out the PIDs like this:

Code:
5555
6666
7777

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash variable assignment failure/unary operator expected

I have a little code block (executing on AIX 7.1) that I cannot understand why the NOTFREE=0 does not appear to be assigned even though it goes through that block. This causes a unary operator issue. #!/bin/bash PLATFORM="AIX" NEEDSPC=3000 set -x if ; then lsvg | grep -v rootvg | while... (6 Replies)
Discussion started by: port43
6 Replies

2. Shell Programming and Scripting

Loosing trailing new line in Bash var assignment

I have come across a weird behaviour in bash and would love to get to the bottom of it. If I execute echo -e "\na\nb\nc\n" at the command line, I get: a b c However, if I wrap it in an assignment such as: A="$( echo -e "\na\nb\nc\n" )"then I get a b cIt doesn't show very well,... (4 Replies)
Discussion started by: jamesbor
4 Replies

3. Shell Programming and Scripting

Bash variable assignment question

Suppose I have a file named Stuff in the same directory as my script. Does the following assign the file Stuff to a variable? Var="Stuff" Why doesn't this just assign the string Stuff? Or rather how would I assign the string Stuff to a variable in this situation? Also, what exactly is... (3 Replies)
Discussion started by: Riker1204
3 Replies

4. Shell Programming and Scripting

Making a bash script and small C program for a homework assignment

Here's the assignment. I'll bold the parts that are rough for me. Unfortunately, that's quite a bit lol. The syntax is, of course, where my issues lie, for the most part. I don't have a lot of programming experience at all :/. I'd post what I've already done, but I'm so lost I really don't know... (1 Reply)
Discussion started by: twk101
1 Replies

5. Homework & Coursework Questions

Bash Shell Programming assignment.

Please take a look I am stuck on step 4 1. The problem statement, all variables and given/known data: #!/bin/bash ### ULI101 - ASSIGNMENT #2 (PART A) - DUE DATE Wed, Aug 3, 2011, before 12 midnight. ###==================================================================================== ###... (13 Replies)
Discussion started by: almirzaee
13 Replies

6. Shell Programming and Scripting

BASH: Script jams Cygwin to 100% CPU -

I'd like to streamline the code more than a bit to get it to run faster. There's a thread about this and related issues of mine on the Cygwin mailing-list, but I want to eliminate any chances it might just be inefficient/inelegant/crappy code. A previous run of the same script on both Cygwin and... (6 Replies)
Discussion started by: SilversleevesX
6 Replies

7. Shell Programming and Scripting

Is there a way to make bash [or another shell] use all CPU cores to execute a single script?

I wrote a very simple script that matches combinations of alphabetic characters (1-5). I want to use it to test CPU speeds of different hardware/platforms. The problem is that on multi-core/processor systems, only one CPU is being utilized to execute the script. Is there a way to change that?... (16 Replies)
Discussion started by: ph0enix
16 Replies

8. Shell Programming and Scripting

Help with bash script - Need to get CPU usage as a percentage

I'm writing a bash script to log some selections from a sensors output (core temp, mb temp, etc.) and I would also like to have the current cpu usage as a percentage. I have no idea how to go about getting it in a form that a bash script can use. For example, I would simply look in the output of... (3 Replies)
Discussion started by: graysky
3 Replies

9. UNIX for Dummies Questions & Answers

2 script for assignment.

I need a script that will check if what I input is a file or not. Also a short script that copies a file that I tell it to, to a directory I tell it to. Thanks. Those are the only 2 I need, I got the other 8 done. (1 Reply)
Discussion started by: snyper2k2
1 Replies
Login or Register to Ask a Question