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
LIBBASH(7)							  libbash Manual							LIBBASH(7)

NAME
libbash -- A bash shared libraries package. DESCRIPTION
libbash is a package that enables bash dynamic-like shared libraries. Actually its a tool for managing bash scripts whose functions you may want to load and use in scripts of your own. It contains a 'dynamic loader' for the shared libraries ( ldbash(1)), a configuration tool (ldbashconfig(8)), and some libraries. Using ldbash(1) you are able to load loadable bash libraries, such as getopts(1) and hashstash(1). A bash shared library that can be loaded using ldbash(1) must answer 4 requirments: 1. It must be installed in $LIBBASH_PREFIX/lib/bash (default is /usr/lib/bash). 2. It must contain a line that begins with '#EXPORT='. That line will contain (after the '=') a list of functions that the library exports. I.e. all the function that will be usable after loading that library will be listed in that line. 3. It must contain a line that begins with '#REQUIRE='. That line will contain (after the '=') a list of bash libraries that are required for our library. I.e. every bash library that is in use in our bash library must be listed there. 4. The library must be listed (For more information, see ldbashconfig(8)). Basic guidelines for writing library of your own: 1. Be aware, that your library will be actually sourced. So, basically, it should contain (i.e define) only functions. 2. Try to declare all variables intended for internal use as local. 3. Global variables and functions that are intended for internal use (i.e are not defined in '#EXPORT=') should begin with: __<library_name>_ For example, internal function myfoosort of hashstash library should be named as __hashstash_myfoosort This helps to avoid conflicts in global name space when using libraries that come from different vendors. 4. See html manual for full version of this guide. AUTHORS
Hai Zaar <haizaar@haizaar.com> Gil Ran <ril@ran4.net> SEE ALSO
ldbash(1), ldbashconfig(8), getopts(1), hashstash(1) colors(1) messages(1) urlcoding(1) locks(1) Linux Epoch Linux
All times are GMT -4. The time now is 01:51 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy