Sponsored Content
Full Discussion: CPU assignment bash script
Top Forums Shell Programming and Scripting CPU assignment bash script Post 302422956 by mcky on Wednesday 19th of May 2010 09:50:57 PM
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

 

9 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

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

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

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

8. 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

9. 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
RBASH(1)						      General Commands Manual							  RBASH(1)

NAME
rbash - restricted bash, see bash(1) RESTRICTED SHELL
If bash is started with the name rbash, or the -r option is supplied at invocation, the shell becomes restricted. A restricted shell is used to set up an environment more controlled than the standard shell. It behaves identically to bash with the exception that the follow- ing are disallowed or not performed: o changing directories with cd o setting or unsetting the values of SHELL, PATH, ENV, or BASH_ENV o specifying command names containing / o specifying a file name containing a / as an argument to the . builtin command o specifying a filename containing a slash as an argument to the -p option to the hash builtin command o importing function definitions from the shell environment at startup o parsing the value of SHELLOPTS from the shell environment at startup o redirecting output using the >, >|, <>, >&, &>, and >> redirection operators o using the exec builtin command to replace the shell with another command o adding or deleting builtin commands with the -f and -d options to the enable builtin command o using the enable builtin command to enable disabled shell builtins o specifying the -p option to the command builtin command o turning off restricted mode with set +r or set +o restricted. These restrictions are enforced after any startup files are read. When a command that is found to be a shell script is executed, rbash turns off any restrictions in the shell spawned to execute the script. SEE ALSO
bash(1) GNU Bash-4.0 2004 Apr 20 RBASH(1)
All times are GMT -4. The time now is 04:44 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy