#!/bin/bash # convert cpu No. into a bitmask for taskset # To avoid this conversion, you can simply use "-c" option # @ http://scriptdemo.blogspot.com # check whether it is a valid number getValid() { if [[ $1 =~ "^[0-9]+$" ]]; then if [ $1 -lt 1 ] || [ $1 -gt ${numCPUs} ]; then echo "FAIL" else echo $1; fi else echo "cpu No. must be between 1-${numCPUs}" echo "FAIL" fi } numCPUs=`cat /proc/cpuinfo | grep "model name" | wc -l` if [ $# -eq 0 ]; then echo "Usage: cpumask.sh CPU_No" exit elif [ $# -eq 1 ]; then if [[ $1 =~ "^[0-9]+$" ]]; then cpuNo=`getValid $1` else echo "cpu No. must be between 1-${numCPUS}" exit fi if [[ ${cpuNo} =~ "FAIL" ]]; then echo "invalid cpu No: $1" echo " valid cpu No: 1-${numCPUs}" exit else cpuMask=`echo "ibase=10; obase=16; 2^(${cpuNo}-1)" | bc` fi else cpuMask=0 eval "cpuNoInput=( `echo $* | sed 's/\ /\n/g' | sort | uniq` )" for cputmp in ${cpuNoInput[*]} do cpuNo=`getValid ${cputmp}` if [[ ${cpuNo} =~ "FAIL" ]]; then echo "invalid cpu No: ${cputmp}" echo " valid cpu No: 1-${numCPUs}" exit fi cpuMasktmp=`echo "ibase=10; obase=16; 2^(${cpuNo}-1)" | bc` cpuMask=`echo "ibase=16; obase=10; ${cpuMask}+${cpuMasktmp}" | bc` done fi cpuMaskBinary=`echo "ibase=16; obase=2; ${cpuMask}" | bc` echo " Total cpus availbale: ${numCPUs}" echo " request cpu mask(B): ${cpuMaskBinary}" echo " request cpu mask(H): ${cpuMask}" echo " taskset 0x${cpuMask} YourCommand" |
Thursday, December 22, 2011
[Bash] convert cpu No. into a bitmask
Labels:
bash,
bc,
binary,
conversion,
cpu affinity,
hex,
linux,
script,
taskset
Subscribe to:
Post Comments (Atom)
1 comment:
have i actually run it? CPUS vs CPUs
Post a Comment