#!/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
Thursday, December 8, 2011
[Bash] list the symbolic link
#!/bin/bash # track the ori-file from symbolic link # Usage: # lslk.sh YourFile(s) # @ http://scriptdemo.blogspot.com for nf in $* do if [ -e $nf ]; then echo ${nf} tmpStr="|" cDir=`dirname ${nf}` while [ -h $nf ] do tmpStr="${tmpStr}-" nf=`ls -l ${nf} | awk -F\> '{print $2}' | sed -e 's/\*//g' -e 's/^\ //'` [ `dirname ${nf} | cut -c1` != '/' ] && nf=${cDir}/${nf} [ -h ${nf} ] && echo "${tmpStr}${nf}" cDir=`dirname ${nf}` done echo "${tmpStr}`ls -l ${nf}`" fi echo "" done |
Labels:
bash,
linux,
ls,
script,
symbolic link
Subscribe to:
Posts (Atom)