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
TASKSET(1)							   User Commands							TASKSET(1)

NAME
taskset - set or retrieve a process's CPU affinity SYNOPSIS
taskset [options] mask command [argument...] taskset [options] -p [mask] pid DESCRIPTION
taskset is used to set or retrieve the CPU affinity of a running process given its pid, or to launch a new command with a given CPU affin- ity. CPU affinity is a scheduler property that "bonds" a process to a given set of CPUs on the system. The Linux scheduler will honor the given CPU affinity and the process will not run on any other CPUs. Note that the Linux scheduler also supports natural CPU affinity: the scheduler attempts to keep processes on the same CPU as long as practical for performance reasons. Therefore, forcing a specific CPU affinity is useful only in certain applications. The CPU affinity is represented as a bitmask, with the lowest order bit corresponding to the first logical CPU and the highest order bit corresponding to the last logical CPU. Not all CPUs may exist on a given system but a mask may specify more CPUs than are present. A retrieved mask will reflect only the bits that correspond to CPUs physically on the system. If an invalid mask is given (i.e., one that corresponds to no valid CPUs on the current system) an error is returned. The masks may be specified in hexadecimal (with or without a leading "0x"), or as a CPU list with the --cpu-list option. For example, 0x00000001 is processor #0, 0x00000003 is processors #0 and #1, 0xFFFFFFFF is processors #0 through #31, 32 is processors #1, #4, and #5, --cpu-list 0-2,6 is processors #0, #1, #2, and #6. When taskset returns, it is guaranteed that the given program has been scheduled to a legal CPU. OPTIONS
-a, --all-tasks Set or retrieve the CPU affinity of all the tasks (threads) for a given PID. -c, --cpu-list Interpret mask as numerical list of processors instead of a bitmask. Numbers are separated by commas and may include ranges. For example: 0,5,8-11. -p, --pid Operate on an existing PID and do not launch a new task. -V, --version Display version information and exit. -h, --help Display help text and exit. USAGE
The default behavior is to run a new command with a given affinity mask: taskset mask command [arguments] You can also retrieve the CPU affinity of an existing task: taskset -p pid Or set it: taskset -p mask pid PERMISSIONS
A user can change the CPU affinity of a process belonging to the same user. A user must possess CAP_SYS_NICE to change the CPU affinity of a process belonging to another user. A user can retrieve the affinity mask of any process. SEE ALSO
chrt(1), nice(1), renice(1), sched_getaffinity(2), sched_setaffinity(2) See sched(7) for a description of the Linux scheduling scheme. AUTHOR
Written by Robert M. Love. COPYRIGHT
Copyright (C) 2004 Robert M. Love. This is free software; see the source for copying conditions. There is NO warranty; not even for MER- CHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. AVAILABILITY
The taskset command is part of the util-linux package and is available from https://www.kernel.org/pub/linux/utils/util-linux/. util-linux August 2014 TASKSET(1)
All times are GMT -4. The time now is 03:34 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy